Skip to content

Commit 009b431

Browse files
committed
save methods for JSON objects and arrays
1 parent a997cca commit 009b431

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

core/src/processing/core/PApplet.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6109,12 +6109,33 @@ public JSONObject loadJSONObject(String filename) {
61096109
}
61106110

61116111

6112+
public boolean saveJSONObject(JSONObject json, String filename) {
6113+
return saveJSONObject(json, filename);
6114+
}
6115+
6116+
6117+
public boolean saveJSONObject(JSONObject json, String filename, String options) {
6118+
return json.save(saveFile(filename), options);
6119+
}
6120+
6121+
61126122
public JSONArray loadJSONArray(String filename) {
61136123
JSONTokener tokener = new JSONTokener(createReader(filename));
61146124
return new JSONArray(tokener);
61156125
}
61166126

61176127

6128+
public boolean saveJSONArray(JSONArray json, String filename) {
6129+
return saveJSONArray(json, filename);
6130+
}
6131+
6132+
6133+
public boolean saveJSONArray(JSONArray json, String filename, String options) {
6134+
return json.save(saveFile(filename), options);
6135+
}
6136+
6137+
6138+
61186139
/**
61196140
* @webref input:files
61206141
* @see Table

core/src/processing/data/JSONArray.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,17 @@ of this software and associated documentation files (the "Software"), to deal
3434
SOFTWARE.
3535
*/
3636

37+
import java.io.File;
3738
import java.io.IOException;
39+
import java.io.OutputStream;
40+
import java.io.PrintWriter;
3841
import java.io.StringWriter;
3942
import java.io.Writer;
4043
import java.lang.reflect.Array;
4144
import java.util.ArrayList;
4245

46+
import processing.core.PApplet;
47+
4348
/**
4449
* A JSONArray is an ordered sequence of values. Its external text form is a
4550
* string wrapped in square brackets with commas separating the values. The
@@ -847,6 +852,22 @@ public Object removeIndex(int index) {
847852
// }
848853

849854

855+
protected boolean save(OutputStream output) {
856+
return save(PApplet.createWriter(output));
857+
}
858+
859+
860+
public boolean save(File file, String options) {
861+
return save(PApplet.createWriter(file));
862+
}
863+
864+
865+
public boolean save(PrintWriter output) {
866+
output.print(format(2));
867+
output.flush();
868+
return true;
869+
}
870+
850871

851872
/**
852873
* Return the JSON data formatted with two spaces for indents.

core/src/processing/data/JSONObject.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ of this software and associated documentation files (the "Software"), to deal
3434
SOFTWARE.
3535
*/
3636

37+
import java.io.File;
3738
import java.io.IOException;
39+
import java.io.OutputStream;
40+
import java.io.PrintWriter;
3841
import java.io.StringWriter;
3942
import java.io.Writer;
4043
import java.lang.reflect.Method;
@@ -45,6 +48,8 @@ of this software and associated documentation files (the "Software"), to deal
4548
import java.util.Map;
4649
import java.util.Set;
4750

51+
import processing.core.PApplet;
52+
4853
/**
4954
* A JSONObject is an unordered collection of name/value pairs. Its external
5055
* form is a string wrapped in curly braces with colons between the names and
@@ -1414,6 +1419,23 @@ static protected void testValidity(Object o) {
14141419
// }
14151420

14161421

1422+
protected boolean save(OutputStream output) {
1423+
return save(PApplet.createWriter(output));
1424+
}
1425+
1426+
1427+
public boolean save(File file, String options) {
1428+
return save(PApplet.createWriter(file));
1429+
}
1430+
1431+
1432+
public boolean save(PrintWriter output) {
1433+
output.print(format(2));
1434+
output.flush();
1435+
return true;
1436+
}
1437+
1438+
14171439
/**
14181440
* Return the JSON data formatted with two spaces for indents.
14191441
* Chosen to do this since it's the most common case (e.g. with println()).

0 commit comments

Comments
 (0)