Yaml序列化具有自定义名称的字段

Yaml序列化具有自定义名称的字段

本文介绍了如何在Java中使用Snake Yaml序列化具有自定义名称的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to serialize a Java instance having fields like follows.

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")注释来实现.

How do I serialize them in different names than the actual field names? In Gson this is possible by using @SerializedName("first-name") annotation as follows.

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

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

Is there something similar to above in snakeyaml. The dependency details for snakeyaml is as follows,

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

下面是程序的主要类,其中包含flyx

Below is the main class of the program with the answer provided by 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 

推荐答案

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

这篇关于如何在Java中使用Snake Yaml序列化具有自定义名称的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 11:18