JSON processing library for Selenium WebDriver providing serialization and deserialization capabilities
—
High-level JSON serialization and deserialization operations using the main Json class. This provides the simplest interface for converting between Java objects and JSON strings or readers, with built-in type coercion and configurable processing options.
The main entry point for JSON processing operations, providing both serialization and deserialization methods with sensible defaults.
/**
* The Json class is the entrypoint for the JSON processing features of the Selenium API.
* Supports built-in JSON processing for standard types and facilities for custom data types.
*/
public class Json {
// Serialization methods
/**
* Serialize the specified object to JSON string representation.
* Uses the default maximum depth limit.
*
* @param toConvert the object to be serialized
* @return JSON string representing the specified object
*/
public String toJson(Object toConvert);
/**
* Serialize the specified object to JSON string representation.
*
* @param toConvert the object to be serialized
* @param maxDepth maximum depth of nested object traversal
* @return JSON string representing the specified object
* @throws JsonException if an I/O exception is encountered
*/
public String toJson(Object toConvert, int maxDepth);
// Deserialization methods with String input
/**
* Deserialize the specified JSON string into an object of the specified type.
* Uses the BY_NAME strategy to assign values to properties.
*
* @param source serialized source as JSON string
* @param typeOfT data type for deserialization (class or TypeToken)
* @return object of the specified type deserialized from source
* @param <T> result type (as specified by typeOfT)
* @throws JsonException if an I/O exception is encountered
*/
public <T> T toType(String source, Type typeOfT);
/**
* Deserialize the specified JSON string into an object of the specified type.
*
* @param source serialized source as JSON string
* @param typeOfT data type for deserialization (class or TypeToken)
* @param setter strategy used to assign values during deserialization
* @return object of the specified type deserialized from source
* @param <T> result type (as specified by typeOfT)
* @throws JsonException if an I/O exception is encountered
*/
public <T> T toType(String source, Type typeOfT, PropertySetting setter);
// Deserialization methods with Reader input
/**
* Deserialize the JSON string supplied by the specified Reader into an object of the specified type.
* Uses the BY_NAME strategy to assign values to properties.
*
* @param source Reader that supplies a serialized JSON string
* @param typeOfT data type for deserialization (class or TypeToken)
* @return object of the specified type deserialized from source
* @param <T> result type (as specified by typeOfT)
* @throws JsonException if an I/O exception is encountered
*/
public <T> T toType(Reader source, Type typeOfT);
/**
* Deserialize the JSON string supplied by the specified Reader into an object of the specified type.
*
* @param source Reader that supplies a serialized JSON string
* @param typeOfT data type for deserialization (class or TypeToken)
* @param setter strategy used to assign values during deserialization
* @return object of the specified type deserialized from source
* @param <T> result type (as specified by typeOfT)
* @throws JsonException if an I/O exception is encountered
*/
public <T> T toType(Reader source, Type typeOfT, PropertySetting setter);
// Factory methods for streaming processors
/**
* Create a new JsonInput object to traverse the JSON string supplied by the specified Reader.
* Uses the BY_NAME strategy to assign values to properties in deserialized objects.
*
* @param from Reader that supplies a serialized JSON string
* @return JsonInput object to traverse the JSON string supplied by from
* @throws UncheckedIOException if an I/O exception occurs
*/
public JsonInput newInput(Reader from) throws UncheckedIOException;
/**
* Create a new JsonOutput object to produce a serialized JSON string in the specified Appendable.
*
* @param to Appendable that consumes a serialized JSON string
* @return JsonOutput object to produce a JSON string in to
* @throws UncheckedIOException if an I/O exception occurs
*/
public JsonOutput newOutput(Appendable to) throws UncheckedIOException;
}Pre-defined constants for common use cases and content types.
/**
* The value of Content-Type headers for HTTP requests and responses with JSON entities
*/
public static final String JSON_UTF_8 = "application/json; charset=utf-8";
/**
* Specifier for List<Map<String, Object>> input/output type
*/
public static final Type LIST_OF_MAPS_TYPE = new TypeToken<List<Map<String, Object>>>() {}.getType();
/**
* Specifier for Map<String, Object> input/output type
*/
public static final Type MAP_TYPE = new TypeToken<Map<String, Object>>() {}.getType();
/**
* Specifier for Object input/output type
*/
public static final Type OBJECT_TYPE = new TypeToken<Object>() {}.getType();import org.openqa.selenium.json.Json;
Json json = new Json();
// Serialize simple objects
String result = json.toJson("Hello World");
// Result: "\"Hello World\""
String result = json.toJson(42);
// Result: "42"
String result = json.toJson(Arrays.asList(1, 2, 3));
// Result: "[1,2,3]"
// Serialize with depth limit
String result = json.toJson(complexNestedObject, 5);import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.TypeToken;
Json json = new Json();
// Deserialize to simple types
String text = json.toType("\"Hello World\"", String.class);
Integer number = json.toType("42", Integer.class);
// Deserialize to generic collections using TypeToken
List<Integer> numbers = json.toType(
"[1,2,3]",
new TypeToken<List<Integer>>() {}.getType()
);
// Deserialize to Map using predefined type constant
Map<String, Object> data = json.toType(
"{\"name\":\"John\",\"age\":30}",
Json.MAP_TYPE
);// Object with fromJson static method
public class Person {
private String name;
private int age;
public static Person fromJson(Map<String, Object> data) {
Person person = new Person();
person.name = (String) data.get("name");
person.age = ((Number) data.get("age")).intValue();
return person;
}
public Map<String, Object> toJson() {
Map<String, Object> result = new HashMap<>();
result.put("name", name);
result.put("age", age);
return result;
}
}
// Usage
Json json = new Json();
Person person = new Person("John", 30);
String jsonString = json.toJson(person);
Person restored = json.toType(jsonString, Person.class);// JavaBean-compliant class
public class Product {
private String name;
private double price;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
}
// Usage
Json json = new Json();
Product product = new Product();
product.setName("Laptop");
product.setPrice(999.99);
String jsonString = json.toJson(product);
Product restored = json.toType(jsonString, Product.class);
// Use BY_FIELD strategy to set fields directly
Product restored = json.toType(jsonString, Product.class, PropertySetting.BY_FIELD);import java.io.StringReader;
import java.io.FileReader;
Json json = new Json();
// From StringReader
try (StringReader reader = new StringReader("{\"name\":\"John\"}")) {
Person person = json.toType(reader, Person.class);
}
// From FileReader
try (FileReader reader = new FileReader("data.json")) {
List<Person> people = json.toType(
reader,
new TypeToken<List<Person>>() {}.getType()
);
}Install with Tessl CLI
npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-json