working on JSON API

This commit is contained in:
benfry
2012-12-15 17:47:44 +00:00
parent 7838cb1ff9
commit ca76f3dad8
3 changed files with 364 additions and 310 deletions

View File

@@ -30,9 +30,6 @@ import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
/**
* A JSONArray is an ordered sequence of values. Its external text form is a
@@ -141,31 +138,36 @@ public class JSONArray {
* and ends with <code>]</code>&nbsp;<small>(right bracket)</small>.
* @throws JSONException If there is a syntax error.
*/
public JSONArray(String source) {
this(new JSONTokener(source));
}
/**
* Construct a JSONArray from a Collection.
* @param collection A Collection.
*/
public JSONArray(Collection collection) {
myArrayList = new ArrayList<Object>();
if (collection != null) {
Iterator iter = collection.iterator();
while (iter.hasNext()) {
myArrayList.add(JSONObject.wrap(iter.next()));
}
static public JSONArray parse(String source) {
try {
return new JSONArray(new JSONTokener(source));
} catch (Exception e) {
return null;
}
}
// /**
// * Construct a JSONArray from a Collection.
// * @param collection A Collection.
// */
// public JSONArray(Collection collection) {
// myArrayList = new ArrayList<Object>();
// if (collection != null) {
// Iterator iter = collection.iterator();
// while (iter.hasNext()) {
// myArrayList.add(JSONObject.wrap(iter.next()));
// }
// }
// }
// TODO not decided whether we keep this one, but used heavily by JSONObject
/**
* Construct a JSONArray from an array
* @throws JSONException If not an array.
*/
public JSONArray(Object array) {
protected JSONArray(Object array) {
this();
if (array.getClass().isArray()) {
int length = Array.getLength(array);
@@ -611,6 +613,18 @@ public class JSONArray {
// }
public JSONArray append(JSONArray value) {
myArrayList.add(value);
return this;
}
public JSONArray append(JSONObject value) {
myArrayList.add(value);
return this;
}
/**
* Append an object value. This increases the array's length by one.
* @param value An object value. The value should be a
@@ -618,8 +632,8 @@ public class JSONArray {
* JSONObject.NULL object.
* @return this.
*/
public JSONArray append(Object value) {
this.myArrayList.add(value);
protected JSONArray append(Object value) {
myArrayList.add(value);
return this;
}
@@ -728,6 +742,18 @@ public class JSONArray {
// }
public JSONArray setArray(int index, JSONArray value) {
set(index, value);
return this;
}
public JSONArray setObject(int index, JSONObject value) {
set(index, value);
return this;
}
/**
* 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
@@ -772,7 +798,8 @@ public class JSONArray {
* @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) {
// TODO not sure on this one
protected boolean isNull(int index) {
return JSONObject.NULL.equals(this.opt(index));
}
@@ -812,12 +839,12 @@ 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.
* Make a JSON text of this JSONArray as a single line. 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.
* Warning: This method assumes that the data structure is acyclic.
*
* @return a printable, displayable, transmittable
* representation of the array.
@@ -825,7 +852,7 @@ public class JSONArray {
@Override
public String toString() {
try {
return this.toString(0);
return toString(-1);
} catch (Exception e) {
return null;
}
@@ -833,15 +860,14 @@ public class JSONArray {
/**
* Make a prettyprinted JSON text of this JSONArray.
* Make a pretty-printed 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.
* indentation. Use -1 to specify no indentation and no newlines.
* @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) {
StringWriter sw = new StringWriter();
@@ -854,53 +880,56 @@ public class JSONArray {
* 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.
* Warning: This method assumes that the data structure is acyclic.
*
* @return The writer.
* @throws JSONException
*/
public Writer write(Writer writer) {
return this.write(writer, 0, 0);
protected Writer write(Writer writer) {
return this.write(writer, -1, 0);
}
/**
* 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.
* Warning: This method assumes that the data structure is acyclic.
*
* @param indentFactor
* The number of spaces to add to each level of indentation.
* Use -1 to specify no indentation and no newlines.
* @param indent
* The indention of the top level.
* @return The writer.
* @throws JSONException
*/
Writer write(Writer writer, int indentFactor, int indent) {
protected Writer write(Writer writer, int indentFactor, int indent) {
try {
boolean commanate = false;
int length = this.size();
writer.write('[');
// Use -1 to signify 'no indent'
int thisFactor = (indentFactor == -1) ? 0 : indentFactor;
if (length == 1) {
JSONObject.writeValue(writer, this.myArrayList.get(0),
indentFactor, indent);
thisFactor, indent);
} else if (length != 0) {
final int newindent = indent + indentFactor;
final int newindent = indent + thisFactor;
for (int i = 0; i < length; i += 1) {
if (commanate) {
writer.write(',');
}
if (indentFactor > 0) {
if (indentFactor != -1) {
writer.write('\n');
}
JSONObject.indent(writer, newindent);
JSONObject.writeValue(writer, this.myArrayList.get(i),
indentFactor, newindent);
thisFactor, newindent);
commanate = true;
}
if (indentFactor > 0) {
if (indentFactor != -1) {
writer.write('\n');
}
JSONObject.indent(writer, indent);
@@ -916,7 +945,7 @@ 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.
* Warning: This method assumes that the data structure is acyclic.
* @param separator A string that will be inserted between the elements.
* @return a string.
* @throws JSONException If the array contains an invalid number.