Add JSONObject.putAll(Map)
Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/806795e0 Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/806795e0 Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/806795e0 Branch: refs/heads/5.4-js-rewrite Commit: 806795e0e92daa2f92edcb509fcb5bebac8e2c7c Parents: a25b7ac Author: Howard M. Lewis Ship <hls...@apache.org> Authored: Sun Jul 8 14:59:04 2012 -0700 Committer: Howard M. Lewis Ship <hls...@apache.org> Committed: Sun Jul 8 14:59:04 2012 -0700 ---------------------------------------------------------------------- .../java/org/apache/tapestry5/json/JSONObject.java | 19 +++++++++++++++ .../test/groovy/json/specs/JSONObjectSpec.groovy | 13 ++++++++++ 2 files changed, 32 insertions(+), 0 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/806795e0/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 2ea4319..e171cc0 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 @@ -954,4 +954,23 @@ public final class JSONObject extends JSONCollection { return Collections.unmodifiableMap(properties); } + + /** + * Invokes {@link #put(String, Object)} for each value from the map. + * + * @param newProperties + * to add to this JSONObject + * @return this JSONObject + */ + public JSONObject putAll(Map<String, Object> newProperties) + { + assert newProperties != null; + + for (Map.Entry<String, Object> e : newProperties.entrySet()) + { + put(e.getKey(), e.getValue()); + } + + return this; + } } http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/806795e0/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 c4bd4ef..1741ce7 100644 --- a/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy +++ b/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy @@ -776,4 +776,17 @@ class JSONObjectSpec extends Specification { object.length() == 0 map.isEmpty() } + + def "can add new properties via putAll()"() { + def object = new JSONObject("fred", "flintstone") + + when: + + def result = object.putAll([barney: "rubble", wilma: "flintstone"]) + + then: + + result.is object + object.toCompactString() == /{"wilma":"flintstone","fred":"flintstone","barney":"rubble"}/ + } }