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: <u
| 96 | * @see JsonOutput |
| 97 | */ |
| 98 | public class Json { |
| 99 | /** |
| 100 | * The value of {@code Content-Type} headers for HTTP requests and responses with JSON entities |
| 101 | */ |
| 102 | public static final String JSON_UTF_8 = "application/json; charset=utf-8"; |
| 103 | |
| 104 | /** Specifier for {@code List<Map<String, Object>} input/output type */ |
| 105 | public static final Type LIST_OF_MAPS_TYPE = |
| 106 | new TypeToken<List<Map<String, Object>>>() {}.getType(); |
| 107 | |
| 108 | /** Specifier for {@code Map<String, Object>} input/output type */ |
| 109 | public static final Type MAP_TYPE = new TypeToken<Map<String, Object>>() {}.getType(); |
| 110 | |
| 111 | /** Specifier for {@code Object} input/output type */ |
| 112 | public static final Type OBJECT_TYPE = new TypeToken<Object>() {}.getType(); |
| 113 | |
| 114 | /** |
| 115 | * The default coercer is stateless apart from its cache of resolved coercion functions, so a |
| 116 | * single shared instance lets that cache accumulate across the many short-lived {@code Json} |
| 117 | * instances created throughout the codebase. |
| 118 | */ |
| 119 | private static final JsonTypeCoercer DEFAULT_COERCER = JsonTypeCoercer.createShared(); |
| 120 | |
| 121 | /** Strings longer than this are parsed through a {@link StringReader} instead of copied. */ |
| 122 | private static final int MAX_DIRECT_PARSE_LENGTH = 1024 * 1024; |
| 123 | |
| 124 | private final JsonTypeCoercer fromJson; |
| 125 | |
| 126 | public Json() { |
| 127 | this.fromJson = DEFAULT_COERCER; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @param extraCoercers Additional {@link TypeCoercer}s, tried in the given order before the |
| 132 | * built-in ones — lets a caller customize how a particular type is converted to or from JSON |
| 133 | * (a stricter check, a different wire representation, support for a type the built-in |
| 134 | * coercers don't handle, etc.) for just this {@code Json} instance. |
| 135 | */ |
| 136 | public Json(Iterable<TypeCoercer<?>> extraCoercers) { |
| 137 | this.fromJson = new JsonTypeCoercer(DEFAULT_COERCER, extraCoercers); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Serialize the specified object to JSON string representation.<br> |
| 142 | * <b>NOTE</b>: This method limits traversal of nested objects to the default {@link |
| 143 | * JsonOutput#MAX_DEPTH maximum depth}. |
| 144 | * |
| 145 | * @param toConvert the object to be serialized |
| 146 | * @return JSON string representing the specified object |
| 147 | */ |
| 148 | public String toJson(@Nullable Object toConvert) { |
| 149 | return toJson(toConvert, JsonOutput.MAX_DEPTH); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Serialize the specified object to JSON string representation. |
| 154 | * |
| 155 | * @param toConvert the object to be serialized |
nothing calls this directly
no test coverage detected