XStream
  1. XStream
  2. XSTR-588

using JettisonMappedXmlDriver, a zero-length List corrupts the following field

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.3.1
    • Fix Version/s: 1.4
    • Component/s: None
    • Labels:
      None
    • JDK version and platform:
      Java: 1.5.0_16; Java HotSpot(TM) Client VM 1.5.0_16-133, OS X 10.5.7

      Description

      Serializing empty lists corrupts the field in the object following the list. In the output below, the second two groups of output (where TestObject has objects in the List) are correct. In the first group of output, where the list is empty, the XML is correct but the JSON output shows the second boolean as an array[1] of boolean, which is incorrect. Note also that if I move the second boolean to before the list field in the object, the JSON outputs correctly.

      Here is the JUnit test code:

      JSONSerializerTest.java
      package com.concerttechnology.map.protocol;
      
      import com.thoughtworks.xstream.XStream;
      import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
      import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
      import org.junit.After;
      import org.junit.AfterClass;
      import org.junit.Before;
      import org.junit.BeforeClass;
      import org.junit.Test;
      
      public class JSONSerializerTest {
      
          private XStream jsonSerializer;
          private XStream xmlSerializer;
      
          public JSONSerializerTest() {
              jsonSerializer = new XStream(new PureJavaReflectionProvider(), new JettisonMappedXmlDriver());
              xmlSerializer =  new XStream(new PureJavaReflectionProvider());
              jsonSerializer.setMode(XStream.NO_REFERENCES);
              xmlSerializer.setMode(XStream.NO_REFERENCES);
          }
      
          @BeforeClass
          public static void setUpClass() throws Exception {
          }
      
          @AfterClass
          public static void tearDownClass() throws Exception {
          }
      
          @Before
          public void setUp() {
          }
      
          @After
          public void tearDown() {
          }
      
          @Test
          public void testTestObjectEmpty() {
              TestObject testObject = new TestObject();
      
              String xmlOutput = xmlSerializer.toXML(testObject);
              System.out.println(xmlOutput);
              String jsonOutput = jsonSerializer.toXML(testObject);
              System.out.println(jsonOutput);
              System.out.println();
          }
       
          @Test
          public void testTestObjectSingleListItem() {
              TestObject testObject = new TestObject();
      
              testObject.listOfStrings.add("foo");
              String xmlOutput = xmlSerializer.toXML(testObject);
              System.out.println(xmlOutput);
              String jsonOutput = jsonSerializer.toXML(testObject);
              System.out.println(jsonOutput);
              System.out.println();
          }
       
          @Test
          public void testTestObjectMultipleListItem() {
              TestObject testObject = new TestObject();
      
              testObject.listOfStrings.add("foo");
              testObject.listOfStrings.add("bar");
              String xmlOutput = xmlSerializer.toXML(testObject);
              System.out.println(xmlOutput);
              String jsonOutput = jsonSerializer.toXML(testObject);
              System.out.println(jsonOutput);
              System.out.println();
          }
      
      

      Here is the test object:

      TestObject.java
      package com.concerttechnology.map.protocol;
      
      import java.util.ArrayList;
      import java.util.List;
      
      public class TestObject {
      
          public boolean aBoolean;
          public List<String> listOfStrings;
          public boolean anotherBoolean;
      
          public TestObject() {
              listOfStrings = new ArrayList<String>();
          }
      }
      

      creates the following output (xml first, then the corresponding JSON):

      <com.concerttechnology.map.protocol.TestObject>
        <aBoolean>false</aBoolean>
        <listOfStrings/>
        <anotherBoolean>false</anotherBoolean>
      </com.concerttechnology.map.protocol.TestObject>
      
      {"com.concerttechnology.map.protocol.TestObject":{"aBoolean":false,"listOfStrings":"","anotherBoolean":[false]}}
      
      <com.concerttechnology.map.protocol.TestObject>
        <aBoolean>false</aBoolean>
        <listOfStrings>
          <string>foo</string>
        </listOfStrings>
        <anotherBoolean>false</anotherBoolean>
      </com.concerttechnology.map.protocol.TestObject>
      
      {"com.concerttechnology.map.protocol.TestObject":{"aBoolean":false,"listOfStrings":{"string":["foo"]},"anotherBoolean":false}}
      
      <com.concerttechnology.map.protocol.TestObject>
        <aBoolean>false</aBoolean>
        <listOfStrings>
          <string>foo</string>
          <string>bar</string>
        </listOfStrings>
        <anotherBoolean>false</anotherBoolean>
      </com.concerttechnology.map.protocol.TestObject>
      
      {"com.concerttechnology.map.protocol.TestObject":{"aBoolean":false,"listOfStrings":{"string":["foo","bar"]},"anotherBoolean":false}}
      

        People

        • Assignee:
          Jörg Schaible
          Reporter:
          Steve Petersen
        • Votes:
          0 Vote for this issue
          Watchers:
          0 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: