request for java.util.UUID support
It would be cool to automatically support java.util.UUID fields.
Currently, I use this:
public class UUIDConstructor extends Constructor {
public UUIDConstructor() {
yamlClassConstructors.put(NodeId.scalar, new UUIDConstruct());
}
class UUIDConstruct extends Constructor.ConstructScalar {
@Override
public Object construct(Node nnode) {
if (nnode.getTag().equals(Tag.STR)) {
Construct dateConstructor = yamlConstructors.get(Tag.STR);
String date = (String) dateConstructor.construct(nnode);
try {
return UUID.fromString(date);
} catch (IllegalArgumentException e) {
return super.construct(nnode);
}
} else {
return super.construct(nnode);
}
}
}
}
Thanks!
Comments (8)
-
-
reporter @@asomov : What I mean by "automatically support java.util.UUID" is this:
If my YAML looks like this:
uuid: de305d54-75b4-431b-adb2-eb6b9e546014
...and the Java bean looks like this:
class Bean { private java.util.UUID uuid; /* insert setter and getter for 'uuid' here */ }
then the marshalling/unmarshalling should automatically work. It currently doesn't because java.util.UUID doesn't have a constructor that accepts a java.lang.String, and you have to use the java.util.UUID.fromString() method.
I hope that clears things up. I also realized that that variable called 'date' should be called 'uuid' in the example above. :)
-
- changed status to open
I think this is a very valid point. But it should be wider: how to provide a factory method for a known class to create instances from scalars. Let us see what we can do. Do you have a proposal how the API may be adapted ?
-
reporter @asomov : I don't have a proposal for an API (designing a good API is hard), but I personally like the way Jackson handles this kind of situation (http://wiki.fasterxml.com/JacksonHowToCustomDeserializers).
-
Can you please test it the latest snapshot (1.17-SNAPSHOT)?
It should be fixed.
can be create with this YAML:
value: 14 id: 7f511847-781a-45df-9c8d-1e32e028b9b3
-
reporter @asomov : sorry i don't know when I'll get the chance to try this next.
-
- changed status to resolved
Fix issue 306: UUID support is complete
→ <<cset 8939e0aa430d>>
-
The test has examples for
JavaBeans
and for implicit tag: https://bitbucket.org/asomov/snakeyaml/src/tip/src/test/java/org/yaml/snakeyaml/issues/issue306/UuidSupportTest.javaIt will be delivered in version 1.17
- Log in to comment
Can you please be more specific when you say "automatically support java.util.UUID" ? UUID is not defined in the YAML 1.1 specification.
Your solution is good enough to be used. You may also check
Implicit types
in the documentation.