Support custom name for enum value
I'd like to read/write YAML where the YAML value will be "true" or "false", and Java part is an enum.
Sadly, it is not possible to have enum value named "true" or "false", and SnakeYAML won't find it if I use another name (for example "TRUE" or "FALSE").
Comments (10)
-
-
reporter In fact, I tried to replace an old String enum by a "new" Java 5 one. The source is here: https://github.com/cbeust/testng/blob/master/src/main/java/org/testng/xml/XmlSuite.java
This class is used in XML and YAML conversion (https://github.com/cbeust/testng/blob/master/src/main/java/org/testng/internal/Yaml.java).
The problem is the parallel attribute where possible values are:
tests
,methods
,classes
,instances
,none
,true
andfalse
.I started to use uppercase name for enum values but SnakeYAML didn't find attributes. And I cannot use lowercase due to
true
andfalse
values.I think that having a way to specify the enum name we want could be a nice solution. Or maybe SnakeYAML provides a way to use custom converter but I didn't find something like that.
I can have 2 different classes too but I'd prefer to have only one description class for many export if possible.
-
I must admit that after your clarification it became even less clear. Can you please show an example of a YAML file you want to use ?
-
reporter a1 and a2 are good examples: https://github.com/cbeust/testng/tree/master/src/test/resources/yaml
-
-
assigned issue to
I have added example for enum: https://bitbucket.org/asomov/snakeyaml/src/56679d7bb9b9/src/test/java/org/yaml/snakeyaml/issues/issue311/
As you can see the boolean (and string) scalar has to use quotes to prevent implicit conversion.
Does it solve the issue ?
-
assigned issue to
-
reporter It doesn't solve the problem because the expected value in xml/yaml is in lower case ("true", "false", ...).
-
I have improved the example, now this YAML can be parsed (lower case, no quotes):
{boolField: true, enumField: true, name: '10'}
-
reporter A bit verbose but it is working well! Thank you!
For my information, why do you not provide something like XmlAdapter or JsonSerializer/JsonDeserializer
-
- changed status to resolved
I have put the answer to the FAQ (since it is frequently asked): https://bitbucket.org/asomov/snakeyaml/wiki/FAQ#markdown-header-why-snakeyaml-does-not-use-something-like-xmladapter-or-jsonserializerjsondeserializer-or-annotations-or-xxx
-
reporter Good!
I've just seen Jackson support yaml too (via snakeyaml). Its annotations or converters may work.http://wiki.fasterxml.com/JacksonExtensionYAML
Again, thank for the help.
- Log in to comment
What is the YAML document and what is the Java code ? By the way, you can turn off the implicit conversion true/false -> boolean.