6

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

Damien-Amen
  • 7,232
  • 12
  • 46
  • 75

2 Answers2

6

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.

flyx
  • 35,506
  • 7
  • 89
  • 126
  • 2
    I am fairly new to YAML world and have been evaluating the various offerings and Jackson indeed seems to use SnakeYaml under the hood - so I am asking the same question you are... why use Jackson YAML and not SnakeYaml directly? – Volksman Apr 04 '18 at 18:55
  • @Volksman Jackson may already be in use for parsing JSON or XML, and so some might rather use something they are already using or are familiar with. – naugler Nov 08 '21 at 21:46
3

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>
CDV
  • 106
  • 3