[英]Writing Java object instance to YAML using Jackson
I have a 'Example' Pojo class as mentioned below. 我有一个'示例'Pojo类,如下所述。 Can any one tel to save instance of Example class to YAML file using Jackson.
任何一个tel都可以使用Jackson将Example类的实例保存到YAML文件中。
public class Example {
String name;
int value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
} }
Jackson has a module that supports YAML . 杰克逊有一个支持YAML的模块 。 Ensure that you add the required dependency to your project, then you can use it as following:
确保将所需的依赖项添加到项目中,然后可以按如下方式使用它:
// Create an ObjectMapper mapper for YAML
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
// Write object as YAML file
mapper.writeValue(new File("/path/to/yaml/file"), example);
Alternatively you can write your object as a string: 或者,您可以将对象编写为字符串:
// Write object as YAML string
String yaml = mapper.writeValueAsString(example);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.