Jump to content
  • Advertisement

Parsing At The Root Using SnakeYaml Of YAML Document With A Map

Recommended Posts

Posted (edited)

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

  typeID: 2
  limit: 300
  typeID: 8
  limit: 100

It will load a map which will looks like this:

1	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?

Example for using TypeDescription

Edited by bamand

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!