使用Snakeyaml库将对象序列化为Java中的YAML [英] Serializing Object to YAML in Java using snakeyaml Library

查看:712
本文介绍了使用Snakeyaml库将对象序列化为Java中的YAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对序列化没有太多经验.在尝试序列化以下类的简单对象时,我从YAML库获得了此No JavaBean properties found exception.

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.

这是课程:

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

    public String getValue(){
        return value;
    }
}

这是我如何使用SnakeYAMAL进行序列化:

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?

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主要用于序列化JavaBean.

SnakeYAML is designed primarily for serializing JavaBeans.

您在上面给出的示例不符合JavaBean规范.要成为JavaBean,对象必须具有无参数构造函数,并且每个字段都必须具有getter和setter.

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.

如果您将类重写为Bean,SnakeYAML应该可以毫无问题地对其进行序列化.另外,SnakeYAML可以序列化公共字段,因此,如果将value的可见性更改为public,则SnakeYAML将会找到并序列化它.

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.

如果您真的想避免更改MyClass,则可以明确地告诉SnakeYAML序列化只读属性,如下所示:

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);

但是,当您将非JavaBean对象反序列化回Object时,将非JavaBean对象转储到YAML可能会导致问题,因此我建议使用JavaBeans作为最简单,最安全的解决方案.

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.

以下是将MyClass转换为JavaBean的示例:

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;
    }
}

这篇关于使用Snakeyaml库将对象序列化为Java中的YAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆