public class JsonReader extends Object implements Closeable
JsonReader.jsonToJava(String json)
. This will
return a typed Java object graph.JsonReader.jsonToMaps(String json)
. This will
return an untyped object representation of the JSON String as a Map of Maps, where
the fields are the Map keys, and the field values are the associated Map's values. You can
call the JsonWriter.objectToJson() method with the returned Map, and it will serialize
the Graph into the equivalent JSON stream from which it was read.
JsonReader(InputStream in)
and then call
readObject()
. Cast the return value of readObject() to the Java class that was the root of
the graph.
JsonReader(InputStream in, true)
and then call
readObject()
. The return value will be a Map of Maps.
Modifier and Type | Class and Description |
---|---|
static interface |
JsonReader.ClassFactory
Subclass this interface and create a class that will return a new instance of the
passed in Class (c).
|
static interface |
JsonReader.ClassFactoryEx
Subclass this interface and create a class that will return a new instance of the
passed in Class (c).
|
static class |
JsonReader.CollectionFactory
Use to create new instances of collection interfaces (needed for empty collections)
|
static interface |
JsonReader.Factory |
static interface |
JsonReader.JsonClassReader
Implement this interface to add a custom JSON reader.
|
static interface |
JsonReader.JsonClassReaderBase
Implement this interface to add a custom JSON reader.
|
static interface |
JsonReader.JsonClassReaderEx
Implement this interface to add a custom JSON reader.
|
static class |
JsonReader.MapFactory
Use to create new instances of Map interfaces (needed for empty Maps)
|
static interface |
JsonReader.MissingFieldHandler
Used to react to fields missing when reading an object.
|
Modifier and Type | Field and Description |
---|---|
static String |
CUSTOM_READER_MAP |
static String |
JSON_READER |
static String |
MISSING_FIELD_HANDLER |
protected JsonReader.MissingFieldHandler |
missingFieldHandler |
static String |
NOT_CUSTOM_READER_MAP |
protected Set<Class> |
notCustom |
static String |
OBJECT_RESOLVER |
protected ConcurrentMap<Class,JsonReader.JsonClassReaderBase> |
readers |
static String |
TYPE_NAME_MAP |
static String |
UNKNOWN_OBJECT |
static String |
USE_MAPS |
Constructor and Description |
---|
JsonReader() |
JsonReader(InputStream inp) |
JsonReader(InputStream inp,
boolean useMaps) |
JsonReader(InputStream inp,
Map<String,Object> optionalArgs) |
JsonReader(Map<String,Object> optionalArgs)
Use this constructor if you already have a JsonObject graph and want to parse it into
Java objects by calling jsonReader.jsonObjectsToJava(rootJsonObject) after constructing
the JsonReader.
|
Modifier and Type | Method and Description |
---|---|
void |
addNotCustomReader(Class c)
Force json-io to use it's internal generic approach to writing the
passed in class, even if a Custom JSON reader is specified for its
parent class.
|
void |
addReader(Class c,
JsonReader.JsonClassReaderBase reader)
Call this method to add your custom JSON reader to json-io.
|
static void |
assignInstantiator(Class c,
JsonReader.Factory f)
For difficult to instantiate classes, you can add your own ClassFactory
or ClassFactoryEx which will be called when the passed in class 'c' is
encountered.
|
void |
close() |
protected Object |
convertParsedMapsToJava(JsonObject root)
This method converts a root Map, (which contains nested Maps
and so forth representing a Java Object graph), to a Java
object instance.
|
Map |
getArgs() |
Map<Long,JsonObject> |
getObjectsRead() |
Object |
getRefTarget(JsonObject jObj) |
Object |
jsonObjectsToJava(JsonObject root)
Convert a root JsonObject that represents parsed JSON, into
an actual Java object.
|
static Object |
jsonToJava(InputStream inputStream,
Map<String,Object> optionalArgs)
Convert the passed in JSON string into a Java object graph.
|
static Object |
jsonToJava(String json)
Convert the passed in JSON string into a Java object graph.
|
static Object |
jsonToJava(String json,
Map<String,Object> optionalArgs)
Convert the passed in JSON string into a Java object graph.
|
static Map |
jsonToMaps(InputStream inputStream,
Map<String,Object> optionalArgs)
Map args = ["USE_MAPS": true]
Use JsonReader.jsonToJava(inputStream, args)
Note that the return type will match the JSON type (array, object, string, long, boolean, or null).
|
static Map |
jsonToMaps(String json)
Map args = ["USE_MAPS": true]
Use JsonReader.jsonToJava(String json, args)
Note that the return type will match the JSON type (array, object, string, long, boolean, or null).
|
static Map |
jsonToMaps(String json,
Map<String,Object> optionalArgs)
Map args = ["USE_MAPS": true]
Use JsonReader.jsonToJava(String json, args)
Note that the return type will match the JSON type (array, object, string, long, boolean, or null).
|
static Object |
newInstance(Class c) |
static Object |
newInstance(Class c,
JsonObject jsonObject) |
Object |
readObject()
Read JSON input from the stream that was set up in the constructor, turning it into
Java Maps (JsonObject's).
|
void |
setMissingFieldHandler(JsonReader.MissingFieldHandler handler) |
protected boolean |
useMaps() |
public static final String CUSTOM_READER_MAP
public static final String NOT_CUSTOM_READER_MAP
public static final String USE_MAPS
public static final String UNKNOWN_OBJECT
public static final String JSON_READER
public static final String OBJECT_RESOLVER
public static final String TYPE_NAME_MAP
public static final String MISSING_FIELD_HANDLER
protected final ConcurrentMap<Class,JsonReader.JsonClassReaderBase> readers
protected JsonReader.MissingFieldHandler missingFieldHandler
public JsonReader()
public JsonReader(InputStream inp)
public JsonReader(Map<String,Object> optionalArgs)
optionalArgs
- Map of optional arguments for the JsonReader.public JsonReader(InputStream inp, boolean useMaps)
public JsonReader(InputStream inp, Map<String,Object> optionalArgs)
public static void assignInstantiator(Class c, JsonReader.Factory f)
c
- Class to assign an ClassFactory tof
- ClassFactory that will create 'c' instancespublic void addReader(Class c, JsonReader.JsonClassReaderBase reader)
c
- Class to assign a custom JSON reader toreader
- The JsonClassReader which will read the custom JSON format of 'c'public void addNotCustomReader(Class c)
c
- Class to which to force no custom JSON reading to occur.
Normally, this is not needed, however, if a reader is assigned to a
parent class of 'c', then calling this method on 'c' will prevent
any custom reader from processing class 'c'public void setMissingFieldHandler(JsonReader.MissingFieldHandler handler)
public Map getArgs()
public static Object jsonToJava(String json)
json
- String JSON inputpublic static Object jsonToJava(String json, Map<String,Object> optionalArgs)
json
- String JSON inputoptionalArgs
- Map of optional parameters to control parsing. See readme file for details.public static Object jsonToJava(InputStream inputStream, Map<String,Object> optionalArgs)
inputStream
- InputStream containing JSON inputoptionalArgs
- Map of optional parameters to control parsing. See readme file for details.public static Map jsonToMaps(String json)
json
- String of JSON contentpublic static Map jsonToMaps(String json, Map<String,Object> optionalArgs)
json
- String of JSON contentoptionalArgs
- Map of optional arguments to control customization. See readme file for
details on these options.public static Map jsonToMaps(InputStream inputStream, Map<String,Object> optionalArgs)
inputStream
- containing JSON contentoptionalArgs
- Map of optional arguments to control customization. See readme file for
details on these options.public Map<Long,JsonObject> getObjectsRead()
public Object getRefTarget(JsonObject jObj)
public Object readObject()
public Object jsonObjectsToJava(JsonObject root)
root
- JsonObject instance that was the root object from the
JSON input that was parsed in an earlier call to JsonReader.protected boolean useMaps()
protected Object convertParsedMapsToJava(JsonObject root)
root
- JsonObject instance that was the root object from the
JSON input that was parsed in an earlier call to JsonReader.public static Object newInstance(Class c, JsonObject jsonObject)
public void close()
close
in interface Closeable
close
in interface AutoCloseable
Copyright © 2016. All rights reserved.