You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2013/11/19 20:17:35 UTC

git commit: TAP5-2209" Make JSONObject and JSONArray implement Serializable

Updated Branches:
  refs/heads/master 15d1a8402 -> 04f0d2563


TAP5-2209" Make JSONObject and JSONArray implement Serializable


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/04f0d256
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/04f0d256
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/04f0d256

Branch: refs/heads/master
Commit: 04f0d25635ffe24e86acddd713d9d953e0a0b0fd
Parents: 15d1a84
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Tue Nov 19 11:17:21 2013 -0800
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Tue Nov 19 11:17:21 2013 -0800

----------------------------------------------------------------------
 .../apache/tapestry5/json/JSONCollection.java   |  5 ++--
 .../org/apache/tapestry5/json/JSONLiteral.java  |  6 +++--
 .../org/apache/tapestry5/json/JSONObject.java   | 10 +++++++-
 .../org/apache/tapestry5/json/package-info.java |  7 ++++--
 .../groovy/json/specs/JSONObjectSpec.groovy     | 26 +++++++++++++++++++-
 5 files changed, 46 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04f0d256/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONCollection.java
----------------------------------------------------------------------
diff --git a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONCollection.java b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONCollection.java
index e29ddbb..f06118b 100644
--- a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONCollection.java
+++ b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONCollection.java
@@ -1,4 +1,4 @@
-// Copyright 2010 The Apache Software Foundation
+// Copyright 2010, 2013 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@ package org.apache.tapestry5.json;
 
 import java.io.CharArrayWriter;
 import java.io.PrintWriter;
+import java.io.Serializable;
 
 /**
  * Base class for {@link JSONArray} and {@link JSONObject} that exists to organize the code
@@ -23,7 +24,7 @@ import java.io.PrintWriter;
  * 
  * @since 5.2.0
  */
-public abstract class JSONCollection
+public abstract class JSONCollection implements Serializable
 {
     /**
      * Converts this JSON collection into a parsable string representation.

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04f0d256/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONLiteral.java
----------------------------------------------------------------------
diff --git a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONLiteral.java b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONLiteral.java
index e5f7fc7..8627134 100644
--- a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONLiteral.java
+++ b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONLiteral.java
@@ -1,4 +1,4 @@
-// Copyright 2009 The Apache Software Foundation
+// Copyright 2009, 2013 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -14,6 +14,8 @@
 
 package org.apache.tapestry5.json;
 
+import java.io.Serializable;
+
 /**
  * A way of including some text (often, text that violates the normal JSON specification) as part of a JSON object or
  * array. This is used in a few places where data is nominally JSON but actually includes some non-conformant elements,
@@ -21,7 +23,7 @@ package org.apache.tapestry5.json;
  * 
  * @since 5.1.0.2
  */
-public class JSONLiteral implements JSONString
+public class JSONLiteral implements JSONString, Serializable
 {
     private final String text;
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04f0d256/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONObject.java
----------------------------------------------------------------------
diff --git a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONObject.java b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONObject.java
index c8ac282..c8dd777 100644
--- a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONObject.java
+++ b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONObject.java
@@ -34,6 +34,8 @@ package org.apache.tapestry5.json;
  * SOFTWARE.
  */
 
+import java.io.ObjectStreamException;
+import java.io.Serializable;
 import java.util.*;
 
 /**
@@ -98,7 +100,7 @@ public final class JSONObject extends JSONCollection
      * JSONObject.NULL is equivalent to the value that JavaScript calls null, whilst Java's null is equivalent to the
      * value that JavaScript calls undefined.
      */
-    private static final class Null implements JSONString
+    private static final class Null implements JSONString, Serializable
     {
         /**
          * A Null object is equal to the null value and to itself.
@@ -128,6 +130,12 @@ public final class JSONObject extends JSONCollection
         {
             return "null";
         }
+
+        // Serialization magic: after de-serializing, it will be back to the singleton instance of NULL.
+        private Object readResolve() throws ObjectStreamException
+        {
+            return NULL;
+        }
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04f0d256/tapestry-json/src/main/java/org/apache/tapestry5/json/package-info.java
----------------------------------------------------------------------
diff --git a/tapestry-json/src/main/java/org/apache/tapestry5/json/package-info.java b/tapestry-json/src/main/java/org/apache/tapestry5/json/package-info.java
index 75872e6..62c1972 100644
--- a/tapestry-json/src/main/java/org/apache/tapestry5/json/package-info.java
+++ b/tapestry-json/src/main/java/org/apache/tapestry5/json/package-info.java
@@ -1,4 +1,4 @@
-// Copyright 2012 The Apache Software Foundation
+// Copyright 2012, 2013 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -13,6 +13,9 @@
 // limitations under the License.
 
 /**
- * Repackaged, improved (and tested) version of code originally from json.org
+ * Repackaged, improved (and tested) version of code originally from json.org.
+ * <p/>
+ * Starting in release 5.4, {@link org.apache.tapestry5.json.JSONObject} and {@link org.apache.tapestry5.json.JSONArray}
+ * are serializable.
  */
 package org.apache.tapestry5.json;

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/04f0d256/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy
----------------------------------------------------------------------
diff --git a/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy b/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy
index 9258598..c2ffba4 100644
--- a/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy
+++ b/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy
@@ -726,7 +726,7 @@ class JSONObjectSpec extends Specification {
 
     def print(object, compact) {
 
-        withPrintWriter { pw -> object.print(pw, compact)}
+        withPrintWriter { pw -> object.print(pw, compact) }
     }
 
     def "prettyPrint() to PrintWriter"() {
@@ -863,4 +863,28 @@ class JSONObjectSpec extends Specification {
         object.get("false").is false
     }
 
+    private static copyViaSerialization(source) {
+
+        def bos = new ByteArrayOutputStream()
+
+        bos.withObjectOutputStream { it << source }
+
+        def bis = new ByteArrayInputStream(bos.toByteArray())
+
+        bis.withObjectInputStream { it.readObject() }
+    }
+
+    def "serialize and de-serialize"() {
+        when:
+
+        def source = new JSONObject("string", "a string", "null", JSONObject.NULL)
+
+        def copy = copyViaSerialization source
+
+        then:
+
+        source == copy
+
+    }
+
 }