mirror of
https://github.com/apache/struts.git
synced 2026-08-07 07:37:20 +00:00
WW-3489 if excludeNullProperties, ignore nulls in maps
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1129976 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -5,6 +5,8 @@ import org.apache.struts2.json.annotations.JSONFieldBridge;
|
||||
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{
|
||||
@@ -27,6 +29,44 @@ public class JSONWriterTest extends StrutsTestCase{
|
||||
TestUtils.assertEquals(JSONWriter.class.getResource("jsonwriter-write-bean-01.txt"), json);
|
||||
}
|
||||
|
||||
@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();
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"booleanField":true,
|
||||
"byteField":0,
|
||||
"charField":"s",
|
||||
"doubleField":10.1,
|
||||
"enumBean":"Two",
|
||||
"enumField":"ValueA",
|
||||
"floatField":1.5,
|
||||
"intField":10,
|
||||
"longField":100,
|
||||
"map":{"a":"x","c":"z"},
|
||||
"stringField":"str"
|
||||
}
|
||||
Reference in New Issue
Block a user