Interface JsonStringOnlyUtil


public interface 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.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final com.google.gson.Gson
     
  • Method Summary

    Static Methods
    Modifier and Type
    Method
    Description
    static String
    Deserialize a JSON string literal (or raw token) into a Java String.
    static List<String>
    Deserialize a JSON array into a List.
    static String
    Serialize a Java String into a JSON string literal.
    static String
    Serialize a list of strings into a JSON array.
  • Field Details

    • GSON

      static final com.google.gson.Gson GSON
  • Method Details

    • toJson

      static String toJson(String s)
      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

      static String fromJson(String json)
      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

      static String toJsonList(List<String> list)
      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

      static List<String> fromJsonList(String json)
      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"