From 38f03585e46aa963963d2a68e75db888e7240cdb Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Thu, 1 Oct 2015 16:02:48 +0000 Subject: [PATCH] Added InputStream and Writer in constructor of Json classes --- src/zutil/parser/json/JSONObjectInputStream.java | 13 ++++++++++--- src/zutil/parser/json/JSONObjectOutputStream.java | 14 +++++++++----- src/zutil/parser/json/JSONParser.java | 7 ++++--- src/zutil/parser/json/JSONWriter.java | 3 ++- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/zutil/parser/json/JSONObjectInputStream.java b/src/zutil/parser/json/JSONObjectInputStream.java index f87d1a6..fa80f3c 100644 --- a/src/zutil/parser/json/JSONObjectInputStream.java +++ b/src/zutil/parser/json/JSONObjectInputStream.java @@ -48,10 +48,17 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C private HashMap objectCache; - public JSONObjectInputStream(Reader in) { - this.parser = new JSONParser(in); + private JSONObjectInputStream() { this.registeredClasses = new HashMap<>(); - this.objectCache = new HashMap<>(); + this.objectCache = new HashMap<>(); + } + public JSONObjectInputStream(Reader in) { + this(); + this.parser = new JSONParser(in); + } + public JSONObjectInputStream(InputStream in) { + this(); + this.parser = new JSONParser(in); } diff --git a/src/zutil/parser/json/JSONObjectOutputStream.java b/src/zutil/parser/json/JSONObjectOutputStream.java index 7229dfb..2adaa11 100644 --- a/src/zutil/parser/json/JSONObjectOutputStream.java +++ b/src/zutil/parser/json/JSONObjectOutputStream.java @@ -31,10 +31,7 @@ import zutil.parser.DataNode.DataType; import static zutil.parser.json.JSONObjectInputStream.*; import javax.activation.UnsupportedDataTypeException; -import java.io.Closeable; -import java.io.IOException; -import java.io.ObjectOutput; -import java.io.OutputStream; +import java.io.*; import java.lang.reflect.Array; import java.lang.reflect.Field; import java.lang.reflect.Modifier; @@ -50,9 +47,16 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput private HashMap objectCache; private JSONWriter out; - public JSONObjectOutputStream(OutputStream out) { + private JSONObjectOutputStream() { this.generateMetaData = true; this.objectCache = new HashMap(); + } + public JSONObjectOutputStream(OutputStream out) { + this(); + this.out = new JSONWriter(out); + } + public JSONObjectOutputStream(Writer out) { + this(); this.out = new JSONWriter(out); } diff --git a/src/zutil/parser/json/JSONParser.java b/src/zutil/parser/json/JSONParser.java index b923ec5..08a7e78 100644 --- a/src/zutil/parser/json/JSONParser.java +++ b/src/zutil/parser/json/JSONParser.java @@ -29,9 +29,7 @@ import zutil.parser.DataNode.DataType; import zutil.parser.Parser; import zutil.struct.MutableInt; -import java.io.IOException; -import java.io.Reader; -import java.io.StringReader; +import java.io.*; import java.util.regex.Pattern; /** @@ -48,6 +46,9 @@ public class JSONParser extends Parser { public JSONParser(Reader in){ this.in = in; } + public JSONParser(InputStream in){ + this.in = new InputStreamReader(in); + } /** * Starts parsing from the input. diff --git a/src/zutil/parser/json/JSONWriter.java b/src/zutil/parser/json/JSONWriter.java index 10d6c10..2961808 100644 --- a/src/zutil/parser/json/JSONWriter.java +++ b/src/zutil/parser/json/JSONWriter.java @@ -31,6 +31,7 @@ import zutil.parser.DataNode.DataType; import java.io.OutputStream; import java.io.PrintStream; import java.io.PrintWriter; +import java.io.Writer; import java.util.Iterator; /** @@ -55,7 +56,7 @@ public class JSONWriter{ * * @param out the OutputStream that the Nodes will be sent to */ - public JSONWriter(PrintStream out){ + public JSONWriter(Writer out){ this( new PrintWriter(out) ); }