Title: [2034] trunk: XStream creates invalid JSON with JsonHierarchicalStreamDriver for custom converters since XStream 1.4 (XSTR-728).

Diff

Modified: trunk/xstream/src/java/com/thoughtworks/xstream/io/json/AbstractJsonWriter.java (2033 => 2034)


--- trunk/xstream/src/java/com/thoughtworks/xstream/io/json/AbstractJsonWriter.java	2013-03-02 19:29:21 UTC (rev 2033)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/io/json/AbstractJsonWriter.java	2013-03-12 22:15:00 UTC (rev 2034)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009, 2010, 2011, 2012 XStream Committers.
+ * Copyright (C) 2009, 2010, 2011, 2012, 2013 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -554,7 +554,7 @@
      * @since 1.4.4
      */
     protected Type getType(Class clazz) {
-        return (clazz == Mapper.Null.class || clazz == null)
+        return clazz == Mapper.Null.class
             ? Type.NULL
             : (clazz == Boolean.class || clazz == Boolean.TYPE) 
                 ? Type.BOOLEAN 

Modified: trunk/xstream/src/test/com/thoughtworks/acceptance/someobjects/Handler.java (2033 => 2034)


--- trunk/xstream/src/test/com/thoughtworks/acceptance/someobjects/Handler.java	2013-03-02 19:29:21 UTC (rev 2033)
+++ trunk/xstream/src/test/com/thoughtworks/acceptance/someobjects/Handler.java	2013-03-12 22:15:00 UTC (rev 2034)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2004 Joe Walnes.
- * Copyright (C) 2006, 2007 XStream Committers.
+ * Copyright (C) 2006, 2007, 2013 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -22,6 +22,10 @@
 {
     private Protocol protocol;
 
+    public Handler(Protocol p) {
+        protocol = p;
+    }
+    
     public Protocol getProtocol()
     {
         return protocol;

Modified: trunk/xstream/src/test/com/thoughtworks/acceptance/someobjects/Protocol.java (2033 => 2034)


--- trunk/xstream/src/test/com/thoughtworks/acceptance/someobjects/Protocol.java	2013-03-02 19:29:21 UTC (rev 2033)
+++ trunk/xstream/src/test/com/thoughtworks/acceptance/someobjects/Protocol.java	2013-03-12 22:15:00 UTC (rev 2034)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2004 Joe Walnes.
- * Copyright (C) 2006, 2007 XStream Committers.
+ * Copyright (C) 2006, 2007, 2013 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -22,6 +22,10 @@
 {
     private String id;
 
+    public Protocol(String id) {
+        this.id = id;
+    }
+    
     public String getId()
     {
         return id;

Modified: trunk/xstream/src/test/com/thoughtworks/xstream/io/json/JsonWriterFormatTest.java (2033 => 2034)


--- trunk/xstream/src/test/com/thoughtworks/xstream/io/json/JsonWriterFormatTest.java	2013-03-02 19:29:21 UTC (rev 2033)
+++ trunk/xstream/src/test/com/thoughtworks/xstream/io/json/JsonWriterFormatTest.java	2013-03-12 22:15:00 UTC (rev 2034)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009, 2010, 2011 XStream Committers.
+ * Copyright (C) 2009, 2010, 2011, 2013 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -22,11 +22,19 @@
 
 import com.thoughtworks.acceptance.objects.OpenSourceSoftware;
 import com.thoughtworks.acceptance.objects.SampleLists;
+import com.thoughtworks.acceptance.someobjects.Handler;
+import com.thoughtworks.acceptance.someobjects.Protocol;
 import com.thoughtworks.acceptance.someobjects.X;
 import com.thoughtworks.acceptance.someobjects.Y;
 import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.converters.Converter;
+import com.thoughtworks.xstream.converters.MarshallingContext;
+import com.thoughtworks.xstream.converters.UnmarshallingContext;
 import com.thoughtworks.xstream.converters.extended.ToStringConverter;
 import com.thoughtworks.xstream.core.util.OrderRetainingMap;
+import com.thoughtworks.xstream.io.ExtendedHierarchicalStreamWriterHelper;
+import com.thoughtworks.xstream.io.HierarchicalStreamReader;
+import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
 import com.thoughtworks.xstream.io.json.JsonWriter.Format;
 
 import junit.framework.AssertionFailedError;
@@ -57,6 +65,36 @@
         }
     }
     
+    private final static class HandlerConverter implements Converter {
+        public boolean canConvert(Class type) {
+            return type == Handler.class;
+        }
+    
+        public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
+            Handler h = (Handler)source;
+            writer.startNode("str");
+            writer.setValue("test");
+            writer.endNode();
+            writer.startNode("protocol");
+            context.convertAnother(h.getProtocol());
+            writer.endNode();
+            ExtendedHierarchicalStreamWriterHelper.startNode(writer, "i", int.class);
+            writer.setValue("42");
+            writer.endNode();
+        }
+    
+        public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
+            reader.moveDown();
+            reader.moveUp();
+            reader.moveDown();
+            Protocol p = (Protocol)context.convertAnother(null, Protocol.class); 
+            reader.moveUp();
+            reader.moveDown();
+            reader.moveUp();
+            return new Handler(p);
+        }
+    }
+
     public JsonWriterFormatTest(
         String name, Object target, String json, int writerMode, JsonWriter.Format format) {
         super(name);
@@ -72,12 +110,14 @@
         xstream.alias("collections", SampleLists.class);
         xstream.alias("x", X.class);
         xstream.alias("ys", YString.class);
+        xstream.alias("h", Handler.class);
         xstream.useAttributeFor(OpenSourceSoftware.class, "license");
         try {
             xstream.registerConverter(new ToStringConverter(YString.class));
         } catch (NoSuchMethodException e) {
             throw new AssertionFailedError(e.getMessage());
         }
+        xstream.registerConverter(new HandlerConverter());
     }
 
     protected void runTest() throws Throwable {
@@ -144,6 +184,7 @@
         targets.put("EmptyX", emptyX);
         targets.put("Collections", lists);
         targets.put("EmptyList", new ArrayList());
+        targets.put("CustomConverter", new Handler(new Protocol("ldap")));
 
         final Map results = new HashMap();
         results.put("optimizedMinimalString", "{'string':'text'}");
@@ -254,6 +295,15 @@
         results.put("explicitMinimalEmptyList", "{'list':[[],[]]}");
         results.put("explicitPrettyEmptyList", "{'list': [\n  [\n  ],\n  [\n  ]\n]}");
         results.put("explicitCompactEmptyList", "{'list': [\n  [],\n  []\n]}");
+        results.put("optimizedMinimalCustomConverter", "{'h':{'str':'test','protocol':{'id':'ldap'},'i':42}}");
+        results.put("optimizedPrettyCustomConverter", "{'h': {\n  'str': 'test',\n  'protocol': {\n    'id': 'ldap'\n  },\n  'i': 42\n}}");
+        results.put("optimizedCompactCustomConverter", "{'h': {\n  'str': 'test',\n  'protocol': {\n    'id': 'ldap'\n  },\n  'i': 42\n}}");
+        results.put("noRootMinimalCustomConverter", "{'str':'test','protocol':{'id':'ldap'},'i':42}");
+        results.put("noRootPrettyCustomConverter", "{\n  'str': 'test',\n  'protocol': {\n    'id': 'ldap'\n  },\n  'i': 42\n}");
+        results.put("noRootCompactCustomConverter", "{\n  'str': 'test',\n  'protocol': {\n    'id': 'ldap'\n  },\n  'i': 42\n}");
+        results.put("explicitMinimalCustomConverter", "{'h':[[],[{'str':[[],['test']]},{'protocol':[[],[{'id':[[],['ldap']]}]]},{'i':[[],[42]]}]]}");
+        results.put("explicitPrettyCustomConverter", "{'h': [\n  [\n  ],\n  [\n    {\n      'str': [\n        [\n        ],\n        [\n          'test'\n        ]\n      ]\n    },\n    {\n      'protocol': [\n        [\n        ],\n        [\n          {\n            'id': [\n              [\n              ],\n              [\n                'ldap'\n              ]\n            ]\n          }\n        ]\n      ]\n    },\n    {\n      'i': [\n        [\n        ],\n        [\n          42\n        ]\n      ]\n    }\n  ]\n]}");
+        results.put("explicitCompactCustomConverter", "{'h': [\n  [],\n  [\n    {\n      'str': [\n        [],\n        [\n          'test'\n        ]\n      ]\n    },\n    {\n      'protocol': [\n        [],\n        [\n          {\n            'id': [\n              [],\n              [\n                'ldap'\n              ]\n            ]\n          }\n        ]\n      ]\n    },\n    {\n      'i': [\n        [],\n        [\n          42\n        ]\n      ]\n    }\n  ]\n]}");
         
         TestSuite suite = new TestSuite(JsonWriterFormatTest.class.getName());
         for (final Iterator iterMode = modes.entrySet().iterator(); iterMode.hasNext();) {

Modified: trunk/xstream-distribution/src/content/changes.html (2033 => 2034)


--- trunk/xstream-distribution/src/content/changes.html	2013-03-02 19:29:21 UTC (rev 2033)
+++ trunk/xstream-distribution/src/content/changes.html	2013-03-12 22:15:00 UTC (rev 2034)
@@ -48,6 +48,8 @@
     	<li>JIRA:XSTR-723: XStream will now detect a working enhanced mode dynamically instead using lists of known
     	vendors. This allows enhanced support for JamVM if it is bundled with OpenJDK. It will currently fail on a 
     	runtime based on GNU	Classpath (at least up to version 0.98).</li>
+    	<li>JIRA:XSTR-728: XStream creates invalid JSON with JsonHierarchicalStreamDriver for custom converters since
+    	XStream 1.4.</li>
     </ul>
 
     <h2>Minor changes</h2>

To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to