This commit is contained in:
drtshock
2014-01-20 12:22:09 -07:00
parent 0362170e9b
commit 2d6ac31c2c
19 changed files with 660 additions and 641 deletions
+200 -175
View File
@@ -34,47 +34,30 @@ import java.util.Iterator;
import java.util.Map;
/**
* 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 <code>get</code> and <code>opt</code>
* methods for accessing the values by index, and <code>put</code> methods for
* adding or replacing values. The values can be any of these types:
* <code>Boolean</code>, <code>JSONArray</code>, <code>JSONObject</code>,
* <code>Number</code>, <code>String</code>, or the
* <code>JSONObject.NULL object</code>.
* <p>
* The constructor can convert a JSON text into a Java object. The
* <code>toString</code> method converts to JSON text.
* <p>
* A <code>get</code> method returns a value if one can be found, and throws an
* exception if one cannot be found. An <code>opt</code> method returns a
* default value instead of throwing an exception, and so is useful for
* obtaining optional values.
* <p>
* The generic <code>get()</code> and <code>opt()</code> methods return an
* object which you can cast or query for type. There are also typed
* <code>get</code> and <code>opt</code> methods that do type checking and type
* coercion for you.
* <p>
* The texts produced by the <code>toString</code> methods strictly conform to
* JSON syntax rules. The constructors are more forgiving in the texts they will
* accept:
* <ul>
* <li>An extra <code>,</code>&nbsp;<small>(comma)</small> may appear just
* before the closing bracket.</li>
* <li>The <code>null</code> value will be inserted when there is <code>,</code>
* &nbsp;<small>(comma)</small> elision.</li>
* <li>Strings may be quoted with <code>'</code>&nbsp;<small>(single
* quote)</small>.</li>
* <li>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:
* <code>{ } [ ] / \ : , = ; #</code> and if they do not look like numbers and
* if they are not the reserved words <code>true</code>, <code>false</code>, or
* <code>null</code>.</li>
* <li>Values can be separated by <code>;</code> <small>(semicolon)</small> as
* well as by <code>,</code> <small>(comma)</small>.</li>
* </ul>
* 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 <code>get</code> and <code>opt</code> methods for
* accessing the values by index, and <code>put</code> methods for adding or replacing values. The values can be any of
* these types: <code>Boolean</code>, <code>JSONArray</code>, <code>JSONObject</code>, <code>Number</code>,
* <code>String</code>, or the <code>JSONObject.NULL object</code>.
* <p/>
* The constructor can convert a JSON text into a Java object. The <code>toString</code> method converts to JSON text.
* <p/>
* A <code>get</code> method returns a value if one can be found, and throws an exception if one cannot be found. An
* <code>opt</code> method returns a default value instead of throwing an exception, and so is useful for obtaining
* optional values.
* <p/>
* The generic <code>get()</code> and <code>opt()</code> methods return an object which you can cast or query for type.
* There are also typed <code>get</code> and <code>opt</code> methods that do type checking and type coercion for you.
* <p/>
* The texts produced by the <code>toString</code> methods strictly conform to JSON syntax rules. The constructors are
* more forgiving in the texts they will accept: <ul> <li>An extra <code>,</code>&nbsp;<small>(comma)</small> may appear
* just before the closing bracket.</li> <li>The <code>null</code> value will be inserted when there is <code>,</code>
* &nbsp;<small>(comma)</small> elision.</li> <li>Strings may be quoted with <code>'</code>&nbsp;<small>(single
* quote)</small>.</li> <li>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: <code>{ }
* [ ] / \ : , = ; #</code> and if they do not look like numbers and if they are not the reserved words
* <code>true</code>, <code>false</code>, or <code>null</code>.</li> <li>Values can be separated by <code>;</code>
* <small>(semicolon)</small> as well as by <code>,</code> <small>(comma)</small>.</li> </ul>
*
* @author JSON.org
* @version 2012-11-13
@@ -95,7 +78,9 @@ public class JSONArray {
/**
* Construct a JSONArray from a JSONTokener.
*
* @param x A JSONTokener
*
* @throws JSONException If there is a syntax error.
*/
public JSONArray(JSONTokener x) throws JSONException {
@@ -132,10 +117,11 @@ public class JSONArray {
/**
* Construct a JSONArray from a source JSON text.
* @param source A string that begins with
* <code>[</code>&nbsp;<small>(left bracket)</small>
* and ends with <code>]</code>&nbsp;<small>(right bracket)</small>.
* @throws JSONException If there is a syntax error.
*
* @param source A string that begins with <code>[</code>&nbsp;<small>(left bracket)</small> and ends with
* <code>]</code>&nbsp;<small>(right bracket)</small>.
*
* @throws JSONException If there is a syntax error.
*/
public JSONArray(String source) throws JSONException {
this(new JSONTokener(source));
@@ -143,7 +129,8 @@ public class JSONArray {
/**
* Construct a JSONArray from a Collection.
* @param collection A Collection.
*
* @param collection A Collection.
*/
public JSONArray(Collection<?> collection) {
this.myArrayList = new ArrayList<Object>();
@@ -157,6 +144,7 @@ public class JSONArray {
/**
* Construct a JSONArray from an array
*
* @throws JSONException If not an array.
*/
public JSONArray(Object array) throws JSONException {
@@ -174,9 +162,11 @@ public class JSONArray {
/**
* Get the object value associated with an index.
* @param index
* The index must be between 0 and length() - 1.
*
* @param index The index must be between 0 and length() - 1.
*
* @return An object value.
*
* @throws JSONException If there is no value for the index.
*/
public Object get(int index) throws JSONException {
@@ -188,13 +178,13 @@ public class JSONArray {
}
/**
* Get the boolean value associated with an index.
* The string values "true" and "false" are converted to boolean.
* 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 length() - 1.
*
* @return The truth.
* @throws JSONException If there is no value for the index or if the
* value is not convertible to boolean.
*
* @throws JSONException If there is no value for the index or if the value is not convertible to boolean.
*/
public boolean getBoolean(int index) throws JSONException {
Object object = this.get(index);
@@ -214,9 +204,10 @@ public class JSONArray {
* Get the double value associated with an index.
*
* @param index The index must be between 0 and length() - 1.
*
* @return The value.
* @throws JSONException If the key is not found or if the value cannot
* be converted to a number.
*
* @throws JSONException If the key is not found or if the value cannot be converted to a number.
*/
public double getDouble(int index) throws JSONException {
Object object = this.get(index);
@@ -234,7 +225,9 @@ public class JSONArray {
* Get the int value associated with an index.
*
* @param index The index must be between 0 and length() - 1.
*
* @return The value.
*
* @throws JSONException If the key is not found or if the value is not a number.
*/
public int getInt(int index) throws JSONException {
@@ -251,10 +244,12 @@ public class JSONArray {
/**
* Get the JSONArray associated with an index.
*
* @param index The index must be between 0 and length() - 1.
*
* @return A JSONArray value.
* @throws JSONException If there is no value for the index. or if the
* value is not a JSONArray
*
* @throws JSONException If there is no value for the index. or if the value is not a JSONArray
*/
public JSONArray getJSONArray(int index) throws JSONException {
Object object = this.get(index);
@@ -267,10 +262,12 @@ public class 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
*
* @throws JSONException If there is no value for the index or if the value is not a JSONObject
*/
public JSONObject getJSONObject(int index) throws JSONException {
Object object = this.get(index);
@@ -285,9 +282,10 @@ public class JSONArray {
* Get the long value associated with an index.
*
* @param index The index must be between 0 and length() - 1.
*
* @return The value.
* @throws JSONException If the key is not found or if the value cannot
* be converted to a number.
*
* @throws JSONException If the key is not found or if the value cannot be converted to a number.
*/
public long getLong(int index) throws JSONException {
Object object = this.get(index);
@@ -303,8 +301,11 @@ public class JSONArray {
/**
* Get the string associated with an index.
*
* @param index The index must be between 0 and length() - 1.
*
* @return A string value.
*
* @throws JSONException If there is no string value for the index.
*/
public String getString(int index) throws JSONException {
@@ -317,7 +318,9 @@ public class JSONArray {
/**
* Determine if the value is null.
*
* @param index The index must be between 0 and length() - 1.
*
* @return true if the value at the index is null, or if there is no value.
*/
public boolean isNull(int index) {
@@ -325,11 +328,13 @@ public class JSONArray {
}
/**
* Make a string from the contents of this JSONArray. The
* <code>separator</code> string is inserted between each element.
* Warning: This method assumes that the data structure is acyclical.
* Make a string from the contents of this JSONArray. The <code>separator</code> 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) throws JSONException {
@@ -356,9 +361,10 @@ public class JSONArray {
/**
* Get the optional object value associated with an index.
*
* @param index The index must be between 0 and length() - 1.
* @return An object value, or null if there is no
* object at that index.
*
* @return An object value, or null if there is no object at that index.
*/
public Object opt(int index) {
return (index < 0 || index >= this.length())
@@ -367,11 +373,11 @@ public class JSONArray {
}
/**
* 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".
* 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 length() - 1.
*
* @return The truth.
*/
public boolean optBoolean(int index) {
@@ -379,12 +385,12 @@ public class JSONArray {
}
/**
* 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).
* 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 length() - 1.
* @param defaultValue A boolean default.
* @param defaultValue A boolean default.
*
* @return The truth.
*/
public boolean optBoolean(int index, boolean defaultValue) {
@@ -396,11 +402,11 @@ public class JSONArray {
}
/**
* 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.
* 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 length() - 1.
*
* @return The value.
*/
public double optDouble(int index) {
@@ -408,12 +414,12 @@ public class JSONArray {
}
/**
* 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.
* 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.
* @param defaultValue The default value.
*
* @return The value.
*/
public double optDouble(int index, double defaultValue) {
@@ -425,11 +431,11 @@ public class JSONArray {
}
/**
* 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.
* 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 length() - 1.
*
* @return The value.
*/
public int optInt(int index) {
@@ -437,11 +443,12 @@ public class JSONArray {
}
/**
* 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.
* 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 length() - 1.
* @param defaultValue The default value.
* @param defaultValue The default value.
*
* @return The value.
*/
public int optInt(int index, int defaultValue) {
@@ -454,9 +461,10 @@ public class JSONArray {
/**
* 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.
*
* @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 = this.opt(index);
@@ -464,11 +472,11 @@ public class JSONArray {
}
/**
* 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.
* 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 length() - 1.
*
* @return A JSONObject value.
*/
public JSONObject optJSONObject(int index) {
@@ -477,11 +485,11 @@ public class JSONArray {
}
/**
* 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.
* 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 length() - 1.
*
* @return The value.
*/
public long optLong(int index) {
@@ -489,11 +497,12 @@ public class JSONArray {
}
/**
* 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.
* 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 length() - 1.
* @param defaultValue The default value.
* @param defaultValue The default value.
*
* @return The value.
*/
public long optLong(int index, long defaultValue) {
@@ -505,11 +514,11 @@ public class JSONArray {
}
/**
* 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.
* 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 length() - 1.
*
* @return A String value.
*/
public String optString(int index) {
@@ -517,11 +526,11 @@ public class JSONArray {
}
/**
* Get the optional string associated with an index.
* The defaultValue is returned if the key is not found.
* 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 length() - 1.
* @param defaultValue The default value.
* @param defaultValue The default value.
*
* @return A String value.
*/
public String optString(int index, String defaultValue) {
@@ -535,6 +544,7 @@ public class JSONArray {
* Append a boolean value. This increases the array's length by one.
*
* @param value A boolean value.
*
* @return this.
*/
public JSONArray put(boolean value) {
@@ -543,9 +553,10 @@ public class JSONArray {
}
/**
* Put a value in the JSONArray, where the value will be a
* JSONArray which is produced from a Collection.
* Put 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 put(Collection<?> value) {
@@ -557,8 +568,10 @@ public class JSONArray {
* 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.
*
* @throws JSONException if the value is not finite.
*/
public JSONArray put(double value) throws JSONException {
Double d = new Double(value);
@@ -571,6 +584,7 @@ public class JSONArray {
* Append an int value. This increases the array's length by one.
*
* @param value An int value.
*
* @return this.
*/
public JSONArray put(int value) {
@@ -582,6 +596,7 @@ public class JSONArray {
* Append an long value. This increases the array's length by one.
*
* @param value A long value.
*
* @return this.
*/
public JSONArray put(long value) {
@@ -590,9 +605,10 @@ public class JSONArray {
}
/**
* Put a value in the JSONArray, where the value will be a
* JSONObject which is produced from a Map.
* 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 put(Map<?, ?> value) {
@@ -602,9 +618,10 @@ public class JSONArray {
/**
* 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, Long, or String, or the
* JSONObject.NULL object.
*
* @param value An object value. The value should be a Boolean, Double, Integer, JSONArray, JSONObject, Long, or
* String, or the JSONObject.NULL object.
*
* @return this.
*/
public JSONArray put(Object value) {
@@ -613,12 +630,14 @@ public class JSONArray {
}
/**
* 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.
* 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 put(int index, boolean value) throws JSONException {
@@ -627,13 +646,14 @@ public class JSONArray {
}
/**
* Put a value in the JSONArray, where the value will be a
* JSONArray which is produced from a Collection.
* 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.
*
* @throws JSONException If the index is negative or if the value is not finite.
*/
public JSONArray put(int index, Collection<?> value) throws JSONException {
this.put(index, new JSONArray(value));
@@ -641,14 +661,15 @@ public class JSONArray {
}
/**
* 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.
* 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.
*
* @throws JSONException If the index is negative or if the value is not finite.
*/
public JSONArray put(int index, double value) throws JSONException {
this.put(index, new Double(value));
@@ -656,12 +677,14 @@ public class JSONArray {
}
/**
* 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.
* 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 put(int index, int value) throws JSONException {
@@ -670,12 +693,14 @@ public class JSONArray {
}
/**
* 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.
* 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 put(int index, long value) throws JSONException {
@@ -684,13 +709,14 @@ public class JSONArray {
}
/**
* Put a value in the JSONArray, where the value will be a
* JSONObject that is produced from a Map.
* Put a value in the JSONArray, where the value will be a JSONObject that 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.
*
* @throws JSONException If the index is negative or if the the value is an invalid number.
*/
public JSONArray put(int index, Map<?, ?> value) throws JSONException {
this.put(index, new JSONObject(value));
@@ -698,16 +724,16 @@ public class JSONArray {
}
/**
* 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.
* 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 The value to put into the array. The value should be a
* Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
* JSONObject.NULL object.
* @param value The value to put into the array. The value should be a Boolean, Double, Integer, JSONArray,
* JSONObject, Long, or String, or the JSONObject.NULL object.
*
* @return this.
* @throws JSONException If the index is negative or if the the value is
* an invalid number.
*
* @throws JSONException If the index is negative or if the the value is an invalid number.
*/
public JSONArray put(int index, Object value) throws JSONException {
JSONObject.testValidity(value);
@@ -727,9 +753,10 @@ public class JSONArray {
/**
* Remove an index and close the hole.
*
* @param index The index of the element to be removed.
* @return The value that was associated with the index,
* or null if there was no value.
*
* @return The value that was associated with the index, or null if there was no value.
*/
public Object remove(int index) {
Object o = this.opt(index);
@@ -738,12 +765,12 @@ public class JSONArray {
}
/**
* 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.
* 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) throws JSONException {
@@ -758,15 +785,13 @@ public class JSONArray {
}
/**
* 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.
* <p>
* 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.
* <p/>
* Warning: This method assumes that the data structure is acyclical.
*
* @return a printable, displayable, transmittable
* representation of the array.
* @return a printable, displayable, transmittable representation of the array.
*/
public String toString() {
try {
@@ -777,14 +802,15 @@ public class JSONArray {
}
/**
* 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 <code>[</code>&nbsp;<small>(left bracket)</small> and ending
* with <code>]</code>&nbsp;<small>(right bracket)</small>.
* 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
* <code>[</code>&nbsp;<small>(left bracket)</small> and ending with <code>]</code>&nbsp;<small>(right
* bracket)</small>.
*
* @throws JSONException
*/
public String toString(int indentFactor) throws JSONException {
@@ -795,12 +821,12 @@ public class JSONArray {
}
/**
* Write the contents of the JSONArray as JSON text to a writer. For
* compactness, no whitespace is added.
* <p>
* Write the contents of the JSONArray as JSON text to a writer. For compactness, no whitespace is added.
* <p/>
* Warning: This method assumes that the data structure is acyclical.
*
* @return The writer.
*
* @throws JSONException
*/
public Writer write(Writer writer) throws JSONException {
@@ -808,16 +834,15 @@ public class JSONArray {
}
/**
* Write the contents of the JSONArray as JSON text to a writer. For
* compactness, no whitespace is added.
* <p>
* Write the contents of the JSONArray as JSON text to a writer. For compactness, no whitespace is added.
* <p/>
* 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.
* @param indentFactor The number of spaces to add to each level of indentation.
* @param indent The indention of the top level.
*
* @return The writer.
*
* @throws JSONException
*/
Writer write(Writer writer, int indentFactor, int indent)
@@ -2,6 +2,7 @@ package org.json;
/**
* The JSONException is thrown by the JSON.org classes when things are amiss.
*
* @author JSON.org
* @version 2010-12-24
*/
@@ -12,6 +13,7 @@ public class JSONException extends Exception {
/**
* Constructs a JSONException with an explanatory message.
*
* @param message Detail about the reason for the exception.
*/
public JSONException(String message) {
File diff suppressed because it is too large Load Diff
+5 -8
View File
@@ -1,18 +1,15 @@
package org.json;
/**
* The <code>JSONString</code> interface allows a <code>toJSONString()</code>
* method so that a class can change the behavior of
* <code>JSONObject.toString()</code>, <code>JSONArray.toString()</code>,
* and <code>JSONWriter.value(</code>Object<code>)</code>. The
* <code>toJSONString</code> method will be used instead of the default behavior
* of using the Object's <code>toString()</code> method and quoting the result.
* The <code>JSONString</code> interface allows a <code>toJSONString()</code> method so that a class can change the
* behavior of <code>JSONObject.toString()</code>, <code>JSONArray.toString()</code>, and
* <code>JSONWriter.value(</code>Object<code>)</code>. The <code>toJSONString</code> method will be used instead of the
* default behavior of using the Object's <code>toString()</code> method and quoting the result.
*/
public interface JSONString {
/**
* The <code>toJSONString</code> method allows a class to produce its own JSON
* serialization.
* The <code>toJSONString</code> method allows a class to produce its own JSON serialization.
*
* @return A strictly syntactically correct JSON text.
*/
+49 -44
View File
@@ -1,11 +1,6 @@
package org.json;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.*;
/*
* Copyright (c) 2002 JSON.org
@@ -32,9 +27,9 @@ import java.io.StringReader;
*/
/**
* A JSONTokener takes a source string and extracts characters and tokens from
* it. It is used by the JSONObject and JSONArray constructors to parse
* JSON source strings.
* A JSONTokener takes a source string and extracts characters and tokens from it. It is used by the JSONObject and
* JSONArray constructors to parse JSON source strings.
*
* @author JSON.org
* @version 2012-02-16
*/
@@ -51,7 +46,7 @@ public class JSONTokener {
/**
* Construct a JSONTokener from a Reader.
*
* @param reader A reader.
* @param reader A reader.
*/
public JSONTokener(Reader reader) {
this.reader = reader.markSupported()
@@ -75,16 +70,15 @@ public class JSONTokener {
/**
* Construct a JSONTokener from a string.
*
* @param s A source string.
* @param s A source string.
*/
public JSONTokener(String s) {
this(new StringReader(s));
}
/**
* Back up one character. This provides a sort of lookahead capability,
* so that you can test for a digit or letter before attempting to parse
* the next number or identifier.
* Back up one character. This provides a sort of lookahead capability, so that you can test for a digit or letter
* before attempting to parse the next number or identifier.
*/
public void back() throws JSONException {
if (this.usePrevious || this.index <= 0) {
@@ -98,8 +92,9 @@ public class JSONTokener {
/**
* Get the hex value of a character (base16).
* @param c A character between '0' and '9' or between 'A' and 'F' or
* between 'a' and 'f'.
*
* @param c A character between '0' and '9' or between 'A' and 'F' or between 'a' and 'f'.
*
* @return An int between 0 and 15, or -1 if c was not a hex digit.
*/
public static int dehexchar(char c) {
@@ -120,8 +115,8 @@ public class JSONTokener {
}
/**
* Determine if the source string still contains characters that next()
* can consume.
* Determine if the source string still contains characters that next() can consume.
*
* @return true if not yet at the end of the source.
*/
public boolean more() throws JSONException {
@@ -170,10 +165,12 @@ public class JSONTokener {
}
/**
* Consume the next character, and check that it matches a specified
* character.
* Consume the next character, and check that it matches a specified character.
*
* @param c The character to match.
*
* @return The character.
*
* @throws JSONException if the character does not match.
*/
public char next(char c) throws JSONException {
@@ -188,11 +185,11 @@ public class JSONTokener {
/**
* Get the next n characters.
*
* @param n The number of characters to take.
* @param n The number of characters to take.
*
* @return A string of n characters.
* @throws JSONException
* Substring bounds error if there are not
* n characters remaining in the source string.
*
* @throws JSONException Substring bounds error if there are not n characters remaining in the source string.
*/
public String next(int n) throws JSONException {
if (n == 0) {
@@ -214,8 +211,10 @@ public class JSONTokener {
/**
* Get the next char in the string, skipping whitespace.
* @throws JSONException
*
* @return A character, or 0 if there are no more characters.
*
* @throws JSONException
*/
public char nextClean() throws JSONException {
for (; ; ) {
@@ -227,14 +226,14 @@ public class JSONTokener {
}
/**
* Return the characters up to the next close quote character.
* Backslash processing is done. The formal JSON format does not
* allow strings in single quotes, but an implementation is allowed to
* accept them.
* @param quote The quoting character, either
* <code>"</code>&nbsp;<small>(double quote)</small> or
* <code>'</code>&nbsp;<small>(single quote)</small>.
* Return the characters up to the next close quote character. Backslash processing is done. The formal JSON format
* does not allow strings in single quotes, but an implementation is allowed to accept them.
*
* @param quote The quoting character, either <code>"</code>&nbsp;<small>(double quote)</small> or
* <code>'</code>&nbsp;<small>(single quote)</small>.
*
* @return A String.
*
* @throws JSONException Unterminated string.
*/
public String nextString(char quote) throws JSONException {
@@ -288,9 +287,10 @@ public class JSONTokener {
}
/**
* Get the text up but not including the specified character or the
* end of line, whichever comes first.
* @param delimiter A delimiter character.
* Get the text up but not including the specified character or the end of line, whichever comes first.
*
* @param delimiter A delimiter character.
*
* @return A string.
*/
public String nextTo(char delimiter) throws JSONException {
@@ -308,9 +308,11 @@ public class JSONTokener {
}
/**
* Get the text up but not including one of the specified delimiter
* characters or the end of line, whichever comes first.
* Get the text up but not including one of the specified delimiter characters or the end of line, whichever comes
* first.
*
* @param delimiters A set of delimiter characters.
*
* @return A string, trimmed.
*/
public String nextTo(String delimiters) throws JSONException {
@@ -330,11 +332,12 @@ public class JSONTokener {
}
/**
* Get the next value. The value can be a Boolean, Double, Integer,
* JSONArray, JSONObject, Long, or String, or the JSONObject.NULL object.
* @throws JSONException If syntax error.
* Get the next value. The value can be a Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
* JSONObject.NULL object.
*
* @return An object.
*
* @throws JSONException If syntax error.
*/
public Object nextValue() throws JSONException {
char c = this.nextClean();
@@ -376,11 +379,12 @@ public class JSONTokener {
}
/**
* Skip characters until the next character is the requested character.
* If the requested character is not found, no characters are skipped.
* Skip characters until the next character is the requested character. If the requested character is not found, no
* characters are skipped.
*
* @param to A character to skip to.
* @return The requested character, or zero if the requested character
* is not found.
*
* @return The requested character, or zero if the requested character is not found.
*/
public char skipTo(char to) throws JSONException {
char c;
@@ -411,6 +415,7 @@ public class JSONTokener {
* Make a JSONException to signal a syntax error.
*
* @param message The error message.
*
* @return A JSONException object, suitable for throwing
*/
public JSONException syntaxError(String message) {