Package valorless.rarespawns.utils
Class JsonStringOnlyUtil
java.lang.Object
valorless.rarespawns.utils.JsonStringOnlyUtil
Tiny JSON helper that treats values strictly as strings.
Behavior:
- toJson("abc") -> "\"abc\"" (a proper JSON string)
- fromJson("\"abc\"") -> "abc"
- fromJson("abc") -> "abc" (unquoted input is returned as-is, NOT parsed to a number)
- fromJson("null") -> null
- toJsonList(["a","b"]) -> "[\"a\",\"b\"]"
- fromJsonList("[\"a\",1,foo]") -> ["a", "1", "foo"]
Important:
- This utility intentionally does NOT convert numeric tokens into Java numbers.
If the input is an unquoted token (e.g. 1e761457) it will be returned as the literal string "1e761457",
avoiding Double.POSITIVE_INFINITY or other numeric conversions.
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringDeserialize a JSON string literal (or raw token) into a Java String.fromJsonList(String json) Deserialize a JSON array into a List. static StringSerialize a Java String into a JSON string literal.static StringtoJsonList(List<String> list) Serialize a list of strings into a JSON array.
-
Method Details
-
toJson
Serialize a Java String into a JSON string literal. Example: toJson("hello") -> "\"hello\""- Parameters:
s- input string (may be null)- Returns:
- JSON representation (the four characters "null" if s == null)
-
fromJson
Deserialize a JSON string literal (or raw token) into a Java String. Rules: - If the trimmed input equals "null" -> returns null. - If input begins with a double-quote -> use a proper JSON string parse (handles escapes). - Otherwise -> return the trimmed input exactly as-is (no numeric conversion). This avoids parsing huge scientific-notation numbers into Infinity.- Parameters:
json- JSON input (may be a quoted JSON string, the literal "null", or an unquoted token)- Returns:
- the Java String or null
-
toJsonList
Serialize a list of strings into a JSON array. Null elements will be serialized as JSON null.- Parameters:
list- list of strings (may be null)- Returns:
- JSON array text or "null" if list is null
-
fromJsonList
Deserialize a JSON array into a List. Rules per element: - JSON null -> Java null - JSON string -> unescaped string value - Any other JSON primitive/object/array -> the element's JSON text (element.toString()) If the input is not a JSON array but a single token, returns a singleton list containing fromJson(input). - Parameters:
json- JSON input- Returns:
- List of Strings (empty list if array is empty), or null if input is the literal "null"
-