();
}
public void add( int index, Object value ) {
add( index, value, new JsonConfig() );
}
public void add( int index, Object value, JsonConfig jsonConfig ) {
this.elements.add( index, processValue( value, jsonConfig ) );
}
public boolean add( Object value ) {
return add( value, new JsonConfig() );
}
public boolean add( Object value, JsonConfig jsonConfig ) {
element( value, jsonConfig );
return true;
}
public boolean addAll( Collection collection ) {
return addAll( collection, new JsonConfig() );
}
public boolean addAll( Collection collection, JsonConfig jsonConfig ) {
if( collection == null || collection.size() == 0 ){
return false;
}
for (Object a : collection) {
element(a, jsonConfig);
}
return true;
}
public boolean addAll( int index, Collection collection ) {
return addAll( index, collection, new JsonConfig() );
}
public boolean addAll( int index, Collection collection, JsonConfig jsonConfig ) {
if( collection == null || collection.size() == 0 ){
return false;
}
int offset = 0;
for (Object a : collection) {
this.elements.add(index + (offset++), processValue(a, jsonConfig));
}
return true;
}
public void clear() {
elements.clear();
}
public int compareTo( Object obj ) {
if( obj != null && (obj instanceof JSONArray) ){
JSONArray other = (JSONArray) obj;
int size1 = size();
int size2 = other.size();
if( size1 < size2 ){
return -1;
}else if( size1 > size2 ){
return 1;
}else if( this.equals( other ) ){
return 0;
}
}
return -1;
}
public boolean contains( Object o ) {
return contains( o, new JsonConfig() );
}
public boolean contains( Object o, JsonConfig jsonConfig ) {
return elements.contains( processValue( o, jsonConfig ) );
}
public boolean containsAll( Collection collection ) {
return containsAll( collection, new JsonConfig() );
}
public boolean containsAll( Collection collection, JsonConfig jsonConfig ) {
return elements.containsAll( fromObject( collection, jsonConfig ) );
}
/**
* Remove an element, if present.
*
* @param index the index of the element.
* @return this.
*/
public JSONArray discard( int index ) {
elements.remove( index );
return this;
}
/**
* Remove an element, if present.
*
* @param index the element.
* @return this.
*/
public JSONArray discard( Object o ) {
elements.remove( o );
return this;
}
/**
* Append a boolean value. This increases the array's length by one.
*
* @param value A boolean value.
* @return this.
*/
public JSONArray element( boolean value ) {
return element( value ? Boolean.TRUE : Boolean.FALSE );
}
/**
* Append a value in the JSONArray, where the value will be a JSONArray which
* is produced from a Collection.
*
* @param value A Collection value.
* @return this.
*/
public JSONArray element( Collection value ) {
return element( value, new JsonConfig() );
}
/**
* Append a value in the JSONArray, where the value will be a JSONArray which
* is produced from a Collection.
*
* @param value A Collection value.
* @return this.
*/
public JSONArray element( Collection value, JsonConfig jsonConfig ) {
if( value instanceof JSONArray ){
elements.add( value );
return this;
}else{
return element( _fromCollection( value, jsonConfig ) );
}
}
/**
* Append a double value. This increases the array's length by one.
*
* @param value A double value.
* @throws JSONException if the value is not finite.
* @return this.
*/
public JSONArray element( double value ) {
Double d = value;
JSONUtils.testValidity( d );
return element( d );
}
/**
* Append an int value. This increases the array's length by one.
*
* @param value An int value.
* @return this.
*/
public JSONArray element( int value ) {
return element( new Integer( value ) );
}
/**
* Put or replace a boolean 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.
*
* @param index The subscript.
* @param value A boolean value.
* @return this.
* @throws JSONException If the index is negative.
*/
public JSONArray element( int index, boolean value ) {
return element( index, value ? Boolean.TRUE : Boolean.FALSE );
}
/**
* Put a value in the JSONArray, where the value will be a JSONArray which is
* produced from a Collection.
*
* @param index The subscript.
* @param value A Collection value.
* @return this.
* @throws JSONException If the index is negative or if the value is not
* finite.
*/
public JSONArray element( int index, Collection value ) {
return element( index, value, new JsonConfig() );
}
/**
* Put a value in the JSONArray, where the value will be a JSONArray which is
* produced from a Collection.
*
* @param index The subscript.
* @param value A Collection value.
* @return this.
* @throws JSONException If the index is negative or if the value is not
* finite.
*/
public JSONArray element( int index, Collection value, JsonConfig jsonConfig ) {
if( value instanceof JSONArray ){
if( index < 0 ){
throw new JSONException( "JSONArray[" + index + "] not found." );
}
if( index < size() ){
elements.set( index, value );
}else{
while( index != size() ){
element( JSONNull.getInstance() );
}
element( value, jsonConfig );
}
return this;
}else{
return element( index, _fromCollection( value, jsonConfig ) );
}
}
/**
* Put or replace a double value. If the index is greater than the length of
* the JSONArray, then null elements will be added as necessary to pad it
* out.
*
* @param index The subscript.
* @param value A double value.
* @return this.
* @throws JSONException If the index is negative or if the value is not
* finite.
*/
public JSONArray element( int index, double value ) {
return element( index, new Double( value ) );
}
/**
* Put or replace an int value. If the index is greater than the length of
* the JSONArray, then null elements will be added as necessary to pad it
* out.
*
* @param index The subscript.
* @param value An int value.
* @return this.
* @throws JSONException If the index is negative.
*/
public JSONArray element( int index, int value ) {
return element( index, new Integer( value ) );
}
/**
* Put or replace a long value. If the index is greater than the length of
* the JSONArray, then null elements will be added as necessary to pad it
* out.
*
* @param index The subscript.
* @param value A long value.
* @return this.
* @throws JSONException If the index is negative.
*/
public JSONArray element( int index, long value ) {
return element( index, new Long( value ) );
}
/**
* Put a value in the JSONArray, where the value will be a JSONObject which
* is produced from a Map.
*
* @param index The subscript.
* @param value The Map value.
* @return this.
* @throws JSONException If the index is negative or if the the value is an
* invalid number.
*/
public JSONArray element( int index, Map value ) {
return element( index, value, new JsonConfig() );
}
/**
* Put a value in the JSONArray, where the value will be a JSONObject which
* is produced from a Map.
*
* @param index The subscript.
* @param value The Map value.
* @return this.
* @throws JSONException If the index is negative or if the the value is an
* invalid number.
*/
public JSONArray element( int index, Map value, JsonConfig jsonConfig ) {
if( value instanceof JSONObject ){
if( index < 0 ){
throw new JSONException( "JSONArray[" + index + "] not found." );
}
if( index < size() ){
elements.set( index, value );
}else{
while( index != size() ){
element( JSONNull.getInstance() );
}
element( value, jsonConfig );
}
return this;
}else{
return element( index, JSONObject.fromObject( value, jsonConfig ) );
}
}
/**
* 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.
*
* @param index The subscript.
* @param value An object value. The value should be a Boolean, Double,
* Integer, JSONArray, JSONObject, JSONFunction, Long, String,
* JSONString or the JSONNull object.
* @return this.
* @throws JSONException If the index is negative or if the the value is an
* invalid number.
*/
public JSONArray element( int index, Object value ) {
return element( index, value, new JsonConfig() );
}
/**
* 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.
*
* @param index The subscript.
* @param value An object value. The value should be a Boolean, Double,
* Integer, JSONArray, JSONObject, JSONFunction, Long, String,
* JSONString or the JSONNull object.
* @return this.
* @throws JSONException If the index is negative or if the the value is an
* invalid number.
*/
public JSONArray element( int index, Object value, JsonConfig jsonConfig ) {
JSONUtils.testValidity( value );
if( index < 0 ){
throw new JSONException( "JSONArray[" + index + "] not found." );
}
if( index < size() ){
this.elements.set( index, processValue( value, jsonConfig ) );
}else{
while( index != size() ){
element( JSONNull.getInstance() );
}
element( value, jsonConfig );
}
return this;
}
/**
* Put or replace a String 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.
* The string may be a valid JSON formatted string, in tha case, it will be
* transformed to a JSONArray, JSONObject or JSONNull.
*
* @param index The subscript.
* @param value A String value.
* @return this.
* @throws JSONException If the index is negative or if the the value is an
* invalid number.
*/
public JSONArray element( int index, String value ) {
return element( index, value, new JsonConfig() );
}
/**
* Put or replace a String 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.
* The string may be a valid JSON formatted string, in tha case, it will be
* transformed to a JSONArray, JSONObject or JSONNull.
*
* @param index The subscript.
* @param value A String value.
* @return this.
* @throws JSONException If the index is negative or if the the value is an
* invalid number.
*/
public JSONArray element( int index, String value, JsonConfig jsonConfig ) {
if( index < 0 ){
throw new JSONException( "JSONArray[" + index + "] not found." );
}
if( index < size() ){
if( value == null ){
this.elements.set( index, "" );
}else if( JSONUtils.mayBeJSON( value ) ){
try{
this.elements.set( index, JSONSerializer.toJSON( value, jsonConfig ) );
}catch( JSONException jsone ){
this.elements.set( index, JSONUtils.stripQuotes( value ) );
}
}else{
this.elements.set( index, JSONUtils.stripQuotes( value ) );
}
}else{
while( index != size() ){
element( JSONNull.getInstance() );
}
element( value, jsonConfig );
}
return this;
}
/**
* Append an JSON value. This increases the array's length by one.
*
* @param value An JSON value.
* @return this.
*/
public JSONArray element( JSONNull value ) {
this.elements.add( value );
return this;
}
/**
* Append an JSON value. This increases the array's length by one.
*
* @param value An JSON value.
* @return this.
*/
public JSONArray element( JSONObject value ) {
this.elements.add( value );
return this;
}
/**
* Append an long value. This increases the array's length by one.
*
* @param value A long value.
* @return this.
*/
public JSONArray element( long value ) {
return element( JSONUtils.transformNumber(value) );
}
/**
* Put a value in the JSONArray, where the value will be a JSONObject which
* is produced from a Map.
*
* @param value A Map value.
* @return this.
*/
public JSONArray element( Map value ) {
return element( value, new JsonConfig() );
}
/**
* Put a value in the JSONArray, where the value will be a JSONObject which
* is produced from a Map.
*
* @param value A Map value.
* @return this.
*/
public JSONArray element( Map value, JsonConfig jsonConfig ) {
if( value instanceof JSONObject ){
elements.add( value );
return this;
}else{
return element( JSONObject.fromObject( value, jsonConfig ) );
}
}
/**
* Append an object value. This increases the array's length by one.
*
* @param value An object value. The value should be a Boolean, Double,
* Integer, JSONArray, JSONObject, JSONFunction, Long, String,
* JSONString or the JSONNull object.
* @return this.
*/
public JSONArray element( Object value ) {
return element( value, new JsonConfig() );
}
/**
* Append an object value. This increases the array's length by one.
*
* @param value An object value. The value should be a Boolean, Double,
* Integer, JSONArray, JSONObject, JSONFunction, Long, String,
* JSONString or the JSONNull object.
* @return this.
*/
public JSONArray element( Object value, JsonConfig jsonConfig ) {
return addValue( value, jsonConfig );
}
/**
* Append a String value. This increases the array's length by one.
* The string may be a valid JSON formatted string, in tha case, it will be
* transformed to a JSONArray, JSONObject or JSONNull.
*
* @param value A String value.
* @return this.
*/
public JSONArray element( String value ) {
return element( value, new JsonConfig() );
}
/**
* Append a String value. This increases the array's length by one.
* The string may be a valid JSON formatted string, in tha case, it will be
* transformed to a JSONArray, JSONObject or JSONNull.
*
* @param value A String value.
* @return this.
*/
public JSONArray element( String value, JsonConfig jsonConfig ) {
if( value == null ) {
this.elements.add("");
} else if( JSONUtils.hasQuotes( value )) {
this.elements.add(value);
} else if( JSONNull.getInstance().equals( value )) {
this.elements.add( JSONNull.getInstance() );
} else if( JSONUtils.isJsonKeyword(value,jsonConfig)) {
this.elements.add(value);
} else if( JSONUtils.mayBeJSON( value ) ){
try{
this.elements.add( JSONSerializer.toJSON( value, jsonConfig ) );
}catch( JSONException jsone ){
this.elements.add( value );
}
} else {
this.elements.add(value);
}
return this;
}
public boolean equals( Object obj ) {
if( obj == this ){
return true;
}
if( obj == null ){
return false;
}
if( !(obj instanceof JSONArray) ){
return false;
}
JSONArray other = (JSONArray) obj;
if( other.size() != size() ){
return false;
}
int max = size();
for( int i = 0; i < max; i++ ){
Object o1 = get( i );
Object o2 = other.get( i );
// handle nulls
if( JSONNull.getInstance()
.equals( o1 ) ){
if( JSONNull.getInstance()
.equals( o2 ) ){
continue;
}else{
return false;
}
}else{
if( JSONNull.getInstance()
.equals( o2 ) ){
return false;
}
}
if( o1 instanceof JSONArray && o2 instanceof JSONArray ){
JSONArray e = (JSONArray) o1;
JSONArray a = (JSONArray) o2;
if( !a.equals( e ) ){
return false;
}
}else{
if( o1 instanceof String && o2 instanceof JSONFunction ){
if( !o1.equals( String.valueOf( o2 ) ) ){
return false;
}
}else if( o1 instanceof JSONFunction && o2 instanceof String ){
if( !o2.equals( String.valueOf( o1 ) ) ){
return false;
}
}else if( o1 instanceof JSONObject && o2 instanceof JSONObject ){
if( !o1.equals( o2 ) ){
return false;
}
}else if( o1 instanceof JSONArray && o2 instanceof JSONArray ){
if( !o1.equals( o2 ) ){
return false;
}
}else if( o1 instanceof JSONFunction && o2 instanceof JSONFunction ){
if( !o1.equals( o2 ) ){
return false;
}
}else{
if( o1 instanceof String ){
if( !o1.equals( String.valueOf( o2 ) ) ){
return false;
}
}else if( o2 instanceof String ){
if( !o2.equals( String.valueOf( o1 ) ) ){
return false;
}
}else{
Morpher m1 = JSONUtils.getMorpherRegistry()
.getMorpherFor( o1.getClass() );
Morpher m2 = JSONUtils.getMorpherRegistry()
.getMorpherFor( o2.getClass() );
if( m1 != null && m1 != IdentityObjectMorpher.getInstance() ){
if( !o1.equals( JSONUtils.getMorpherRegistry()
.morph( o1.getClass(), o2 ) ) ){
return false;
}
}else if( m2 != null && m2 != IdentityObjectMorpher.getInstance() ){
if( !JSONUtils.getMorpherRegistry()
.morph( o1.getClass(), o1 )
.equals( o2 ) ){
return false;
}
}else{
if( !o1.equals( o2 ) ){
return false;
}
}
}
}
}
}
return true;
}
/**
* Get the object value associated with an index.
*
* @param index The index must be between 0 and size() - 1.
* @return An object value.
*/
public Object get( int index ) {
/*
* Object o = opt( index ); if( o == null ){ throw new JSONException(
* "JSONArray[" + index + "] not found." ); } return o;
*/
return this.elements.get( index );
}
/**
* Get the boolean value associated with an index. The string values "true"
* and "false" are converted to boolean.
*
* @param index The index must be between 0 and size() - 1.
* @return The truth.
* @throws JSONException If there is no value for the index or if the value
* is not convertable to boolean.
*/
public boolean getBoolean( int index ) {
Object o = get( index );
if( o != null ){
if( o.equals( Boolean.FALSE )
|| (o instanceof String && ((String) o).equalsIgnoreCase( "false" )) ){
return false;
}else if( o.equals( Boolean.TRUE )
|| (o instanceof String && ((String) o).equalsIgnoreCase( "true" )) ){
return true;
}
}
throw new JSONException( "JSONArray[" + index + "] is not a Boolean." );
}
/**
* Get the double value associated with an index.
*
* @param index The index must be between 0 and size() - 1.
* @return The value.
* @throws JSONException If the key is not found or if the value cannot be
* converted to a number.
*/
public double getDouble( int index ) {
Object o = get( index );
if( o != null ){
try{
return o instanceof Number ? ((Number) o).doubleValue()
: Double.parseDouble( (String) o );
}catch( Exception e ){
throw new JSONException( "JSONArray[" + index + "] is not a number." );
}
}
throw new JSONException( "JSONArray[" + index + "] is not a number." );
}
/**
* Get the int value associated with an index.
*
* @param index The index must be between 0 and size() - 1.
* @return The value.
* @throws JSONException 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.
*/
public int getInt( int index ) {
Object o = get( index );
if( o != null ){
return o instanceof Number ? ((Number) o).intValue() : (int) getDouble( index );
}
throw new JSONException( "JSONArray[" + index + "] is not a number." );
}
/**
* Get the JSONArray associated with an index.
*
* @param index The index must be between 0 and size() - 1.
* @return A JSONArray value.
* @throws JSONException If there is no value for the index. or if the value
* is not a JSONArray
*/
public JSONArray getJSONArray( int index ) {
Object o = get( index );
if( o != null && o instanceof JSONArray ){
return (JSONArray) o;
}
throw new JSONException( "JSONArray[" + index + "] is not a JSONArray." );
}
/**
* Get the JSONObject associated with an index.
*
* @param index subscript
* @return A JSONObject value.
* @throws JSONException If there is no value for the index or if the value
* is not a JSONObject
*/
public JSONObject getJSONObject( int index ) {
Object o = get( index );
if( JSONNull.getInstance()
.equals( o ) ){
return new JSONObject( true );
}else if( o instanceof JSONObject ){
return (JSONObject) o;
}
throw new JSONException( "JSONArray[" + index + "] is not a JSONObject." );
}
/**
* Get the long value associated with an index.
*
* @param index The index must be between 0 and size() - 1.
* @return The value.
* @throws JSONException If the key is not found or if the value cannot be
* converted to a number.
*/
public long getLong( int index ) {
Object o = get( index );
if( o != null ){
return o instanceof Number ? ((Number) o).longValue() : (long) getDouble( index );
}
throw new JSONException( "JSONArray[" + index + "] is not a number." );
}
/**
* Get the string associated with an index.
*
* @param index The index must be between 0 and size() - 1.
* @return A string value.
* @throws JSONException If there is no value for the index.
*/
public String getString( int index ) {
Object o = get( index );
if( o != null ){
return o.toString();
}
throw new JSONException( "JSONArray[" + index + "] not found." );
}
public int hashCode() {
int hashcode = 29;
for( Iterator e = elements.iterator(); e.hasNext(); ){
Object element = e.next();
hashcode += JSONUtils.hashCode( element );
}
return hashcode;
}
public int indexOf( Object o ) {
return elements.indexOf( o );
}
public boolean isArray() {
return true;
}
public boolean isEmpty() {
return this.elements.isEmpty();
}
public boolean isExpandElements() {
return expandElements;
}
/**
* Returns an Iterator for this JSONArray
*/
public Iterator iterator() {
return new JSONArrayListIterator();
}
/**
* Make a string from the contents of this JSONArray. The
* separator
string is inserted between each element. Warning:
* This method assumes that the data structure is acyclical.
*
* @param separator A string that will be inserted between the elements.
* @return a string.
* @throws JSONException If the array contains an invalid number.
*/
public String join( String separator ) {
return join( separator, false );
}
/**
* Make a string from the contents of this JSONArray. The
* separator
string is inserted between each element. Warning:
* This method assumes that the data structure is acyclical.
*
* @param separator A string that will be inserted between the elements.
* @return a string.
* @throws JSONException If the array contains an invalid number.
*/
public String join( String separator, boolean stripQuotes ) {
int len = size();
StringBuffer sb = new StringBuffer();
for( int i = 0; i < len; i += 1 ){
if( i > 0 ){
sb.append( separator );
}
String value = JSONUtils.valueToString( this.elements.get( i ) );
sb.append( stripQuotes ? JSONUtils.stripQuotes( value ) : value );
}
return sb.toString();
}
public int lastIndexOf( Object o ) {
return elements.lastIndexOf( o );
}
public ListIterator listIterator() {
return listIterator( 0 );
}
public ListIterator listIterator( int index ) {
if( index < 0 || index > size() )
throw new IndexOutOfBoundsException( "Index: " + index );
return new JSONArrayListIterator( index );
}
/**
* Get the optional object value associated with an index.
*
* @param index The index must be between 0 and size() - 1.
* @return An object value, or null if there is no object at that index.
*/
public Object opt( int index ) {
return (index < 0 || index >= size()) ? null : this.elements.get( index );
}
/**
* Get the optional boolean value associated with an index. It returns false
* if there is no value at that index, or if the value is not Boolean.TRUE or
* the String "true".
*
* @param index The index must be between 0 and size() - 1.
* @return The truth.
*/
public boolean optBoolean( int index ) {
return optBoolean( index, false );
}
/**
* Get the optional boolean value associated with an index. It returns the
* defaultValue if there is no value at that index or if it is not a Boolean
* or the String "true" or "false" (case insensitive).
*
* @param index The index must be between 0 and size() - 1.
* @param defaultValue A boolean default.
* @return The truth.
*/
public boolean optBoolean( int index, boolean defaultValue ) {
try{
return getBoolean( index );
}catch( Exception e ){
return defaultValue;
}
}
/**
* Get the optional double value associated with an index. NaN is returned if
* there is no value for the index, or if the value is not a number and
* cannot be converted to a number.
*
* @param index The index must be between 0 and size() - 1.
* @return The value.
*/
public double optDouble( int index ) {
return optDouble( index, Double.NaN );
}
/**
* Get the optional double value associated with an index. The defaultValue
* is returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index subscript
* @param defaultValue The default value.
* @return The value.
*/
public double optDouble( int index, double defaultValue ) {
try{
return getDouble( index );
}catch( Exception e ){
return defaultValue;
}
}
/**
* Get the optional int value associated with an index. Zero is returned if
* there is no value for the index, or if the value is not a number and
* cannot be converted to a number.
*
* @param index The index must be between 0 and size() - 1.
* @return The value.
*/
public int optInt( int index ) {
return optInt( index, 0 );
}
/**
* Get the optional int value associated with an index. The defaultValue is
* returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index The index must be between 0 and size() - 1.
* @param defaultValue The default value.
* @return The value.
*/
public int optInt( int index, int defaultValue ) {
try{
return getInt( index );
}catch( Exception e ){
return defaultValue;
}
}
/**
* Get the optional JSONArray associated with an index.
*
* @param index subscript
* @return A JSONArray value, or null if the index has no value, or if the
* value is not a JSONArray.
*/
public JSONArray optJSONArray( int index ) {
Object o = opt( index );
return o instanceof JSONArray ? (JSONArray) o : null;
}
/**
* Get the optional JSONObject associated with an index. Null is returned if
* the key is not found, or null if the index has no value, or if the value
* is not a JSONObject.
*
* @param index The index must be between 0 and size() - 1.
* @return A JSONObject value.
*/
public JSONObject optJSONObject( int index ) {
Object o = opt( index );
return o instanceof JSONObject ? (JSONObject) o : null;
}
/**
* Get the optional long value associated with an index. Zero is returned if
* there is no value for the index, or if the value is not a number and
* cannot be converted to a number.
*
* @param index The index must be between 0 and size() - 1.
* @return The value.
*/
public long optLong( int index ) {
return optLong( index, 0 );
}
/**
* Get the optional long value associated with an index. The defaultValue is
* returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index The index must be between 0 and size() - 1.
* @param defaultValue The default value.
* @return The value.
*/
public long optLong( int index, long defaultValue ) {
try{
return getLong( index );
}catch( Exception e ){
return defaultValue;
}
}
/**
* Get the optional string value associated with an index. It returns an
* empty string if there is no value at that index. If the value is not a
* string and is not null, then it is coverted to a string.
*
* @param index The index must be between 0 and size() - 1.
* @return A String value.
*/
public String optString( int index ) {
return optString( index, "" );
}
/**
* Get the optional string associated with an index. The defaultValue is
* returned if the key is not found.
*
* @param index The index must be between 0 and size() - 1.
* @param defaultValue The default value.
* @return A String value.
*/
public String optString( int index, String defaultValue ) {
Object o = opt( index );
return o != null ? o.toString() : defaultValue;
}
public Object remove( int index ) {
return elements.remove( index );
}
public boolean remove( Object o ) {
return elements.remove( o );
}
public boolean removeAll( Collection collection ) {
return removeAll( collection, new JsonConfig() );
}
public boolean removeAll( Collection collection, JsonConfig jsonConfig ) {
return elements.removeAll( fromObject( collection, jsonConfig ) );
}
public boolean retainAll( Collection collection ) {
return retainAll( collection, new JsonConfig() );
}
public boolean retainAll( Collection collection, JsonConfig jsonConfig ) {
return elements.retainAll( fromObject( collection, jsonConfig ) );
}
public Object set( int index, Object value ) {
return set( index, value, new JsonConfig() );
}
public Object set( int index, Object value, JsonConfig jsonConfig ) {
Object previous = get( index );
element( index, value, jsonConfig );
return previous;
}
public void setExpandElements( boolean expandElements ) {
this.expandElements = expandElements;
}
/**
* Get the number of elements in the JSONArray, included nulls.
*
* @return The length (or size).
*/
public int size() {
return this.elements.size();
}
public List subList( int fromIndex, int toIndex ) {
return elements.subList( fromIndex, toIndex );
}
/**
* Produce an Object[] with the contents of this JSONArray.
*/
public Object[] toArray() {
return this.elements.toArray();
}
public Object[] toArray( Object[] array ) {
return elements.toArray( array );
}
/**
* Produce a JSONObject by combining a JSONArray of names with the values of
* this JSONArray.
*
* @param names A JSONArray containing a list of key strings. These will be
* paired with the values.
* @return A JSONObject, or null if there are no names or if this JSONArray
* has no values.
* @throws JSONException If any of the names are null.
*/
public JSONObject toJSONObject( JSONArray names ) {
if( names == null || names.size() == 0 || size() == 0 ){
return null;
}
JSONObject jo = new JSONObject();
for( int i = 0; i < names.size(); i++ ){
jo.element( names.getString( i ), this.opt( i ) );
}
return jo;
}
/**
* Make a JSON text of this JSONArray. For compactness, no unnecessary
* whitespace is added. If it is not possible to produce a syntactically
* correct JSON text then null will be returned instead. This could occur if
* the array contains an invalid number.
*
* Warning: This method assumes that the data structure is acyclical.
*
* @return a printable, displayable, transmittable representation of the
* array.
*/
public String toString() {
try{
return '[' + join( "," ) + ']';
}catch( Exception e ){
return null;
}
}
/**
* Make a prettyprinted JSON text of this JSONArray. Warning: This method
* assumes that the data structure is acyclical.
*
* @param indentFactor The number of spaces to add to each level of
* indentation.
* @return a printable, displayable, transmittable representation of the
* object, beginning with [
(left
* bracket) and ending with ]
(right
* bracket) .
* @throws JSONException
*/
public String toString( int indentFactor ) {
if( indentFactor == 0 ){
return this.toString();
}
return toString( indentFactor, 0 );
}
/**
* Make a prettyprinted JSON text of this JSONArray. Warning: This method
* assumes that the data structure is acyclical.
*
* @param indentFactor The number of spaces to add to each level of
* indentation.
* @param indent The indention of the top level.
* @return a printable, displayable, transmittable representation of the
* array.
* @throws JSONException
*/
public String toString( int indentFactor, int indent ) {
int len = size();
if( len == 0 ){
return "[]";
}
if( indentFactor == 0 ){
return this.toString();
}
int i;
StringBuffer sb = new StringBuffer( "[" );
if( len == 1 ){
sb.append( JSONUtils.valueToString( this.elements.get( 0 ), indentFactor, indent ) );
}else{
int newindent = indent + indentFactor;
sb.append( '\n' );
for( i = 0; i < len; i += 1 ){
if( i > 0 ){
sb.append( ",\n" );
}
for( int j = 0; j < newindent; j += 1 ){
sb.append( ' ' );
}
sb.append( JSONUtils.valueToString( this.elements.get( i ), indentFactor, newindent ) );
}
sb.append( '\n' );
for( i = 0; i < indent; i += 1 ){
sb.append( ' ' );
}
for( i = 0; i < indent; i += 1 ){
sb.insert( 0, ' ' );
}
}
sb.append( ']' );
return sb.toString();
}
protected void write(Writer writer, WritingVisitor visitor) throws IOException {
boolean b = false;
int len = size();
writer.write( '[' );
for( int i = 0; i < len; i += 1 ){
if( b ){
writer.write( ',' );
}
Object v = this.elements.get( i );
if( v instanceof JSON ){
visitor.on((JSON)v,writer);
}else{
visitor.on(v,writer);
}
b = true;
}
writer.write( ']' );
}
/**
* Adds a String without performing any conversion on it.
*/
protected JSONArray addString( String str ) {
if( str != null ){
elements.add( str );
}
return this;
}
/**
* Append an object value. This increases the array's length by one.
*
* @param value An object value. The value should be a Boolean, Double,
* Integer, JSONArray, JSONObject, JSONFunction, Long, String,
* JSONString or the JSONNull object.
* @return this.
*/
private JSONArray _addValue( Object value, JsonConfig jsonConfig ) {
this.elements.add(value);
return this;
}
protected Object _processValue( Object value, JsonConfig jsonConfig ) {
if( value instanceof JSONTokener ) {
return _fromJSONTokener( (JSONTokener) value, jsonConfig );
}else if( value != null && Enum.class.isAssignableFrom( value.getClass() ) ){
return ((Enum) value).name();
}else if( value instanceof Annotation || (value != null && value.getClass()
.isAnnotation()) ){
throw new JSONException( "Unsupported type" );
}
return super._processValue( value, jsonConfig );
}
/**
* Append an object value. This increases the array's length by one.
*
* @param value An object value. The value should be a Boolean, Double,
* Integer, JSONArray, JSONObject, JSONFunction, Long, String,
* JSONString or the JSONNull object.
* @return this.
*/
private JSONArray addValue( Object value, JsonConfig jsonConfig ) {
return _addValue( processValue( value, jsonConfig ), jsonConfig );
}
private Object processValue( Object value, JsonConfig jsonConfig ) {
if( value != null ){
JsonValueProcessor jsonValueProcessor = jsonConfig.findJsonValueProcessor( value.getClass() );
if( jsonValueProcessor != null ){
value = jsonValueProcessor.processArrayValue( value, jsonConfig );
if( !JsonVerifier.isValidJsonValue( value ) ){
throw new JSONException( "Value is not a valid JSON value. " + value );
}
}
}
return _processValue( value, jsonConfig );
}
private class JSONArrayListIterator implements ListIterator {
int currentIndex = 0;
int lastIndex = -1;
JSONArrayListIterator() {
}
JSONArrayListIterator( int index ) {
currentIndex = index;
}
public boolean hasNext() {
return currentIndex != size();
}
public Object next() {
try {
Object next = get( currentIndex );
lastIndex = currentIndex++;
return next;
} catch( IndexOutOfBoundsException e ) {
throw new NoSuchElementException();
}
}
public void remove() {
if( lastIndex == -1 )
throw new IllegalStateException();
try {
JSONArray.this.remove( lastIndex );
if( lastIndex < currentIndex ){
currentIndex--;
}
lastIndex = -1;
} catch( IndexOutOfBoundsException e ) {
throw new ConcurrentModificationException();
}
}
public boolean hasPrevious() {
return currentIndex != 0;
}
public Object previous() {
try {
int index = currentIndex - 1;
Object previous = get( index );
lastIndex = currentIndex = index;
return previous;
} catch( IndexOutOfBoundsException e ) {
throw new NoSuchElementException();
}
}
public int nextIndex() {
return currentIndex;
}
public int previousIndex() {
return currentIndex - 1;
}
public void set( Object obj ) {
if( lastIndex == -1 ){
throw new IllegalStateException();
}
try {
JSONArray.this.set( lastIndex, obj );
} catch( IndexOutOfBoundsException ex ) {
throw new ConcurrentModificationException();
}
}
public void add( Object obj ) {
try {
JSONArray.this.add( currentIndex++, obj );
lastIndex = -1;
} catch( IndexOutOfBoundsException ex ) {
throw new ConcurrentModificationException();
}
}
}
}