-
-
Save skynode/476798c1434e849c15bbaa54386356e4 to your computer and use it in GitHub Desktop.
InputStream from JSONObject. This class will help feeding JSON from the application server.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/JSONInputStream.java b/JSONInputStream.java | |
new file mode 100644 | |
index 0000000..93708ff | |
--- /dev/null | |
+++ b/JSONInputStream.java | |
@@ -0,0 +1,75 @@ | |
+package org.json; | |
+ | |
+import java.io.ByteArrayInputStream; | |
+import java.io.ByteArrayOutputStream; | |
+import java.io.IOException; | |
+import java.io.InputStream; | |
+import java.io.OutputStreamWriter; | |
+import java.nio.charset.Charset; | |
+import java.util.Iterator; | |
+ | |
+public class JSONInputStream extends InputStream { | |
+ | |
+ private final Iterator<Object> keyIterator; | |
+ private final JSONObject source; | |
+ private ByteArrayInputStream buffer = new ByteArrayInputStream("{".getBytes()); | |
+ private boolean isFinished = false; | |
+ private boolean commanate = false; | |
+ private final Charset charset; | |
+ | |
+ public JSONInputStream(JSONObject object) { | |
+ this(object, Charset.defaultCharset()); | |
+ } | |
+ | |
+ @SuppressWarnings("unchecked") | |
+ public JSONInputStream(JSONObject object, Charset charset) { | |
+ if (object == null || charset == null) { | |
+ throw new NullPointerException(); | |
+ } | |
+ this.keyIterator = object.keys(); | |
+ this.source = object; | |
+ this.charset = charset; | |
+ } | |
+ | |
+ @Override | |
+ public int read() throws IOException { | |
+ if (this.isFinished) { | |
+ return -1; | |
+ } | |
+ | |
+ int result = buffer.read(); | |
+ if (result == -1) { | |
+ loadNextToBuffer(); | |
+ commanate = true; | |
+ result = buffer == null ? -1 : buffer.read(); | |
+ if (result == -1) { | |
+ // finish loading all keys | |
+ this.isFinished = true; | |
+ result = '}'; | |
+ } | |
+ } | |
+ return result; | |
+ } | |
+ | |
+ private void loadNextToBuffer() throws IOException { | |
+ if (!keyIterator.hasNext()) { | |
+ buffer = null; | |
+ return; | |
+ } | |
+ | |
+ ByteArrayOutputStream output = new ByteArrayOutputStream(); | |
+ OutputStreamWriter writer = new OutputStreamWriter(output, charset); | |
+ if (commanate) { | |
+ writer.write(","); | |
+ } | |
+ Object key = keyIterator.next(); | |
+ try { | |
+ source.writeContent(writer, key); | |
+ } catch (JSONException e) { | |
+ throw new IOException("JSONException occurred when creating String from JSONObject", e); | |
+ } | |
+ writer.flush(); | |
+ this.buffer = new ByteArrayInputStream(output.toByteArray()); | |
+ } | |
+ | |
+} | |
diff --git a/JSONObject.java b/JSONObject.java | |
old mode 100755 | |
new mode 100644 | |
index a2b3edf..62066af | |
--- a/JSONObject.java | |
+++ b/JSONObject.java | |
@@ -1611,16 +1611,7 @@ public class JSONObject { | |
writer.write(','); | |
} | |
Object key = keys.next(); | |
- writer.write(quote(key.toString())); | |
- writer.write(':'); | |
- Object value = this.map.get(key); | |
- if (value instanceof JSONObject) { | |
- ((JSONObject)value).write(writer); | |
- } else if (value instanceof JSONArray) { | |
- ((JSONArray)value).write(writer); | |
- } else { | |
- writer.write(valueToString(value)); | |
- } | |
+ writeContent(writer, key); | |
commanate = true; | |
} | |
writer.write('}'); | |
@@ -1629,4 +1620,17 @@ public class JSONObject { | |
throw new JSONException(exception); | |
} | |
} | |
+ | |
+ void writeContent(Writer writer, Object key) throws IOException, JSONException { | |
+ writer.write(quote(key.toString())); | |
+ writer.write(':'); | |
+ Object value = this.map.get(key); | |
+ if (value instanceof JSONObject) { | |
+ ((JSONObject)value).write(writer); | |
+ } else if (value instanceof JSONArray) { | |
+ ((JSONArray)value).write(writer); | |
+ } else { | |
+ writer.write(valueToString(value)); | |
+ } | |
+ } | |
} | |
\ No newline at end of file | |
diff --git a/Test.java b/Test.java | |
old mode 100755 | |
new mode 100644 | |
index 4cd1ace..4d01f8b | |
--- a/Test.java | |
+++ b/Test.java | |
@@ -5,7 +5,13 @@ import java.util.Collection; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Map; | |
+import java.io.ByteArrayOutputStream; | |
+import java.io.IOException; | |
+import java.io.InputStreamReader; | |
+import java.io.OutputStreamWriter; | |
import java.io.StringWriter; | |
+import java.nio.charset.Charset; | |
+ | |
import junit.framework.TestCase; | |
/* | |
@@ -859,6 +865,31 @@ public class Test extends TestCase { | |
} | |
} | |
+ public void testJSONInputStream() throws JSONException, IOException { | |
+ for (String pattern : new String[]{ | |
+ "{}", "{\"message\":\"value\"}", "{array:[0,1,2,3,]}", "{\"nested\":[1,2,[3,4,5],6,[7,8],9]}", | |
+ "{\"0\":0}","{\"null\":null}","{\"multiline\":\"1\\r\\n2\\r3\\n4\"}" | |
+ }) { | |
+ JSONObject jsonObject = new JSONObject(pattern); | |
+ JSONInputStream stream = new JSONInputStream(jsonObject, Charset.forName("UTF-8")); | |
+ assertEquals(jsonObject.toString(), createStringFrom(stream)); | |
+ } | |
+ } | |
+ | |
+ private String createStringFrom(JSONInputStream input) throws IOException { | |
+ assert input != null; | |
+ InputStreamReader reader = new InputStreamReader(input); | |
+ ByteArrayOutputStream output = new ByteArrayOutputStream(); | |
+ OutputStreamWriter writer = new OutputStreamWriter(output); | |
+ char[] buffer = new char[1024]; | |
+ int length; | |
+ while ((length = reader.read(buffer)) != -1) { | |
+ writer.write(buffer, 0, length); | |
+ } | |
+ writer.flush(); | |
+ return output.toString(); | |
+ } | |
+ | |
/** | |
* Beany is a typical class that implements JSONString. It also | |
* provides some beany methods that can be used to |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment