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.
In YAML, maps are represented using the colon ( : ) character. We can also use quotation marks ( " or ' ) to enclose the keys and values if we need to.
How to Inject a Map From a YAML File. Spring Boot has taken data externalization to the next level by providing a handy annotation called @ConfigurationProperties. This annotation is introduced to easily inject external properties from configuration files directly into Java objects.
YAML is a data serialization language that is often used for writing configuration files. Depending on whom you ask, YAML stands for yet another markup language or YAML ain't markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.
A colon followed by a space (or newline) ": " is an indicator for a mapping. A space followed by the pound sign " #" starts a comment. …and then the colon will be preserved. The list of allowed escapes can be found in the YAML Specification under “Escape Sequences” (YAML 1.1) or “Escape Characters” (YAML 1.2).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With