Class Json

java.lang.Object
org.openqa.selenium.json.Json

public class Json extends Object
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 a fromJson static method can be immutable.
    • Classes that declare setter methods adhering to the JavaBean specification.
      NOTE: Deserialized JavaBean objects are mutable, which may be undesirable.
  • 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.
The standard types supported by built-in processing: You can serialize objects for which no explicit coercer has been specified, and the Json API will use a generic process to provide best-effort JSON output. For the most predictable results, though, it's best to provide a 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

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The value of Content-Type headers for HTTP requests and responses with JSON entities
    static final Type
    Specifier for List<Map<String, Object> input/output type
    static final Type
    Specifier for Map<String, Object> input/output type
    static final Type
    Specifier for Object input/output type
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Create a new JsonInput object to traverse the JSON string supplied the specified Reader.
    NOTE: The JsonInput object returned by this method uses the BY_NAME strategy to assign values to properties objects it deserializes.
    Create a new JsonOutput object to produce a serialized JSON string in the specified Appendable.
    toJson(Object toConvert)
    Serialize the specified object to JSON string representation.
    NOTE: This method limits traversal of nested objects to the default maximum depth.
    toJson(Object toConvert, int maxDepth)
    Serialize the specified object to JSON string representation.
    <T> T
    toType(Reader source, Type typeOfT)
    Deserialize the JSON string supplied by the specified Reader into an object of the specified type.
    NOTE: This method uses the BY_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 specified Reader into an object of the specified type.
    <T> T
    toType(String source, Type typeOfT)
    Deserialize the specified JSON string into an object of the specified type.
    NOTE: This method uses the BY_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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • JSON_UTF_8

      public static final String JSON_UTF_8
      The value of Content-Type headers for HTTP requests and responses with JSON entities
      See Also:
    • LIST_OF_MAPS_TYPE

      public static final Type LIST_OF_MAPS_TYPE
      Specifier for List<Map<String, Object> input/output type
    • MAP_TYPE

      public static final Type MAP_TYPE
      Specifier for Map<String, Object> input/output type
    • OBJECT_TYPE

      public static final Type OBJECT_TYPE
      Specifier for Object input/output type
  • Constructor Details

    • Json

      public Json()
  • Method Details

    • toJson

      public String toJson(Object toConvert)
      Serialize the specified object to JSON string representation.
      NOTE: This method limits traversal of nested objects to the default maximum depth.
      Parameters:
      toConvert - the object to be serialized
      Returns:
      JSON string representing the specified object
    • toJson

      public String toJson(Object toConvert, int maxDepth)
      Serialize the specified object to JSON string representation.
      Parameters:
      toConvert - the object to be serialized
      maxDepth - maximum depth of nested object traversal
      Returns:
      JSON string representing the specified object
      Throws:
      JsonException - if an I/O exception is encountered
    • toType

      public <T> T toType(String source, Type typeOfT)
      Deserialize the specified JSON string into an object of the specified type.
      NOTE: This method uses the BY_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 string
      typeOfT - data type for deserialization (class or TypeToken)
      Returns:
      object of the specified type deserialized from [source]
      Throws:
      JsonException - if an I/O exception is encountered
    • toType

      public <T> T toType(String source, Type typeOfT, PropertySetting setter)
      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 string
      typeOfT - data type for deserialization (class or TypeToken)
      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

      public <T> T toType(Reader source, Type typeOfT)
      Deserialize the JSON string supplied by the specified Reader into an object of the specified type.
      NOTE: This method uses the BY_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 string
      typeOfT - data type for deserialization (class or TypeToken)
      Returns:
      object of the specified type deserialized from [source]
      Throws:
      JsonException - if an I/O exception is encountered
    • toType

      public <T> T toType(Reader source, Type typeOfT, PropertySetting setter)
      Deserialize the JSON string supplied by the specified Reader into an object of the specified type.
      Type Parameters:
      T - result type (as specified by [typeOfT])
      Parameters:
      source - Reader that supplies a serialized JSON string
      typeOfT - data type for deserialization (class or TypeToken)
      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

      public JsonInput newInput(Reader from) throws UncheckedIOException
      Create a new JsonInput object to traverse the JSON string supplied the specified Reader.
      NOTE: The JsonInput object returned by this method uses the BY_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

      public JsonOutput newOutput(Appendable to) throws UncheckedIOException
      Create a new JsonOutput object to produce a serialized JSON string in the specified Appendable.
      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