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/02 01:53:11 UTC
[2/2] git commit: Add a copy() method to JSONObject
Add a copy() method to JSONObject
Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/1ff0ae9e
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/1ff0ae9e
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/1ff0ae9e
Branch: refs/heads/master
Commit: 1ff0ae9e21dd978e9df0e6854d009b0a6a032247
Parents: 9c574e7
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Wed Oct 16 18:09:28 2013 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Fri Nov 1 17:51:35 2013 -0700
----------------------------------------------------------------------
.../org/apache/tapestry5/json/JSONObject.java | 20 +++++++++++++++---
.../groovy/json/specs/JSONObjectSpec.groovy | 22 ++++++++++++++++++++
2 files changed, 39 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/1ff0ae9e/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 8b78ad8..575d1ea 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
@@ -1,4 +1,4 @@
-// Copyright 2007, 2010, 2011, 2012 The Apache Software Foundation
+// Copyright 2007-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.
@@ -150,10 +150,23 @@ public final class JSONObject extends JSONCollection
}
/**
+ * Returns a new JSONObject that is a shallow copy of this JSONObject.
+ *
+ * @since 5.4
+ */
+ public JSONObject copy()
+ {
+ JSONObject dupe = new JSONObject();
+ dupe.properties.putAll(properties);
+
+ return dupe;
+ }
+
+ /**
* Constructs a new JSONObject using a series of String keys and object values.
* Object values sholuld be compatible with {@link #put(String, Object)}. Keys must be strings
* (toString() will be invoked on each key).
- *
+ * <p/>
* Prior to release 5.4, keysAndValues was type String...; changing it to Object... makes
* it much easier to initialize a JSONObject in a single statement, which is more readable.
*
@@ -999,7 +1012,8 @@ public final class JSONObject extends JSONCollection
throw new IllegalStateException(String.format("JSONObject[%s] is not a JSONObject.", quote(key)));
}
- if (nested == null) {
+ if (nested == null)
+ {
nested = new JSONObject();
properties.put(key, nested);
}
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/1ff0ae9e/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 9500d33..4cdd50b 100644
--- a/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy
+++ b/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy
@@ -38,6 +38,28 @@ class JSONObjectSpec extends Specification {
fullCopy.toCompactString() == /{"fred":"flintstone","barney":"rubble"}/
}
+ def "copy all properties of JSONObject"() {
+ def master = new JSONObject("fred", "flintstone", "barney", "rubble")
+
+ when:
+
+ def fullCopy = master.copy()
+
+ then:
+
+ master == fullCopy
+
+ // And they are independent:
+
+ when:
+
+ master.put("wilma", "flintstone")
+
+ then:
+
+ master != fullCopy
+ }
+
def "unknown keys when copying a JSONObject are ignored"() {
def master = new JSONObject("fred", "flintstone", "barney", "rubble")