Serializing Object to YAML in Java using snakeyaml

2019-07-17 21:10发布

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条回答
走好不送
2楼-- · 2019-07-17 21:28

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;
    }
}
查看更多
登录 后发表回答