Hi,
I'm considering using XStream to serialize and deserialize plugin
configurations in OpenRocket, an open-source model rocketry software.
XStream looks nice for the job, as the plugin writer doesn't need to
do anything other than implement a configuration pojo, and XStream can
serialize it properly.
Is there any way to make all collections automatically implicit, and
without the type of the collection being defined? For example
serializing the following class:
class PluginConfiguration {
private List<String> greetings = Arrays.asList("Hello", "World");
}
by default produces:
<testing.PluginConfiguration>
<greetings class="java.util.Arrays$ArrayList">
<a class="string-array">
<string>Hello</string>
<string>World</string>
</a>
</greetings>
</testing.PluginConfiguration>
I tried extending CollectionConverter and adding the condition
"List.class.isAssignableFrom(type)" to the canConvert method, and
registering the converter at high priority. This results in the
following:
<testing.PluginConfiguration>
<greetings class="java.util.Arrays$ArrayList">
<string>Hello</string>
<string>World</string>
</greetings>
</testing.PluginConfiguration>
I'm not exactly sure why this removed the <a> element from the middle
but hey - that's what I want... However it still contains the
class="java.util.Arrays$ArrayList" definition. What I want is that
lists are serialized as lists without storing the exact list type, and
deserialized for example as an ArrayList.
This is a compatibility issue as well, since it's not in any way
guaranteed that other JRE versions/implementations will even have a
class java.util.Arrays$ArrayList.
Ideally I think the (de)serialization should be based on the field
type, not the actual object type. If the field is declared
"List<String>" then it should be serialized implicitly, and any List
implementation can be used when deserializing (though a modifiable
list such as ArrayList is probably safest). If the field has an
explicit type, then that should be used.
This seems like a common use-case, but I haven't found any discussion
about it. Is it possible to implement a converter that does this?
Thanks!
--
Sampo Niskanen <=> http://www.iki.fi/sampo.niskanen/
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email