mirror of
https://github.com/processing/processing4.git
synced 2026-02-12 18:10:43 +01:00
fix up write/save, add indent= option
This commit is contained in:
@@ -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.
|
||||
* <p>
|
||||
* 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();
|
||||
|
||||
Reference in New Issue
Block a user