Tuesday, October 20, 2009

Better support for type-safe collections

Java 5 introduced a nice feature - type-safe collections (generic collections). Unfortunately due to erasure the actual type is removed by the compiler and there is no way to detect the type of objects in a collection.
That is why all the YAML parsers require additional configuration to parse a collection. SnakeYAML is using TypeDescription to detect the runtime class.
Fortunately if the collection is a JavaBean property there is a way to detect the runtime class. Thanks to Benjamin Bentmann for the idea.
Check the issue 25.

JavaBeanLoader beanLoader = new JavaBeanLoader(ListBean.class);
ListBean parsed = beanLoader.load(output);

These 2 lines parse ListBean with properly created Developer instances. Parsing JavaBeans with SnakeYAML has become even simpler!
At the moment only List is supported. Does anybody need to extend the support to Set and Map ?

4 comments:

alex13 said...

Sets and Maps support would be nice, may be not high priority.

What about dumping ?
Does snakeyaml omit explicit tag in case real class of the object is the same as generic type for the collection ? (sorry for the question. I should know it myself, but had no time to play with snakeyaml recently. hope to do it ASAP)

Andrey Somov said...

At the moment dumping emits redundant global tags. I would like to fix it after I finish the parsing.
Release 1.5 of SnakeYAML shall contain the complete implementation.

Andrey Somov said...

Dumping and parsing work as expected. All the collections are supported.

SnakeYAML 1.5 is ready to go!

maslovalex said...

yep. I have noticed from the project's rss.

nice work. thx