Writing Java object instance to YAML using Jackson

后端 未结 1 776
孤独总比滥情好
孤独总比滥情好 2020-12-19 07:56

I have a \'Example\' Pojo class as mentioned below. Can any one tel to save instance of Example class to YAML file using Jackson.

public class Example {

Str         


        
相关标签:
1条回答
  • 2020-12-19 08:40

    Jackson has a module that supports 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);
    
    0 讨论(0)
提交回复
热议问题