java snakeyaml,使用Snakeyaml库将对象序列化为Java中的YAML

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 data = new HashMap();

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?

解决方案

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值