Class Zson

java.lang.Object
dev.nolij.zson.Zson

public final class Zson extends Object
  • Field Details

    • indent

      public String indent
    • expandArrays

      public boolean expandArrays
    • quoteKeys

      public boolean quoteKeys
  • Constructor Details

    • Zson

      public Zson()
  • Method Details

    • entry

      @NotNull @Contract("_, _, _ -> new") public static Map.Entry<String,ZsonValue> entry(@NotNull @NotNull String key, @NotNull @NotNull String comment, @Nullable @Nullable Object value)
      Create a new entry with the given key, comment, and value.
    • entry

      @NotNull @Contract(value="_, _ -> new", pure=true) public static Map.Entry<String,ZsonValue> entry(@NotNull @NotNull String key, @Nullable @Nullable Object value)
      Create a new entry with the given key and value, and no comment.
    • object

      @NotNull @SafeVarargs @Contract("_ -> new") public static @NotNull Map<String,ZsonValue> object(@NotNull Map.Entry<String,ZsonValue>... entries)
      Create a new JSON object with the given entries.
      Parameters:
      entries - the entries to add to the object. The array must not be null, and none of the entries may be null.
      Returns:
      a new JSON object with the given entries.
    • array

      @NotNull @Contract("_ -> new") public static @NotNull List<?> array(@NotNull @NotNull Object... values)
      Create a new JSON array with the given values.
      Parameters:
      values - the values to add to the array. The array must not be null, but may contain null values.
      Returns:
      a new JSON array with the given values.
    • convertEnum

      public static <E extends Enum<E>> void convertEnum(Map<String,ZsonValue> json, String key, Class<E> enumClass)
    • unescape

      @Nullable @Contract("null -> null; !null -> !null") public static @Nullable String unescape(@Nullable @Nullable String string)
      "Un-escapes" a string by replacing escape sequences with their actual characters.
      Parameters:
      string - the string to un-escape. May be null.
      Returns:
      the un-escaped string, or null if the input was null.
    • escape

      @Nullable @Contract("null, _ -> null; !null, _ -> !null") public static @Nullable String escape(@Nullable @Nullable String string, char escapeQuotes)
      Escapes a string by replacing special characters with escape sequences. The following characters are escaped:
      • Control characters: \0, \t, \b, \n, \r, \f
      • Quotes: \' and \"
      • Backslash: \\
      • Surrogate, private use, and unassigned characters
      Parameters:
      string - the string to escape. May be null.
      escapeQuotes - the character to escape quotes with.
      Returns:
      the escaped string, or null if the input was null.
    • obj2Map

      @NotNull @Contract("_ -> new") public static @NotNull Map<String,ZsonValue> obj2Map(@Nullable @Nullable Object object)
      Converts the given object to a JSON map. Fields of the object will be serialized in order of declaration. Fields will not be included in the map if: Additionally, fields annotated with @ZsonField(comment = "...") will have their comments included in the map.
      Parameters:
      object - the object to serialize. If null, an empty object will be returned.
      Returns:
      a JSON map representing the object.
    • map2Obj

      @NotNull @Contract("_ , _ -> new") public static <T> T map2Obj(@NotNull @NotNull Map<String,ZsonValue> map, @NotNull @NotNull Class<T> type)
      Converts the given map to an object of the given type. The map must contain all fields of the object, but they may be in any order. Fields will be set in order of declaration.
      Type Parameters:
      T - the type of object to create.
      Parameters:
      map - the map to deserialize. Must not be null.
      type - the type of object to create. Must not be null, and must have a no-args constructor.
      Returns:
      a new object of the given type with fields set from the map.
    • parseFile

      @Nullable @Contract(pure=true) public static <T> T parseFile(@NotNull @NotNull Path path) throws IOException
      Parses a JSON value from the contents of the given Path. If the file contains multiple JSON values, only the first one will be parsed.
      Parameters:
      path - The path to the file to parse
      Returns:
      see parse(Reader)
      Throws:
      IOException
    • parseString

      @Nullable @Contract(pure=true) public static <T> T parseString(@NotNull @Language("json5") @NotNull String serialized)
      Parses a JSON value from the given String. If the string contains multiple JSON values, only the first one will be parsed.
      Parameters:
      serialized - The JSON string to parse
      Returns:
      see parse(Reader)
    • parse

      @Nullable @Contract(mutates="param") public static <T> T parse(Reader input) throws IOException
      Parses a JSON value from the given Reader. The reader should be positioned at the start of the JSON value. If the reader contains multiple JSON values, only the first one will be parsed.
      Returns:
      One of:
      • Map - for JSON objects
      • List - for JSON arrays
      • String - for JSON strings
      • Number - for JSON numbers
      • Boolean - for JSON booleans
      • null - for JSON nulls
      Throws:
      IOException
    • stringify

      @NotNull public @NotNull String stringify(@NotNull @NotNull Map<String,ZsonValue> data)
      Converts the given data to a JSON5 string.
      Parameters:
      data - The data to convert.
      Returns:
      The JSON5 string.
    • write

      @Contract(mutates="param2") public void write(@NotNull @NotNull Map<String,ZsonValue> data, @NotNull @NotNull Path path) throws IOException
      Writes the given data in JSON5 format to the given file.
      Parameters:
      data - The data to write.
      path - The file to write to.
      Throws:
      IOException - If an I/O error occurs.
    • write

      @Contract(mutates="param2") public void write(@NotNull @NotNull Map<String,ZsonValue> data, @NotNull @NotNull Appendable output) throws IOException
      Writes the given data in JSON5 format to the given output.
      Parameters:
      data - The data to write.
      output - The output to write to.
      Throws:
      IOException - If an I/O error occurs.
    • value

      public String value(Object value, String format)
      Converts the given object to a JSON5 value.
      Parameters:
      value - The value to convert.
      Returns:
      a JSON5-compatible string representation of the value.
    • value

      public String value(Object value)
    • withIndent

      @Contract(value="_ -> this", mutates="this") public Zson withIndent(String indent)
    • withExpandArrays

      @Contract(value="_ -> this", mutates="this") public Zson withExpandArrays(boolean expandArrays)
    • withQuoteKeys

      @Contract(value="_ -> this", mutates="this") public Zson withQuoteKeys(boolean quoteKeys)