Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing Yaml in Java

I have the following YAML I want to parse using Jackson parser in Java.

android:
    "7.0":
        - nexus
        - S8
    "6.0":
        - s7
        - g5
ios:
    "10.0":
        - iphone 7
        - iphone 8

I created a created class which has getter and setter as Java Object for android. It works fine. But how do I do the same for 6.0 and 7.0? I'm usingJackson` Parser

like image 899
Damien-Amen Avatar asked Nov 27 '17 21:11

Damien-Amen


People also ask

How do I read a YAML file in Java?

Read YAML File as Map in Java The Yaml instance introduces us to methods, such as load() which allow us to read and parse any InputStream , Reader or String with valid YAML data: InputStream inputStream = new FileInputStream(new File("student. yml")); Yaml yaml = new Yaml(); Map<String, Object> data = yaml.

What is YAML in Java?

yml) File: YAML is a configuration language. Languages like Python, Ruby, Java heavily use it for configuring the various properties while developing the applications. If you have ever used Elastic Search instance and MongoDB database, both of these applications use YAML(. yml) as their default configuration format.

How do I edit a YAML file in Java?

You will need YAMLMapper (from jackson-databind-yaml ) which is the YAML-specific implementation of ObjectMapper (from jackson-databind ). ObjectMapper objectMapper = new YAMLMapper(); Then it is easy: just read the YAML file, modify the contents, and write the YAML file.

How do I read a YML file value?

If your key is loginUrl (inside your yaml file), you can inject its value with the @Value annotation, inside a Spring component. @Value("${loginUrl}") private String loginUrl; If it's a second level property, the path is @Value("${yourFirstKey. loginUrl}") .


2 Answers

No idea whether Jackson supports that; here's a solution with plain SnakeYaml (I will never understand why people use Jackson for parsing YAML when all it does is basically take away the detailed configuration possible with SnakeYaml which it uses as backend):

class AndroidValues {
     // showing what needs to be done for "7.0". "8.0" works similarly.
     private List<String> v7_0;

     public List<String> getValuesFor7_0() {
         return v7_0;
     }

     public void setValuesFor7_0(List<String> value) {
         v7_0 = value;
     }
}

// ... in your loading code:

Constructor constructor = new Constructor(YourRoot.class);
TypeDescription androidDesc = new TypeDescription(AndroidValues.class);
androidDesc.substituteProperty("7.0", List.class, "getValuesFor7_0", "setValuesFor7_0");
androidDesc.putListPropertyType("7.0", String.class);
constructor.addTypeDescription(androidDesc);
Yaml yaml = new Yaml(constructor);

// and then load the root type with it

Note: Code has not been tested.

like image 137
flyx Avatar answered Oct 16 '22 16:10

flyx


I think that you should try annotation com.fasterxml.jackson.annotation.JsonProperty. I'll provide a short example below.

Sample YAML file:

---
"42": "some value"

Data transfer object class:

public class Entity {

    @JsonProperty("42")
    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

Parser:

public class Parser {

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        Entity entity = mapper.readValue(new File("src/main/resources/sample.yml"), Entity.class);
        System.out.println(entity.getValue());
    }

}

The console output should be: some value.

P.S. I tested it with the following dependencies:

    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-yaml</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.2.3</version>
    </dependency>
like image 24
CDV Avatar answered Oct 16 '22 16:10

CDV