Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.apache.tapestry5.json
Class JSONArray

    java.lang.Object
      org.apache.tapestry5.json.JSONCollection
          org.apache.tapestry5.json.JSONArray

All Implemented Interfaces:
  java.lang.Iterable<java.lang.Object>

    public final class JSONArray

extends JSONCollection
implements java.lang.Iterable<java.lang.Object>

A JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with commas separating the values. The internal form is an object having `get` and `opt` methods for accessing the values by index, and `put` methods for adding or replacing values. The values can be any of these types: `Boolean`, `JSONArray`, `JSONObject`, `Number`, `String`, or the `JSONObject.NULL object`.

The constructor can convert a JSON text into a Java object. The `toString` method converts to JSON text.

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 texts produced by the `toString` methods strictly conform to JSON syntax rules. The constructors are more forgiving in the texts they will accept:

- An extra `,` (comma) may appear just before the closing bracket.
- The `null` value will be inserted when there is `,` (comma) elision.
- 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`.
- Values can be separated 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.

Constructor Summary
`JSONArray()` Construct an empty JSONArray.
`JSONArray(java.lang.Object... values)`
`JSONArray(java.lang.String text)`

Method Summary
`boolean` | `equals(java.lang.Object obj)`
`java.lang.Object` | `get(int index)` Get the object value associated with an index.
`boolean` | `getBoolean(int index)` Get the boolean value associated with an index.
`double` | `getDouble(int index)` Get the double value associated with an index.
`int` | `getInt(int index)` Get the int value associated with an index.
`JSONArray` | `getJSONArray(int index)` Get the JSONArray associated with an index.
`JSONObject` | `getJSONObject(int index)` Get the JSONObject associated with an index.
`long` | `getLong(int index)` Get the long value associated with an index.
`java.lang.String` | `getString(int index)` Get the string associated with an index.
`boolean` | `isNull(int index)` Determine if the value is null.
`java.util.Iterator<java.lang.Object>` | `iterator()`
`int` | `length()` Get the number of elements in the JSONArray, included nulls.
`JSONArray` | `put(int index, java.lang.Object value)` Put or replace an object value in the JSONArray.
`JSONArray` | `put(java.lang.Object value)` Append an object value.
`java.lang.Object` | `remove(int index)` Remove the object associated with the index.

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`

Constructor Detail

### JSONArray

    public JSONArray()

  Construct an empty JSONArray.

### JSONArray

    public JSONArray(java.lang.String text)

### JSONArray

    public JSONArray(java.lang.Object... values)

Method Detail

### iterator

    public java.util.Iterator<java.lang.Object> iterator()

  Specified by:
    `iterator` in interface `java.lang.Iterable<java.lang.Object>`

### get

    public java.lang.Object get(int index)

  Get the object value associated with an index.

  Parameters:
    `index` - The index must be between 0 and length() - 1.
  Returns:
    An object value.
  Throws:
    `java.lang.RuntimeException` - If there is no value for the index.

### remove

    public java.lang.Object remove(int index)

  Remove the object associated with the index.

  Parameters:
    `index` - The index must be between 0 and length() - 1.
  Returns:
    An object removed.
  Throws:
    `java.lang.RuntimeException` - If there is no value for the index.

### getBoolean

    public boolean getBoolean(int index)

  Get the boolean value associated with an index. The string values "true" and "false" are converted to boolean.

  Parameters:
    `index` - The index must be between 0 and length() - 1.
  Returns:
    The truth.
  Throws:
    `java.lang.RuntimeException` - If there is no value for the index or if the value is not convertable to boolean.

### getDouble

    public double getDouble(int index)

  Get the double value associated with an index.

  Parameters:
    `index` - The index must be between 0 and length() - 1.
  Returns:
    The value.
  Throws:
    `java.lang.IllegalArgumentException` - If the key is not found or if the value cannot be converted to a number.

### getInt

    public int getInt(int index)

  Get the int value associated with an index.

  Parameters:
    `index` - The index must be between 0 and length() - 1.
  Returns:
    The value.
  Throws:
    `java.lang.IllegalArgumentException` - If the key is not found or if the value cannot be converted to a number. if the value cannot be converted to a number.

### getJSONArray

    public JSONArray getJSONArray(int index)

  Get the JSONArray associated with an index.

  Parameters:
    `index` - The index must be between 0 and length() - 1.
  Returns:
    A JSONArray value.
  Throws:
    `java.lang.RuntimeException` - If there is no value for the index. or if the value is not a JSONArray

### getJSONObject

    public JSONObject getJSONObject(int index)

  Get the JSONObject associated with an index.

  Parameters:
    `index` - subscript
  Returns:
    A JSONObject value.
  Throws:
    `java.lang.RuntimeException` - If there is no value for the index or if the value is not a JSONObject

### getLong

    public long getLong(int index)

  Get the long value associated with an index.

  Parameters:
    `index` - The index must be between 0 and length() - 1.
  Returns:
    The value.
  Throws:
    `java.lang.IllegalArgumentException` - If the key is not found or if the value cannot be converted to a number.

### getString

    public java.lang.String getString(int index)

  Get the string associated with an index.

  Parameters:
    `index` - The index must be between 0 and length() - 1.
  Returns:
    A string value.
  Throws:
    `java.lang.RuntimeException` - If there is no value for the index.

### isNull

    public boolean isNull(int index)

  Determine if the value is null.

  Parameters:
    `index` - The index must be between 0 and length() - 1.
  Returns:
    true if the value at the index is null, or if there is no value.

### length

    public int length()

  Get the number of elements in the JSONArray, included nulls.

  Returns:
    The length (or size).

### put

    public JSONArray put(java.lang.Object value)

  Append an object value. This increases the array's length by one.

  Parameters:
    `value` - An object value. The value should be a Boolean, Double, Integer, JSONArray, JSONObject, JSONLiteral, Long, or String, or the JSONObject.NULL singleton.
  Returns:
    this array

### put

    public JSONArray put(int index,
                         java.lang.Object value)

  Put or replace an object value in the JSONArray. If the index is greater than the length of the JSONArray, then null elements will be added as necessary to pad it out.

  Parameters:
    `index` - The subscript.
    `value` - The value to put into the array. The value should be a Boolean, Double, Integer, JSONArray, JSONObject, JSONString, Long, or String, or the JSONObject.NULL singeton.
  Returns:
    this array
  Throws:
    `java.lang.RuntimeException` - If the index is negative or if the the value is an invalid number.

### equals

    public boolean equals(java.lang.Object obj)

  Overrides:
    `equals` in class `java.lang.Object`

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2003-2012 The Apache Software Foundation.