Deserialization of JSON into an array of Properties

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Deserialization of JSON into an array of Properties

Michael Minella
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!

Thanks,
Michael Minella

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.util.Properties;


import org.junit.Test;


import com.thoughtworks.xstream.XStream;

import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;

import com.thoughtworks.xstream.io.xml.DomDriver;


public class XStreamPropertiesTest {


private XStream xstream;


@Test

public void testPropertiesArrayJson() throws Exception {

xstream = new XStream(new JettisonMappedXmlDriver());

testLogic();

}


@Test

public void testPropertiesArrayXML() throws Exception {

xstream = new XStream(new DomDriver());

testLogic();

}


private void testLogic() {

TestClass test = new TestClass();

test.setProps(new Properties[3]);


ByteArrayOutputStream out = new ByteArrayOutputStream();


xstream.toXML(test, out);

System.err.println("out = " + out.toString());

TestClass result = (TestClass) xstream.fromXML(new ByteArrayInputStream(out.toByteArray()));

}


public static class TestClass {

private Properties[] props;


public void setProps(Properties[] props) {

this.props = props;

}


public Properties[] getProps() {

return props;

}

}

}


Reply | Threaded
Open this post in threaded view
|

Re: Deserialization of JSON into an array of Properties

Jörg Schaible-4
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 | Threaded
Open this post in threaded view
|

Re: Deserialization of JSON into an array of Properties

Michael Minella
Jörg,

Thank you for your insight.  Jettison 1.2 was really the only version I had not tried but it seems to work with that version (with XStream 1.4.4).  It does not work with 1.0.1, 1.1, 1.3.3 (the last one was expected).  Thanks again!

Michael


On Tue, Feb 4, 2014 at 4:32 AM, Jörg Schaible <[hidden email]> wrote:
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 | Threaded
Open this post in threaded view
|

Re: Re: Deserialization of JSON into an array of Properties

Jörg Schaible-2
Hi Michael,

Michael Minella wrote:

> Jörg,
>
> Thank you for your insight.  Jettison 1.2 was really the only version I
> had
> not tried but it seems to work with that version (with XStream 1.4.4).

Glad to hear.

> It
> does *not* work with 1.0.1, 1.1, 1.3.3 (the last one was expected).
> Thanks again!

This seems unfortunately a never ending story. Jettison 1.1.x was as
incompatible as 1.3.x, Jettison 1.0.x works with Java 1.4, but produces a
slightly different format and does cover less cases ...

Alas, since we do not control Jettison ... :-/

Cheers,
Jörg

>
> Michael
>
>
> On Tue, Feb 4, 2014 at 4:32 AM, Jörg Schaible
> <[hidden email]>wrote:
>
>> 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
>>
>>
>>



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

    http://xircles.codehaus.org/manage_email