public interface ObjectReader extends Closeable
beginArray()
then use hasNext()
to check if there is
a next element and then call next()
to advance. When you call next in an array it will
read the next value and return its type ValueType
. Use it to check values type and to
retrieve its value (if it is a literal) use one of valueAsXXX methods, otherwise it is an array
or object. When hasNext returns false terminate the array by calling endArray()
.
beginObject()
then use hasNext()
to check if there is
a next property and then call next()
to read the name/value pair and name()
to
retrieve its name. If the value is a literal retrieve its value with valueAsXXX methods,
otherwise it is an array or object. When you finished reading all properties call
endObject()
.
metadata(nameOfTheMetadataProperty)
.
nextObjectMetadata()
to read the next objects metadata without calling
beginObject. This is useful when you want to handle some metadata in a converter and then
delegate the rest to another converter (that will call beginObject or again nextObjectMetadata,
so for him it will be transparent that you retrieved already some metadata and he will still be
able to retrieve the same data).
JsonReader.valueAsInt()
will parse it and return 123.
It does also conversion between numeric types (double <-> int etc).
skipValue()
. If the value is an object or an array it will skip
all its content.
public static void main(String[] args) { // we will read from json to Person Person.read(new JsonReader("{\"name\":\"eugen\",\"age\":26, \"childrenYearOfBirth\":[]}")); } class Person { String name; int age; List<Integer> childrenYearOfBirth; public static Person read(ObjectReader reader) { Person p = new Person(); for (; reader.hasNext();) { if ("name".equals(reader.name())) p.name = reader.valueAsString(); else if ("age".equals(reader.name())) p.age = reader.valueAsInt(); else if ("childrenYearOfBirth".equals(reader.name())) { if (reader.getValueType() == TypeValue.NULL) p.childrenYearOfBirth = null; else { reader.beginArray(); p.childrenYearOfBirth = new ArrayList<Integer>(); for (int i = 0; reader.hasNext(); i++) p.childrenYearOfBirth.add(reader.valueAsInt()); reader.endArray(); } } } return p; } }
ValueType
,
JsonReader
,
ObjectWriter
,
JsonWriter
Modifier and Type | Method and Description |
---|---|
ObjectReader |
beginArray()
Starts reading an array.
|
ObjectReader |
beginObject()
Starts reading a object.
|
int |
column() |
JsonType |
enclosingType() |
ObjectReader |
endArray()
Ends the array.
|
ObjectReader |
endObject()
Ends the object.
|
ValueType |
getValueType() |
boolean |
hasNext() |
String |
metadata(String name) |
String |
name() |
ValueType |
next()
If we are in a object it will read the next name/value pair and if we are in an array it will
read the next value (except if value is of complex type, in that case after the call to
next() you must use one of beginXXX methods).
|
ObjectReader |
nextObjectMetadata()
Will read nexts object metadata.
|
int |
row() |
ObjectReader |
skipValue()
If the value is of complex type it will skip its content.
|
boolean |
valueAsBoolean() |
byte[] |
valueAsByteArray() |
double |
valueAsDouble() |
float |
valueAsFloat() |
int |
valueAsInt() |
long |
valueAsLong() |
short |
valueAsShort() |
String |
valueAsString() |
ObjectReader beginObject()
endObject()
when the
objects contains no more properties.JsonStreamException
ObjectReader endObject()
JsonStreamException
ObjectReader beginArray()
endArray()
when the array
contains no more values.JsonStreamException
ObjectReader endArray()
JsonStreamException
ObjectReader nextObjectMetadata()
metadata(String)
method. For example if you call
beginObject()
you wont be able to do it anymore (however you still can retrieve the
metadata!).JsonStreamException
ValueType next()
ValueType
for possible types.JsonStreamException
boolean hasNext()
JsonStreamException
ObjectReader skipValue()
JsonStreamException
String metadata(String name)
name
- the name of the metadata to retrieve.JsonStreamException
String name()
next()
before.JsonStreamException
String valueAsString()
JsonStreamException
int valueAsInt()
JsonStreamException
NumberFormatException
valueAsString()
long valueAsLong()
JsonStreamException
NumberFormatException
valueAsString()
double valueAsDouble()
JsonStreamException
NumberFormatException
valueAsString()
short valueAsShort()
JsonStreamException
NumberFormatException
valueAsString()
float valueAsFloat()
JsonStreamException
NumberFormatException
valueAsString()
boolean valueAsBoolean()
JsonStreamException
valueAsString()
byte[] valueAsByteArray()
JsonStreamException
JsonType enclosingType()
int column()
int row()
Copyright © 2019. All rights reserved.