Skip to content

Commit 6bff9b0

Browse files
sbabcocdiemol
andauthored
Add JavaDoc to 'Json' classes (#12584)
Co-authored-by: Diego Molina <[email protected]>
1 parent 02fe22a commit 6bff9b0

File tree

7 files changed

+710
-202
lines changed

7 files changed

+710
-202
lines changed

java/src/org/openqa/selenium/json/Input.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,61 @@
2424

2525
/**
2626
* Similar to a {@link Reader} but with the ability to peek a single character ahead.
27-
*
28-
* <p>For the sake of providing a useful {@link #toString()} implementation, keeps the most recently
27+
* <p>
28+
* For the sake of providing a useful {@link #toString()} implementation, keeps the most recently
2929
* read characters in the input buffer.
3030
*/
3131
class Input {
32-
public static final char EOF = (char) -1;
33-
// the number of chars to buffer
32+
/** end-of-file indicator (0xFFFD) */
33+
public static final char EOF = (char) -1; // NOTE: Produces Unicode replacement character (0xFFFD)
34+
/** the number of chars to buffer */
3435
private static final int BUFFER_SIZE = 4096;
35-
// the number of chars to remember, safe to set to 0
36+
/** the number of chars to remember, safe to set to 0 */
3637
private static final int MEMORY_SIZE = 128;
3738

3839
private final Reader source;
39-
// a buffer used to minimize read calls and to keep the chars to remember
40+
/** a buffer used to minimize read calls and to keep the chars to remember */
4041
private final char[] buffer;
41-
// the filled area in the buffer
42+
/** the filled area in the buffer */
4243
private int filled;
43-
// the last position read in the buffer
44+
/** the last position read in the buffer */
4445
private int position;
4546

47+
/**
48+
* Initialize a new instance of the {@link Input} class with the specified source.
49+
*
50+
* @param source {@link Reader} object that supplies the input to be processed
51+
*/
4652
public Input(Reader source) {
4753
this.source = Require.nonNull("Source", source);
4854
this.buffer = new char[BUFFER_SIZE + MEMORY_SIZE];
4955
this.filled = 0;
5056
this.position = -1;
5157
}
5258

59+
/**
60+
* Extract the next character from the input without consuming it.
61+
*
62+
* @return the next input character; {@link #EOF} if input is exhausted
63+
*/
5364
public char peek() {
5465
return fill() ? buffer[position + 1] : EOF;
5566
}
5667

68+
/**
69+
* Read and consume the next character from the input.
70+
*
71+
* @return the next input character; {@link #EOF} if input is exhausted
72+
*/
5773
public char read() {
5874
return fill() ? buffer[++position] : EOF;
5975
}
6076

77+
/**
78+
* Return a string containing the most recently consumed input characters.
79+
*
80+
* @return {@link String} with up to 128 consumed input characters
81+
*/
6182
@Override
6283
public String toString() {
6384
int offset;
@@ -74,6 +95,13 @@ public String toString() {
7495
return "Last " + length + " characters read: " + new String(buffer, offset, length);
7596
}
7697

98+
/**
99+
* If all buffered input has been consumed, read the next chunk into the buffer.<br>
100+
* <b>NOTE</b>: The last 128 character of consumed input is retained for debug output.
101+
*
102+
* @return {@code true} if new input is available; {@code false} if input is exhausted
103+
* @throws UncheckedIOException if an I/O exception is encountered
104+
*/
77105
private boolean fill() {
78106
// do we need to fill the buffer?
79107
while (filled == position + 1) {

java/src/org/openqa/selenium/json/Json.java

Lines changed: 138 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,133 @@
2727
import java.util.List;
2828
import java.util.Map;
2929

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+
*/
3087
public class Json {
88+
/** The value of {@code Content-Type} headers for HTTP requests and
89+
* responses with JSON entities */
3190
public static final String JSON_UTF_8 = "application/json; charset=utf-8";
3291

92+
/** Specifier for {@code List<Map<String, Object>} input/output type */
3393
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 */
3596
public static final Type MAP_TYPE = new TypeToken<Map<String, Object>>() {}.getType();
97+
/** Specifier for {@code Object} input/output type */
3698
public static final Type OBJECT_TYPE = new TypeToken<Object>() {}.getType();
3799

38100
private final JsonTypeCoercer fromJson = new JsonTypeCoercer();
39101

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+
*/
40110
public String toJson(Object toConvert) {
41111
return toJson(toConvert, JsonOutput.MAX_DEPTH);
42112
}
43113

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+
*/
44122
public String toJson(Object toConvert, int maxDepth) {
45123
try (Writer writer = new StringWriter();
46-
JsonOutput jsonOutput = newOutput(writer)) {
124+
JsonOutput jsonOutput = newOutput(writer)) {
47125
jsonOutput.write(toConvert, maxDepth);
48126
return writer.toString();
49127
} catch (IOException e) {
50128
throw new JsonException(e);
51129
}
52130
}
53131

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+
*/
54143
public <T> T toType(String source, Type typeOfT) {
55144
return toType(source, typeOfT, PropertySetting.BY_NAME);
56145
}
57146

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+
*/
58157
public <T> T toType(String source, Type typeOfT, PropertySetting setter) {
59158
try (StringReader reader = new StringReader(source)) {
60159
return toType(reader, typeOfT, setter);
@@ -63,10 +162,31 @@ public <T> T toType(String source, Type typeOfT, PropertySetting setter) {
63162
}
64163
}
65164

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+
*/
66176
public <T> T toType(Reader source, Type typeOfT) {
67177
return toType(source, typeOfT, PropertySetting.BY_NAME);
68178
}
69179

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+
*/
70190
public <T> T toType(Reader source, Type typeOfT, PropertySetting setter) {
71191
if (setter == null) {
72192
throw new JsonException("Mechanism for setting properties must be set");
@@ -77,10 +197,26 @@ public <T> T toType(Reader source, Type typeOfT, PropertySetting setter) {
77197
}
78198
}
79199

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+
*/
80209
public JsonInput newInput(Reader from) throws UncheckedIOException {
81210
return new JsonInput(from, fromJson, PropertySetting.BY_NAME);
82211
}
83212

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+
*/
84220
public JsonOutput newOutput(Appendable to) throws UncheckedIOException {
85221
return new JsonOutput(to);
86222
}

0 commit comments

Comments
 (0)