Package dev.nolij.zson
Class Zson
java.lang.Object
dev.nolij.zson.Zson
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic @NotNull List<?> Create a new JSON array with the given values.static <E extends Enum<E>>
voidCreate a new entry with the given key, comment, and value.Create a new entry with the given key and value, and no comment.static @Nullable StringEscapes a string by replacing special characters with escape sequences.static <T> TConverts the given map to an object of the given type.Converts the given object to a JSON map.Create a new JSON object with the given entries.static <T> TParses a JSON value from the givenReader.static <T> TParses a JSON value from the contents of the givenPath.static <T> TparseString(@NotNull String serialized) Parses a JSON value from the givenString.@NotNull StringConverts the given data to a JSON5 string.static @Nullable String"Un-escapes" a string by replacing escape sequences with their actual characters.Converts the given object to a JSON5 value.withExpandArrays(boolean expandArrays) withIndent(String indent) withQuoteKeys(boolean quoteKeys) voidwrite(@NotNull Map<String, ZsonValue> data, @NotNull Appendable output) Writes the given data in JSON5 format to the given output.voidWrites the given data in JSON5 format to the given file.
-
Field Details
-
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
-
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.
- Control characters:
-
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:- They are static and not annotated with
@ZsonField(include = true) - They are transient
- They are not public (AKA private, protected, or package-private) and not annotated with
@ZsonField(include = true) - They are annotated with
@ZsonField(exclude = true)
@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.
- They are static and not annotated with
-
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 givenPath. 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 givenString. 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
Parses a JSON value from the givenReader. 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. -
stringify
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 IOExceptionWrites 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 IOExceptionWrites 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
Converts the given object to a JSON5 value.- Parameters:
value- The value to convert.- Returns:
- a JSON5-compatible string representation of the value.
-
value
-
withIndent
-
withExpandArrays
-
withQuoteKeys
-