Hi Michael,

Michael Minella wrote:

> I'm running into an issue performing a round trip serializing then
> deserializing an object that contains an empty array of Properties.  I've
> created a small test case below that illustrates the issue.  It seems to
> work fine when going to XML (using the DomDriver), but not JSON (using
> the JettisonMappedXmlDriver).  Do I need to create a custom converter for
> this, is this a bug, or am I just missing something.  Any insight that can
> be provided is appreciated. Thanks in advance!

This works for me (the array type does not matter):

============= %< =============
 public void testEmptyArray()
 {
   xstream.alias("exception", Exception.class);
   Exception[] exceptions = new Exception[3];
   String json = xstream.toXML(exceptions);
   assertEquals(
     "{'exception-array':[{'null':['','','']}]}".replace('\'', '"'),
     json);
   Exception[] exceptions2 = (Exception[])xstream.fromXML(json);
   assertEquals(json, xstream.toXML(exceptions2));
 }
============= %< =============

Note, that I am using Jettison 1.2, newer versions are no longer compatible:
 - http://xstream.codehaus.org/download.html#optional-deps
 - http://xstream.codehaus.org/faq.html#JSON_Jettison_version
 - http://jira.codehaus.org/browse/JETTISON-111

Remember, XStream writes here into a StAX interface i.e. from its PoV this 
is plain XML. Jettison is responsible for turning this XML into JSON 
representation (and vice versa). Therefore you should be aware that you get 
a different JSON representation if the Properties instance contain one ore 
more elements (look at the nesting level):

============= %< =============
 public void testProperties()
 {
   Properties properties = new Properties();
   properties.setProperty("key.1", "Value 1");
   String json = xstream.toXML(properties);
   assertEquals("{'properties':[{'property':{'@name':'key.1','@value':'Value 
1'}}]}".replace('\'', '"'), json);
   Properties properties2 = (Properties)xstream.fromXML(json);
   assertEquals(json, xstream.toXML(properties2));
                
   properties.setProperty("key.2", "Value 2");
   json = xstream.toXML(properties);
   assertEquals("{'properties':[{'property':
[{'@name':'key.2','@value':'Value 2'},{'@name':'key.1','@value':'Value 
1'}]}]}".replace('\'', '"'), json);
   properties2 = (Properties)xstream.fromXML(json);
   assertEquals(json, xstream.toXML(properties2));
 }
============= %< =============

You get best results with simple objects and plain lists/arrays, the 
conversion to and from JSON *is* limited, see word of warning:

 - http://xstream.codehaus.org/json-tutorial.html

Cheers,
Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to