org.quickconnectfamily.json
Class JSONInputStream
java.lang.Object
org.quickconnectfamily.json.JSONStream
org.quickconnectfamily.json.JSONInputStream
public class JSONInputStream
- extends JSONStream
The JSONInputStream class is used when you want to read JSON from any type of InputStream such
as a FileInputStream or a SocketInputStream. If you want to convert JSON string to the
appropriate Object and Array representations use the JSONUtilities.parse methods instead.
When your JSON is being parsed any JSON objects that it finds will be converted to HashMaps and
any arrays found will be converted to ArrayLists. All keys and values in the HashMaps generated
are Strings and all values in the ArrayLists generated are Strings.
- Example 1 JSON: ["1", "hello", {"name":"fred","age":"23"}]
- Example 1 Result: An ArrayList with three values: a String "1", a String "hello", and a HashMap
as the third value in the ArrayList. This HashMap has two key/value pairs: "name"/"fred" and "age"/"23".
- Example 2 JSON: {"state":"Idaho", "city":"Rexburg", "people":["bob","sue"]}
- Example 2 Result: A HashMap with three key/value pairs: "state"/"Idaho", "city"/"Rexburg", and
"people"/ArrayList. The ArrayList that is the value for the "people" key has two String values
"bob" and "sue".
While these examples only show two levels of depth your JSON string can go to any depth level. You
can have any combination of arrays, objects, sub-arrays, and sub-objects.
- Author:
- Lee S. Barney
Method Summary |
void |
close()
Closes the stream used. |
java.lang.Object |
readObject()
Reads a HashMap or ArrayList from the underlying stream |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
JSONInputStream
public JSONInputStream(java.io.InputStream theByteStream)
- Parameters:
theByteStream
- - the stream from which the JSON is to be read.
readObject
public java.lang.Object readObject()
throws JSONException
- Reads a HashMap or ArrayList from the underlying stream
- Returns:
- an Object of type HashMap if the JSON being read is an object or of type
ArrayList if the JSON being read is an array.
- Throws:
JSONException
close
public void close()
throws java.io.IOException
- Closes the stream used.
- Throws:
java.io.IOException