How to serialize fields with custom names using snake yaml in Java

限于喜欢 提交于 2020-07-09 13:52:07

问题


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

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;

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>

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

The above results in the following output

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


回答1:


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


来源:https://stackoverflow.com/questions/54351787/how-to-serialize-fields-with-custom-names-using-snake-yaml-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!