org.apache.tapestry5.json

## Class JSONObject

- java.lang.Object
- - org.apache.tapestry5.json.JSONCollection
  - - org.apache.tapestry5.json.JSONObject

- All Implemented Interfaces:
    Serializable

      public final class JSONObject
      extends JSONCollection

  A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names. The internal form is an object having `get` and `opt` methods for accessing the values by name, and `put` methods for adding or replacing values by name. The values can be any of these types: `Boolean`, `JSONArray`, `JSONLiteral`, `JSONObject`, `Number`, `String`, or the `JSONObject.NULL` object. A JSONObject constructor can be used to convert an external form JSON text into an internal form whose values can be retrieved with the `get` and `opt` methods, or to convert values into a JSON text using the `put` and `toString` methods. A `get` method returns a value if one can be found, and throws an exception if one cannot be found. An `opt` method returns a default value instead of throwing an exception, and so is useful for obtaining optional values. The generic `get()` and `opt()` methods return an object, which you can cast or query for type. There are also typed `get` and `opt` methods that do type checking and type coersion for you. The `put` methods adds values to an object. For example,

       myString = new JSONObject().put("JSON", "Hello, World!").toString();

  produces the string `{"JSON": "Hello, World"}`. The texts produced by the `toString` methods strictly conform to the JSON syntax rules. The constructors are more forgiving in the texts they will accept:
  - An extra `,` (comma) may appear just before the closing brace.
  - Strings may be quoted with `'` (single quote).
  - Strings do not need to be quoted at all if they do not begin with a quote or single quote, and if they do not contain leading or trailing spaces, and if they do not contain any of these characters: `{ } [ ] / \ : , = ; #` and if they do not look like numbers and if they are not the reserved words `true`, `false`, or `null`.
  - Keys can be followed by `=` or `=>` as well as by `:`.
  - Values can be followed by `;` (semicolon) as well as by `,` (comma).
  - Numbers may have the `0-` (octal) or `0x-` (hex) prefix.
  - Comments written in the slashshlash, slashstar, and hash conventions will be ignored.

  This class, and the other related classes, have been heavily modified from the original source, to fit Tapestry standards and to make use of JDK 1.5 features such as generics. Further, since the interest of Tapestry is primarily constructing JSON (and not parsing it), many of the non-essential methods have been removed (since the original code came with no tests). Finally, support for the `JSONLiteral` type has been added, which allows the exact output to be controlled; useful when a JSONObject is being used as a configuration object, and must contain values that are not simple data, such as an inline function (making the result not JSON).
  See Also:
    Serialized Form

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static Object` | `NULL` It is sometimes more convenient and less ambiguous to have a `NULL` object than to use Java's `null` value.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `JSONObject()` Construct an empty JSONObject.
    `JSONObject(JSONObject source, String... propertyNames)` Construct a JSONObject from a subset of another JSONObject.
    `JSONObject(Object... keysAndValues)` Constructs a new JSONObject using a series of String keys and object values.
    `JSONObject(String string)` Construct a JSONObject from a string.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `JSONObject` | `accumulate(String key, Object value)` Accumulate values under a key.
    `JSONObject` | `append(String key, Object value)` Append values to the array under a key.
    `JSONObject` | `copy()` Returns a new JSONObject that is a shallow copy of this JSONObject.
    `boolean` | `equals(Object obj)` Returns true if the other object is a JSONObject and its set of properties matches this object's properties.
    `Object` | `get(String key)` Get the value object associated with a key.
    `boolean` | `getBoolean(String key)` Get the boolean value associated with a key.
    `double` | `getDouble(String key)` Get the double value associated with a key.
    `int` | `getInt(String key)` Get the int value associated with a key.
    `JSONArray` | `getJSONArray(String key)` Get the JSONArray value associated with a key.
    `JSONObject` | `getJSONObject(String key)` Get the JSONObject value associated with a key.
    `long` | `getLong(String key)` Get the long value associated with a key.
    `String` | `getString(String key)` Get the string associated with a key.
    `boolean` | `has(String key)` Determine if the JSONObject contains a specific key.
    `JSONObject` | `in(String key)` Navigates into a nested JSONObject, creating the JSONObject if necessary.
    `boolean` | `isNull(String key)` Determine if the value associated with the key is null or if there is no value.
    `Set<String>` | `keys()` Get an enumeration of the keys of the JSONObject.
    `int` | `length()` Get the number of keys stored in the JSONObject.
    `JSONArray` | `names()` Produce a JSONArray containing the names of the elements of this JSONObject.
    `Object` | `opt(String key)` Get an optional value associated with a key.
    `JSONObject` | `put(String key, Object value)` Put a key/value pair in the JSONObject.
    `JSONObject` | `putAll(Map<String,?> newProperties)` Invokes `put(String, Object)` for each value from the map.
    `static String` | `quote(String string)` Produce a string in double quotes with backslash sequences in all the right places, allowing JSON text to be delivered in HTML.
    `Object` | `remove(String key)` Remove a name and its value, if present.
    `Map<String,Object>` | `toMap()` Returns a Map of the keys and values of the JSONObject.

    - ### Methods inherited from class org.apache.tapestry5.json.JSONCollection

      `prettyPrint, print, print, toCompactString, toString, toString`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### NULL

          public static final Object NULL

      It is sometimes more convenient and less ambiguous to have a `NULL` object than to use Java's `null` value. `JSONObject.NULL.equals(null)` returns `true`. `JSONObject.NULL.toString()` returns `"null"`.

  - ### Constructor Detail

    - #### JSONObject

          public JSONObject()

      Construct an empty JSONObject.

    - #### JSONObject

          public JSONObject(Object... keysAndValues)

      Constructs a new JSONObject using a series of String keys and object values. Object values sholuld be compatible with `put(String, Object)`. Keys must be strings (toString() will be invoked on each key). 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.
      Since:
        5.2.0

    - #### JSONObject

          public JSONObject(JSONObject source,
                            String... propertyNames)

      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.
      Parameters:
        `source` - A JSONObject.
        `propertyNames` - The strings to copy.
      Throws:
        `RuntimeException` - If a value is a non-finite number.

    - #### JSONObject

          public JSONObject(String string)

      Construct a JSONObject from a string. This is the most commonly used JSONObject constructor.
      Parameters:
        `string` - A string beginning with `{` (left brace) and ending with `}` (right brace).
      Throws:
        `RuntimeException` - If there is a syntax error in the source string.

  - ### Method Detail

    - #### copy

          public JSONObject copy()

      Returns a new JSONObject that is a shallow copy of this JSONObject.
      Since:
        5.4

    - #### accumulate

          public JSONObject accumulate(String key,
                                       Object value)

      Accumulate values under a key. It is similar to the put method except that if there is already an object stored 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.
      Parameters:
        `key` - A key string.
        `value` - An object to be accumulated under the key.
      Returns:
        this.
      Throws:
        `RuntimeException` - if the value is an invalid number or if the key is null.

    - #### append

          public JSONObject append(String key,
                                   Object value)

      Append values to the array under a key. If the key does not exist in the JSONObject, then the key is put in the 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.
      Parameters:
        `key` - A key string.
        `value` - An object to be accumulated under the key.
      Returns:
        this. @ If the key is null or if the current value associated with the key is not a JSONArray.

    - #### get

          public Object get(String key)

      Get the value object associated with a key.
      Parameters:
        `key` - A key string.
      Returns:
        The object associated with the key.
      Throws:
        `RuntimeException` - if the key is not found.
      See Also:
        `opt(String)`

    - #### getBoolean

          public boolean getBoolean(String key)

      Get the boolean value associated with a key.
      Parameters:
        `key` - A key string.
      Returns:
        The truth.
      Throws:
        `RuntimeException` - if the value does not exist, is not a Boolean or the String "true" or "false".

    - #### getDouble

          public double getDouble(String key)

      Get the double value associated with a key.
      Parameters:
        `key` - A key string.
      Returns:
        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.

    - #### getInt

          public int getInt(String key)

      Get the int value associated with a key. If the number value is too large for an int, it will be clipped.
      Parameters:
        `key` - A key string.
      Returns:
        The integer value.
      Throws:
        `RuntimeException` - if the key is not found or if the value cannot be converted to an integer.

    - #### getJSONArray

          public JSONArray getJSONArray(String key)

      Get the JSONArray value associated with a key.
      Parameters:
        `key` - A key string.
      Returns:
        A JSONArray which is the value.
      Throws:
        `RuntimeException` - if the key is not found or if the value is not a JSONArray.

    - #### getJSONObject

          public JSONObject getJSONObject(String key)

      Get the JSONObject value associated with a key.
      Parameters:
        `key` - A key string.
      Returns:
        A JSONObject which is the value.
      Throws:
        `RuntimeException` - if the key is not found or if the value is not a JSONObject.

    - #### getLong

          public long getLong(String key)

      Get the long value associated with a key. If the number value is too long for a long, it will be clipped.
      Parameters:
        `key` - A key string.
      Returns:
        The long value.
      Throws:
        `RuntimeException` - if the key is not found or if the value cannot be converted to a long.

    - #### getString

          public String getString(String key)

      Get the string associated with a key.
      Parameters:
        `key` - A key string.
      Returns:
        A string which is the value.
      Throws:
        `RuntimeException` - if the key is not found.

    - #### has

          public boolean has(String key)

      Determine if the JSONObject contains a specific key.
      Parameters:
        `key` - A key string.
      Returns:
        true if the key exists in the JSONObject.

    - #### isNull

          public boolean isNull(String key)

      Determine if the value associated with the key is null or if there is no value.
      Parameters:
        `key` - A key string.
      Returns:
        true if there is no value associated with the key or if the value is the JSONObject.NULL object.

    - #### keys

          public Set<String> keys()

      Get an enumeration of the keys of the JSONObject. Caution: the set should not be modified.
      Returns:
        An iterator of the keys.

    - #### length

          public int length()

      Get the number of keys stored in the JSONObject.
      Returns:
        The number of keys in the JSONObject.

    - #### names

          public JSONArray names()

      Produce a JSONArray containing the names of the elements of this JSONObject.
      Returns:
        A JSONArray containing the key strings, or null if the JSONObject is empty.

    - #### opt

          public Object opt(String key)

      Get an optional value associated with a key.
      Parameters:
        `key` - A key string.
      Returns:
        An object which is the value, or null if there is no value.
      See Also:
        `get(String)`

    - #### put

          public JSONObject put(String key,
                                Object value)

      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.
      Parameters:
        `key` - A key string.
        `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.
      Returns:
        this.
      Throws:
        `RuntimeException` - If the value is non-finite number or if the key is null.

    - #### quote

          public static String quote(String string)

      Produce a string in double quotes with backslash sequences in all the right places, allowing JSON text to be delivered in HTML. In JSON text, a string cannot contain a control character or an unescaped quote or backslash.
      Parameters:
        `string` - A String
      Returns:
        A String correctly formatted for insertion in a JSON text.

    - #### remove

          public Object remove(String key)

      Remove a name and its value, if present.
      Parameters:
        `key` - The name to be removed.
      Returns:
        The value that was associated with the name, or null if there was no value.

    - #### equals

          public boolean equals(Object obj)

      Returns true if the other object is a JSONObject and its set of properties matches this object's properties.
      Overrides:
        `equals` in class `Object`

    - #### toMap

          public Map<String,Object> toMap()

      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 `NULL` in the map.
      Returns:
        unmodifiable map of properties and values
      Since:
        5.4

    - #### putAll

          public JSONObject putAll(Map<String,?> newProperties)

      Invokes `put(String, Object)` for each value from the map.
      Parameters:
        `newProperties` - to add to this JSONObject
      Returns:
        this JSONObject
      Since:
        5.4

    - #### in

          public JSONObject in(String key)

      Navigates into a nested JSONObject, creating the JSONObject if necessary. They key must not exist, or must be a JSONObject.
      Parameters:
        `key` -
      Returns:
        the nested JSONObject
      Throws:
        `IllegalStateException` - if the current value for the key is not null and not JSONObject

${project.version} - Copyright © 2003-2015 The Apache Software Foundation.