Interface JsonString
- All Superinterfaces:
JsonValue
The interface that represents JSON string.
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.
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();
- Since:
- 28
- External Specifications
-
Method Summary
Modifier and TypeMethodDescriptionasString()Returns theStringvalue represented by thisJsonValueif it is an instance ofJsonString; otherwise, throws aJsonValueException.static JsonStringReturns theJsonStringcreated from the givenString.toString()Returns the JSON string represented by thisJsonString.Methods declared in interface JsonValue
asBoolean, asDouble, asInt, asList, asLong, asMap, get, get, tryGet, tryValueModifier and TypeMethodDescriptiondefault booleanReturns thebooleanvalue represented by thisJsonValueif it is an instance ofJsonBoolean; otherwise, throws aJsonValueException.default doubleasDouble()Returns adoubleif thisJsonValueis an instance ofJsonNumberthat can be converted, as if byDouble.parseDouble, to a finitedoublevalue; otherwise, throws aJsonValueException.default intasInt()Returns anintif thisJsonValueis an instance ofJsonNumberthat can be converted exactly; otherwise, throws aJsonValueException.asList()Returns an unmodifiable list of theJsonValues if thisJsonValueis an instance ofJsonArray; otherwise, throws aJsonValueException.default longasLong()Returns alongif thisJsonValueis an instance ofJsonNumberthat can be converted exactly; otherwise, throws aJsonValueException.asMap()Returns an unmodifiable map ofStringtoJsonValueif thisJsonValueis an instance ofJsonObject; otherwise, throws aJsonValueException.default JsonValueget(int index) Returns theJsonValueassociated with the given index if thisJsonValueis an instance ofJsonArray; otherwise, throws aJsonValueException.default JsonValueReturns theJsonValueassociated with the given member name if thisJsonValueis an instance ofJsonObject; otherwise, throws aJsonValueException.Returns anOptionalcontaining the value of a given member of thisJsonObject, or an emptyOptionalif the member is absent; throwsJsonValueExceptionif thisJsonValueis not aJsonObject.tryValue()Returns anOptionalcontaining thisJsonValueif it is not an instance ofJsonNull, otherwise an emptyOptional.
-
Method Details
-
of
Returns theJsonStringcreated from the givenString. UnlikeJson.parse(String),srcis not expected to be surrounded by quotation marks and the special characters do not need to be escaped. As a result,srcis equal toJsonString.of(src).asString().- Parameters:
src- the given sourceString. Non-null.- Returns:
- the
JsonStringcreated from the givenString - Throws:
NullPointerException- ifsrcisnull
-
toString
String toString()Returns the JSON string represented by thisJsonString. If thisJsonStringwas created by parsing a JSON text, it preserves the original text representation of the corresponding JSON string. Otherwise, the returned JSON string is the sourceStringpassed to the factory methodof(String)surrounded by double quotes with special characters properly escaped. -
asString
String asString()Returns theStringvalue represented by thisJsonValueif it is an instance ofJsonString; otherwise, throws aJsonValueException. If thisJsonStringwas created by parsing a JSON text, any escaped characters in the original JSON text are converted to their unescaped form.- Specified by:
asStringin interfaceJsonValue- Returns:
- the
Stringvalue represented by thisJsonValueif it is an instance ofJsonString; otherwise, throws aJsonValueException - See Also:
-