Author: etnu
Date: Thu Feb 12 07:19:15 2009
New Revision: 743636
URL: http://svn.apache.org/viewvc?rev=743636&view=rev
Log:
Enhanced JsonSerializer to discard properties with null rvalues to save on
output size. Client code doesn't distinguish between null and undefined.
Modified:
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/JsonSerializer.java
incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/JsonSerializerTest.java
Modified:
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/JsonSerializer.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/JsonSerializer.java?rev=743636&r1=743635&r2=743636&view=diff
==============================================================================
---
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/JsonSerializer.java
(original)
+++
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/JsonSerializer.java
Thu Feb 12 07:19:15 2009
@@ -43,6 +43,8 @@
*
* The append*() methods can be used to serialize directly into an Appendable,
such as an output
* stream. This avoids unnecessary copies to intermediate objects.
+ *
+ * To reduce output size, null values in json arrays and objects will always
be removed.
*/
public final class JsonSerializer {
// Multiplier to use for allocating the buffer.
@@ -184,15 +186,19 @@
buf.append('{');
boolean firstDone = false;
for (Map.Entry<String, Method> entry : methods.entrySet()) {
- if (firstDone) {
- buf.append(',');
- } else {
- firstDone = true;
- }
- appendString(buf, entry.getKey());
- buf.append(':');
try {
- append(buf, entry.getValue().invoke(pojo));
+ Object value = entry.getValue().invoke(pojo);
+ if (value != null) {
+ // Drop null values.
+ if (firstDone) {
+ buf.append(',');
+ } else {
+ firstDone = true;
+ }
+ appendString(buf, entry.getKey());
+ buf.append(':');
+ append(buf, value);
+ }
} catch (IllegalArgumentException e) {
// Shouldn't be possible.
throw new RuntimeException(e);
@@ -216,12 +222,14 @@
buf.append('[');
boolean firstDone = false;
for (Object o : array) {
- if (firstDone) {
- buf.append(',');
- } else {
- firstDone = true;
+ if (o != null) {
+ if (firstDone) {
+ buf.append(',');
+ } else {
+ firstDone = true;
+ }
+ append(buf, o);
}
- append(buf, o);
}
buf.append(']');
}
@@ -234,12 +242,15 @@
buf.append('[');
boolean firstDone = false;
for (int i = 0, j = array.length(); i < j; ++i) {
- if (firstDone) {
- buf.append(',');
- } else {
- firstDone = true;
+ Object value = array.opt(i);
+ if (value != null) {
+ if (firstDone) {
+ buf.append(',');
+ } else {
+ firstDone = true;
+ }
+ append(buf, value);
}
- append(buf, array.opt(i));
}
buf.append(']');
}
@@ -254,12 +265,14 @@
buf.append('[');
boolean firstDone = false;
for (Object o : collection) {
- if (firstDone) {
- buf.append(',');
- } else {
- firstDone = true;
+ if (o != null) {
+ if (firstDone) {
+ buf.append(',');
+ } else {
+ firstDone = true;
+ }
+ append(buf, o);
}
- append(buf, o);
}
buf.append(']');
}
@@ -269,19 +282,21 @@
*
* @throws IOException If {...@link Appendable#append(char)} throws an
exception.
*/
- public static void appendMap(Appendable buf, Map<String, ?> map)
- throws IOException {
+ public static void appendMap(Appendable buf, Map<String, ?> map) throws
IOException {
buf.append('{');
boolean firstDone = false;
for (Map.Entry<String, ?> entry : map.entrySet()) {
- if (firstDone) {
- buf.append(',');
- } else {
- firstDone = true;
+ Object value = entry.getValue();
+ if (value != null) {
+ if (firstDone) {
+ buf.append(',');
+ } else {
+ firstDone = true;
+ }
+ appendString(buf, entry.getKey());
+ buf.append(':');
+ append(buf, value);
}
- appendString(buf, entry.getKey());
- buf.append(':');
- append(buf, entry.getValue());
}
buf.append('}');
}
@@ -297,15 +312,18 @@
Iterator<String> keys = object.keys();
boolean firstDone = false;
while (keys.hasNext()) {
- if (firstDone) {
- buf.append(',');
- } else {
- firstDone = true;
- }
String key = keys.next();
- appendString(buf, key);
- buf.append(':');
- append(buf, object.opt(key));
+ Object value = object.opt(key);
+ if (value != null) {
+ if (firstDone) {
+ buf.append(',');
+ } else {
+ firstDone = true;
+ }
+ appendString(buf, key);
+ buf.append(':');
+ append(buf, value);
+ }
}
buf.append('}');
}
Modified:
incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/JsonSerializerTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/JsonSerializerTest.java?rev=743636&r1=743635&r2=743636&view=diff
==============================================================================
---
incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/JsonSerializerTest.java
(original)
+++
incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/JsonSerializerTest.java
Thu Feb 12 07:19:15 2009
@@ -33,6 +33,7 @@
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
+import java.util.HashMap;
import java.util.Map;
/**
@@ -51,30 +52,34 @@
@Test
public void serializeSimpleMap() throws Exception {
- Map<String, String> map = ImmutableMap.of("hello", "world", "foo", "bar");
+ Map<String, String> map = new HashMap<String, String>(3, 1);
+ map.put("hello", "world");
+ map.put("foo", "bar");
+ map.put("remove", null);
assertJsonEquals("{hello:'world',foo:'bar'}",
JsonSerializer.serialize(map));
}
@Test
public void serializeSimpleCollection() throws Exception {
- Collection<String> collection = Arrays.asList("foo", "bar", "baz");
+ Collection<String> collection = Arrays.asList("foo", null, "bar", "baz",
null);
assertJsonEquals("['foo','bar','baz']",
JsonSerializer.serialize(collection));
}
@Test
public void serializeArray() throws Exception {
- String[] array = new String[] {"foo", "bar", "baz"};
+ String[] array = new String[] {"foo", null, "bar", "baz"};
assertJsonEquals("['foo','bar','baz']", JsonSerializer.serialize(array));
}
@Test
public void serializeJsonArray() throws Exception {
- JSONArray array = new JSONArray(new String[] {"foo", "bar", "baz"});
+ JSONArray array = new JSONArray(new String[] {"foo", null, "bar", "baz"});
assertJsonEquals("['foo','bar','baz']", JsonSerializer.serialize(array));
}
@Test
public void serializePrimitives() throws Exception {
+ assertEquals("null", JsonSerializer.serialize((Object) null));
assertEquals("\"hello\"", JsonSerializer.serialize("hello"));
assertEquals("100", JsonSerializer.serialize(100));
assertEquals("125.0", JsonSerializer.serialize(125.0f));
@@ -104,6 +109,9 @@
return 3;
}
+ public Object getNullValue() {
+ return null;
+ }
}
@Test