如何在Java中使用Snake Yaml序列化具有自定义名称的字段

时间:2019-01-24 16:57:04

标签: java serialization yaml pojo snakeyaml

我正在尝试序列化具有如下字段的Java实例。

public class Person{
    private String firstName;
    private String lastName;

    public String getFirstName() {

        return firstName;
    }

    public void setFirstName(String firstName) {

        this.firstName = firstName;
    }

    public String getLastName() {

        return lastName;
    }

    public void setLastName(String lastName) {

        this.lastName = lastName;
    }
}

如何使用与实际字段名称不同的名称来序列化它们?在Gson中,可以通过如下使用@SerializedName("first-name")注释来实现。

@SerializedName("first-name")
private String firstName;

snakeyaml中是否存在与上面类似的内容。 snakeyaml的依赖项详细信息如下,

        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.17</version>
        </dependency>

下面是该程序的主要类别,其答案由flyx

提供
public class Demo {

    public static void main(String[] args) {

        Person person = new Person();
        person.setFirstName("Kasun");
        person.setLastName("Siyambalapitiya");

        Constructor constructor = new Constructor(Person.class);
        Representer representer = new Representer();
        TypeDescription personDesc = new TypeDescription(Person.class);
        personDesc.substituteProperty("first-name", Person.class, "getFirstName", "setFirstName");
        personDesc.substituteProperty("last-name", Person.class, "getLastName", "setLastName");
        constructor.addTypeDescription(personDesc);
        representer.addTypeDescription(personDesc);
        Yaml yaml = new Yaml(constructor, representer);
        String yamlString = yaml.dumpAs(person, Tag.MAP, DumperOptions.FlowStyle.BLOCK);

        Path updateDescriptorFilePath =
                Paths.get(File.separator + "tmp" + File.separator + "sample.json");
        try {
            FileUtils.writeStringToFile(updateDescriptorFilePath.toFile(), yamlString);
        } catch (IOException e) {
            System.out.println(e.getCause());
        }
    }
}

上面的结果是以下输出

/tmp  cat sample.json
first-name: Kasun
last-name: Siyambalapitiya
firstName: Kasun
lastName: Siyambalapitiya
 /tmp   

2 个答案:

答案 0 :(得分:0)

Constructor constructor = new Constructor(Person.class);
Representer representer = new Representer();
TypeDescription personDesc = new TypeDescription(Person.class);
personDesc.substituteProperty("first-name", Person.class,
        "getFirstName", "setFirstName");
constructor.addTypeDescription(personDesc);
representer.addTypeDescription(personDesc);
Yaml yaml = new Yaml(constructor, representer);
// and then load /dump your file with it

答案 1 :(得分:0)

对于那些遇到这个问题的人,我通过使用替换和排除来解决它:

TypeDescription personDesc = new TypeDescription(Person.class);
personDesc.substituteProperty("first-name", Person.class, "getFirstName", "setFirstName");
personDesc.substituteProperty("last-name", Person.class, "getLastName", "setLastName");
personDesc.setExcludes("firstName", "lastName");

这对我有用 - 至少对于转储 YAML。