wink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r1061494 - in /incubator/wink/trunk/wink-json4j/src: main/java/org/apache/wink/json4j/JSONObject.java test/java/org/apache/wink/json4j/tests/JSONObjectTest.java
Date Thu, 20 Jan 2011 20:24:54 GMT
Author: bluk
Date: Thu Jan 20 20:24:54 2011
New Revision: 1061494

URL: http://svn.apache.org/viewvc?rev=1061494&view=rev
Log:
Fix JSON put of null elements during copy

Thanks to Mark Nesbitt for the suggestion/fix.

Modified:
    incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/JSONObject.java
    incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/JSONObjectTest.java

Modified: incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/JSONObject.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/JSONObject.java?rev=1061494&r1=1061493&r2=1061494&view=diff
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/JSONObject.java
(original)
+++ incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/JSONObject.java
Thu Jan 20 20:24:54 2011
@@ -99,7 +99,8 @@ public class JSONObject extends HashMap 
     }
 
     /**
-     * Create a new instance of this class taking selected values from the underlying one.
+     * Create a new instance of this class taking selected values from the underlying one.
 If a key is not
+     * found, then it will be copied into the new JSONObject.
      * @param obj The JSONObject to extract values from.
      * @param keys The keys to take from the JSONObject and apply to this instance.
      * @throws JSONException Thrown if a key is duplicated in the string[] keys
@@ -112,7 +113,10 @@ public class JSONObject extends HashMap 
                     throw new JSONException("Duplicate key: " + keys[i]);
                 }
                 try {
-                    this.put(keys[i], obj.get(keys[i]));
+                	Object item = obj.opt(keys[i]);
+                    if (item != null) {
+                        this.put(keys[i], item);
+                    }
                 } catch (Exception ex) {
                     JSONException jex = new JSONException("Error occurred during JSONObject
creation");
                     jex.initCause(ex);

Modified: incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/JSONObjectTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/JSONObjectTest.java?rev=1061494&r1=1061493&r2=1061494&view=diff
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/JSONObjectTest.java
(original)
+++ incubator/wink/trunk/wink-json4j/src/test/java/org/apache/wink/json4j/tests/JSONObjectTest.java
Thu Jan 20 20:24:54 2011
@@ -1333,6 +1333,29 @@ public class JSONObjectTest extends Test
             assertTrue(false);
         }
     }
+    
+    /**
+     * Test JSONObject creation does not throw exception if key is missing and 
+     * does not add missing value.
+     */
+    public void test_MissingKeyIsIgnored() {
+        JSONObject obj = new JSONObject();
+        try {
+            obj.put("key1", "key1");
+            obj.put("key2", "key2");
+            obj.put("key3", "key3");
+            String[] keys = {"key1", "key3"};
+            JSONObject testObj = new JSONObject(obj, keys);
+            assertTrue(testObj.size() == 2);
+            assertTrue(testObj.opt("key2") == null);
+            assertTrue(testObj.opt("key3").equals("key3"));
+            assertTrue(testObj.opt("key1").equals("key1"));
+        }
+        catch (JSONException jex) {
+            jex.printStackTrace();
+            assertTrue(false);
+        }
+    }
 
     /***********************************************************/
     /* The following tests checks UTF-8 encoded DBCS characters*/



Mime
View raw message