Re: [Simple-support] Serializing/ deserializing List<Object> containing objects of different types
Brought to you by:
niallg
|
From: Niall G. <gal...@ya...> - 2008-02-10 19:58:36
|
Hi,
Yes you can use different types, however with regard
to primitives you can not type it as "Object". Object
is a special case in that it has no information
regarding the object. Primitives like strings and
integers undergo a different process of serialization,
so if the serializer sees "Object" it thinks the list
contains annotated types, so it attempts to serialize
them as such.
However, in your case because there is no type
information. You can do this.
@Root
public class SimpleTest
{
@ElementList(type=String.class)
private List<Object> test = new
ArrayList<Object>();
public static void main(String [] args) throws
Exception
{
SimpleTest test = new SimpleTest();
test.test.add(10);
test.test.add(0.5);
test.test.add("string");
Persister persister = new Persister();
persister.write(test, System.out);
}
}
which will prodice this following (however you will
have problems deserializing)
<simpleTest>
<test class="java.util.ArrayList">
<string class="java.lang.Integer">10</string>
<string class="java.lang.Double">0.5</string>
<string>string</string>
</test>
</simpleTest>
The best solution, is probably to add the following
callback method also.
@Persist
private void persist() {
for(int i = 0; i < test.size(); i++) {
Object val = test.get(i);
String text = String.valueOf(val);
test.set(i, text);
}
}
This method is called just before serialization. You
can then convert all values to strings, which will
enable deserializaion also, as it produces.
<simpleTest>
<test class="java.util.ArrayList">
<string>10</string>
<string>0.5</string>
<string>string</string>
</test>
</simpleTest>
Niall
--- Stanislaw Osinski <sta...@ca...>
wrote:
> Hello,
>
> First of all, thanks for the great piece of
> software, it simplifies things a
> lot!
>
> I'm not yet an advanced user of Simple XML, hence a
> possibly silly question.
> The following code:
>
> @Root
> public class SimpleTest
> {
> @ElementList
> private List<Object> test = new
> ArrayList<Object>();
>
> public static void main(String [] args) throws
> Exception
> {
> SimpleTest test = new SimpleTest();
> test.test.add(10);
> test.test.add(0.5);
> test.test.add("string");
>
> Persister persister = new Persister();
> persister.write(test, System.out);
> }
> }
>
> produces something like this on output:
>
> <simpleTest>
> <test class="java.util.ArrayList">
> <object class="java.lang.Integer"/>
> <object class="java.lang.Double"/>
> <object class="java.lang.String"/>
> </test>
> </simpleTest>
>
>
> Clearly, the information on output is incomplete to
> deserialize the object
> successfully.
>
> Question: can I (how?) serialize/ deserialize Lists
> and Maps containing
> objects of different types as values?
>
> Cheers,
>
> Staszek
> >
-------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio
> 2008.
>
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/>
_______________________________________________
> Simple-support mailing list
> Sim...@li...
>
https://lists.sourceforge.net/lists/listinfo/simple-support
>
____________________________________________________________________________________
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs
|