From b70855d74f79c1c0e230652e979e0ea35f26adce Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Fri, 26 Apr 2013 15:16:46 -0400 Subject: [PATCH] save methods for JSON objects and arrays --- core/src/processing/core/PApplet.java | 21 +++++++++++++++++++++ core/src/processing/data/JSONArray.java | 21 +++++++++++++++++++++ core/src/processing/data/JSONObject.java | 22 ++++++++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index d80a33c34..a5b4c939a 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -6109,12 +6109,33 @@ public class PApplet extends Applet } + public boolean saveJSONObject(JSONObject json, String filename) { + return saveJSONObject(json, filename); + } + + + public boolean saveJSONObject(JSONObject json, String filename, String options) { + return json.save(saveFile(filename), options); + } + + public JSONArray loadJSONArray(String filename) { JSONTokener tokener = new JSONTokener(createReader(filename)); return new JSONArray(tokener); } + public boolean saveJSONArray(JSONArray json, String filename) { + return saveJSONArray(json, filename); + } + + + public boolean saveJSONArray(JSONArray json, String filename, String options) { + return json.save(saveFile(filename), options); + } + + + /** * @webref input:files * @see Table diff --git a/core/src/processing/data/JSONArray.java b/core/src/processing/data/JSONArray.java index c195065e5..2ef5b9667 100644 --- a/core/src/processing/data/JSONArray.java +++ b/core/src/processing/data/JSONArray.java @@ -34,12 +34,17 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +import java.io.File; import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintWriter; import java.io.StringWriter; import java.io.Writer; import java.lang.reflect.Array; import java.util.ArrayList; +import processing.core.PApplet; + /** * A JSONArray is an ordered sequence of values. Its external text form is a * string wrapped in square brackets with commas separating the values. The @@ -847,6 +852,22 @@ public class JSONArray { // } + protected boolean save(OutputStream output) { + return save(PApplet.createWriter(output)); + } + + + public boolean save(File file, String options) { + return save(PApplet.createWriter(file)); + } + + + public boolean save(PrintWriter output) { + output.print(format(2)); + output.flush(); + return true; + } + /** * Return the JSON data formatted with two spaces for indents. diff --git a/core/src/processing/data/JSONObject.java b/core/src/processing/data/JSONObject.java index b01074ded..464c3ea07 100644 --- a/core/src/processing/data/JSONObject.java +++ b/core/src/processing/data/JSONObject.java @@ -34,7 +34,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +import java.io.File; import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintWriter; import java.io.StringWriter; import java.io.Writer; import java.lang.reflect.Method; @@ -45,6 +48,8 @@ import java.util.Iterator; import java.util.Map; import java.util.Set; +import processing.core.PApplet; + /** * A JSONObject is an unordered collection of name/value pairs. Its external * form is a string wrapped in curly braces with colons between the names and @@ -1414,6 +1419,23 @@ public class JSONObject { // } + protected boolean save(OutputStream output) { + return save(PApplet.createWriter(output)); + } + + + public boolean save(File file, String options) { + return save(PApplet.createWriter(file)); + } + + + public boolean save(PrintWriter output) { + output.print(format(2)); + output.flush(); + return true; + } + + /** * Return the JSON data formatted with two spaces for indents. * Chosen to do this since it's the most common case (e.g. with println()).