Overview
The ConverterFunctions class provides static utility methods for creating JSON-to-object mapper functions used by CDP Command and Event instances to extract values from DevTools Protocol responses.
Description
ConverterFunctions is a utility class containing factory methods that produce Function<JsonInput, X> instances. These mappers are designed to parse CDP JSON response objects, extracting a specific named key and converting it to a desired Java type. The class provides three factory methods:
map(String keyName, Type typeOfX) -- extracts a key from a JSON object and deserializes it using a java.lang.reflect.Type.
map(String keyName, Function<JsonInput, X> read) -- extracts a key from a JSON object and applies a custom reader function.
empty() -- handles CDP responses that return an empty JSON object ({}), returning null.
Both map overloads iterate through all keys of the JSON object, matching on the specified key name and skipping all others.
Usage
Use ConverterFunctions when building custom CDP commands or events that need to extract specific values from JSON response objects. These utilities are used extensively by the auto-generated CDP domain classes to define response mappers for commands and events.
Code Reference
Source Location
Signature
public class ConverterFunctions {
public static <X> Function<JsonInput, @Nullable X> map(String keyName, Type typeOfX)
public static <X> Function<JsonInput, @Nullable X> map(String keyName, Function<JsonInput, @Nullable X> read)
public static Function<JsonInput, @Nullable Void> empty()
}
Import
import org.openqa.selenium.devtools.ConverterFunctions;
I/O Contract
Method: map(String keyName, Type typeOfX)
| Parameter |
Type |
Required |
Description
|
keyName |
String |
Yes |
The JSON object key to extract from the response
|
typeOfX |
java.lang.reflect.Type |
Yes |
The Java type to deserialize the key's value into
|
| Returns |
Description
|
Function<JsonInput, @Nullable X> |
A mapper function that extracts and deserializes the named key from a JSON object; returns null if the key is not found
|
Method: map(String keyName, Function read)
| Parameter |
Type |
Required |
Description
|
keyName |
String |
Yes |
The JSON object key to extract from the response
|
read |
Function<JsonInput, @Nullable X> |
Yes |
A custom function to read and convert the JSON value
|
| Returns |
Description
|
Function<JsonInput, @Nullable X> |
A mapper function that finds the named key and applies the custom reader; returns null if the key is not found
|
Method: empty()
| Returns |
Description
|
Function<JsonInput, @Nullable Void> |
A mapper function that consumes an empty JSON object and returns null
|
Usage Examples
// Extract a String value from a CDP response by key name
Function<JsonInput, String> sessionIdMapper =
ConverterFunctions.map("sessionId", String.class);
// Use the mapper in a Command
Command<String> attachCommand = new Command<>(
"Target.attachToTarget",
Map.of("targetId", targetId, "flatten", true),
ConverterFunctions.map("sessionId", String.class)
);
// Use with a custom reader function for complex types
Function<JsonInput, MyType> customMapper =
ConverterFunctions.map("result", input -> {
// Custom deserialization logic
return new MyType(input.read(String.class));
});
// Use empty() for commands that return no meaningful data
Command<Void> enableCommand = new Command<>(
"Network.enable",
Map.of(),
ConverterFunctions.empty()
);
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.