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 afromJson
static method can be immutable. - Classes that declare setter methods adhering to the JavaBean
specification.
NOTE: DeserializedJavaBean
objects 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
JavaBean
specification.
- 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:
-
JsonTypeCoercer
JsonInput
JsonOutput
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
The value ofContent-Type
headers for HTTP requests and responses with JSON entitiesstatic final Type
Specifier forList<Map<String, Object>
input/output typestatic final Type
Specifier forMap<String, Object>
input/output typestatic final Type
Specifier forObject
input/output type -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCreate a newJsonInput
object to traverse the JSON string supplied the specifiedReader
.
NOTE: TheJsonInput
object returned by this method uses theBY_NAME
strategy to assign values to properties objects it deserializes.newOutput
(Appendable to) Create a newJsonOutput
object 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> T
Deserialize the JSON string supplied by the specifiedReader
into an object of the specified type.
NOTE: This method uses theBY_NAME
strategy to assign values to properties in the deserialized object.<T> T
toType
(Reader source, Type typeOfT, PropertySetting setter) Deserialize the JSON string supplied by the specifiedReader
into an object of the specified type.<T> T
Deserialize the specified JSON string into an object of the specified type.
NOTE: This method uses theBY_NAME
strategy to assign values to properties in the deserialized object.<T> T
toType
(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-Type
headers 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 forObject
input/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_NAME
strategy 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 specifiedReader
into an object of the specified type.
NOTE: This method uses theBY_NAME
strategy to assign values to properties in the deserialized object.- Type Parameters:
T
- result type (as specified by [typeOfT])- Parameters:
source
-Reader
that 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 specifiedReader
into an object of the specified type.- Type Parameters:
T
- result type (as specified by [typeOfT])- Parameters:
source
-Reader
that 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 newJsonInput
object to traverse the JSON string supplied the specifiedReader
.
NOTE: TheJsonInput
object returned by this method uses theBY_NAME
strategy to assign values to properties objects it deserializes.- Parameters:
from
-Reader
that supplies a serialized JSON string- Returns:
JsonInput
object to traverse the JSON string supplied by [from]- Throws:
UncheckedIOException
- if an I/O exception occurs
-
newOutput
Create a newJsonOutput
object to produce a serialized JSON string in the specifiedAppendable
.- Parameters:
to
-Appendable
that consumes a serialized JSON string- Returns:
JsonOutput
object to product a JSON string in [to]- Throws:
UncheckedIOException
- if an I/O exception occurs
-