diff --git a/core/src/processing/data/JSONArray.java b/core/src/processing/data/JSONArray.java index da68c1fd8..d1d0b50d6 100644 --- a/core/src/processing/data/JSONArray.java +++ b/core/src/processing/data/JSONArray.java @@ -36,7 +36,6 @@ SOFTWARE. import java.io.File; import java.io.IOException; -import java.io.OutputStream; import java.io.PrintWriter; import java.io.Reader; import java.io.StringWriter; @@ -1094,18 +1093,39 @@ public class JSONArray { // } - protected boolean save(OutputStream output) { - return save(PApplet.createWriter(output)); - } +// protected boolean save(OutputStream output) { +// return write(PApplet.createWriter(output), null); +// } public boolean save(File file, String options) { - return save(PApplet.createWriter(file)); + return write(PApplet.createWriter(file), options); } - public boolean save(PrintWriter output) { - output.print(format(2)); + public boolean write(PrintWriter output) { + return write(output, null); + } + + + public boolean write(PrintWriter output, String options) { + int indentFactor = 2; + if (options != null) { + String[] opts = PApplet.split(options, ','); + for (String opt : opts) { + if (opt.equals("compact")) { + indentFactor = -1; + } else if (opt.startsWith("indent=")) { + indentFactor = PApplet.parseInt(opt.substring(7), -2); + if (indentFactor == -2) { + throw new IllegalArgumentException("Could not read a number from " + opt); + } + } else { + System.err.println("Ignoring " + opt); + } + } + } + output.print(format(indentFactor)); output.flush(); return true; } @@ -1139,27 +1159,26 @@ public class JSONArray { public String format(int indentFactor) { StringWriter sw = new StringWriter(); synchronized (sw.getBuffer()) { - return this.write(sw, indentFactor, 0).toString(); + return this.writeInternal(sw, indentFactor, 0).toString(); } } - /** - * Write the contents of the JSONArray as JSON text to a writer. For - * compactness, no whitespace is added. - *

- * Warning: This method assumes that the data structure is acyclic. - * - * @return The writer. - */ - 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. +// *

+// * Warning: This method assumes that the data structure is acyclic. +// * +// * @return The writer. +// */ +// 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. + * Write the contents of the JSONArray as JSON text to a writer. *

* Warning: This method assumes that the data structure is acyclic. * @@ -1171,7 +1190,7 @@ public class JSONArray { * @return The writer. * @throws JSONException */ - protected Writer write(Writer writer, int indentFactor, int indent) { + protected Writer writeInternal(Writer writer, int indentFactor, int indent) { try { boolean commanate = false; int length = this.size(); diff --git a/core/src/processing/data/JSONObject.java b/core/src/processing/data/JSONObject.java index 4665d7d51..8f1c8c3d4 100644 --- a/core/src/processing/data/JSONObject.java +++ b/core/src/processing/data/JSONObject.java @@ -1557,14 +1557,28 @@ public class JSONObject { return write(PApplet.createWriter(file), options); } + public boolean write(PrintWriter output) { return write(output, null); } + public boolean write(PrintWriter output, String options) { int indentFactor = 2; - if (options != null && options.equals("compact")) { - indentFactor = -1; + if (options != null) { + String[] opts = PApplet.split(options, ','); + for (String opt : opts) { + if (opt.equals("compact")) { + indentFactor = -1; + } else if (opt.startsWith("indent=")) { + indentFactor = PApplet.parseInt(opt.substring(7), -2); + if (indentFactor == -2) { + throw new IllegalArgumentException("Could not read a number from " + opt); + } + } else { + System.err.println("Ignoring " + opt); + } + } } output.print(format(indentFactor)); output.flush(); @@ -1602,7 +1616,7 @@ public class JSONObject { public String format(int indentFactor) { StringWriter w = new StringWriter(); synchronized (w.getBuffer()) { - return this.write(w, indentFactor, 0).toString(); + return this.writeInternal(w, indentFactor, 0).toString(); } } @@ -1735,16 +1749,16 @@ public class JSONObject { if (value == null || value.equals(null)) { writer.write("null"); } else if (value instanceof JSONObject) { - ((JSONObject) value).write(writer, indentFactor, indent); + ((JSONObject) value).writeInternal(writer, indentFactor, indent); } else if (value instanceof JSONArray) { - ((JSONArray) value).write(writer, indentFactor, indent); + ((JSONArray) value).writeInternal(writer, indentFactor, indent); } else if (value instanceof Map) { - new JSONObject(value).write(writer, indentFactor, indent); + new JSONObject(value).writeInternal(writer, indentFactor, indent); } else if (value instanceof Collection) { - new JSONArray(value).write(writer, indentFactor, + new JSONArray(value).writeInternal(writer, indentFactor, indent); } else if (value.getClass().isArray()) { - new JSONArray(value).write(writer, indentFactor, indent); + new JSONArray(value).writeInternal(writer, indentFactor, indent); } else if (value instanceof Number) { writer.write(numberToString((Number) value)); } else if (value instanceof Boolean) { @@ -1773,15 +1787,14 @@ public class JSONObject { } /** - * Write the contents of the JSONObject as JSON text to a writer. For - * compactness, no whitespace is added. + * Write the contents of the JSONObject as JSON text to a writer. *

* Warning: This method assumes that the data structure is acyclical. * * @return The writer. * @throws JSONException */ - protected Writer write(Writer writer, int indentFactor, int indent) { + protected Writer writeInternal(Writer writer, int indentFactor, int indent) { try { boolean commanate = false; final int length = this.size();