Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve documentation about default JsonReader and JsonWriter behavior #2341

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 33 additions & 35 deletions gson/src/main/java/com/google/gson/TypeAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
* By default Gson converts application classes to JSON using its built-in type
* adapters. If Gson's default JSON conversion isn't appropriate for a type,
* extend this class to customize the conversion. Here's an example of a type
* adapter for an (X,Y) coordinate point: <pre> {@code
*
* adapter for an (X,Y) coordinate point: <pre>{@code
* public class PointAdapter extends TypeAdapter<Point> {
* public Point read(JsonReader reader) throws IOException {
* if (reader.peek() == JsonToken.NULL) {
Expand Down Expand Up @@ -85,8 +84,7 @@
* guarantees of {@link Gson} might not apply.
*
* <p>To use a custom type adapter with Gson, you must <i>register</i> it with a
* {@link GsonBuilder}: <pre> {@code
*
* {@link GsonBuilder}: <pre>{@code
* GsonBuilder builder = new GsonBuilder();
* builder.registerTypeAdapter(Point.class, new PointAdapter());
* // if PointAdapter didn't check for nulls in its read/write methods, you should instead use
Expand All @@ -102,14 +100,12 @@
// <h2>JSON Conversion</h2>
// <p>A type adapter registered with Gson is automatically invoked while serializing
// or deserializing JSON. However, you can also use type adapters directly to serialize
// and deserialize JSON. Here is an example for deserialization: <pre> {@code
//
// and deserialize JSON. Here is an example for deserialization: <pre>{@code
// String json = "{'origin':'0,0','points':['1,2','3,4']}";
// TypeAdapter<Graph> graphAdapter = gson.getAdapter(Graph.class);
// Graph graph = graphAdapter.fromJson(json);
// }</pre>
// And an example for serialization: <pre> {@code
//
// And an example for serialization: <pre>{@code
// Graph graph = new Graph(...);
// TypeAdapter<Graph> graphAdapter = gson.getAdapter(Graph.class);
// String json = graphAdapter.toJson(graph);
Expand All @@ -134,12 +130,12 @@ public TypeAdapter() {

/**
* Converts {@code value} to a JSON document and writes it to {@code out}.
* The strictness {@link Strictness#LEGACY_STRICT} is used for writing the JSON data.
* To use a different strictness setting create a {@link JsonWriter}, call its
* {@link JsonWriter#setStrictness(Strictness)} method and then use
* {@link #write(JsonWriter, Object)} for writing.
*
* @param value the Java object to convert. May be null.
* <p>A {@link JsonWriter} with default configuration is used for writing the
* JSON data. To customize this behavior, create a {@link JsonWriter}, configure
* it and then use {@link #write(JsonWriter, Object)} instead.
*
* @param value the Java object to convert. May be {@code null}.
* @since 2.2
*/
public final void toJson(Writer out, T value) throws IOException {
Expand All @@ -151,8 +147,7 @@ public final void toJson(Writer out, T value) throws IOException {
* This wrapper method is used to make a type adapter null tolerant. In general, a
* type adapter is required to handle nulls in write and read methods. Here is how this
* is typically done:<br>
* <pre> {@code
*
* <pre>{@code
* Gson gson = new GsonBuilder().registerTypeAdapter(Foo.class,
* new TypeAdapter<Foo>() {
* public Foo read(JsonReader in) throws IOException {
Expand All @@ -173,8 +168,7 @@ public final void toJson(Writer out, T value) throws IOException {
* }</pre>
* You can avoid this boilerplate handling of nulls by wrapping your type adapter with
* this method. Here is how we will rewrite the above example:
* <pre> {@code
*
* <pre>{@code
* Gson gson = new GsonBuilder().registerTypeAdapter(Foo.class,
* new TypeAdapter<Foo>() {
* public Foo read(JsonReader in) throws IOException {
Expand Down Expand Up @@ -208,13 +202,13 @@ public final TypeAdapter<T> nullSafe() {

/**
* Converts {@code value} to a JSON document.
* The strictness {@link Strictness#LEGACY_STRICT} is used for writing the JSON data.
* To use a different strictness setting create a {@link JsonWriter}, call its
* {@link JsonWriter#setStrictness(Strictness)} method and then use
* {@link #write(JsonWriter, Object)} for writing.
*
* <p>A {@link JsonWriter} with default configuration is used for writing the
* JSON data. To customize this behavior, create a {@link JsonWriter}, configure
* it and then use {@link #write(JsonWriter, Object)} instead.
*
* @throws JsonIOException wrapping {@code IOException}s thrown by {@link #write(JsonWriter, Object)}
* @param value the Java object to convert. May be null.
* @param value the Java object to convert. May be {@code null}.
* @since 2.2
*/
public final String toJson(T value) {
Expand All @@ -230,7 +224,7 @@ public final String toJson(T value) {
/**
* Converts {@code value} to a JSON tree.
*
* @param value the Java object to convert. May be null.
* @param value the Java object to convert. May be {@code null}.
* @return the converted JSON tree. May be {@link JsonNull}.
* @throws JsonIOException wrapping {@code IOException}s thrown by {@link #write(JsonWriter, Object)}
* @since 2.2
Expand All @@ -249,20 +243,22 @@ public final JsonElement toJsonTree(T value) {
* Reads one JSON value (an array, object, string, number, boolean or null)
* and converts it to a Java object. Returns the converted object.
*
* @return the converted Java object. May be null.
* @return the converted Java object. May be {@code null}.
*/
public abstract T read(JsonReader in) throws IOException;

/**
* Converts the JSON document in {@code in} to a Java object. The strictness
* {@link Strictness#LEGACY_STRICT} is used for reading the JSON data. To use a different
* strictness setting create a {@link JsonReader}, call its {@link JsonReader#setStrictness(Strictness)}
* method and then use {@link #read(JsonReader)} for reading.
* Converts the JSON document in {@code in} to a Java object.
*
* <p>A {@link JsonReader} with default configuration (that is with
* {@link Strictness#LEGACY_STRICT} as strictness) is used for reading the JSON data.
* To customize this behavior, create a {@link JsonReader}, configure it and then
* use {@link #read(JsonReader)} instead.
*
* <p>No exception is thrown if the JSON data has multiple top-level JSON elements,
* or if there is trailing data.
*
* @return the converted Java object. May be null.
* @return the converted Java object. May be {@code null}.
* @since 2.2
*/
public final T fromJson(Reader in) throws IOException {
Expand All @@ -271,15 +267,17 @@ public final T fromJson(Reader in) throws IOException {
}

/**
* Converts the JSON document in {@code json} to a Java object. The strictness
* {@link Strictness#LEGACY_STRICT} is used for reading the JSON data. To use a different
* strictness setting create a {@link JsonReader}, call its {@link JsonReader#setStrictness(Strictness)}
* method and then use {@link #read(JsonReader)} for reading.
* Converts the JSON document in {@code json} to a Java object.
*
* <p>A {@link JsonReader} with default configuration (that is with
* {@link Strictness#LEGACY_STRICT} as strictness) is used for reading the JSON data.
* To customize this behavior, create a {@link JsonReader}, configure it and then
* use {@link #read(JsonReader)} instead.
*
* <p>No exception is thrown if the JSON data has multiple top-level JSON elements,
* or if there is trailing data.
*
* @return the converted Java object. May be null.
* @return the converted Java object. May be {@code null}.
* @since 2.2
*/
public final T fromJson(String json) throws IOException {
Expand All @@ -290,7 +288,7 @@ public final T fromJson(String json) throws IOException {
* Converts {@code jsonTree} to a Java object.
*
* @param jsonTree the JSON element to convert. May be {@link JsonNull}.
* @return the converted Java object. May be null.
* @return the converted Java object. May be {@code null}.
* @throws JsonIOException wrapping {@code IOException}s thrown by {@link #read(JsonReader)}
* @since 2.2
*/
Expand Down
12 changes: 12 additions & 0 deletions gson/src/main/java/com/google/gson/stream/JsonReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.gson.stream;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.Strictness;
import com.google.gson.internal.JsonReaderInternalAccess;
import com.google.gson.internal.TroubleshootingGuide;
Expand Down Expand Up @@ -64,6 +66,16 @@
* Null literals can be consumed using either {@link #nextNull()} or {@link
* #skipValue()}.
*
* <h2>Configuration</h2>
* The behavior of this reader can be customized with the following methods:
* <ul>
* <li>{@link #setStrictness(Strictness)}, the default is {@link Strictness#LEGACY_STRICT}
* </ul>
*
* The default configuration of {@code JsonReader} instances used internally by
* the {@link Gson} class differs, and can be adjusted with the various
* {@link GsonBuilder} methods.
*
* <h2>Example</h2>
* Suppose we'd like to parse a stream of messages such as the following: <pre> {@code
* [
Expand Down
16 changes: 16 additions & 0 deletions gson/src/main/java/com/google/gson/stream/JsonWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.FormattingStyle;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.Strictness;
import java.io.Closeable;
import java.io.Flushable;
Expand Down Expand Up @@ -61,6 +63,20 @@
* Finally close the object using {@link #endObject()}.
* </ul>
*
* <h2>Configuration</h2>
* The behavior of this writer can be customized with the following methods:
* <ul>
* <li>{@link #setFormattingStyle(FormattingStyle)}, the default is {@link FormattingStyle#COMPACT}
* <li>{@link #setHtmlSafe(boolean)}, by default HTML characters are not escaped
* in the JSON output
* <li>{@link #setStrictness(Strictness)}, the default is {@link Strictness#LEGACY_STRICT}
* <li>{@link #setSerializeNulls(boolean)}, by default {@code null} is serialized
* </ul>
*
* The default configuration of {@code JsonWriter} instances used internally by
* the {@link Gson} class differs, and can be adjusted with the various
* {@link GsonBuilder} methods.
*
* <h2>Example</h2>
* Suppose we'd like to encode a stream of messages such as the following: <pre> {@code
* [
Expand Down