30
30
/**
31
31
* The <b>Json</b> class is the entrypoint for the JSON processing features of the Selenium API.
32
32
* These features include:
33
+ *
33
34
* <ul>
34
- * <li>Built-in JSON deserialization to primitives and collections from the standard types shown below.</li>
35
+ * <li>Built-in JSON deserialization to primitives and collections from the standard types shown
36
+ * below.
35
37
* <li>Facilities to deserialize JSON to custom data types:
36
- * <ul>
37
- * <li>Classes that declare a {@code fromJson(T)} static method, where <b>T</b> is any of the standard
38
- * types shown below.</li>
39
- * <li>Classes that declare a {@code fromJson(JsonInput)} static method.<br>
40
- * <b>NOTE</b>: Objects deserialized via a {@code fromJson} static method can be immutable.</li>
41
- * <li>Classes that declare setter methods adhering to the
42
- * <a href="https://docs.oracle.com/javase/tutorial/javabeans/writing/index.html">JavaBean</a>
43
- * specification.<br>
44
- * <b>NOTE</b>: Deserialized {@code JavaBean} objects are mutable, which may be undesirable.</li>
45
- * </ul>
46
- * </li>
47
- * <li>Built-in JSON serialization from primitives and collections from the standard types shown below.</li>
38
+ * <ul>
39
+ * <li>Classes that declare a {@code fromJson(T)} static method, where <b>T</b> is any of
40
+ * the standard types shown below.
41
+ * <li>Classes that declare a {@code fromJson(JsonInput)} static method.<br>
42
+ * <b>NOTE</b>: Objects deserialized via a {@code fromJson} static method can be
43
+ * immutable.
44
+ * <li>Classes that declare setter methods adhering to the <a
45
+ * href="https://docs.oracle.com/javase/tutorial/javabeans/writing/index.html">JavaBean</a>
46
+ * specification.<br>
47
+ * <b>NOTE</b>: Deserialized {@code JavaBean} objects are mutable, which may be
48
+ * undesirable.
49
+ * </ul>
50
+ * <li>Built-in JSON serialization from primitives and collections from the standard types shown
51
+ * below.
48
52
* <li>Facilities to serialize custom data types to JSON:
49
- * <ul>
50
- * <li>Classes that declare a {@code toJson()} method returning a primitive or collection from
51
- * the standard types shown below.</li>
52
- * <li>Classes that declare getter methods adhering to the {@code JavaBean} specification.</li>
53
- * </ul>
54
- * </li>
53
+ * <ul>
54
+ * <li>Classes that declare a {@code toJson()} method returning a primitive or collection
55
+ * from the standard types shown below.
56
+ * <li>Classes that declare getter methods adhering to the {@code JavaBean} specification.
57
+ * </ul>
55
58
* </ul>
56
59
*
57
60
* The standard types supported by built-in processing:
61
+ *
58
62
* <ul>
59
63
* <li><b>Numeric Types</b>:<br>
60
- * {@link java.lang.Byte Byte}, {@link java.lang.Double Double}, {@link java.lang.Float Float},
61
- * {@link java.lang.Integer Integer}, {@link java.lang.Long Long}, {@link java.lang.Short Short}
62
- * </li>
64
+ * {@link java.lang.Byte Byte}, {@link java.lang.Double Double}, {@link java.lang.Float
65
+ * Float}, {@link java.lang.Integer Integer}, {@link java.lang.Long Long}, {@link
66
+ * java.lang.Short Short}
63
67
* <li><b>Collection Types</b>:<br>
64
68
* {@link java.util.List List}, {@link java.util.Set Set}
65
- * </li>
66
69
* <li><b>Standard Java Types</b>:<br>
67
- * {@link java.util.Map Map}, {@link java.lang.Boolean Boolean}, {@link java.lang.String String},
68
- * {@link java.lang.Enum Enum}, {@link java.net.URI URI}, {@link java.net.URL URL},
69
- * {@link java.util.UUID UUID}, {@link java.time.Instant Instant}, {@link java.lang.Object Object}
70
- * </li>
70
+ * {@link java.util.Map Map}, {@link java.lang.Boolean Boolean}, {@link java.lang.String
71
+ * String}, {@link java.lang.Enum Enum}, {@link java.net.URI URI}, {@link java.net.URL URL},
72
+ * {@link java.util.UUID UUID}, {@link java.time.Instant Instant}, {@link java.lang.Object
73
+ * Object}
71
74
* </ul>
72
75
*
73
- * You can serialize objects for which no explicit coercer has been specified, and the <b>Json</b> API will use a
74
- * generic process to provide best-effort JSON output. For the most predictable results, though, it's best to
75
- * provide a {@code toJson()} method for the <b>Json</b> API to use for serialization. This is especially beneficial
76
- * for objects that contain transient properties that should be omitted from the JSON output.
77
- * <p>
78
- * You can deserialize objects for which no explicit handling has been defined. Note that the data type of the
79
- * result will be {@code Map<String,?>}, which means that you'll need to perform type checking and casting every
80
- * time you extract an entry value from the result. For this reason, it's best to declare a type-specific
81
- * {@code fromJson()} method in every type you need to deserialize.
76
+ * You can serialize objects for which no explicit coercer has been specified, and the <b>Json</b>
77
+ * API will use a generic process to provide best-effort JSON output. For the most predictable
78
+ * results, though, it's best to provide a {@code toJson()} method for the <b>Json</b> API to use
79
+ * for serialization. This is especially beneficial for objects that contain transient properties
80
+ * that should be omitted from the JSON output.
81
+ *
82
+ * <p>You can deserialize objects for which no explicit handling has been defined. Note that the
83
+ * data type of the result will be {@code Map<String,?>}, which means that you'll need to perform
84
+ * type checking and casting every time you extract an entry value from the result. For this reason,
85
+ * it's best to declare a type-specific {@code fromJson()} method in every type you need to
86
+ * deserialize.
82
87
*
83
88
* @see JsonTypeCoercer
84
89
* @see JsonInput
85
90
* @see JsonOutput
86
91
*/
87
92
public class Json {
88
- /** The value of {@code Content-Type} headers for HTTP requests and
89
- * responses with JSON entities */
93
+ /**
94
+ * The value of {@code Content-Type} headers for HTTP requests and responses with JSON entities
95
+ */
90
96
public static final String JSON_UTF_8 = "application/json; charset=utf-8" ;
91
97
92
98
/** Specifier for {@code List<Map<String, Object>} input/output type */
93
99
public static final Type LIST_OF_MAPS_TYPE =
94
- new TypeToken <List <Map <String , Object >>>() {}.getType ();
100
+ new TypeToken <List <Map <String , Object >>>() {}.getType ();
101
+
95
102
/** Specifier for {@code Map<String, Object>} input/output type */
96
103
public static final Type MAP_TYPE = new TypeToken <Map <String , Object >>() {}.getType ();
104
+
97
105
/** Specifier for {@code Object} input/output type */
98
106
public static final Type OBJECT_TYPE = new TypeToken <Object >() {}.getType ();
99
107
100
108
private final JsonTypeCoercer fromJson = new JsonTypeCoercer ();
101
109
102
110
/**
103
111
* Serialize the specified object to JSON string representation.<br>
104
- * <b>NOTE</b>: This method limits traversal of nested objects to the default
105
- * {@link JsonOutput#MAX_DEPTH maximum depth}.
112
+ * <b>NOTE</b>: This method limits traversal of nested objects to the default {@link
113
+ * JsonOutput#MAX_DEPTH maximum depth}.
106
114
*
107
115
* @param toConvert the object to be serialized
108
116
* @return JSON string representing the specified object
@@ -121,7 +129,7 @@ public String toJson(Object toConvert) {
121
129
*/
122
130
public String toJson (Object toConvert , int maxDepth ) {
123
131
try (Writer writer = new StringWriter ();
124
- JsonOutput jsonOutput = newOutput (writer )) {
132
+ JsonOutput jsonOutput = newOutput (writer )) {
125
133
jsonOutput .write (toConvert , maxDepth );
126
134
return writer .toString ();
127
135
} catch (IOException e ) {
@@ -131,8 +139,8 @@ public String toJson(Object toConvert, int maxDepth) {
131
139
132
140
/**
133
141
* Deserialize the specified JSON string into an object of the specified type.<br>
134
- * <b>NOTE</b>: This method uses the {@link PropertySetting#BY_NAME BY_NAME} strategy to assign values to properties
135
- * in the deserialized object.
142
+ * <b>NOTE</b>: This method uses the {@link PropertySetting#BY_NAME BY_NAME} strategy to assign
143
+ * values to properties in the deserialized object.
136
144
*
137
145
* @param source serialized source as JSON string
138
146
* @param typeOfT data type for deserialization (class or {@link TypeToken})
@@ -163,9 +171,10 @@ public <T> T toType(String source, Type typeOfT, PropertySetting setter) {
163
171
}
164
172
165
173
/**
166
- * Deserialize the JSON string supplied by the specified {@code Reader} into an object of the specified type.<br>
167
- * <b>NOTE</b>: This method uses the {@link PropertySetting#BY_NAME BY_NAME} strategy to assign values to properties
168
- * in the deserialized object.
174
+ * Deserialize the JSON string supplied by the specified {@code Reader} into an object of the
175
+ * specified type.<br>
176
+ * <b>NOTE</b>: This method uses the {@link PropertySetting#BY_NAME BY_NAME} strategy to assign
177
+ * values to properties in the deserialized object.
169
178
*
170
179
* @param source {@link Reader} that supplies a serialized JSON string
171
180
* @param typeOfT data type for deserialization (class or {@link TypeToken})
@@ -178,7 +187,8 @@ public <T> T toType(Reader source, Type typeOfT) {
178
187
}
179
188
180
189
/**
181
- * Deserialize the JSON string supplied by the specified {@code Reader} into an object of the specified type.
190
+ * Deserialize the JSON string supplied by the specified {@code Reader} into an object of the
191
+ * specified type.
182
192
*
183
193
* @param source {@link Reader} that supplies a serialized JSON string
184
194
* @param typeOfT data type for deserialization (class or {@link TypeToken})
@@ -198,9 +208,11 @@ public <T> T toType(Reader source, Type typeOfT, PropertySetting setter) {
198
208
}
199
209
200
210
/**
201
- * Create a new {@code JsonInput} object to traverse the JSON string supplied the specified {@code Reader}.<br>
202
- * <b>NOTE</b>: The {@code JsonInput} object returned by this method uses the {@link PropertySetting#BY_NAME BY_NAME}
203
- * strategy to assign values to properties objects it deserializes.
211
+ * Create a new {@code JsonInput} object to traverse the JSON string supplied the specified {@code
212
+ * Reader}.<br>
213
+ * <b>NOTE</b>: The {@code JsonInput} object returned by this method uses the {@link
214
+ * PropertySetting#BY_NAME BY_NAME} strategy to assign values to properties objects it
215
+ * deserializes.
204
216
*
205
217
* @param from {@link Reader} that supplies a serialized JSON string
206
218
* @return {@link JsonInput} object to traverse the JSON string supplied by [from]
@@ -211,7 +223,8 @@ public JsonInput newInput(Reader from) throws UncheckedIOException {
211
223
}
212
224
213
225
/**
214
- * Create a new {@code JsonOutput} object to produce a serialized JSON string in the specified {@code Appendable}.
226
+ * Create a new {@code JsonOutput} object to produce a serialized JSON string in the specified
227
+ * {@code Appendable}.
215
228
*
216
229
* @param to {@link Appendable} that consumes a serialized JSON string
217
230
* @return {@link JsonOutput} object to product a JSON string in [to]
0 commit comments