Skip to content

Commit bbf967b

Browse files
committed
working on JSON API
1 parent cd542de commit bbf967b

File tree

3 files changed

+361
-307
lines changed

3 files changed

+361
-307
lines changed

core/src/processing/data/JSONArray.java

Lines changed: 72 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ of this software and associated documentation files (the "Software"), to deal
3030
import java.io.Writer;
3131
import java.lang.reflect.Array;
3232
import java.util.ArrayList;
33-
import java.util.Collection;
34-
import java.util.Iterator;
35-
import java.util.Map;
3633

3734
/**
3835
* A JSONArray is an ordered sequence of values. Its external text form is a
@@ -141,31 +138,36 @@ private JSONArray(JSONTokener x) {
141138
* and ends with <code>]</code>&nbsp;<small>(right bracket)</small>.
142139
* @throws JSONException If there is a syntax error.
143140
*/
144-
public JSONArray(String source) {
145-
this(new JSONTokener(source));
141+
static public JSONArray parse(String source) {
142+
try {
143+
return new JSONArray(new JSONTokener(source));
144+
} catch (Exception e) {
145+
return null;
146+
}
146147
}
147148

148149

149-
/**
150-
* Construct a JSONArray from a Collection.
151-
* @param collection A Collection.
152-
*/
153-
public JSONArray(Collection collection) {
154-
myArrayList = new ArrayList<Object>();
155-
if (collection != null) {
156-
Iterator iter = collection.iterator();
157-
while (iter.hasNext()) {
158-
myArrayList.add(JSONObject.wrap(iter.next()));
159-
}
160-
}
161-
}
150+
// /**
151+
// * Construct a JSONArray from a Collection.
152+
// * @param collection A Collection.
153+
// */
154+
// public JSONArray(Collection collection) {
155+
// myArrayList = new ArrayList<Object>();
156+
// if (collection != null) {
157+
// Iterator iter = collection.iterator();
158+
// while (iter.hasNext()) {
159+
// myArrayList.add(JSONObject.wrap(iter.next()));
160+
// }
161+
// }
162+
// }
162163

163164

165+
// TODO not decided whether we keep this one, but used heavily by JSONObject
164166
/**
165167
* Construct a JSONArray from an array
166168
* @throws JSONException If not an array.
167169
*/
168-
public JSONArray(Object array) {
170+
protected JSONArray(Object array) {
169171
this();
170172
if (array.getClass().isArray()) {
171173
int length = Array.getLength(array);
@@ -611,15 +613,27 @@ public JSONArray append(boolean value) {
611613
// }
612614

613615

616+
public JSONArray append(JSONArray value) {
617+
myArrayList.add(value);
618+
return this;
619+
}
620+
621+
622+
public JSONArray append(JSONObject value) {
623+
myArrayList.add(value);
624+
return this;
625+
}
626+
627+
614628
/**
615629
* Append an object value. This increases the array's length by one.
616630
* @param value An object value. The value should be a
617631
* Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
618632
* JSONObject.NULL object.
619633
* @return this.
620634
*/
621-
public JSONArray append(Object value) {
622-
this.myArrayList.add(value);
635+
protected JSONArray append(Object value) {
636+
myArrayList.add(value);
623637
return this;
624638
}
625639

@@ -728,6 +742,18 @@ public JSONArray setBoolean(int index, boolean value) {
728742
// }
729743

730744

745+
public JSONArray setArray(int index, JSONArray value) {
746+
set(index, value);
747+
return this;
748+
}
749+
750+
751+
public JSONArray setObject(int index, JSONObject value) {
752+
set(index, value);
753+
return this;
754+
}
755+
756+
731757
/**
732758
* Put or replace an object value in the JSONArray. If the index is greater
733759
* than the length of the JSONArray, then null elements will be added as
@@ -772,7 +798,8 @@ public int size() {
772798
* @param index The index must be between 0 and length() - 1.
773799
* @return true if the value at the index is null, or if there is no value.
774800
*/
775-
public boolean isNull(int index) {
801+
// TODO not sure on this one
802+
protected boolean isNull(int index) {
776803
return JSONObject.NULL.equals(this.opt(index));
777804
}
778805

@@ -812,36 +839,35 @@ public Object removeIndex(int index) {
812839

813840

814841
/**
815-
* Make a JSON text of this JSONArray. For compactness, no
816-
* unnecessary whitespace is added. If it is not possible to produce a
817-
* syntactically correct JSON text then null will be returned instead. This
818-
* could occur if the array contains an invalid number.
842+
* Make a JSON text of this JSONArray as a single line. For compactness,
843+
* no unnecessary whitespace is added. If it is not possible to produce
844+
* a syntactically correct JSON text then null will be returned instead.
845+
* This could occur if the array contains an invalid number.
819846
* <p>
820-
* Warning: This method assumes that the data structure is acyclical.
847+
* Warning: This method assumes that the data structure is acyclic.
821848
*
822849
* @return a printable, displayable, transmittable
823850
* representation of the array.
824851
*/
825852
@Override
826853
public String toString() {
827854
try {
828-
return this.toString(0);
855+
return toString(-1);
829856
} catch (Exception e) {
830857
return null;
831858
}
832859
}
833860

834861

835862
/**
836-
* Make a prettyprinted JSON text of this JSONArray.
863+
* Make a pretty-printed JSON text of this JSONArray.
837864
* Warning: This method assumes that the data structure is acyclical.
838865
* @param indentFactor The number of spaces to add to each level of
839-
* indentation.
866+
* indentation. Use -1 to specify no indentation and no newlines.
840867
* @return a printable, displayable, transmittable
841868
* representation of the object, beginning
842869
* with <code>[</code>&nbsp;<small>(left bracket)</small> and ending
843870
* with <code>]</code>&nbsp;<small>(right bracket)</small>.
844-
* @throws JSONException
845871
*/
846872
public String toString(int indentFactor) {
847873
StringWriter sw = new StringWriter();
@@ -854,53 +880,56 @@ public String toString(int indentFactor) {
854880
* Write the contents of the JSONArray as JSON text to a writer. For
855881
* compactness, no whitespace is added.
856882
* <p>
857-
* Warning: This method assumes that the data structure is acyclical.
883+
* Warning: This method assumes that the data structure is acyclic.
858884
*
859885
* @return The writer.
860-
* @throws JSONException
861886
*/
862-
public Writer write(Writer writer) {
863-
return this.write(writer, 0, 0);
887+
protected Writer write(Writer writer) {
888+
return this.write(writer, -1, 0);
864889
}
865890

866891
/**
867892
* Write the contents of the JSONArray as JSON text to a writer. For
868893
* compactness, no whitespace is added.
869894
* <p>
870-
* Warning: This method assumes that the data structure is acyclical.
895+
* Warning: This method assumes that the data structure is acyclic.
871896
*
872897
* @param indentFactor
873898
* The number of spaces to add to each level of indentation.
899+
* Use -1 to specify no indentation and no newlines.
874900
* @param indent
875901
* The indention of the top level.
876902
* @return The writer.
877903
* @throws JSONException
878904
*/
879-
Writer write(Writer writer, int indentFactor, int indent) {
905+
protected Writer write(Writer writer, int indentFactor, int indent) {
880906
try {
881907
boolean commanate = false;
882908
int length = this.size();
883909
writer.write('[');
884910

911+
// Use -1 to signify 'no indent'
912+
int thisFactor = (indentFactor == -1) ? 0 : indentFactor;
913+
885914
if (length == 1) {
886915
JSONObject.writeValue(writer, this.myArrayList.get(0),
887-
indentFactor, indent);
916+
thisFactor, indent);
888917
} else if (length != 0) {
889-
final int newindent = indent + indentFactor;
918+
final int newindent = indent + thisFactor;
890919

891920
for (int i = 0; i < length; i += 1) {
892921
if (commanate) {
893922
writer.write(',');
894923
}
895-
if (indentFactor > 0) {
924+
if (indentFactor != -1) {
896925
writer.write('\n');
897926
}
898927
JSONObject.indent(writer, newindent);
899928
JSONObject.writeValue(writer, this.myArrayList.get(i),
900-
indentFactor, newindent);
929+
thisFactor, newindent);
901930
commanate = true;
902931
}
903-
if (indentFactor > 0) {
932+
if (indentFactor != -1) {
904933
writer.write('\n');
905934
}
906935
JSONObject.indent(writer, indent);
@@ -916,7 +945,7 @@ Writer write(Writer writer, int indentFactor, int indent) {
916945
/**
917946
* Make a string from the contents of this JSONArray. The
918947
* <code>separator</code> string is inserted between each element.
919-
* Warning: This method assumes that the data structure is acyclical.
948+
* Warning: This method assumes that the data structure is acyclic.
920949
* @param separator A string that will be inserted between the elements.
921950
* @return a string.
922951
* @throws JSONException If the array contains an invalid number.

0 commit comments

Comments
 (0)