Package org.openqa.selenium.json
Class Json
java.lang.Object
org.openqa.selenium.json.Json
The Json class is the entrypoint for the JSON processing features of the Selenium API.
These features include:
- Built-in JSON deserialization to primitives and collections from the standard types shown below.
- Facilities to deserialize JSON to custom data types:
- Classes that declare a
fromJson(T)static method, where T is any of the standard types shown below. - Classes that declare a
fromJson(JsonInput)static method.
NOTE: Objects deserialized via afromJsonstatic method can be immutable. - Classes that declare setter methods adhering to the JavaBean
specification.
NOTE: DeserializedJavaBeanobjects are mutable, which may be undesirable.
- Classes that declare a
- Built-in JSON serialization from primitives and collections from the standard types shown below.
- Facilities to serialize custom data types to JSON:
- Classes that declare a
toJson()method returning a primitive or collection from the standard types shown below. - Classes that declare getter methods adhering to the
JavaBeanspecification.
- Classes that declare a
- Numeric Types:
Byte,Double,Float,Integer,Long,Short - Collection Types:
List,Set - Standard Java Types:
Map,Boolean,String,Enum,URI,URL,UUID,Instant,Object
toJson() method for the Json API to use
for serialization. This is especially beneficial for objects that contain transient properties
that should be omitted from the JSON output.
You can deserialize objects for which no explicit handling has been defined. Note that the
data type of the result will be Map<String,?>, which means that you'll need to perform
type checking and casting every time you extract an entry value from the result. For this reason,
it's best to declare a type-specific fromJson() method in every type you need to
deserialize.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe value ofContent-Typeheaders for HTTP requests and responses with JSON entitiesstatic final TypeSpecifier forList<Map<String, Object>input/output typestatic final TypeSpecifier forMap<String, Object>input/output typestatic final TypeSpecifier forObjectinput/output type -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCreate a newJsonInputobject to traverse the JSON string supplied the specifiedReader.
NOTE: TheJsonInputobject returned by this method uses theBY_NAMEstrategy to assign values to properties objects it deserializes.newOutput(Appendable to) Create a newJsonOutputobject to produce a serialized JSON string in the specifiedAppendable.Serialize the specified object to JSON string representation.
NOTE: This method limits traversal of nested objects to the defaultmaximum depth.Serialize the specified object to JSON string representation.<T> TDeserialize the JSON string supplied by the specifiedReaderinto an object of the specified type.
NOTE: This method uses theBY_NAMEstrategy to assign values to properties in the deserialized object.<T> TtoType(Reader source, Type typeOfT, PropertySetting setter) Deserialize the JSON string supplied by the specifiedReaderinto an object of the specified type.<T> TDeserialize the specified JSON string into an object of the specified type.
NOTE: This method uses theBY_NAMEstrategy to assign values to properties in the deserialized object.<T> TtoType(String source, Type typeOfT, PropertySetting setter) Deserialize the specified JSON string into an object of the specified type.
-
Field Details
-
JSON_UTF_8
The value ofContent-Typeheaders for HTTP requests and responses with JSON entities- See Also:
-
LIST_OF_MAPS_TYPE
Specifier forList<Map<String, Object>input/output type -
MAP_TYPE
Specifier forMap<String, Object>input/output type -
OBJECT_TYPE
Specifier forObjectinput/output type
-
-
Constructor Details
-
Json
public Json()
-
-
Method Details
-
toJson
Serialize the specified object to JSON string representation.
NOTE: This method limits traversal of nested objects to the defaultmaximum depth.- Parameters:
toConvert- the object to be serialized- Returns:
- JSON string representing the specified object
-
toJson
Serialize the specified object to JSON string representation.- Parameters:
toConvert- the object to be serializedmaxDepth- maximum depth of nested object traversal- Returns:
- JSON string representing the specified object
- Throws:
JsonException- if an I/O exception is encountered
-
toType
Deserialize the specified JSON string into an object of the specified type.
NOTE: This method uses theBY_NAMEstrategy to assign values to properties in the deserialized object.- Type Parameters:
T- result type (as specified by [typeOfT])- Parameters:
source- serialized source as JSON stringtypeOfT- data type for deserialization (class orTypeToken)- Returns:
- object of the specified type deserialized from [source]
- Throws:
JsonException- if an I/O exception is encountered
-
toType
Deserialize the specified JSON string into an object of the specified type.- Type Parameters:
T- result type (as specified by [typeOfT])- Parameters:
source- serialized source as JSON stringtypeOfT- data type for deserialization (class orTypeToken)setter- strategy used to assign values during deserialization- Returns:
- object of the specified type deserialized from [source]
- Throws:
JsonException- if an I/O exception is encountered
-
toType
Deserialize the JSON string supplied by the specifiedReaderinto an object of the specified type.
NOTE: This method uses theBY_NAMEstrategy to assign values to properties in the deserialized object.- Type Parameters:
T- result type (as specified by [typeOfT])- Parameters:
source-Readerthat supplies a serialized JSON stringtypeOfT- data type for deserialization (class orTypeToken)- Returns:
- object of the specified type deserialized from [source]
- Throws:
JsonException- if an I/O exception is encountered
-
toType
Deserialize the JSON string supplied by the specifiedReaderinto an object of the specified type.- Type Parameters:
T- result type (as specified by [typeOfT])- Parameters:
source-Readerthat supplies a serialized JSON stringtypeOfT- data type for deserialization (class orTypeToken)setter- strategy used to assign values during deserialization- Returns:
- object of the specified type deserialized from [source]
- Throws:
JsonException- if an I/O exception is encountered
-
newInput
Create a newJsonInputobject to traverse the JSON string supplied the specifiedReader.
NOTE: TheJsonInputobject returned by this method uses theBY_NAMEstrategy to assign values to properties objects it deserializes.- Parameters:
from-Readerthat supplies a serialized JSON string- Returns:
JsonInputobject to traverse the JSON string supplied by [from]- Throws:
UncheckedIOException- if an I/O exception occurs
-
newOutput
Create a newJsonOutputobject to produce a serialized JSON string in the specifiedAppendable.- Parameters:
to-Appendablethat consumes a serialized JSON string- Returns:
JsonOutputobject to product a JSON string in [to]- Throws:
UncheckedIOException- if an I/O exception occurs
-