Title: [1992] trunk: JsonWriter does not write BigInteger and BigDecimal as number values (XSTR-710).
Revision
1992
Author
joehni
Date
2012-10-19 10:34:38 -0500 (Fri, 19 Oct 2012)

Log Message

JsonWriter does not write BigInteger and BigDecimal as number values (XSTR-710). Declare AbstractJsonWriter.getType as protected.

Modified Paths

Diff

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


--- trunk/xstream/src/java/com/thoughtworks/xstream/io/json/AbstractJsonWriter.java	2012-10-18 21:48:15 UTC (rev 1991)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/io/json/AbstractJsonWriter.java	2012-10-19 15:34:38 UTC (rev 1992)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009, 2010, 2011 XStream Committers.
+ * Copyright (C) 2009, 2010, 2011, 2012 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -11,10 +11,13 @@
 package com.thoughtworks.xstream.io.json;
 
 import java.io.Externalizable;
+import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.List;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 
 import com.thoughtworks.xstream.converters.ConversionException;
 import com.thoughtworks.xstream.core.util.FastStack;
@@ -165,9 +168,10 @@
     private static final int STATE_END_ELEMENTS = 1 << 8;
     private static final int STATE_SET_VALUE = 1 << 9;
 
-    private static final List NUMBER_TYPES = Arrays.asList(new Class[]{
+    private static final Set NUMBER_TYPES = new HashSet(Arrays.asList(new Class[]{
         byte.class, Byte.class, short.class, Short.class, int.class, Integer.class, long.class,
-        Long.class, float.class, Float.class, double.class, Double.class});
+        Long.class, float.class, Float.class, double.class, Double.class, BigInteger.class,
+        BigDecimal.class}));
     private int mode;
     private FastStack stack = new FastStack(16);
     private int expectedStates;
@@ -542,7 +546,14 @@
         throw new IllegalWriterStateException(currentState, requiredState, elementToAdd);
     }
 
-    private Type getType(Class clazz) {
+    /**
+     * Method to return the appropriate JSON type for a Java type.
+     * 
+     * @param clazz the type
+     * @return One of the {@link Type} instances
+     * @since upcoming
+     */
+    protected Type getType(Class clazz) {
         return (clazz == Mapper.Null.class || clazz == null)
             ? Type.NULL
             : (clazz == Boolean.class || clazz == Boolean.TYPE) 

Modified: trunk/xstream/src/test/com/thoughtworks/xstream/io/json/JsonHierarchicalStreamDriverTest.java (1991 => 1992)


--- trunk/xstream/src/test/com/thoughtworks/xstream/io/json/JsonHierarchicalStreamDriverTest.java	2012-10-18 21:48:15 UTC (rev 1991)
+++ trunk/xstream/src/test/com/thoughtworks/xstream/io/json/JsonHierarchicalStreamDriverTest.java	2012-10-19 15:34:38 UTC (rev 1992)
@@ -14,6 +14,8 @@
 import java.awt.Color;
 import java.io.InputStream;
 import java.io.Reader;
+import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
@@ -89,18 +91,32 @@
             + "  'long1': 5,\n"
             + "  'long2': 42,\n"
             + "  'greeting': 'hello',\n"
-            + "  'num1': 2,\n"
-            + "  'num2': 3,\n"
-            + "  'bool': true,\n"
-            + "  'bool2': true,\n"
+            + "  'int1': 2,\n"
+            + "  'int2': 3,\n"
+            + "  'short1': 6,\n"
+            + "  'short2': 7,\n"
+            + "  'byte1': 8,\n"
+            + "  'byte2': 9,\n"
+            + "  'bool1': true,\n"
+            + "  'bool2': false,\n"
             + "  'char1': 'A',\n"
             + "  'char2': 'B',\n"
+            + "  'float1': 1.1,\n"
+            + "  'float2': 1.2,\n"
+            + "  'double1': 2.1,\n"
+            + "  'double2': 2.2,\n"
+            + "  'bigInt': 511,\n"
+            + "  'bigDec': 3.14,\n"
             + "  'innerMessage': {\n"
             + "    'long1': 0,\n"
             + "    'greeting': 'bonjour',\n"
-            + "    'num1': 3,\n"
-            + "    'bool': false,\n"
-            + "    'char1': '\\u0000'\n"
+            + "    'int1': 3,\n"
+            + "    'short1': 0,\n"
+            + "    'byte1': 0,\n"
+            + "    'bool1': false,\n"
+            + "    'char1': '\\u0000',\n"
+            + "    'float1': 0.0,\n"
+            + "    'double1': 0.0\n"
             + "  }\n"
             + "}}");
 
@@ -109,15 +125,25 @@
         Message message = new Message("hello");
         message.long1 = 5L;
         message.long2 = new Long(42);
-        message.num1 = 2;
-        message.num2 = new Integer(3);
-        message.bool = true;
-        message.bool2 = Boolean.TRUE;
+        message.int1 = 2;
+        message.int2 = new Integer(3);
+        message.short1 = (short)6;
+        message.short2 = new Short((short)7);
+        message.byte1 = (byte)8;
+        message.byte2 = new Byte((byte)9);
+        message.bool1 = true;
+        message.bool2 = Boolean.FALSE;
         message.char1 = 'A';
         message.char2 = new Character('B');
+        message.float1 = 1.1f;
+        message.float2 = new Float(1.2f);
+        message.double1 = 2.1;
+        message.double2 = new Double(2.2);
+        message.bigInt = new BigInteger(new byte[]{(byte)1,(byte)0xFF});
+        message.bigDec = new BigDecimal(314).divide(new BigDecimal(100));
 
         Message message2 = new Message("bonjour");
-        message2.num1 = 3;
+        message2.int1 = 3;
 
         message.innerMessage = message2;
 
@@ -128,12 +154,22 @@
         long long1;
         Long long2;
         String greeting;
-        int num1;
-        Integer num2;
-        boolean bool;
+        int int1;
+        Integer int2;
+        short short1;
+        Short short2;
+        byte byte1;
+        Byte byte2;
+        boolean bool1;
         Boolean bool2;
         char char1;
         Character char2;
+        float float1;
+        Float float2;
+        double double1;
+        Double double2;
+        BigInteger bigInt;
+        BigDecimal bigDec;
         Message innerMessage;
 
         public Message(String greeting) {

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


--- trunk/xstream-distribution/src/content/changes.html	2012-10-18 21:48:15 UTC (rev 1991)
+++ trunk/xstream-distribution/src/content/changes.html	2012-10-19 15:34:38 UTC (rev 1992)
@@ -41,6 +41,7 @@
     <h2>Minor changes</h2>
     
     <ul>
+    	<li>JIRA:XSTR-710: JsonWriter does not write BigInteger and BigDecimal as number values.</li>
     	<li>JIRA:XSTR-708: SqlTimestampConverter does not ignore timezone.</li>
     	<li>JIRA:XSTR-707: Creation of XmllPullParser with the XmlPullParserFactory may fail in OSGi environment.</li>
     	<li>JIRA:XSTR-705: Unnecessary synchronization accessing the field cache decreases performance.</li>

To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to