Serializing Object to YAML in Java using snakeyaml Library

心不动则不痛 提交于 2019-12-12 14:09:41

问题


I do not have much experience with Serialization. While trying to serialize a simple object of the class below i get this No JavaBean properties found exception from YAML library.

Here is the class:

public class MyClass {
    String value;
    public MyClass(String args) {
        value = args;
    }

    public String getValue(){
        return value;
    }
}

And here is how i am serializing using SnakeYAMAL:

import java.util.HashMap;
import java.util.Map;

import org.yaml.snakeyaml.Yaml;


public class Test {

    public static void main(String[] args) {

        MyClass obj = new MyClass("this is my data");

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("MyClass", obj);
        Yaml yaml = new Yaml();
        String output = yaml.dump(data);
        System.out.println(output);
    }

}

and upon executing, this exception is thrown:

Exception in thread "main" org.yaml.snakeyaml.error.YAMLException: No JavaBean properties found in MyClass
    at org.yaml.snakeyaml.introspector.PropertyUtils.getProperties(PropertyUtils.java:112) ...

Can you tell me what is it that i am missing in doing this, or how should i do it properly?

EDIT:

public class MyClass {
    String value;
    public MyClass() {}

    public String setValue(String value){
        this.value = value;
    }

    public String getValue(){
        return value;
    }
}

and if i set the value before serializing it, it somehow works. Do you think it is proper solution or not a recommended approach?


回答1:


SnakeYAML is designed primarily for serializing JavaBeans.

The example you give above does not conform to the JavaBean specification. To be a JavaBean, an object must have a no-argument constructor, and every field must have a getter and a setter.

If you rewrite your class as a bean, SnakeYAML should serialize it with no problems. Also, SnakeYAML can serialize public fields, so you if you change value's visibility to public then SnakeYAML will find and serialize it.

If you really want to avoid altering MyClass, you can explicitly tell SnakeYAML to serialize read-only properties, something like this:

PropertyUtils propUtils = new PropertyUtils();
propUtils.setAllowReadOnlyProperties(true);
Representer repr = new Representer();
repr.setPropertyUtils(propUtils);
Yaml yaml = new Yaml(new Constructor(), repr);

However, dumping non-JavaBean objects to YAML may cause problems when you come to de-serialize them back to an Object, so I recommend using JavaBeans as the easiest and safest solution.

Edit: Here is an example of MyClass converted to a JavaBean:

public class MyClass {
    String value;

    /* public, no-argument constructor */
    public MyClass() {
    }

    /* Every field has a public getter... */
    public String getValue(){
        return value;
    }

    /* ... and a public setter */
    public void setValue(String value) {
        this.value = value;
    }
}


来源:https://stackoverflow.com/questions/8488462/serializing-object-to-yaml-in-java-using-snakeyaml-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!