- All Implemented Interfaces:
AutoCloseable
string
or input provider
.The reader can be used to read json arrays, objects, primitives and null values.
The reader expects only one json element per input.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final JsonConfig
The json config used by this reader.private final StringReader
The internal reader used to read the json content.The scope stack used to keep track of the current json scope. -
Constructor Summary
ConstructorsConstructorDescriptionJsonReader
(@NotNull String string) Constructs a new json reader with the given string and the default configuration.JsonReader
(@NotNull String string, @NotNull JsonConfig config) Constructs a new json reader with the given string and configuration.JsonReader
(@NotNull InputProvider input) Constructs a new json reader with the given input and the default configuration.JsonReader
(@NotNull InputProvider input, @NotNull JsonConfig config) Constructs a new json reader with the given input and configuration. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
@NotNull JsonElement
readJson()
Reads the next json element from the input.private @NotNull JsonArray
Reads a json array from the underlying reader.
The json array is expected to be formatted as follows:
Empty array:[]
Array with one element:["value"]
Array with multiple elements:["value1", "value2", "value3"]
private @NotNull JsonPrimitive
Reads a json boolean value from the underlying reader.
The reader expects the next four or five characters to be 'true' or 'false'.private @NotNull JsonElement
Reads the next json element from the input.
Supported json elements are objects, arrays, primitives and null values.private @NotNull JsonNull
Reads a json null value from the underlying reader.
The reader expects the next four characters to be 'null'.private @NotNull JsonObject
Reads a json object from the underlying reader
The json object is expected to be formatted as follows:
Empty object:{}
Object with one entry:{"key": "value"}
Object with multiple entries:{"key1": "value1", "key2": "value2", "key3": "value3"}'
/li>private @NotNull JsonElement
Reads a json value from the underlying reader.
A json value can be either a string, number, boolean or null.
-
Field Details
-
scope
The scope stack used to keep track of the current json scope. -
config
The json config used by this reader. -
reader
The internal reader used to read the json content.
-
-
Constructor Details
-
JsonReader
Constructs a new json reader with the given string and the default configuration.- Parameters:
string
- The string to read from- Throws:
NullPointerException
- If the string is null
-
JsonReader
Constructs a new json reader with the given string and configuration.- Parameters:
string
- The string to read fromconfig
- The configuration to use- Throws:
NullPointerException
- If the string or configuration is null
-
JsonReader
Constructs a new json reader with the given input and the default configuration.- Parameters:
input
- The input to create the reader for- Throws:
NullPointerException
- If the input is null
-
JsonReader
Constructs a new json reader with the given input and configuration.- Parameters:
input
- The input to create the reader forconfig
- The configuration to use- Throws:
NullPointerException
- If the input or configuration is null
-
-
Method Details
-
readJson
Reads the next json element from the input.
In strict mode, this reader only accepts one json element per input.
- Returns:
- The next json element
- Throws:
JsonSyntaxException
- If the json is invalid- See Also:
-
readJsonElement
Reads the next json element from the input.
Supported json elements are objects, arrays, primitives and null values.- Returns:
- The read json element
- Throws:
JsonSyntaxException
- If the json is invalid (depends on the configuration)- See Also:
-
readJsonArray
Reads a json array from the underlying reader.
The json array is expected to be formatted as follows:
- Empty array:
[]
- Array with one element:
["value"]
- Array with multiple elements:
["value1", "value2", "value3"]
In strict mode, the reader expects no trailing comma after the last element.
- Returns:
- The read json array
- Throws:
JsonSyntaxException
- If the json array is invalid
- Empty array:
-
readJsonObject
Reads a json object from the underlying reader
The json object is expected to be formatted as follows:
- Empty object:
{}
- Object with one entry:
{"key": "value"}
- Object with multiple entries:
{"key1": "value1", "key2": "value2", "key3": "value3"}'
/li>
In strict mode, the reader expects the keys to be quoted and no trailing comma after the last entry.
- Returns:
- The read json object
- Throws:
JsonSyntaxException
- If the json object is invalid
- Empty object:
-
readJsonValue
Reads a json value from the underlying reader.
A json value can be either a string, number, boolean or null.
In strict mode, the reader will throw a exception if the value is not a valid json primitive.
It also accepts only the following values:
null
(lower-case)true
(lower-case)false
(lower-case)
In non-strict mode, the following values are also accepted:
NULL
(upper or mixed case)TRUE
(upper or mixed case)FALSE
(upper or mixed case)
- Returns:
- The read json value
- Throws:
JsonSyntaxException
- If the json value is invalid (depends on the configuration)
-
readJsonNull
Reads a json null value from the underlying reader.
The reader expects the next four characters to be 'null'.
In strict mode, the reader will throw an exception if the value is not 'null'.
In non-strict mode, the reader will accept any case of 'null' including mixed-case.
- Returns:
- The read json null value (always
JsonNull.INSTANCE
) - Throws:
JsonSyntaxException
- If the json null value is invalid (depends on the configuration)
-
readJsonBoolean
Reads a json boolean value from the underlying reader.
The reader expects the next four or five characters to be 'true' or 'false'.
In strict mode, the reader will throw a exception if the value is not 'true' or 'false'.
In non-strict mode, the reader will accept any case of 'true' or 'false' including mixed-case.
- Returns:
- The read json boolean value
- Throws:
JsonSyntaxException
- If the json boolean value is invalid (depends on the configuration)
-
close
- Specified by:
close
in interfaceAutoCloseable
- Throws:
IOException
-