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 2012/07/09 00:21:33 UTC
[1/9] git commit: Add JSONObject.in(String) to create (if needed) and
return a nested JSONObject
Updated Branches:
refs/heads/5.4-js-rewrite cea40f5af -> 358ac07b5
Add JSONObject.in(String) to create (if needed) and return a nested 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/358ac07b
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/358ac07b
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/358ac07b
Branch: refs/heads/5.4-js-rewrite
Commit: 358ac07b58d7783a0d235af255dc31a873cb64cb
Parents: ed52461
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Sun Jul 8 15:21:14 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Sun Jul 8 15:21:14 2012 -0700
----------------------------------------------------------------------
.../java/org/apache/tapestry5/json/JSONObject.java | 28 ++++++++++
.../test/groovy/json/specs/JSONObjectSpec.groovy | 40 +++++++++++++++
2 files changed, 68 insertions(+), 0 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/358ac07b/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 d4f8b6d..c765bb4 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
@@ -974,4 +974,32 @@ public final class JSONObject extends JSONCollection
return this;
}
+
+ /**
+ * Navigates into a nested JSONObject, creating the JSONObject if necessary. They key must not exist,
+ * or must be a JSONObject.
+ *
+ * @param key
+ * @return the nested JSONObject
+ * @throws IllegalStateException
+ * if the current value for the key is not null and not JSONObject
+ */
+ public JSONObject in(String key)
+ {
+ assert key != null;
+
+ Object nested = properties.get(key);
+
+ if (nested != null && !(nested instanceof JSONObject))
+ {
+ throw new IllegalStateException(String.format("JSONObject[%s] is not a JSONObject.", quote(key)));
+ }
+
+ if (nested == null) {
+ nested = new JSONObject();
+ properties.put(key, nested);
+ }
+
+ return (JSONObject) nested;
+ }
}
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/358ac07b/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 db99cb9..b158eec 100644
--- a/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy
+++ b/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy
@@ -789,4 +789,44 @@ class JSONObjectSpec extends Specification {
result.is object
object.toCompactString() == /{"wilma":"flintstone","fred":"flintstone","barney":"rubble"}/
}
+
+ def "in() where the value is not JSONObject is an exception"() {
+ def object = new JSONObject("time", "to rock")
+
+ when:
+
+ object.in("time")
+
+ then:
+
+ IllegalStateException e = thrown()
+
+ e.message == /JSONObject["time"] is not a JSONObject./
+ }
+
+ def "in() creates a JSONObject if necessary"() {
+ def object = new JSONObject()
+
+ when:
+
+ object.in("nested").put("created", true)
+
+ then:
+
+ object.toCompactString() == /{"nested":{"created":true}}/
+ }
+
+ def "in() returns the JSONObject"() {
+ def object = new JSONObject(/{nested: {}}/)
+
+ when:
+
+ object.in("nested").put("time", "to rock")
+
+ then:
+
+ object.toCompactString() == /{"nested":{"time":"to rock"}}/
+
+ }
+
}