Search code examples
javaplayframeworksnakeyaml

How to define a Map in a YAML file in the Play! framework?


I'm using the Play! framework and I have a model (an Entity) that has a variable of type Map.

To make some tests, I load a YAML file every time I start the application, but I don't know the syntax to define a Map.

Let's say I have this class

public class aClass {
    public int arg1;
    public String arg2;
    public Map<String, Integer> arg3;
}

What would my YAML file look like? I tried this:

aClass(object1)
    arg1:   34
    arg2:   aString
    arg3:   [key1: value1, key2: value2] <- What do I do here?

This is one of my many tries. I don't know the syntax and I can't find it on the Internet. I think the Play! uses SnakeYAML as YAML parser.

In the doc I found some examples, but none of them will work.

In my application, everything works except that the Map variable (arg3 in the example) has a size of 0 element.

Thanks.


Solution

  • According to this thread on Google Groups it appears that the syntax you need is to use a question mark.

    The thread has a working map in the following syntax.

    Bloc(b2): 
      labelSeries: testDeValeur 
      criterias: 
        ? key1 
        : value1 
        ? key2 
        : value2 
    

    So, I guess your example would be

    aClass(object1)
      arg1:   34
      arg2:   aString
      arg3:   
        ? key1
        : value1
        ? key2
        : value2
    

    I have tested this however and it does not work! I would suggest raising a bug to take a look into this, because all documentation points at this being correct.