27
27
import java .util .List ;
28
28
import java .util .Map ;
29
29
30
+ /**
31
+ * The <b>Json</b> class is the entrypoint for the JSON processing features of the Selenium API.
32
+ * These features include:
33
+ * <ul>
34
+ * <li>Built-in JSON deserialization to primitives and collections from the standard types shown below.</li>
35
+ * <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>
48
+ * <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>
55
+ * </ul>
56
+ *
57
+ * The standard types supported by built-in processing:
58
+ * <ul>
59
+ * <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>
63
+ * <li><b>Collection Types</b>:<br>
64
+ * {@link java.util.List List}, {@link java.util.Set Set}
65
+ * </li>
66
+ * <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>
71
+ * </ul>
72
+ *
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.
82
+ *
83
+ * @see JsonTypeCoercer
84
+ * @see JsonInput
85
+ * @see JsonOutput
86
+ */
30
87
public class Json {
88
+ /** The value of {@code Content-Type} headers for HTTP requests and
89
+ * responses with JSON entities */
31
90
public static final String JSON_UTF_8 = "application/json; charset=utf-8" ;
32
91
92
+ /** Specifier for {@code List<Map<String, Object>} input/output type */
33
93
public static final Type LIST_OF_MAPS_TYPE =
34
- new TypeToken <List <Map <String , Object >>>() {}.getType ();
94
+ new TypeToken <List <Map <String , Object >>>() {}.getType ();
95
+ /** Specifier for {@code Map<String, Object>} input/output type */
35
96
public static final Type MAP_TYPE = new TypeToken <Map <String , Object >>() {}.getType ();
97
+ /** Specifier for {@code Object} input/output type */
36
98
public static final Type OBJECT_TYPE = new TypeToken <Object >() {}.getType ();
37
99
38
100
private final JsonTypeCoercer fromJson = new JsonTypeCoercer ();
39
101
102
+ /**
103
+ * 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}.
106
+ *
107
+ * @param toConvert the object to be serialized
108
+ * @return JSON string representing the specified object
109
+ */
40
110
public String toJson (Object toConvert ) {
41
111
return toJson (toConvert , JsonOutput .MAX_DEPTH );
42
112
}
43
113
114
+ /**
115
+ * Serialize the specified object to JSON string representation.
116
+ *
117
+ * @param toConvert the object to be serialized
118
+ * @param maxDepth maximum depth of nested object traversal
119
+ * @return JSON string representing the specified object
120
+ * @throws JsonException if an I/O exception is encountered
121
+ */
44
122
public String toJson (Object toConvert , int maxDepth ) {
45
123
try (Writer writer = new StringWriter ();
46
- JsonOutput jsonOutput = newOutput (writer )) {
124
+ JsonOutput jsonOutput = newOutput (writer )) {
47
125
jsonOutput .write (toConvert , maxDepth );
48
126
return writer .toString ();
49
127
} catch (IOException e ) {
50
128
throw new JsonException (e );
51
129
}
52
130
}
53
131
132
+ /**
133
+ * 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.
136
+ *
137
+ * @param source serialized source as JSON string
138
+ * @param typeOfT data type for deserialization (class or {@link TypeToken})
139
+ * @return object of the specified type deserialized from [source]
140
+ * @param <T> result type (as specified by [typeOfT])
141
+ * @throws JsonException if an I/O exception is encountered
142
+ */
54
143
public <T > T toType (String source , Type typeOfT ) {
55
144
return toType (source , typeOfT , PropertySetting .BY_NAME );
56
145
}
57
146
147
+ /**
148
+ * Deserialize the specified JSON string into an object of the specified type.
149
+ *
150
+ * @param source serialized source as JSON string
151
+ * @param typeOfT data type for deserialization (class or {@link TypeToken})
152
+ * @param setter strategy used to assign values during deserialization
153
+ * @return object of the specified type deserialized from [source]
154
+ * @param <T> result type (as specified by [typeOfT])
155
+ * @throws JsonException if an I/O exception is encountered
156
+ */
58
157
public <T > T toType (String source , Type typeOfT , PropertySetting setter ) {
59
158
try (StringReader reader = new StringReader (source )) {
60
159
return toType (reader , typeOfT , setter );
@@ -63,10 +162,31 @@ public <T> T toType(String source, Type typeOfT, PropertySetting setter) {
63
162
}
64
163
}
65
164
165
+ /**
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.
169
+ *
170
+ * @param source {@link Reader} that supplies a serialized JSON string
171
+ * @param typeOfT data type for deserialization (class or {@link TypeToken})
172
+ * @return object of the specified type deserialized from [source]
173
+ * @param <T> result type (as specified by [typeOfT])
174
+ * @throws JsonException if an I/O exception is encountered
175
+ */
66
176
public <T > T toType (Reader source , Type typeOfT ) {
67
177
return toType (source , typeOfT , PropertySetting .BY_NAME );
68
178
}
69
179
180
+ /**
181
+ * Deserialize the JSON string supplied by the specified {@code Reader} into an object of the specified type.
182
+ *
183
+ * @param source {@link Reader} that supplies a serialized JSON string
184
+ * @param typeOfT data type for deserialization (class or {@link TypeToken})
185
+ * @param setter strategy used to assign values during deserialization
186
+ * @return object of the specified type deserialized from [source]
187
+ * @param <T> result type (as specified by [typeOfT])
188
+ * @throws JsonException if an I/O exception is encountered
189
+ */
70
190
public <T > T toType (Reader source , Type typeOfT , PropertySetting setter ) {
71
191
if (setter == null ) {
72
192
throw new JsonException ("Mechanism for setting properties must be set" );
@@ -77,10 +197,26 @@ public <T> T toType(Reader source, Type typeOfT, PropertySetting setter) {
77
197
}
78
198
}
79
199
200
+ /**
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.
204
+ *
205
+ * @param from {@link Reader} that supplies a serialized JSON string
206
+ * @return {@link JsonInput} object to traverse the JSON string supplied by [from]
207
+ * @throws UncheckedIOException if an I/O exception occurs
208
+ */
80
209
public JsonInput newInput (Reader from ) throws UncheckedIOException {
81
210
return new JsonInput (from , fromJson , PropertySetting .BY_NAME );
82
211
}
83
212
213
+ /**
214
+ * Create a new {@code JsonOutput} object to produce a serialized JSON string in the specified {@code Appendable}.
215
+ *
216
+ * @param to {@link Appendable} that consumes a serialized JSON string
217
+ * @return {@link JsonOutput} object to product a JSON string in [to]
218
+ * @throws UncheckedIOException if an I/O exception occurs
219
+ */
84
220
public JsonOutput newOutput (Appendable to ) throws UncheckedIOException {
85
221
return new JsonOutput (to );
86
222
}
0 commit comments