Class JsonParser

java.lang.Object
de.linusdev.data.parser.JsonParser

public class JsonParser extends Object
This class is used to parse AbstractData to a json-string and json-string to SOData.

AbstractData to json-string can parse:
json-string to SOData can parse:
  • false/true to Boolean (ignores case)
  • null to null (ignores case)
  • Integer Numbers (1, 4, 5, ...) to Long
  • Decimal Numbers (5.6, ...) to Double
  • "strings" to String
  • Arrays ([...]) to ArrayList<Object><Object>
  • any other values are not supported and will most likely cause a ParseException

when parsing json-string to SOData:
  • Field Details

  • Constructor Details

    • JsonParser

      public JsonParser()
  • Method Details

    • setIndent

      public void setIndent(@NotNull @NotNull String indent)
      What to use as indent.
      Default: "\t"
      Parameters:
      indent - String
    • setDataSupplier

      public void setDataSupplier(@NotNull @NotNull Supplier<SOData> dataSupplier)
      When this parser reads a json-object, this Supplier is used to create a new SOData object.
      Default: SOData::newOrderedDataWithUnknownSize
      Parameters:
      dataSupplier - Supplier to supply with SOData
    • setArrayWrapperKey

      public void setArrayWrapperKey(@NotNull @NotNull String arrayWrapperKey)
      If the json to read, does not start with a json-object, but instead with a json-array, the array will be available with this key in the returned SOData.
      Default: "array"
      Parameters:
      arrayWrapperKey - key to use when wrapping the array in a SOData
    • setAllowNewLineInStrings

      public void setAllowNewLineInStrings(boolean allowNewLineInStrings)
      Default: true
      Parameters:
      allowNewLineInStrings - whether to allow new lines in keys and string-values while reading
    • parseStream

      @NotNull public @NotNull SOData parseStream(@NotNull @NotNull InputStream stream) throws IOException, ParseException
      parses the content of given stream to a SOData.
      The stream will be closed after parsing finished.
      If only an empty string is being read (for example "" or " "), an empty SOData will be returned.
      Parameters:
      stream - the stream to read the json from
      Returns:
      parsed SOData
      Throws:
      IOException - while parsing
      ParseException - while parsing
    • parseReader

      @NotNull public @NotNull SOData parseReader(@NotNull @NotNull Reader reader) throws IOException, ParseException
      parses the content of given reader to a SOData.
      The reader should not be wrapped in a BufferedReader, as this method does this.
      The reader will be closed after parsing finished.
      If only an empty string is being read (for example "" or " "), an empty SOData will be returned.
      Parameters:
      reader - the reader to read the json from
      Returns:
      parsed SOData
      Throws:
      IOException - while parsing
      ParseException - while parsing
    • writeDataToString

      @NotNull public @NotNull String writeDataToString(@Nullable @Nullable AbstractData<?,?> data) throws IOException
      Parameters:
      data - AbstractData to write to a StringBuffer
      Returns:
      StringBuffer.toString()
      Throws:
      IOException - IOException while writing
    • writeDataToStringBuilder

      @NotNull public @NotNull StringBuilder writeDataToStringBuilder(@Nullable @Nullable AbstractData<?,?> data)
      Parameters:
      data - AbstractData to write to a StringBuffer
      Returns:
      StringBuffer
    • writeData

      public void writeData(@NotNull @NotNull Appendable writer, @Nullable @Nullable AbstractData<?,?> data) throws IOException
      Parameters:
      data - AbstractData to write. null will write an empty Data: "{}"
      writer - Writer to write to
      Throws:
      IOException - IOException while writing