Class JsonReader

java.lang.Object
net.luis.utils.io.data.json.JsonReader
All Implemented Interfaces:
AutoCloseable

public class JsonReader extends Object implements AutoCloseable
A json reader for reading json elements from a 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

    Fields
    Modifier and Type
    Field
    Description
    private final JsonConfig
    The json config used by this reader.
    private final StringReader
    The internal reader used to read the json content.
    private final Deque<Character>
    The scope stack used to keep track of the current json scope.
  • Constructor Summary

    Constructors
    Constructor
    Description
    JsonReader(@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 Type
    Method
    Description
    void
     
    @NotNull JsonElement
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • scope

      private final Deque<Character> scope
      The scope stack used to keep track of the current json scope.
    • config

      private final JsonConfig config
      The json config used by this reader.
    • reader

      private final StringReader reader
      The internal reader used to read the json content.
  • Constructor Details

    • JsonReader

      public JsonReader(@NotNull @NotNull String string)
      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

      public JsonReader(@NotNull @NotNull String string, @NotNull @NotNull JsonConfig config)
      Constructs a new json reader with the given string and configuration.
      Parameters:
      string - The string to read from
      config - The configuration to use
      Throws:
      NullPointerException - If the string or configuration is null
    • JsonReader

      public JsonReader(@NotNull @NotNull InputProvider input)
      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

      public JsonReader(@NotNull @NotNull InputProvider input, @NotNull @NotNull JsonConfig config)
      Constructs a new json reader with the given input and configuration.
      Parameters:
      input - The input to create the reader for
      config - The configuration to use
      Throws:
      NullPointerException - If the input or configuration is null
  • Method Details

    • readJson

      @NotNull public @NotNull JsonElement 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

      @NotNull private @NotNull JsonElement 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

      @NotNull private @NotNull JsonArray 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
    • readJsonObject

      @NotNull private @NotNull JsonObject 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
    • readJsonValue

      @NotNull private @NotNull JsonElement 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

      @NotNull private @NotNull JsonNull 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

      @NotNull private @NotNull JsonPrimitive 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

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException