save methods for JSON objects and arrays

This commit is contained in:
Ben Fry
2013-04-26 15:16:46 -04:00
parent 7274c54196
commit b70855d74f
3 changed files with 64 additions and 0 deletions

View File

@@ -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.

View File

@@ -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()).