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.