Re: [Simple-support] erialize/deserialize Lists with empty Strings
Brought to you by:
niallg
|
From: <Nia...@ub...> - 2008-12-16 14:14:26
|
Hi,
Yes this is true. And here is why the empty string is a bit of a special
case. In XML the following two structures are equal, daft but true.
<string></string>
And
</string>
So how do you write an empty string as an element, versus null? Well for
@Attribute and @Text you will notice I have an empty() attribute for the
annotation so that if I were to write null I could have some form of
replacement like so.
@Attribute(empty="NULL")
private String field = null;
This would be written like so:
field="NULL"
So that deserialization knows to substitute "NULL" the string value with
a null. For elements its not as easy, perhaps there should be a similar
attribute for the @ElementList annotation. But for now, I am afraid the
empty string needs to be differentiated from null by some means. Usually
its best to use an @Persist and @Complete to replace "" values with
something so that they can be converted back to "" when read in again
using an @Commit method callback. Hope this helps.
Niall
-----Original Message-----
From: Joe [mailto:fis...@ya...]
Sent: 16 December 2008 13:56
To: sim...@li...
Subject: [Simple-support] erialize/deserialize Lists with empty Strings
Hi,
The following code prints:
values: [A, null]
Why null and not an empty String?
How can serialize/deserialize Lists with empty Strings?
thx
public class TestData
{
@ElementList(name = "values", required = false)
private List<String> _values = new ArrayList<String>();
TestData()
{
_values.add("A");
_values.add("");
}
List<String> getValues()
{
return _values;
}
public static void main(String[] args) throws Exception
{
Serializer serializer = new Persister();
File xmlFile = new File("test.xml");
serializer.write(new TestData(), xmlFile);
TestData sqlData = serializer.read(TestData.class, xmlFile);
System.out.println("values: " + sqlData.getValues());
}
}
------------------------------------------------------------------------
------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas,
Nevada.
The future of the web can't happen without you. Join us at MIX09 to
help pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.
com/
_______________________________________________
Simple-support mailing list
Sim...@li...
https://lists.sourceforge.net/lists/listinfo/simple-support
Visit our website at http://www.ubs.com
This message contains confidential information and is intended only
for the individual named. If you are not the named addressee you
should not disseminate, distribute or copy this e-mail. Please
notify the sender immediately by e-mail if you have received this
e-mail by mistake and delete this e-mail from your system.
E-mails are not encrypted and cannot be guaranteed to be secure or
error-free as information could be intercepted, corrupted, lost,
destroyed, arrive late or incomplete, or contain viruses. The sender
therefore does not accept liability for any errors or omissions in the
contents of this message which arise as a result of e-mail transmission.
If verification is required please request a hard-copy version. This
message is provided for informational purposes and should not be
construed as a solicitation or offer to buy or sell any securities
or related financial instruments.
UBS Limited is a company registered in England & Wales under company
number 2035362, whose registered office is at 1 Finsbury Avenue,
London, EC2M 2PP, United Kingdom.
UBS AG (London Branch) is registered as a branch of a foreign company
under number BR004507, whose registered office is at
1 Finsbury Avenue, London, EC2M 2PP, United Kingdom.
UBS Clearing and Execution Services Limited is a company registered
in England & Wales under company number 03123037, whose registered
office is at 1 Finsbury Avenue, London, EC2M 2PP, United Kingdom.
|