Re: [Simple-support] erialize/deserialize Lists with empty Strings
Brought to you by:
niallg
|
From: <Nia...@ub...> - 2008-12-16 16:13:05
|
Hi,
Your suggestion should work fine. I think there may be another
alternative using a strategy, so that you could have.
<testData>
<_values class="java.util.Arrays$ArrayList">
<string>A</string>
<string null="false"></string>
<string null="true"></string>
</_values>
</testData>
This would basically involve adding an attribute to all elements where
the value to be written is null. Don't know if deserialization would
respect this though so I would have to investigate. Bottom line though
is that StAX does not treat <string></string> and </string> differently
which would have provided a really nice way to differentiate "" from
null.
Regards,
Niall
-----Original Message-----
From: Joe [mailto:fis...@ya...]
Sent: 16 December 2008 16:07
To: Gallagher, Niall-N
Cc: sim...@li...
Subject: Re: [Simple-support] erialize/deserialize Lists with empty
Strings
Hi,
> So it looks like if you really need the empty string "" then you will
> need to either treat null as a special case, or substitute "" for some
> token such as "NULL".
I think i will use a slightly different approach, inspired by the base64
encoding example in your tutorial.
I encode every value with a prefix (here '*') only null values are
ignored.
So
Arrays.asList("A", "", null)
will be serialized as:
<testData>
<_values class="java.util.Arrays$ArrayList">
<string>*A</string>
<string>*</string>
<string></string>
</_values>
</testData>
Using @Commit the deserialization can be adjusted, to getting back the
original values.
The output of the following example is the expected String:
values: [A, , null]
public class TestData
{
@ElementList()
private List<String> _values = new ArrayList<String>();
public List<String> getValues()
{
return _values;
}
public void setValues(List<String> values)
{
_values = values;
}
@Persist
public void persist()
{
for (int i = 0; i < _values.size(); i++)
if (_values.get(i) != null)
_values.set(i, "*" + _values.get(i));
else
_values.set(i, "");
}
@Commit
public void commit()
{
for (int i = 0; i < _values.size(); i++)
{
String val = _values.get(i);
if (val != null)
_values.set(i, _values.get(i).substring(1));
}
}
public static void main(String[] args) throws Exception
{
Serializer serializer = new Persister();
File xmlFile = new File("test.xml");
TestData data = new TestData();
data.setValues(Arrays.asList("A", "", null));
serializer.write(data, xmlFile);
TestData sqlData = serializer.read(TestData.class, xmlFile);
System.out.println("values: " + sqlData.getValues());
}
}
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.
|