cleaning up JSON a bit, remove bad import

This commit is contained in:
benfry
2012-12-13 17:28:52 +00:00
parent 4194da736c
commit 6e0b51db21
5 changed files with 155 additions and 123 deletions
+140 -117
View File
@@ -185,7 +185,7 @@ public class JSONArray {
* object at that index.
*/
private Object opt(int index) {
if (index < 0 || index >= this.length()) {
if (index < 0 || index >= this.size()) {
return null;
}
return myArrayList.get(index);
@@ -261,15 +261,12 @@ public class JSONArray {
}
/**
* Get a value from an index as a float. JSON uses 'double' values
* internally, so this is simply getDouble() cast to a float.
*/
public float getFloat(int index) {
Object object = this.get(index);
try {
return object instanceof Number
? ((Number)object).floatValue()
: Float.parseFloat((String)object);
} catch (Exception e) {
throw new RuntimeException("JSONArray[" + index + "] is not a number.");
}
return (float) getDouble(index);
}
@@ -526,45 +523,6 @@ public class JSONArray {
// }
/**
* Append a boolean value. This increases the array's length by one.
*
* @param value A boolean value.
* @return this.
*/
public JSONArray append(boolean value) {
this.append(value ? Boolean.TRUE : Boolean.FALSE);
return this;
}
/**
* 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 append(Collection value) {
this.append(new JSONArray(value));
return this;
}
/**
* 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 append(double value) {
Double d = new Double(value);
JSONObject.testValidity(d);
this.append(d);
return this;
}
/**
* Append an int value. This increases the array's length by one.
*
@@ -590,17 +548,69 @@ public class JSONArray {
/**
* 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.
* Append a float value. This increases the array's length by one.
* This will store the value as a double, since there are no floats in JSON.
*
* @param value A float value.
* @throws JSONException if the value is not finite.
* @return this.
*/
public JSONArray append(Map value) {
this.append(new JSONObject(value));
public JSONArray append(float value) {
return append((double) value);
}
/**
* 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 append(double value) {
Double d = new Double(value);
JSONObject.testValidity(d);
this.append(d);
return this;
}
/**
* Append a boolean value. This increases the array's length by one.
*
* @param value A boolean value.
* @return this.
*/
public JSONArray append(boolean value) {
this.append(value ? Boolean.TRUE : Boolean.FALSE);
return this;
}
// /**
// * 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 append(Collection value) {
// this.append(new JSONArray(value));
// return this;
// }
// /**
// * 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 append(Map value) {
// this.append(new JSONObject(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
@@ -614,50 +624,19 @@ 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.
* @param index The subscript.
* @param value A boolean value.
* @return this.
* @throws JSONException If the index is negative.
*/
public JSONArray set(int index, boolean value) {
this.set(index, value ? Boolean.TRUE : Boolean.FALSE);
return this;
}
/**
* 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 set(int index, Collection value) {
this.set(index, new JSONArray(value));
return this;
}
/**
* 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 set(int index, double value) {
this.set(index, new Double(value));
return this;
}
// /**
// * 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 set(int index, Collection value) {
// this.set(index, new JSONArray(value));
// return this;
// }
/**
@@ -669,7 +648,7 @@ public class JSONArray {
* @return this.
* @throws JSONException If the index is negative.
*/
public JSONArray set(int index, int value) {
public JSONArray setInt(int index, int value) {
this.set(index, new Integer(value));
return this;
}
@@ -684,27 +663,71 @@ public class JSONArray {
* @return this.
* @throws JSONException If the index is negative.
*/
public JSONArray set(int index, long value) {
this.set(index, new Long(value));
return this;
public JSONArray setLong(int index, long value) {
return set(index, new Long(value));
}
/**
* Put a value in the JSONArray, where the value will be a
* JSONObject that is produced from a Map.
* Put or replace a float value. If the index is greater than the length
* of the JSONArray, then null elements will be added as necessary to pad
* it out. There are no 'double' values in JSON, so this is passed to
* setDouble(value).
* @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.
* @param value A float value.
* @return this.
* @throws RuntimeException If the index is negative or if the value is
* not finite.
*/
public JSONArray set(int index, Map value) {
this.set(index, new JSONObject(value));
return this;
public JSONArray setFloat(int index, float value) {
return setDouble(index, value);
}
/**
* 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 setDouble(int index, double value) {
return set(index, new Double(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 setBoolean(int index, boolean value) {
return set(index, value ? Boolean.TRUE : Boolean.FALSE);
}
// /**
// * 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.
// */
// public JSONArray set(int index, Map value) {
// this.set(index, new JSONObject(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
@@ -717,15 +740,15 @@ public class JSONArray {
* @throws JSONException If the index is negative or if the the value is
* an invalid number.
*/
public JSONArray set(int index, Object value) {
private JSONArray set(int index, Object value) {
JSONObject.testValidity(value);
if (index < 0) {
throw new RuntimeException("JSONArray[" + index + "] not found.");
}
if (index < this.length()) {
if (index < this.size()) {
this.myArrayList.set(index, value);
} else {
while (index != this.length()) {
while (index != this.size()) {
this.append(JSONObject.NULL);
}
this.append(value);
@@ -739,7 +762,7 @@ public class JSONArray {
*
* @return The length (or size).
*/
public int length() {
public int size() {
return myArrayList.size();
}
@@ -856,7 +879,7 @@ public class JSONArray {
Writer write(Writer writer, int indentFactor, int indent) {
try {
boolean commanate = false;
int length = this.length();
int length = this.size();
writer.write('[');
if (length == 1) {
@@ -899,7 +922,7 @@ public class JSONArray {
* @throws JSONException If the array contains an invalid number.
*/
public String join(String separator) {
int len = this.length();
int len = this.size();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < len; i += 1) {
+3 -3
View File
@@ -764,7 +764,7 @@ public class JSONObject {
while (keys.hasNext()) {
ja.append(keys.next());
}
return ja.length() == 0 ? null : ja;
return ja.size() == 0 ? null : ja;
}
/**
@@ -1368,11 +1368,11 @@ public class JSONObject {
* @throws JSONException If any of the values are non-finite numbers.
*/
public JSONArray toJSONArray(JSONArray names) {
if (names == null || names.length() == 0) {
if (names == null || names.size() == 0) {
return null;
}
JSONArray ja = new JSONArray();
for (int i = 0; i < names.length(); i += 1) {
for (int i = 0; i < names.size(); i += 1) {
ja.append(this.opt(names.getString(i)));
}
return ja;
-1
View File
@@ -33,7 +33,6 @@ import java.util.*;
import processing.core.PApplet;
import processing.core.PConstants;
import quicktime.streaming.SettingsDialog;
// function that will convert awful CSV to TSV.. or something else?
// maybe to write binary instead? then read the binary file once it's ok?
+7
View File
@@ -11,6 +11,13 @@ X nah, just leave these turned on since (potentially) important
X fix table loading quirk with extensions
_ 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()
_ don't let OpenGL fire those mouse events at all
_ phrase the warning differently when OpenGL is in use (or only show then?)
_ api note: size() used in data classes
_ 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()
cleaning/earlier
C textureWrap() CLAMP and REPEAT now added
+5 -2
View File
@@ -21,6 +21,8 @@ X add ESC and cmd/ctrl-W to the Examples window
X move token/syntax coloring out of theme.txt and back into preferences
X what order should 'recent' menu use?
X most recent is at the top
_ unable to open the URL link to reference after updated to 2.0b7
_ http://code.google.com/p/processing/issues/detail?id=1465
earlier
X include debug mode as 'experimental'?
@@ -36,6 +38,7 @@ X removed in 2.0b7, because it has bugs and is no longer compatible
<string>true</string>
</dict>
_ show warning when registering mouse/key events with OpenGL
https://processing-js.lighthouseapp.com/
@@ -68,14 +71,14 @@ _ problem changing sketchbook to new location (e.g. from ex to regular)
_ new libraries are not picked up
_ Contributed modes should show up in mode menu after installation
_ http://code.google.com/p/processing/issues/detail?id=1466
_ using "Add Library" requires restart of Processing before lib recognized
_ http://code.google.com/p/processing/issues/detail?id=1387
_ from Casey
_ list in the PDE would be updated automatically by querying a web service
_ list on the website would be generated using the same web service
_ All I would need to do is update web/contrib_generate/sources.conf
_ and the rest would happen automatically.
_ alternating blue/white backgrounds aren't updated after changing filter
_ using "Add Library" requires restart of Processing before lib recognized
_ http://code.google.com/p/processing/issues/detail?id=1387
2.0 FINAL / new interface