mirror of
https://github.com/processing/processing4.git
synced 2026-02-03 05:39:18 +01:00
working on JSON API
This commit is contained in:
@@ -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> <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> <small>(left bracket)</small> and ending
|
||||
* with <code>]</code> <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.
|
||||
|
||||
@@ -33,7 +33,6 @@ import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Collection;
|
||||
@@ -212,7 +211,7 @@ public class JSONObject {
|
||||
* @throws JSONException If there is a syntax error in the source string
|
||||
* or a duplicated key.
|
||||
*/
|
||||
public JSONObject(JSONTokener x) {
|
||||
protected JSONObject(JSONTokener x) {
|
||||
this();
|
||||
char c;
|
||||
String key;
|
||||
@@ -270,7 +269,7 @@ public class JSONObject {
|
||||
* the JSONObject.
|
||||
* @throws JSONException
|
||||
*/
|
||||
public JSONObject(Map<String, Object> map) {
|
||||
protected JSONObject(HashMap<String, Object> map) {
|
||||
this.map = new HashMap<String, Object>();
|
||||
if (map != null) {
|
||||
Iterator i = map.entrySet().iterator();
|
||||
@@ -304,34 +303,35 @@ public class JSONObject {
|
||||
* @param bean An object that has getter methods that should be used
|
||||
* to make a JSONObject.
|
||||
*/
|
||||
public JSONObject(Object bean) {
|
||||
protected JSONObject(Object bean) {
|
||||
this();
|
||||
this.populateMap(bean);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Construct a JSONObject from an Object, using reflection to find the
|
||||
* public members. The resulting JSONObject's keys will be the strings
|
||||
* from the names array, and the values will be the field values associated
|
||||
* with those keys in the object. If a key is not found or not visible,
|
||||
* then it will not be copied into the new JSONObject.
|
||||
* @param object An object that has fields that should be used to make a
|
||||
* JSONObject.
|
||||
* @param names An array of strings, the names of the fields to be obtained
|
||||
* from the object.
|
||||
*/
|
||||
public JSONObject(Object object, String names[]) {
|
||||
this();
|
||||
Class c = object.getClass();
|
||||
for (int i = 0; i < names.length; i += 1) {
|
||||
String name = names[i];
|
||||
try {
|
||||
this.putOpt(name, c.getField(name).get(object));
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
// holding off on this method until we decide on how to handle reflection
|
||||
// /**
|
||||
// * Construct a JSONObject from an Object, using reflection to find the
|
||||
// * public members. The resulting JSONObject's keys will be the strings
|
||||
// * from the names array, and the values will be the field values associated
|
||||
// * with those keys in the object. If a key is not found or not visible,
|
||||
// * then it will not be copied into the new JSONObject.
|
||||
// * @param object An object that has fields that should be used to make a
|
||||
// * JSONObject.
|
||||
// * @param names An array of strings, the names of the fields to be obtained
|
||||
// * from the object.
|
||||
// */
|
||||
// public JSONObject(Object object, String names[]) {
|
||||
// this();
|
||||
// Class c = object.getClass();
|
||||
// for (int i = 0; i < names.length; i += 1) {
|
||||
// String name = names[i];
|
||||
// try {
|
||||
// this.putOpt(name, c.getField(name).get(object));
|
||||
// } catch (Exception ignore) {
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
@@ -343,8 +343,8 @@ public class JSONObject {
|
||||
* @exception JSONException If there is a syntax error in the source
|
||||
* string or a duplicated key.
|
||||
*/
|
||||
public JSONObject(String source) {
|
||||
this(new JSONTokener(source));
|
||||
static public JSONObject parse(String source) {
|
||||
return new JSONObject(new JSONTokener(source));
|
||||
}
|
||||
|
||||
|
||||
@@ -455,7 +455,7 @@ public class JSONObject {
|
||||
* @param d A double.
|
||||
* @return A String.
|
||||
*/
|
||||
public static String doubleToString(double d) {
|
||||
static protected String doubleToString(double d) {
|
||||
if (Double.isInfinite(d) || Double.isNaN(d)) {
|
||||
return "null";
|
||||
}
|
||||
@@ -483,7 +483,7 @@ public class JSONObject {
|
||||
* @return The object associated with the key.
|
||||
* @throws JSONException if the key is not found.
|
||||
*/
|
||||
public Object get(String key) {
|
||||
private Object get(String key) {
|
||||
if (key == null) {
|
||||
throw new RuntimeException("Null key.");
|
||||
}
|
||||
@@ -496,25 +496,63 @@ public class JSONObject {
|
||||
|
||||
|
||||
/**
|
||||
* Get the boolean value associated with a key.
|
||||
* Get the string associated with a key.
|
||||
*
|
||||
* @param key A key string.
|
||||
* @return The truth.
|
||||
* @throws JSONException
|
||||
* if the value is not a Boolean or the String "true" or "false".
|
||||
* @return A string which is the value.
|
||||
* @throws JSONException if there is no string value for the key.
|
||||
*/
|
||||
public boolean getBoolean(String key) {
|
||||
public String getString(String key) {
|
||||
Object object = this.get(key);
|
||||
if (object.equals(Boolean.FALSE) ||
|
||||
(object instanceof String &&
|
||||
((String)object).equalsIgnoreCase("false"))) {
|
||||
return false;
|
||||
} else if (object.equals(Boolean.TRUE) ||
|
||||
(object instanceof String &&
|
||||
((String)object).equalsIgnoreCase("true"))) {
|
||||
return true;
|
||||
if (object instanceof String) {
|
||||
return (String)object;
|
||||
}
|
||||
throw new RuntimeException("JSONObject[" + quote(key) + "] is not a Boolean.");
|
||||
throw new RuntimeException("JSONObject[" + quote(key) + "] not a string.");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the int value associated with a key.
|
||||
*
|
||||
* @param key A key string.
|
||||
* @return The integer value.
|
||||
* @throws JSONException if the key is not found or if the value cannot
|
||||
* be converted to an integer.
|
||||
*/
|
||||
public int getInt(String key) {
|
||||
Object object = this.get(key);
|
||||
try {
|
||||
return object instanceof Number
|
||||
? ((Number)object).intValue()
|
||||
: Integer.parseInt((String)object);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("JSONObject[" + quote(key) + "] is not an int.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the long value associated with a key.
|
||||
*
|
||||
* @param key A key string.
|
||||
* @return The long value.
|
||||
* @throws JSONException if the key is not found or if the value cannot
|
||||
* be converted to a long.
|
||||
*/
|
||||
public long getLong(String key) {
|
||||
Object object = this.get(key);
|
||||
try {
|
||||
return object instanceof Number
|
||||
? ((Number)object).longValue()
|
||||
: Long.parseLong((String)object);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("JSONObject[" + quote(key) + "] is not a long.", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public float getFloat(String key) {
|
||||
return (float) getDouble(key);
|
||||
}
|
||||
|
||||
|
||||
@@ -538,22 +576,25 @@ public class JSONObject {
|
||||
|
||||
|
||||
/**
|
||||
* Get the int value associated with a key.
|
||||
* Get the boolean value associated with a key.
|
||||
*
|
||||
* @param key A key string.
|
||||
* @return The integer value.
|
||||
* @throws JSONException if the key is not found or if the value cannot
|
||||
* be converted to an integer.
|
||||
* @return The truth.
|
||||
* @throws JSONException
|
||||
* if the value is not a Boolean or the String "true" or "false".
|
||||
*/
|
||||
public int getInt(String key) {
|
||||
public boolean getBoolean(String key) {
|
||||
Object object = this.get(key);
|
||||
try {
|
||||
return object instanceof Number
|
||||
? ((Number)object).intValue()
|
||||
: Integer.parseInt((String)object);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("JSONObject[" + quote(key) + "] is not an int.");
|
||||
if (object.equals(Boolean.FALSE) ||
|
||||
(object instanceof String &&
|
||||
((String)object).equalsIgnoreCase("false"))) {
|
||||
return false;
|
||||
} else if (object.equals(Boolean.TRUE) ||
|
||||
(object instanceof String &&
|
||||
((String)object).equalsIgnoreCase("true"))) {
|
||||
return true;
|
||||
}
|
||||
throw new RuntimeException("JSONObject[" + quote(key) + "] is not a Boolean.");
|
||||
}
|
||||
|
||||
|
||||
@@ -591,84 +632,48 @@ public class JSONObject {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the long value associated with a key.
|
||||
*
|
||||
* @param key A key string.
|
||||
* @return The long value.
|
||||
* @throws JSONException if the key is not found or if the value cannot
|
||||
* be converted to a long.
|
||||
*/
|
||||
public long getLong(String key) {
|
||||
Object object = this.get(key);
|
||||
try {
|
||||
return object instanceof Number
|
||||
? ((Number)object).longValue()
|
||||
: Long.parseLong((String)object);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("JSONObject[" + quote(key) + "] is not a long.", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get an array of field names from a JSONObject.
|
||||
*
|
||||
* @return An array of field names, or null if there are no names.
|
||||
*/
|
||||
public static String[] getNames(JSONObject jo) {
|
||||
int length = jo.length();
|
||||
if (length == 0) {
|
||||
return null;
|
||||
}
|
||||
Iterator iterator = jo.keys();
|
||||
String[] names = new String[length];
|
||||
int i = 0;
|
||||
while (iterator.hasNext()) {
|
||||
names[i] = (String)iterator.next();
|
||||
i += 1;
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get an array of field names from an Object.
|
||||
*
|
||||
* @return An array of field names, or null if there are no names.
|
||||
*/
|
||||
public static String[] getNames(Object object) {
|
||||
if (object == null) {
|
||||
return null;
|
||||
}
|
||||
Class klass = object.getClass();
|
||||
Field[] fields = klass.getFields();
|
||||
int length = fields.length;
|
||||
if (length == 0) {
|
||||
return null;
|
||||
}
|
||||
String[] names = new String[length];
|
||||
for (int i = 0; i < length; i += 1) {
|
||||
names[i] = fields[i].getName();
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the string associated with a key.
|
||||
*
|
||||
* @param key A key string.
|
||||
* @return A string which is the value.
|
||||
* @throws JSONException if there is no string value for the key.
|
||||
*/
|
||||
public String getString(String key) {
|
||||
Object object = this.get(key);
|
||||
if (object instanceof String) {
|
||||
return (String)object;
|
||||
}
|
||||
throw new RuntimeException("JSONObject[" + quote(key) + "] not a string.");
|
||||
}
|
||||
// /**
|
||||
// * Get an array of field names from a JSONObject.
|
||||
// *
|
||||
// * @return An array of field names, or null if there are no names.
|
||||
// */
|
||||
// public static String[] getNames(JSONObject jo) {
|
||||
// int length = jo.length();
|
||||
// if (length == 0) {
|
||||
// return null;
|
||||
// }
|
||||
// Iterator iterator = jo.keys();
|
||||
// String[] names = new String[length];
|
||||
// int i = 0;
|
||||
// while (iterator.hasNext()) {
|
||||
// names[i] = (String)iterator.next();
|
||||
// i += 1;
|
||||
// }
|
||||
// return names;
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * Get an array of field names from an Object.
|
||||
// *
|
||||
// * @return An array of field names, or null if there are no names.
|
||||
// */
|
||||
// public static String[] getNames(Object object) {
|
||||
// if (object == null) {
|
||||
// return null;
|
||||
// }
|
||||
// Class klass = object.getClass();
|
||||
// Field[] fields = klass.getFields();
|
||||
// int length = fields.length;
|
||||
// if (length == 0) {
|
||||
// return null;
|
||||
// }
|
||||
// String[] names = new String[length];
|
||||
// for (int i = 0; i < length; i += 1) {
|
||||
// names[i] = fields[i].getName();
|
||||
// }
|
||||
// return names;
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
@@ -716,7 +721,7 @@ public class JSONObject {
|
||||
* @return true if there is no value associated with the key or if
|
||||
* the value is the JSONObject.NULL object.
|
||||
*/
|
||||
public boolean isNull(String key) {
|
||||
protected boolean isNull(String key) {
|
||||
return JSONObject.NULL.equals(this.opt(key));
|
||||
}
|
||||
|
||||
@@ -747,25 +752,26 @@ public class JSONObject {
|
||||
*
|
||||
* @return The number of keys in the JSONObject.
|
||||
*/
|
||||
public int length() {
|
||||
public int size() {
|
||||
return this.map.size();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Produce a JSONArray containing the names of the elements of this
|
||||
* JSONObject.
|
||||
* @return A JSONArray containing the key strings, or null if the JSONObject
|
||||
* is empty.
|
||||
*/
|
||||
public JSONArray names() {
|
||||
JSONArray ja = new JSONArray();
|
||||
Iterator keys = this.keys();
|
||||
while (keys.hasNext()) {
|
||||
ja.append(keys.next());
|
||||
}
|
||||
return ja.size() == 0 ? null : ja;
|
||||
}
|
||||
// /**
|
||||
// * Produce a JSONArray containing the names of the elements of this
|
||||
// * JSONObject.
|
||||
// * @return A JSONArray containing the key strings, or null if the JSONObject
|
||||
// * is empty.
|
||||
// */
|
||||
// public JSONArray names() {
|
||||
// JSONArray ja = new JSONArray();
|
||||
// Iterator keys = this.keys();
|
||||
// while (keys.hasNext()) {
|
||||
// ja.append(keys.next());
|
||||
// }
|
||||
// return ja.size() == 0 ? null : ja;
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* Produce a string from a Number.
|
||||
@@ -773,7 +779,7 @@ public class JSONObject {
|
||||
* @return A String.
|
||||
* @throws JSONException If n is a non-finite number.
|
||||
*/
|
||||
public static String numberToString(Number number) {
|
||||
private static String numberToString(Number number) {
|
||||
if (number == null) {
|
||||
throw new RuntimeException("Null pointer");
|
||||
}
|
||||
@@ -800,134 +806,134 @@ public class JSONObject {
|
||||
* @param key A key string.
|
||||
* @return An object which is the value, or null if there is no value.
|
||||
*/
|
||||
public Object opt(String key) {
|
||||
private Object opt(String key) {
|
||||
return key == null ? null : this.map.get(key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get an optional boolean associated with a key.
|
||||
* It returns false if there is no such key, or if the value is not
|
||||
* Boolean.TRUE or the String "true".
|
||||
*
|
||||
* @param key A key string.
|
||||
* @return The truth.
|
||||
*/
|
||||
public boolean optBoolean(String key) {
|
||||
return this.optBoolean(key, false);
|
||||
}
|
||||
// /**
|
||||
// * Get an optional boolean associated with a key.
|
||||
// * It returns false if there is no such key, or if the value is not
|
||||
// * Boolean.TRUE or the String "true".
|
||||
// *
|
||||
// * @param key A key string.
|
||||
// * @return The truth.
|
||||
// */
|
||||
// private boolean optBoolean(String key) {
|
||||
// return this.optBoolean(key, false);
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* Get an optional boolean associated with a key.
|
||||
* It returns the defaultValue if there is no such key, or if it is not
|
||||
* a Boolean or the String "true" or "false" (case insensitive).
|
||||
*
|
||||
* @param key A key string.
|
||||
* @param defaultValue The default.
|
||||
* @return The truth.
|
||||
*/
|
||||
public boolean optBoolean(String key, boolean defaultValue) {
|
||||
try {
|
||||
return this.getBoolean(key);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
// /**
|
||||
// * Get an optional boolean associated with a key.
|
||||
// * It returns the defaultValue if there is no such key, or if it is not
|
||||
// * a Boolean or the String "true" or "false" (case insensitive).
|
||||
// *
|
||||
// * @param key A key string.
|
||||
// * @param defaultValue The default.
|
||||
// * @return The truth.
|
||||
// */
|
||||
// private boolean optBoolean(String key, boolean defaultValue) {
|
||||
// try {
|
||||
// return this.getBoolean(key);
|
||||
// } catch (Exception e) {
|
||||
// return defaultValue;
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* Get an optional double associated with a key,
|
||||
* or NaN if there is no such key or if its value is not a number.
|
||||
* If the value is a string, an attempt will be made to evaluate it as
|
||||
* a number.
|
||||
*
|
||||
* @param key A string which is the key.
|
||||
* @return An object which is the value.
|
||||
*/
|
||||
public double optDouble(String key) {
|
||||
return this.optDouble(key, Double.NaN);
|
||||
}
|
||||
// /**
|
||||
// * Get an optional double associated with a key,
|
||||
// * or NaN if there is no such key or if its value is not a number.
|
||||
// * If the value is a string, an attempt will be made to evaluate it as
|
||||
// * a number.
|
||||
// *
|
||||
// * @param key A string which is the key.
|
||||
// * @return An object which is the value.
|
||||
// */
|
||||
// private double optDouble(String key) {
|
||||
// return this.optDouble(key, Double.NaN);
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* Get an optional double associated with a key, or the
|
||||
* defaultValue if there is no such key or if its value is not a number.
|
||||
* If the value is a string, an attempt will be made to evaluate it as
|
||||
* a number.
|
||||
*
|
||||
* @param key A key string.
|
||||
* @param defaultValue The default.
|
||||
* @return An object which is the value.
|
||||
*/
|
||||
public double optDouble(String key, double defaultValue) {
|
||||
try {
|
||||
return this.getDouble(key);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
// /**
|
||||
// * Get an optional double associated with a key, or the
|
||||
// * defaultValue if there is no such key or if its value is not a number.
|
||||
// * If the value is a string, an attempt will be made to evaluate it as
|
||||
// * a number.
|
||||
// *
|
||||
// * @param key A key string.
|
||||
// * @param defaultValue The default.
|
||||
// * @return An object which is the value.
|
||||
// */
|
||||
// private double optDouble(String key, double defaultValue) {
|
||||
// try {
|
||||
// return this.getDouble(key);
|
||||
// } catch (Exception e) {
|
||||
// return defaultValue;
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* Get an optional int value associated with a key,
|
||||
* or zero if there is no such key or if the value is not a number.
|
||||
* If the value is a string, an attempt will be made to evaluate it as
|
||||
* a number.
|
||||
*
|
||||
* @param key A key string.
|
||||
* @return An object which is the value.
|
||||
*/
|
||||
public int optInt(String key) {
|
||||
return this.optInt(key, 0);
|
||||
}
|
||||
// /**
|
||||
// * Get an optional int value associated with a key,
|
||||
// * or zero if there is no such key or if the value is not a number.
|
||||
// * If the value is a string, an attempt will be made to evaluate it as
|
||||
// * a number.
|
||||
// *
|
||||
// * @param key A key string.
|
||||
// * @return An object which is the value.
|
||||
// */
|
||||
// private int optInt(String key) {
|
||||
// return this.optInt(key, 0);
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* Get an optional int value associated with a key,
|
||||
* or the default if there is no such key or if the value is not a number.
|
||||
* If the value is a string, an attempt will be made to evaluate it as
|
||||
* a number.
|
||||
*
|
||||
* @param key A key string.
|
||||
* @param defaultValue The default.
|
||||
* @return An object which is the value.
|
||||
*/
|
||||
public int optInt(String key, int defaultValue) {
|
||||
try {
|
||||
return this.getInt(key);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
// /**
|
||||
// * Get an optional int value associated with a key,
|
||||
// * or the default if there is no such key or if the value is not a number.
|
||||
// * If the value is a string, an attempt will be made to evaluate it as
|
||||
// * a number.
|
||||
// *
|
||||
// * @param key A key string.
|
||||
// * @param defaultValue The default.
|
||||
// * @return An object which is the value.
|
||||
// */
|
||||
// private int optInt(String key, int defaultValue) {
|
||||
// try {
|
||||
// return this.getInt(key);
|
||||
// } catch (Exception e) {
|
||||
// return defaultValue;
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* Get an optional JSONArray associated with a key.
|
||||
* It returns null if there is no such key, or if its value is not a
|
||||
* JSONArray.
|
||||
*
|
||||
* @param key A key string.
|
||||
* @return A JSONArray which is the value.
|
||||
*/
|
||||
public JSONArray optJSONArray(String key) {
|
||||
Object o = this.opt(key);
|
||||
return o instanceof JSONArray ? (JSONArray)o : null;
|
||||
}
|
||||
// /**
|
||||
// * Get an optional JSONArray associated with a key.
|
||||
// * It returns null if there is no such key, or if its value is not a
|
||||
// * JSONArray.
|
||||
// *
|
||||
// * @param key A key string.
|
||||
// * @return A JSONArray which is the value.
|
||||
// */
|
||||
// private JSONArray optJSONArray(String key) {
|
||||
// Object o = this.opt(key);
|
||||
// return o instanceof JSONArray ? (JSONArray)o : null;
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* Get an optional JSONObject associated with a key.
|
||||
* It returns null if there is no such key, or if its value is not a
|
||||
* JSONObject.
|
||||
*
|
||||
* @param key A key string.
|
||||
* @return A JSONObject which is the value.
|
||||
*/
|
||||
public JSONObject optJSONObject(String key) {
|
||||
Object object = this.opt(key);
|
||||
return object instanceof JSONObject ? (JSONObject)object : null;
|
||||
}
|
||||
// /**
|
||||
// * Get an optional JSONObject associated with a key.
|
||||
// * It returns null if there is no such key, or if its value is not a
|
||||
// * JSONObject.
|
||||
// *
|
||||
// * @param key A key string.
|
||||
// * @return A JSONObject which is the value.
|
||||
// */
|
||||
// private JSONObject optJSONObject(String key) {
|
||||
// Object object = this.opt(key);
|
||||
// return object instanceof JSONObject ? (JSONObject)object : null;
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
@@ -1487,7 +1493,7 @@ public class JSONObject {
|
||||
* @param object The object to wrap
|
||||
* @return The wrapped value
|
||||
*/
|
||||
public static Object wrap(Object object) {
|
||||
static protected Object wrap(Object object) {
|
||||
try {
|
||||
if (object == null) {
|
||||
return NULL;
|
||||
@@ -1596,7 +1602,7 @@ public class JSONObject {
|
||||
Writer write(Writer writer, int indentFactor, int indent) {
|
||||
try {
|
||||
boolean commanate = false;
|
||||
final int length = this.length();
|
||||
final int length = this.size();
|
||||
Iterator keys = this.keys();
|
||||
writer.write('{');
|
||||
|
||||
|
||||
@@ -9,6 +9,13 @@ o hint(OPENGL_ERRORS) should be the opposite to enable the reporting, no?
|
||||
o hint(ENABLE_OPENGL_ERRORS) should be the hint.. disabled by default
|
||||
X nah, just leave these turned on since (potentially) important
|
||||
X fix table loading quirk with extensions
|
||||
o full screen not auto-enabling with displayWidth/Height
|
||||
X Opting not to do this, because we can't remove the decorations on the
|
||||
X window at this point. And re-opening a new winodw is a lot of mess.
|
||||
X Better all around to just encourage the use of sketchFullScreen()
|
||||
X or cmd/ctrl-shift-R in the PDE.
|
||||
|
||||
|
||||
_ PImage.resize() greater than loaded image size hangs Java App
|
||||
_ http://code.google.com/p/processing/issues/detail?id=1463
|
||||
_ add a warning message with registerMouseEvent()
|
||||
@@ -19,6 +26,18 @@ _ length() too confusing w/ array.length being built-in (when to use ()?)
|
||||
_ size() a bit confusing with the p5 size command, but less problematic
|
||||
_ also shorter than getCount() or getLength()
|
||||
|
||||
|
||||
decisions on data
|
||||
X are we comfortable with setInt/Float/etc instead of just set(int, blah)
|
||||
X yes, better to have parity
|
||||
X too weird to have to explain why getXxx() needs types and set() doesn't
|
||||
_ get/set with Java's 'Map' class?
|
||||
_ really useful, but leaning toward not including it
|
||||
_ or leave it in as an advanced feature?
|
||||
X createXxx() methods less important
|
||||
X XML.parse() - or new XML("<tag>") or new XML("tag")
|
||||
_ add parseXML() and parseJSONObject(x)
|
||||
|
||||
cleaning/earlier
|
||||
C textureWrap() CLAMP and REPEAT now added
|
||||
C begin/endContour()
|
||||
@@ -40,10 +59,10 @@ X saveTable("filename.tsv") or saveTable("filename.txt", "tsv")
|
||||
X createTable() method in PApplet
|
||||
X removed getUniqueXxxx() and some others, pending names
|
||||
|
||||
_ when using loadFont(), don't enable native fonts unless hint() in use
|
||||
_ but on createFont(), we're probably OK
|
||||
_ might need to make reference notes about the two behaviors
|
||||
_ remove hint(ENABLE_NATIVE_FONTS)
|
||||
o when using loadFont(), don't enable native fonts unless hint() in use
|
||||
o but on createFont(), we're probably OK
|
||||
o might need to make reference notes about the two behaviors
|
||||
_ decision: remove hint(ENABLE_NATIVE_FONTS)
|
||||
|
||||
_ implement mousePressed(Event) etc
|
||||
_ better to do this instead of bringing back the magic event
|
||||
|
||||
Reference in New Issue
Block a user