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

      @Contract("_ -> this") @NotNull public @NotNull JsonParser setIndent(@NotNull @NotNull String indent)
      What to use as indent.
      Default: "\t"
      Parameters:
      indent - String
    • setDataSupplier

      @Contract("_ -> this") @NotNull public @NotNull JsonParser 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

      @Contract("_ -> this") @NotNull public @NotNull JsonParser 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

      @Contract("_ -> this") @NotNull public @NotNull JsonParser setAllowNewLineInStrings(boolean allowNewLineInStrings)
      Default: true
      Parameters:
      allowNewLineInStrings - whether to allow new lines in keys and string-values while reading
    • setIdentifyNumberValues

      @Contract("_ -> this") @NotNull public @NotNull JsonParser setIdentifyNumberValues(boolean identifyNumberValues)
      If enabled it puts a single character (token) after a number, to identify which type of number it is. The tokens are the following:
      Default: false
      Parameters:
      identifyNumberValues - true to enable the feature described above
    • setAllowComments

      @Contract("_, _ -> this") @NotNull public @NotNull JsonParser setAllowComments(boolean allowComments, @Nullable @Nullable de.linusdev.lutils.interfaces.TBiConsumer<@NotNull JsonParser,@NotNull String,?> commentConsumer)
      If enabled it will allow comments in the json. E.g.:
       // This is a single line comment
       /* This is a
          multi line
          comment
       */
      Parameters:
      allowComments - true to allow comments as desribed above
    • parseStream

      @NotNull public @NotNull SOData parseStream(@NotNull @NotNull InputStream stream) throws IOException, de.linusdev.lutils.other.parser.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
      de.linusdev.lutils.other.parser.ParseException - while parsing
    • parseReader

      @NotNull public @NotNull SOData parseReader(@NotNull @NotNull Reader reader) throws IOException, de.linusdev.lutils.other.parser.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
      de.linusdev.lutils.other.parser.ParseException - while parsing
    • parseString

      @NotNull public @NotNull SOData parseString(@NotNull @NotNull String json) throws IOException, de.linusdev.lutils.other.parser.ParseException
      Creates a StringReader of given json and passes it to parse(JsonReader, ParseTracker).
      Parameters:
      json - the json to parse
      Returns:
      parsed SOData
      Throws:
      IOException - while parsing
      de.linusdev.lutils.other.parser.ParseException - while parsing
    • writeDataToString

      @NotNull public @NotNull String writeDataToString(@Nullable @Nullable AbstractData<?,?> data)
      Parameters:
      data - AbstractData to write to a StringBuffer
      Returns:
      StringBuffer.toString()
    • 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:
      writer - Writer to write to
      data - AbstractData to write. null will write an empty Data: "{}"
      Throws:
      IOException - IOException while writing