0 Replies - 489 Views - Last Post: 20 March 2019 - 03:22 AM

#1 dylan009   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-March 19

Parsing at the root using snakeYaml of YAML document with a map

Posted 20 March 2019 - 03:22 AM

I want to read a YAML document to a map of custom objects (instead of maps, which snakeYaml does by default). So this:

  typeID: 20
  limit: 200
  typeID: 8
  limit: 800

It will load a map which will looks like this:

Map<Integer, Item>

where Item is:

class Item {
    private Integer typeId;
    private Integer limit;

I could not find a way to do this with snakeYaml, and I couldn't find a better library for the task either.

The documentation only has examples with maps/collections nested inside other objects, so that you can do the following:

  TypeDescription typeDescription = new TypeDescription(ClassContainingAMap.class);
    typeDescription.putMapPropertyType("propertyNameOfNestedMap", Integer.class, Item.class);
    Constructor constructor = new Constructor(typeDescription);
    Yaml yaml = new Yaml(constructor);
    /* creating an input stream (is) */
    ClassContainingAMap obj = (ClassContainingAMap) yaml.load(is);

Now the question is how do I go about defining the Map format when it is at the root of the document?

Reference Example for using TypeDescription

Is This A Good Question/Topic? 0
  • +

Page 1 of 1