Interface JsonString

All Superinterfaces:
JsonValue

public non-sealed interface JsonString extends JsonValue
The interface that represents JSON string.

A JsonString can be created by parsing JSON text using either Json.parse(String) or Json.parse(char[]).

Alternatively, of(String) can be used to obtain a JsonString directly from a String. The String values of JsonString instances produced by the following expressions are all equivalent:

    Json.parse("\"foo\\t\"").asString();
    Json.parse("\"foo\\u0009\"").asString();
    JsonString.of("foo\t").asString();
Within a valid JSON string, any character may be escaped using either a two-character escape sequence (if applicable) or one or two Unicode escape sequences. A supplementary character is represented by two Unicode escape sequences corresponding to its surrogate pair. Quotation Mark (U+0022), Backslash (Reverse Solidus, U+005C), and the control characters (U+0000 through U+001F) must be escaped.
Since:
28
External Specifications
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the String value represented by this JsonValue if it is an instance of JsonString; otherwise, throws a JsonValueException.
    static JsonString
    of(String src)
    Returns the JsonString created from the given String.
    Returns the JSON string represented by this JsonString.

    Methods declared in interface JsonValue

    asBoolean, asDouble, asInt, asList, asLong, asMap, get, get, tryGet, tryValue
    Modifier and Type
    Method
    Description
    default boolean
    Returns the boolean value represented by this JsonValue if it is an instance of JsonBoolean; otherwise, throws a JsonValueException.
    default double
    Returns a double if this JsonValue is an instance of JsonNumber that can be converted, as if by Double.parseDouble, to a finite double value; otherwise, throws a JsonValueException.
    default int
    Returns an int if this JsonValue is an instance of JsonNumber that can be converted exactly; otherwise, throws a JsonValueException.
    default List<JsonValue>
    Returns an unmodifiable list of the JsonValues if this JsonValue is an instance of JsonArray; otherwise, throws a JsonValueException.
    default long
    Returns a long if this JsonValue is an instance of JsonNumber that can be converted exactly; otherwise, throws a JsonValueException.
    default Map<String, JsonValue>
    Returns an unmodifiable map of String to JsonValue if this JsonValue is an instance of JsonObject; otherwise, throws a JsonValueException.
    default JsonValue
    get(int index)
    Returns the JsonValue associated with the given index if this JsonValue is an instance of JsonArray; otherwise, throws a JsonValueException.
    default JsonValue
    get(String name)
    Returns the JsonValue associated with the given member name if this JsonValue is an instance of JsonObject; otherwise, throws a JsonValueException.
    tryGet(String name)
    Returns an Optional containing the value of a given member of this JsonObject, or an empty Optional if the member is absent; throws JsonValueException if this JsonValue is not a JsonObject.
    Returns an Optional containing this JsonValue if it is not an instance of JsonNull, otherwise an empty Optional.
  • Method Details

    • of

      static JsonString of(String src)
      Returns the JsonString created from the given String. Unlike Json.parse(String), src is not expected to be surrounded by quotation marks and the special characters do not need to be escaped. As a result, src is equal to JsonString.of(src).asString().
      Parameters:
      src - the given source String. Non-null.
      Returns:
      the JsonString created from the given String
      Throws:
      NullPointerException - if src is null
    • toString

      String toString()
      Returns the JSON string represented by this JsonString. If this JsonString was created by parsing a JSON text, it preserves the original text representation of the corresponding JSON string. Otherwise, the returned JSON string is the source String passed to the factory method of(String) surrounded by double quotes with special characters properly escaped.
      Specified by:
      toString in interface JsonValue
      Overrides:
      toString in class Object
      Returns:
      the JSON string represented by this JsonString
      See Also:
    • asString

      String asString()
      Returns the String value represented by this JsonValue if it is an instance of JsonString; otherwise, throws a JsonValueException. If this JsonString was created by parsing a JSON text, any escaped characters in the original JSON text are converted to their unescaped form.
      Specified by:
      asString in interface JsonValue
      Returns:
      the String value represented by this JsonValue if it is an instance of JsonString; otherwise, throws a JsonValueException
      See Also: