Class JsonWriter

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

public class JsonWriter extends Object implements AutoCloseable
A json writer for writing json elements to an output.
The writer can be used to write json arrays, objects, values and null values.
The writer expects only one json element per output.
  • Field Details

    • config

      private final JsonConfig config
      The json config used by the writer.
    • writer

      private final BufferedWriter writer
      The internal writer used to write the json elements.
  • Constructor Details

    • JsonWriter

      public JsonWriter(@NotNull @NotNull OutputProvider output)
      Constructs a new json writer with the default configuration.
      Parameters:
      output - The output to create the writer for
      Throws:
      NullPointerException - If the output is null
    • JsonWriter

      public JsonWriter(@NotNull @NotNull OutputProvider output, @NotNull @NotNull JsonConfig config)
      Constructs a new json writer with the given configuration.
      Parameters:
      output - The output to create the writer for
      config - The configuration to use for the writer
      Throws:
      NullPointerException - If the output or the configuration is null
  • Method Details

    • writeJson

      public void writeJson(@NotNull @NotNull JsonElement json)
      Writes the given json element to the output.
      The json element is written as a string with the configuration of the writer.
      Parameters:
      json - The json element to write
      Throws:
      NullPointerException - If the json element is null
      UncheckedIOException - If an I/O error occurs
    • close

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