Skip to content

Commit

Permalink
[java] Formatting files
Browse files Browse the repository at this point in the history
  • Loading branch information
diemol committed Aug 23, 2023
1 parent 6bff9b0 commit 9dcd124
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 296 deletions.
9 changes: 7 additions & 2 deletions java/src/org/openqa/selenium/json/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,28 @@

/**
* Similar to a {@link Reader} but with the ability to peek a single character ahead.
* <p>
* For the sake of providing a useful {@link #toString()} implementation, keeps the most recently
*
* <p>For the sake of providing a useful {@link #toString()} implementation, keeps the most recently
* read characters in the input buffer.
*/
class Input {
/** end-of-file indicator (0xFFFD) */
public static final char EOF = (char) -1; // NOTE: Produces Unicode replacement character (0xFFFD)

/** the number of chars to buffer */
private static final int BUFFER_SIZE = 4096;

/** the number of chars to remember, safe to set to 0 */
private static final int MEMORY_SIZE = 128;

private final Reader source;

/** a buffer used to minimize read calls and to keep the chars to remember */
private final char[] buffer;

/** the filled area in the buffer */
private int filled;

/** the last position read in the buffer */
private int position;

Expand Down
117 changes: 65 additions & 52 deletions java/src/org/openqa/selenium/json/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,79 +30,87 @@
/**
* The <b>Json</b> class is the entrypoint for the JSON processing features of the Selenium API.
* These features include:
*
* <ul>
* <li>Built-in JSON deserialization to primitives and collections from the standard types shown below.</li>
* <li>Built-in JSON deserialization to primitives and collections from the standard types shown
* below.
* <li>Facilities to deserialize JSON to custom data types:
* <ul>
* <li>Classes that declare a {@code fromJson(T)} static method, where <b>T</b> is any of the standard
* types shown below.</li>
* <li>Classes that declare a {@code fromJson(JsonInput)} static method.<br>
* <b>NOTE</b>: Objects deserialized via a {@code fromJson} static method can be immutable.</li>
* <li>Classes that declare setter methods adhering to the
* <a href="https://docs.oracle.com/javase/tutorial/javabeans/writing/index.html">JavaBean</a>
* specification.<br>
* <b>NOTE</b>: Deserialized {@code JavaBean} objects are mutable, which may be undesirable.</li>
* </ul>
* </li>
* <li>Built-in JSON serialization from primitives and collections from the standard types shown below.</li>
* <ul>
* <li>Classes that declare a {@code fromJson(T)} static method, where <b>T</b> is any of
* the standard types shown below.
* <li>Classes that declare a {@code fromJson(JsonInput)} static method.<br>
* <b>NOTE</b>: Objects deserialized via a {@code fromJson} static method can be
* immutable.
* <li>Classes that declare setter methods adhering to the <a
* href="https://docs.oracle.com/javase/tutorial/javabeans/writing/index.html">JavaBean</a>
* specification.<br>
* <b>NOTE</b>: Deserialized {@code JavaBean} objects are mutable, which may be
* undesirable.
* </ul>
* <li>Built-in JSON serialization from primitives and collections from the standard types shown
* below.
* <li>Facilities to serialize custom data types to JSON:
* <ul>
* <li>Classes that declare a {@code toJson()} method returning a primitive or collection from
* the standard types shown below.</li>
* <li>Classes that declare getter methods adhering to the {@code JavaBean} specification.</li>
* </ul>
* </li>
* <ul>
* <li>Classes that declare a {@code toJson()} method returning a primitive or collection
* from the standard types shown below.
* <li>Classes that declare getter methods adhering to the {@code JavaBean} specification.
* </ul>
* </ul>
*
* The standard types supported by built-in processing:
*
* <ul>
* <li><b>Numeric Types</b>:<br>
* {@link java.lang.Byte Byte}, {@link java.lang.Double Double}, {@link java.lang.Float Float},
* {@link java.lang.Integer Integer}, {@link java.lang.Long Long}, {@link java.lang.Short Short}
* </li>
* {@link java.lang.Byte Byte}, {@link java.lang.Double Double}, {@link java.lang.Float
* Float}, {@link java.lang.Integer Integer}, {@link java.lang.Long Long}, {@link
* java.lang.Short Short}
* <li><b>Collection Types</b>:<br>
* {@link java.util.List List}, {@link java.util.Set Set}
* </li>
* <li><b>Standard Java Types</b>:<br>
* {@link java.util.Map Map}, {@link java.lang.Boolean Boolean}, {@link java.lang.String String},
* {@link java.lang.Enum Enum}, {@link java.net.URI URI}, {@link java.net.URL URL},
* {@link java.util.UUID UUID}, {@link java.time.Instant Instant}, {@link java.lang.Object Object}
* </li>
* {@link java.util.Map Map}, {@link java.lang.Boolean Boolean}, {@link java.lang.String
* String}, {@link java.lang.Enum Enum}, {@link java.net.URI URI}, {@link java.net.URL URL},
* {@link java.util.UUID UUID}, {@link java.time.Instant Instant}, {@link java.lang.Object
* Object}
* </ul>
*
* You can serialize objects for which no explicit coercer has been specified, and the <b>Json</b> API will use a
* generic process to provide best-effort JSON output. For the most predictable results, though, it's best to
* provide a {@code toJson()} method for the <b>Json</b> API to use for serialization. This is especially beneficial
* for objects that contain transient properties that should be omitted from the JSON output.
* <p>
* You can deserialize objects for which no explicit handling has been defined. Note that the data type of the
* result will be {@code Map<String,?>}, which means that you'll need to perform type checking and casting every
* time you extract an entry value from the result. For this reason, it's best to declare a type-specific
* {@code fromJson()} method in every type you need to deserialize.
* You can serialize objects for which no explicit coercer has been specified, and the <b>Json</b>
* API will use a generic process to provide best-effort JSON output. For the most predictable
* results, though, it's best to provide a {@code toJson()} method for the <b>Json</b> API to use
* for serialization. This is especially beneficial for objects that contain transient properties
* that should be omitted from the JSON output.
*
* <p>You can deserialize objects for which no explicit handling has been defined. Note that the
* data type of the result will be {@code Map<String,?>}, which means that you'll need to perform
* type checking and casting every time you extract an entry value from the result. For this reason,
* it's best to declare a type-specific {@code fromJson()} method in every type you need to
* deserialize.
*
* @see JsonTypeCoercer
* @see JsonInput
* @see JsonOutput
*/
public class Json {
/** The value of {@code Content-Type} headers for HTTP requests and
* responses with JSON entities */
/**
* The value of {@code Content-Type} headers for HTTP requests and responses with JSON entities
*/
public static final String JSON_UTF_8 = "application/json; charset=utf-8";

/** Specifier for {@code List<Map<String, Object>} input/output type */
public static final Type LIST_OF_MAPS_TYPE =
new TypeToken<List<Map<String, Object>>>() {}.getType();
new TypeToken<List<Map<String, Object>>>() {}.getType();

/** Specifier for {@code Map<String, Object>} input/output type */
public static final Type MAP_TYPE = new TypeToken<Map<String, Object>>() {}.getType();

/** Specifier for {@code Object} input/output type */
public static final Type OBJECT_TYPE = new TypeToken<Object>() {}.getType();

private final JsonTypeCoercer fromJson = new JsonTypeCoercer();

/**
* Serialize the specified object to JSON string representation.<br>
* <b>NOTE</b>: This method limits traversal of nested objects to the default
* {@link JsonOutput#MAX_DEPTH maximum depth}.
* <b>NOTE</b>: This method limits traversal of nested objects to the default {@link
* JsonOutput#MAX_DEPTH maximum depth}.
*
* @param toConvert the object to be serialized
* @return JSON string representing the specified object
Expand All @@ -121,7 +129,7 @@ public String toJson(Object toConvert) {
*/
public String toJson(Object toConvert, int maxDepth) {
try (Writer writer = new StringWriter();
JsonOutput jsonOutput = newOutput(writer)) {
JsonOutput jsonOutput = newOutput(writer)) {
jsonOutput.write(toConvert, maxDepth);
return writer.toString();
} catch (IOException e) {
Expand All @@ -131,8 +139,8 @@ public String toJson(Object toConvert, int maxDepth) {

/**
* Deserialize the specified JSON string into an object of the specified type.<br>
* <b>NOTE</b>: This method uses the {@link PropertySetting#BY_NAME BY_NAME} strategy to assign values to properties
* in the deserialized object.
* <b>NOTE</b>: This method uses the {@link PropertySetting#BY_NAME BY_NAME} strategy to assign
* values to properties in the deserialized object.
*
* @param source serialized source as JSON string
* @param typeOfT data type for deserialization (class or {@link TypeToken})
Expand Down Expand Up @@ -163,9 +171,10 @@ public <T> T toType(String source, Type typeOfT, PropertySetting setter) {
}

/**
* Deserialize the JSON string supplied by the specified {@code Reader} into an object of the specified type.<br>
* <b>NOTE</b>: This method uses the {@link PropertySetting#BY_NAME BY_NAME} strategy to assign values to properties
* in the deserialized object.
* Deserialize the JSON string supplied by the specified {@code Reader} into an object of the
* specified type.<br>
* <b>NOTE</b>: This method uses the {@link PropertySetting#BY_NAME BY_NAME} strategy to assign
* values to properties in the deserialized object.
*
* @param source {@link Reader} that supplies a serialized JSON string
* @param typeOfT data type for deserialization (class or {@link TypeToken})
Expand All @@ -178,7 +187,8 @@ public <T> T toType(Reader source, Type typeOfT) {
}

/**
* Deserialize the JSON string supplied by the specified {@code Reader} into an object of the specified type.
* Deserialize the JSON string supplied by the specified {@code Reader} into an object of the
* specified type.
*
* @param source {@link Reader} that supplies a serialized JSON string
* @param typeOfT data type for deserialization (class or {@link TypeToken})
Expand All @@ -198,9 +208,11 @@ public <T> T toType(Reader source, Type typeOfT, PropertySetting setter) {
}

/**
* Create a new {@code JsonInput} object to traverse the JSON string supplied the specified {@code Reader}.<br>
* <b>NOTE</b>: The {@code JsonInput} object returned by this method uses the {@link PropertySetting#BY_NAME BY_NAME}
* strategy to assign values to properties objects it deserializes.
* Create a new {@code JsonInput} object to traverse the JSON string supplied the specified {@code
* Reader}.<br>
* <b>NOTE</b>: The {@code JsonInput} object returned by this method uses the {@link
* PropertySetting#BY_NAME BY_NAME} strategy to assign values to properties objects it
* deserializes.
*
* @param from {@link Reader} that supplies a serialized JSON string
* @return {@link JsonInput} object to traverse the JSON string supplied by [from]
Expand All @@ -211,7 +223,8 @@ public JsonInput newInput(Reader from) throws UncheckedIOException {
}

/**
* Create a new {@code JsonOutput} object to produce a serialized JSON string in the specified {@code Appendable}.
* Create a new {@code JsonOutput} object to produce a serialized JSON string in the specified
* {@code Appendable}.
*
* @param to {@link Appendable} that consumes a serialized JSON string
* @return {@link JsonOutput} object to product a JSON string in [to]
Expand Down
41 changes: 21 additions & 20 deletions java/src/org/openqa/selenium/json/JsonInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
import org.openqa.selenium.internal.Require;

/**
* The <b>JsonInput</b> class defines the operations used to deserialize JSON strings into Java objects.
* The <b>JsonInput</b> class defines the operations used to deserialize JSON strings into Java
* objects.
*/
public class JsonInput implements Closeable {

Expand All @@ -51,7 +52,8 @@ public class JsonInput implements Closeable {
* {@code Reader} object specified by [source].
*
* @param source {@link Reader} object that supplies the JSON string to be processed
* @param coercer {@link JsonTypeCoercer} that encapsulates the defined type-specific deserializers
* @param coercer {@link JsonTypeCoercer} that encapsulates the defined type-specific
* deserializers
* @param setter strategy used to assign values during deserialization
*/
JsonInput(Reader source, JsonTypeCoercer coercer, PropertySetting setter) {
Expand Down Expand Up @@ -200,7 +202,7 @@ public String nextName() {
char read = input.read();
if (read != ':') {
throw new JsonException(
"Unable to read name. Expected colon separator, but saw '" + read + "'");
"Unable to read name. Expected colon separator, but saw '" + read + "'");
}
return name;
}
Expand Down Expand Up @@ -232,11 +234,11 @@ public Number nextNumber() {
do {
char read = input.peek();
if (Character.isDigit(read)
|| read == '+'
|| read == '-'
|| read == 'e'
|| read == 'E'
|| read == '.') {
|| read == '+'
|| read == '-'
|| read == 'e'
|| read == 'E'
|| read == '.') {
builder.append(input.read());
} else {
break;
Expand Down Expand Up @@ -290,7 +292,7 @@ public Instant nextInstant() {
public boolean hasNext() {
if (stack.isEmpty()) {
throw new JsonException(
"Unable to determine if an item has next when not in a container type. " + input);
"Unable to determine if an item has next when not in a container type. " + input);
}

skipWhitespace(input);
Expand Down Expand Up @@ -325,7 +327,7 @@ public void endArray() {
if (expectation != Container.COLLECTION) {
// The only other thing we could be closing is a map
throw new JsonException(
"Attempt to close a JSON List, but a JSON Object was expected. " + input);
"Attempt to close a JSON List, but a JSON Object was expected. " + input);
}
input.read();
}
Expand Down Expand Up @@ -413,7 +415,7 @@ public void skipValue() {
*
* @param type data type for deserialization (class or {@link TypeToken})
* @return object of the specified type deserialized from the JSON input stream<br>
* <b>NOTE</b>: Returns {@code null} if the input string is exhausted.
* <b>NOTE</b>: Returns {@code null} if the input string is exhausted.
* @param <T> result type (as specified by [type])
* @throws JsonException if coercion of the next element to the specified type fails
* @throws UncheckedIOException if an I/O exception is encountered
Expand Down Expand Up @@ -448,7 +450,7 @@ private boolean isReadingName() {
private void expect(JsonType type) {
if (peek() != type) {
throw new JsonException(
"Expected to read a " + type + " but instead have: " + peek() + ". " + input);
"Expected to read a " + type + " but instead have: " + peek() + ". " + input);
}

// Special map handling. Woo!
Expand All @@ -474,7 +476,8 @@ private void expect(JsonType type) {
}

/**
* Read the next element from the JSON input stream, converting with the supplied mapper if it's the expected string.
* Read the next element from the JSON input stream, converting with the supplied mapper if it's
* the expected string.
*
* @param toCompare expected element string
* @param mapper function to convert the element string to its corresponding type
Expand All @@ -489,8 +492,8 @@ private <X> X read(String toCompare, Function<String, X> mapper) {
char read = input.read();
if (read != toCompare.charAt(i)) {
throw new JsonException(
String.format(
"Unable to read %s. Saw %s at position %d. %s", toCompare, read, i, input));
String.format(
"Unable to read %s. Saw %s at position %d. %s", toCompare, read, i, input));
}
}

Expand Down Expand Up @@ -526,8 +529,8 @@ private String readString() {
}

/**
* Convert the escape sequence at the current JSON input stream position, appending the result to the provided
* builder.
* Convert the escape sequence at the current JSON input stream position, appending the result to
* the provided builder.
*
* @param builder {@link StringBuilder}
* @throws JsonException if an unsupported escape sequence is found
Expand Down Expand Up @@ -597,9 +600,7 @@ private void skipWhitespace(Input input) {
}
}

/**
* Used to track the current container processing state.
*/
/** Used to track the current container processing state. */
private enum Container {

/** Processing a JSON array */
Expand Down
Loading

0 comments on commit 9dcd124

Please sign in to comment.