Re: [Simple-support] Serialise/Deserialise collections
Brought to you by:
niallg
|
From: Niall G. <gal...@ya...> - 2012-10-27 14:12:42
|
It should not matter what collection you use, but you must make sure the serialization runtime knows the type. The easiest is to declare it in the field with the full class name.
@ElementList(name="Positions")
private PositionSet<Position> positions;
If you do not declare the full type, then you must use the "class=" attribute in your XML.
<Positions class="my.package.PositionSet">
<Position>A</Position>
<Position>B</Position>
</Positions>
Niall
--- On Sat, 27/10/12, Nhat Ngo <phu...@gm...> wrote:
> From: Nhat Ngo <phu...@gm...>
> Subject: [Simple-support] Serialise/Deserialise collections
> To: sim...@li...
> Received: Saturday, 27 October, 2012, 6:57 AM
> Hi all,
> I am using SimpleXML to read/write data to XML
> from a part of a wider package of air traffic management and
> would like to ask for details on how to serialise and
> deserialise an odd setup on collections type. I got the
> following 2 classes:
>
> Position.java
>
>
>
>
>
>
>
>
> @Root(name="Position")
> public class Position {
> @Text
> private String name;
>
> public Position(@Text String name) {
> this.name=name
> }
>
> }PositionSet.java
> (used system-wide and should not be modified in
> functionality)import
> com.google.common.collect.ForwardingSortedSet;
>
> public class PositionSet extends
> ForwardingSortedSet<Position> {
> private final SortedSet<Position>
> delegate;
> public PositionSet() {
> this.delegate = new
> TreeSet<Position>();
>
> }
>
> public PositionSet(Collection<Position>
> delegate) {
> this.delegate = new
> TreeSet<Position>(delegate);
> }
>
> @Override
> protected SortedSet<Position> delegate() {
>
> return delegate;
> }
> }
> I want to produce the following
> XML:<Positions>
> <Position>A</Position>
>
> <Position>B</Position></Positions>
> The easiest option is to create another class
> called Positions.java, load the positions and then add it to
> PositionSet. However, I do not want to pollute the models
> with redundant codes and would like to use PositionSet and
> Position only.
>
> Further from that, there are other similar
> collections construction in the code bases that use
> SortedSet interface. Would it be possible to write a good
> Converter for SortedSet that handle different child elements
> (given they have respective annotated classes).
>
> Thanks,Nhat
>
> -----Inline Attachment Follows-----
>
> ------------------------------------------------------------------------------
> WINDOWS 8 is here.
> Millions of people. Your app in 30 days.
> Visit The Windows 8 Center at Sourceforge for all your go to
> resources.
> http://windows8center.sourceforge.net/
> join-generation-app-and-make-money-coding-fast/
> -----Inline Attachment Follows-----
>
> _______________________________________________
> Simple-support mailing list
> Sim...@li...
> https://lists.sourceforge.net/lists/listinfo/simple-support
>
|