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
[7/9] git commit: Add JSONObject.toMap()
Add JSONObject.toMap()
Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/98794494
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/98794494
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/98794494
Branch: refs/heads/5.4-js-rewrite
Commit: 98794494b1f8ec3004705a52a51c10a21c097d4e
Parents: fc6a64d
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Sun Jul 8 14:48:38 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Sun Jul 8 14:48:38 2012 -0700
----------------------------------------------------------------------
.../java/org/apache/tapestry5/json/JSONObject.java | 131 ++++++++++-----
.../test/groovy/json/specs/JSONObjectSpec.groovy | 45 +++++
2 files changed, 136 insertions(+), 40 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/98794494/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 1487074..2ea4319 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
@@ -104,7 +104,8 @@ public final class JSONObject extends JSONCollection
/**
* A Null object is equal to the null value and to itself.
*
- * @param object An object to test for nullness.
+ * @param object
+ * An object to test for nullness.
* @return true if the object parameter is the JSONObject.NULL object or null.
*/
@Override
@@ -168,9 +169,12 @@ public final class JSONObject extends JSONCollection
* Construct a JSONObject from a subset of another JSONObject. An array of strings is used to identify the keys that
* should be copied. Missing keys are ignored.
*
- * @param source A JSONObject.
- * @param propertyNames The strings to copy.
- * @throws RuntimeException If a value is a non-finite number.
+ * @param source
+ * A JSONObject.
+ * @param propertyNames
+ * The strings to copy.
+ * @throws RuntimeException
+ * If a value is a non-finite number.
*/
public JSONObject(JSONObject source, String... propertyNames)
{
@@ -186,7 +190,8 @@ public final class JSONObject extends JSONCollection
/**
* Construct a JSONObject from a JSONTokener.
*
- * @param x A JSONTokener object containing the source string. @ If there is a syntax error in the source string.
+ * @param x
+ * A JSONTokener object containing the source string. @ If there is a syntax error in the source string.
*/
JSONObject(JSONTokener x)
{
@@ -253,9 +258,11 @@ public final class JSONObject extends JSONCollection
/**
* Construct a JSONObject from a string. This is the most commonly used JSONObject constructor.
*
- * @param string A string beginning with <code>{</code> <small>(left brace)</small> and ending with <code>}</code>
- * <small>(right brace)</small>.
- * @throws RuntimeException If there is a syntax error in the source string.
+ * @param string
+ * A string beginning with <code>{</code> <small>(left brace)</small> and ending with <code>}</code>
+ * <small>(right brace)</small>.
+ * @throws RuntimeException
+ * If there is a syntax error in the source string.
*/
public JSONObject(String string)
{
@@ -267,10 +274,13 @@ public final class JSONObject extends JSONCollection
* under the key then a JSONArray is stored under the key to hold all of the accumulated values. If there is already
* a JSONArray, then the new value is appended to it. In contrast, the put method replaces the previous value.
*
- * @param key A key string.
- * @param value An object to be accumulated under the key.
+ * @param key
+ * A key string.
+ * @param value
+ * An object to be accumulated under the key.
* @return this.
- * @throws {@link RuntimeException} If the value is an invalid number or if the key is null.
+ * @throws {@link
+ * RuntimeException} If the value is an invalid number or if the key is null.
*/
public JSONObject accumulate(String key, Object value)
{
@@ -305,8 +315,10 @@ public final class JSONObject extends JSONCollection
* JSONObject with its value being a JSONArray containing the value parameter. If the key was already associated
* with a JSONArray, then the value parameter is appended to it.
*
- * @param key A key string.
- * @param value An object to be accumulated under the key.
+ * @param key
+ * A key string.
+ * @param value
+ * An object to be accumulated under the key.
* @return this. @ If the key is null or if the current value associated with the key is not a JSONArray.
*/
public JSONObject append(String key, Object value)
@@ -330,7 +342,8 @@ public final class JSONObject extends JSONCollection
/**
* Produce a string from a double. The string "null" will be returned if the number is not finite.
*
- * @param d A double.
+ * @param d
+ * A double.
* @return A String.
*/
static String doubleToString(double d)
@@ -360,9 +373,11 @@ public final class JSONObject extends JSONCollection
/**
* Get the value object associated with a key.
*
- * @param key A key string.
+ * @param key
+ * A key string.
* @return The object associated with the key.
- * @throws RuntimeException if the key is not found.
+ * @throws RuntimeException
+ * if the key is not found.
* @see #opt(String)
*/
public Object get(String key)
@@ -379,9 +394,11 @@ public final class JSONObject extends JSONCollection
/**
* Get the boolean value associated with a key.
*
- * @param key A key string.
+ * @param key
+ * A key string.
* @return The truth.
- * @throws RuntimeException if the value does not exist, is not a Boolean or the String "true" or "false".
+ * @throws RuntimeException
+ * if the value does not exist, is not a Boolean or the String "true" or "false".
*/
public boolean getBoolean(String key)
{
@@ -407,7 +424,8 @@ public final class JSONObject extends JSONCollection
/**
* Get the double value associated with a key.
*
- * @param key A key string.
+ * @param key
+ * A key string.
* @return The numeric value. @throws RuntimeException if the key is not found or if the value is not a Number object and cannot be
* converted to a number.
*/
@@ -432,9 +450,11 @@ public final class JSONObject extends JSONCollection
/**
* Get the int value associated with a key. If the number value is too large for an int, it will be clipped.
*
- * @param key A key string.
+ * @param key
+ * A key string.
* @return The integer value.
- * @throws RuntimeException if the key is not found or if the value cannot be converted to an integer.
+ * @throws RuntimeException
+ * if the key is not found or if the value cannot be converted to an integer.
*/
public int getInt(String key)
{
@@ -450,9 +470,11 @@ public final class JSONObject extends JSONCollection
/**
* Get the JSONArray value associated with a key.
*
- * @param key A key string.
+ * @param key
+ * A key string.
* @return A JSONArray which is the value.
- * @throws RuntimeException if the key is not found or if the value is not a JSONArray.
+ * @throws RuntimeException
+ * if the key is not found or if the value is not a JSONArray.
*/
public JSONArray getJSONArray(String key)
{
@@ -468,9 +490,11 @@ public final class JSONObject extends JSONCollection
/**
* Get the JSONObject value associated with a key.
*
- * @param key A key string.
+ * @param key
+ * A key string.
* @return A JSONObject which is the value.
- * @throws RuntimeException if the key is not found or if the value is not a JSONObject.
+ * @throws RuntimeException
+ * if the key is not found or if the value is not a JSONObject.
*/
public JSONObject getJSONObject(String key)
{
@@ -486,9 +510,11 @@ public final class JSONObject extends JSONCollection
/**
* Get the long value associated with a key. If the number value is too long for a long, it will be clipped.
*
- * @param key A key string.
+ * @param key
+ * A key string.
* @return The long value.
- * @throws RuntimeException if the key is not found or if the value cannot be converted to a long.
+ * @throws RuntimeException
+ * if the key is not found or if the value cannot be converted to a long.
*/
public long getLong(String key)
{
@@ -499,9 +525,11 @@ public final class JSONObject extends JSONCollection
/**
* Get the string associated with a key.
*
- * @param key A key string.
+ * @param key
+ * A key string.
* @return A string which is the value.
- * @throws RuntimeException if the key is not found.
+ * @throws RuntimeException
+ * if the key is not found.
*/
public String getString(String key)
{
@@ -511,7 +539,8 @@ public final class JSONObject extends JSONCollection
/**
* Determine if the JSONObject contains a specific key.
*
- * @param key A key string.
+ * @param key
+ * A key string.
* @return true if the key exists in the JSONObject.
*/
public boolean has(String key)
@@ -522,7 +551,8 @@ public final class JSONObject extends JSONCollection
/**
* Determine if the value associated with the key is null or if there is no value.
*
- * @param key A key string.
+ * @param key
+ * A key string.
* @return true if there is no value associated with the key or if the value is the JSONObject.NULL object.
*/
public boolean isNull(String key)
@@ -570,7 +600,8 @@ public final class JSONObject extends JSONCollection
/**
* Produce a string from a Number.
*
- * @param n A Number
+ * @param n
+ * A Number
* @return A String. @ If n is a non-finite number.
*/
static String numberToString(Number n)
@@ -599,7 +630,8 @@ public final class JSONObject extends JSONCollection
/**
* Get an optional value associated with a key.
*
- * @param key A key string.
+ * @param key
+ * A key string.
* @return An object which is the value, or null if there is no value.
* @see #get(String)
*/
@@ -612,11 +644,14 @@ public final class JSONObject extends JSONCollection
* Put a key/value pair in the JSONObject. If the value is null, then the key will be removed from the JSONObject if
* it is present.
*
- * @param key A key string.
- * @param value An object which is the value. It should be of one of these types: Boolean, Double, Integer,
- * JSONArray, JSONObject, JSONLiteral, Long, String, or the JSONObject.NULL object.
+ * @param key
+ * A key string.
+ * @param value
+ * An object which is the value. It should be of one of these types: Boolean, Double, Integer,
+ * JSONArray, JSONObject, JSONLiteral, Long, String, or the JSONObject.NULL object.
* @return this.
- * @throws RuntimeException If the value is non-finite number or if the key is null.
+ * @throws RuntimeException
+ * If the value is non-finite number or if the key is null.
*/
public JSONObject put(String key, Object value)
{
@@ -639,7 +674,8 @@ public final class JSONObject extends JSONCollection
* within </, allowing JSON text to be delivered in HTML. In JSON text, a string cannot contain a control character
* or an unescaped quote or backslash.
*
- * @param string A String
+ * @param string
+ * A String
* @return A String correctly formatted for insertion in a JSON text.
*/
public static String quote(String string)
@@ -708,7 +744,8 @@ public final class JSONObject extends JSONCollection
/**
* Remove a name and its value, if present.
*
- * @param key The name to be removed.
+ * @param key
+ * The name to be removed.
* @return The value that was associated with the name, or null if there was no value.
*/
public Object remove(String key)
@@ -723,7 +760,8 @@ public final class JSONObject extends JSONCollection
/**
* Throw an exception if the object is an NaN or infinite number, or not a type which may be stored.
*
- * @param value The object to test. @ If o is a non-finite number.
+ * @param value
+ * The object to test. @ If o is a non-finite number.
*/
@SuppressWarnings("unchecked")
static void testValidity(Object value)
@@ -903,4 +941,17 @@ public final class JSONObject extends JSONCollection
return properties.equals(other.properties);
}
+
+ /**
+ * Returns a Map of the keys and values of the JSONObject. The returned map is unmodifiable.
+ * Note that changes to the JSONObject will be reflected in the map. In addition, null values in the JSONObject
+ * are represented as {@link JSONObject#NULL} in the map.
+ *
+ * @return unmodifiable map of properties and values
+ * @since 5.4
+ */
+ public Map<String, Object> toMap()
+ {
+ return Collections.unmodifiableMap(properties);
+ }
}
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/98794494/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 0500b94..c4bd4ef 100644
--- a/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy
+++ b/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy
@@ -731,4 +731,49 @@ class JSONObjectSpec extends Specification {
array.getString(1) == "2"
array.getString(2) == "false"
}
+
+ def "toMap() is not modifiable"() {
+ def object = new JSONObject()
+
+ def map = object.toMap()
+
+ when:
+
+ map.clear()
+
+ then:
+
+ thrown(UnsupportedOperationException)
+ }
+
+ def "can access contents of object as a map"() {
+ def object = new JSONObject("foo", "bar")
+ .put("null", JSONObject.NULL)
+ .put("number", 6)
+
+ when:
+
+ def map = object.toMap()
+
+ then:
+
+ map.foo == "bar"
+ map.number == 6
+ map["null"].is(JSONObject.NULL)
+ }
+
+ def "the map returned by toMap() is live"() {
+ def object = new JSONObject("foo", "bar")
+
+ def map = object.toMap()
+
+ when:
+
+ object.put "foo", null
+
+ then:
+
+ object.length() == 0
+ map.isEmpty()
+ }
}