struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1129976 - in /struts/struts2/trunk/plugins/json/src: main/java/org/apache/struts2/json/JSONWriter.java test/java/org/apache/struts2/json/JSONWriterTest.java test/resources/org/apache/struts2/json/jsonwriter-write-bean-03.txt
Date Wed, 01 Jun 2011 00:06:22 GMT
Author: jafl
Date: Wed Jun  1 00:06:22 2011
New Revision: 1129976

URL: http://svn.apache.org/viewvc?rev=1129976&view=rev
Log:
WW-3489 if excludeNullProperties, ignore nulls in maps

Added:
    struts/struts2/trunk/plugins/json/src/test/resources/org/apache/struts2/json/jsonwriter-write-bean-03.txt
      - copied, changed from r1129833, struts/struts2/trunk/plugins/json/src/test/resources/org/apache/struts2/json/jsonwriter-write-bean-01.txt
Modified:
    struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java
    struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONWriterTest.java

Modified: struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java?rev=1129976&r1=1129975&r2=1129976&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java
(original)
+++ struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java
Wed Jun  1 00:06:22 2011
@@ -342,18 +342,18 @@ class JSONWriter {
      * Add name/value pair to buffer
      */
     private boolean add(String name, Object value, Method method, boolean hasData) throws
JSONException {
-        if (!excludeNullProperties || (value != null)) {
-            if (hasData) {
-                this.add(',');
-            }
-            this.add('"');
-            this.add(name);
-            this.add("\":");
-            this.value(value, method);
-            return true;
+        if (excludeNullProperties && value == null) {
+            return false;
         }
 
-        return false;
+        if (hasData) {
+            this.add(',');
+        }
+        this.add('"');
+        this.add(name);
+        this.add("\":");
+        this.value(value, method);
+        return true;
     }
 
     /**
@@ -368,6 +368,10 @@ class JSONWriter {
         boolean hasData = false;
         while (it.hasNext()) {
             Map.Entry entry = (Map.Entry) it.next();
+            if (excludeNullProperties && entry.getValue() == null) {
+                continue;
+            }
+
             Object key = entry.getKey();
             String expr = null;
             if (this.buildExpr) {

Modified: struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONWriterTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONWriterTest.java?rev=1129976&r1=1129975&r2=1129976&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONWriterTest.java
(original)
+++ struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONWriterTest.java
Wed Jun  1 00:06:22 2011
@@ -5,6 +5,8 @@ import org.apache.struts2.json.annotatio
 import org.apache.struts2.json.bridge.StringBridge;
 import org.junit.Test;
 
+import java.util.Map;
+import java.util.LinkedHashMap;
 import java.net.URL;
 
 public class JSONWriterTest extends StrutsTestCase{
@@ -28,6 +30,44 @@ public class JSONWriterTest extends Stru
     }
 
     @Test
+    public void testWriteExcludeNull() throws Exception {
+        BeanWithMap bean1=new BeanWithMap();
+        bean1.setStringField("str");
+        bean1.setBooleanField(true);
+        bean1.setCharField('s');
+        bean1.setDoubleField(10.1);
+        bean1.setFloatField(1.5f);
+        bean1.setIntField(10);
+        bean1.setLongField(100);
+        bean1.setEnumField(AnEnum.ValueA);
+        bean1.setEnumBean(AnEnumBean.Two);
+
+        Map m = new LinkedHashMap();
+        m.put("a", "x");
+        m.put("b", null);
+        m.put("c", "z");
+        bean1.setMap(m);
+
+        JSONWriter jsonWriter = new JSONWriter();
+        jsonWriter.setEnumAsBean(false);
+        jsonWriter.setIgnoreHierarchy(false);
+        String json = jsonWriter.write(bean1, null, null, true);
+        TestUtils.assertEquals(JSONWriter.class.getResource("jsonwriter-write-bean-03.txt"),
json);
+    }
+
+    private class BeanWithMap extends Bean{
+        private Map map;
+
+        public Map getMap() {
+            return map;
+        }
+
+        public void setMap(Map map) {
+            this.map = map;
+        }
+    }
+
+    @Test
     public void testWriteAnnotatedBean() throws Exception {
         AnnotatedBean bean1=new AnnotatedBean();
         bean1.setStringField("str");

Copied: struts/struts2/trunk/plugins/json/src/test/resources/org/apache/struts2/json/jsonwriter-write-bean-03.txt
(from r1129833, struts/struts2/trunk/plugins/json/src/test/resources/org/apache/struts2/json/jsonwriter-write-bean-01.txt)
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/test/resources/org/apache/struts2/json/jsonwriter-write-bean-03.txt?p2=struts/struts2/trunk/plugins/json/src/test/resources/org/apache/struts2/json/jsonwriter-write-bean-03.txt&p1=struts/struts2/trunk/plugins/json/src/test/resources/org/apache/struts2/json/jsonwriter-write-bean-01.txt&r1=1129833&r2=1129976&rev=1129976&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/json/src/test/resources/org/apache/struts2/json/jsonwriter-write-bean-01.txt
(original)
+++ struts/struts2/trunk/plugins/json/src/test/resources/org/apache/struts2/json/jsonwriter-write-bean-03.txt
Wed Jun  1 00:06:22 2011
@@ -1,6 +1,4 @@
 {
-    "bigDecimal":null,
-    "bigInteger":null,
     "booleanField":true,
     "byteField":0,
     "charField":"s",
@@ -10,6 +8,6 @@
     "floatField":1.5,
     "intField":10,
     "longField":100,
-    "objectField":null,
+    "map":{"a":"x","c":"z"},
     "stringField":"str"
 }



Mime
View raw message