title: In XStream is there a better way to marshall/unmarshall List<Object>'s in JSON and Java
body: <p>I'm using <a href="http://xstream.codehaus.org/json-tutorial.html" rel="noreferrer">XStream</a> and JETTISON's Stax JSON serializer to send/receive messages to/from JSON javascripts clients and Java web applications.</p>
  
  <p>I want to be able to create a list of objects to send to the server and be properly marshalled into Java but the format that XStream and JSON expect it in is very non-intuitive and requires our javascript libraries to jump through hoops.</p>
  
  <p>[EDIT Update issues using <a href="http://code.google.com/p/google-gson/" rel="noreferrer">GSON</a> library]
  I attempted to use the <a href="http://code.google.com/p/google-gson/" rel="noreferrer">GSON library</a> but it cannot deserialize concrete objects when I only have it expect generic super classes (XStream and Jettison handles this because type information is baked into the serialization). </p>
  
  <p><a href="https://sites.google.com/site/gson/gson-user-guide#TOC-Collections-Limitations" rel="noreferrer">GSON FAQ states Collection Limitation</a>:</p>
  
  <blockquote>
    <p>Collections Limitations</p>
    
    <p>Can serialize collection of arbitrary objects but can not deserialize from it</p>
    
    <p>Because there is no way for the user to indicate the type of the resulting object</p>
    
    <p>While deserializing, Collection must be of a specific generic type</p>
  </blockquote>
  
  <p>Maybe I'm using bad java practices but how would I go about building a JSON to Java messaging framework that sent/received various concrete Message objects in JSON format?</p>
  
  <p>For example this fails:</p>
  
  <pre><code>public static void main(String[] args) {
      Gson gson = new Gson();
  
      MockMessage mock1 = new MockMessage();
      MockMessage mock2 = new MockMessage();
      MockMessageOther mock3 = new MockMessageOther();
  
      List&lt;MockMessage&gt; messages = new ArrayList&lt;MockMessage&gt;();
      messages.add(mock1);
      messages.add(mock2);
      messages.add(mock3);
  
      String jsonString = gson.toJson(messages);
  
      //JSON list format is non-intuitive single element array with class name fields
      System.out.println(jsonString);
      List gsonJSONUnmarshalledMessages = (List)gson.fromJson(jsonString, List.class);
      //This will print 3 messages unmarshalled
      System.out.println("XStream format JSON Number of messages unmarshalled: " + gsonJSONUnmarshalledMessages.size());
  }
  
  [{"val":1},{"val":1},{"otherVal":1,"val":1}]
  Exception in thread "main" com.google.gson.JsonParseException: The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@638bd7f1 failed to deserialized json object [{"val":1},{"val":1},{"otherVal":1,"val":1}] given the type interface java.util.List
  </code></pre>
  
  <p>Here's an example, I want to send a list of 3 Message objects, 2 are of the same type and the 3rd is a different type.</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  
  import com.thoughtworks.xstream.XStream;
  import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
  
  class MockMessage {
      int val = 1;
  }
  class MockMessageOther {
      int otherVal = 1;
  }
  
  public class TestJSONXStream {
  
  
      public static void main(String[] args) {
          JettisonMappedXmlDriver xmlDriver = new JettisonMappedXmlDriver();        
          XStream xstream = new XStream(xmlDriver);
  
          MockMessage mock1 = new MockMessage();
          MockMessage mock2 = new MockMessage();
          MockMessageOther mock3 = new MockMessageOther();
  
          List messages = new ArrayList();
          messages.add(mock1);
          messages.add(mock2);
          messages.add(mock3);
  
          String jsonString = xstream.toXML(messages);
  
          //JSON list format is non-intuitive single element array with class name fields
          System.out.println(jsonString);
          List xstreamJSONUnmarshalledMessages = (List)xstream.fromXML(jsonString);
          //This will print 3 messages unmarshalled
          System.out.println("XStream format JSON Number of messages unmarshalled: " + xstreamJSONUnmarshalledMessages.size());
  
          //Attempt to deserialize a reasonable looking JSON string
          String jsonTest = 
                "{"+
                  "\"list\" : ["+ 
                            "{"+
                            "\"MockMessage\" : {"+
                                "\"val\" : 1"+
                            "}"+
                        "}, {"+
                            "\"MockMessage\" : {"+
                                "\"val\" : 1"+
                            "}"+
                        "}, {"+
                            "\"MockMessageOther\" : {"+
                                "\"otherVal\" : 1"+
                            "}"+
                        "} ]"+
                    "};";
  
          List unmarshalledMessages = (List)xstream.fromXML(jsonTest);
  
          //We expect 3 messages but XStream only deserializes one
          System.out.println("Normal format JSON Number of messages unmarshalled: " + unmarshalledMessages.size());
      }
  
  }
  </code></pre>
  
  <p>Intuitively I expect the XStream JSON to be serialized (and able to deserialize correctly) from the following format:</p>
  
  <pre><code>{
      "list" : [ 
          {
          "MockMessage" : {
              "val" : 1
          }
      }, {
          "MockMessage" : {
              "val" : 1
          }
      }, {
          "MockMessageOther" : {
              "otherVal" : 1
          }
      } ]
  }
  </code></pre>
  
  <p>Instead XStream creates a single element list with fields that are named the classnames and nested arrays of Objects of the same type.</p>
  
  <pre><code>{
      "list" : [ {
          "MockMessage" : [ {
              "val" : 1
          }, {
              "val" : 1
          } ],
          "MockMessageOther" : {
              "otherVal" : 1
          }
      } ]
  }
  </code></pre>
  
  <p>The trouble may be caused by it using the XStream XML <a href="http://xstream.codehaus.org/converters.html" rel="noreferrer">CollectionConverter</a>?</p>
  
  <p>Does anyone have a suggestion for a good JSON Java object serialization that allows you to read/write arbitrary Java objects. I looked at the <a href="http://jackson.codehaus.org/" rel="noreferrer">Jackson Java JSON Processor</a> but when you were reading in objects from a stream you had to specify what type of object it was unlike XStream where it will read in any object (because the serialized  XStream JSON contains class name information).</p>