diff --git a/Troubleshooting.md b/Troubleshooting.md index acdb8be6d2..525df0154e 100644 --- a/Troubleshooting.md +++ b/Troubleshooting.md @@ -127,7 +127,7 @@ For example, let's assume you want to deserialize the following JSON data: } ``` -This will fail with an exception similar to this one: `MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 5 column 4 path $.languages[2]` +This will fail with an exception similar to this one: `MalformedJsonException: Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed JSON at line 5 column 4 path $.languages[2]` The problem here is the trailing comma (`,`) after `"French"`, trailing commas are not allowed by the JSON specification. The location information "line 5 column 4" points to the `]` in the JSON data (with some slight inaccuracies) because Gson expected another value after `,` instead of the closing `]`. The JSONPath `$.languages[2]` in the exception message also points there: `$.` refers to the root object, `languages` refers to its member of that name and `[2]` refers to the (missing) third value in the JSON array value of that member (numbering starts at 0, so it is `[2]` instead of `[3]`). The proper solution here is to fix the malformed JSON data. diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 5460417de8..b73552c569 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -107,12 +107,11 @@ * *
For older Gson versions which don't have the {@link Strictness} mode API the following + *
For older Gson versions, which don't have the strictness mode API, the following * workarounds can be used: * *
transient
or static
fields from
* consideration for serialization and deserialization. You can change this behavior through
* {@link GsonBuilder#excludeFieldsWithModifiers(int...)}.* Type typeOfSrc = new TypeToken<Collection<Foo>>(){}.getType(); *- * @param writer Writer to which the JSON representation of src needs to be written. + * @param writer Writer to which the JSON representation of src needs to be written * @throws JsonIOException if there was a problem writing to the writer * @since 1.2 * @@ -828,19 +829,20 @@ public void toJson(Object src, Type typeOfSrc, Appendable writer) throws JsonIOE * Writes the JSON representation of {@code src} of type {@code typeOfSrc} to * {@code writer}. * -
If the {@code Gson} instance has a not-null strictness setting, this setting will be used for reading the JSON - * regardless of the {@linkplain JsonReader#getStrictness() strictness} of the provided {@link JsonReader}. For legacy - * reasons, if the {@code Gson} instance has {@code null} as its strictness setting and the provided {@link JsonReader} - * has a strictness of {@link Strictness#LEGACY_STRICT}, the JSON will be read in {@linkplain Strictness#LENIENT} - * mode. Note that in both cases the old strictness value of the reader will be restored when this method returns. + *
If the {@code Gson} instance has an {@linkplain GsonBuilder#setStrictness(Strictness) explicit strictness setting},
+ * this setting will be used for writing the JSON regardless of the {@linkplain JsonWriter#getStrictness() strictness}
+ * of the provided {@link JsonWriter}. For legacy reasons, if the {@code Gson} instance has no explicit strictness setting
+ * and the writer does not have the strictness {@link Strictness#STRICT}, the JSON will be written in {@link Strictness#LENIENT}
+ * mode.
+ * Note that in all cases the old strictness setting of the writer will be restored when this method returns.
*
*
The 'HTML-safe' and 'serialize {@code null}' settings of this {@code Gson} instance * (configured by the {@link GsonBuilder}) are applied, and the original settings of the * writer are restored once this method returns. * - * @param src the object to be written. - * @param typeOfSrc the type of the object to be written. - * @param writer the {@link JsonWriter} writer to which the provided object will be written. + * @param src the object for which JSON representation is to be created + * @param typeOfSrc the type of the object to be written + * @param writer Writer to which the JSON representation of src needs to be written * * @throws JsonIOException if there was a problem writing to the writer */ @@ -851,7 +853,7 @@ public void toJson(Object src, Type typeOfSrc, JsonWriter writer) throws JsonIOE Strictness oldStrictness = writer.getStrictness(); if (this.strictness != null) { writer.setStrictness(this.strictness); - } else if (writer.getStrictness() == Strictness.LEGACY_STRICT){ + } else if (writer.getStrictness() != Strictness.STRICT) { writer.setStrictness(Strictness.LENIENT); } @@ -911,9 +913,10 @@ public void toJson(JsonElement jsonElement, Appendable writer) throws JsonIOExce *
The following settings are considered: *
If the {@code Gson} instance has a not-null strictness setting, this setting will be used for writing the JSON - * regardless of the {@linkplain JsonWriter#getStrictness() strictness} of the provided {@link JsonWriter}. For legacy - * reasons, if the {@code Gson} instance has {@code null} as its strictness setting and the provided {@link JsonWriter} - * has a strictness of {@link Strictness#LEGACY_STRICT}, the JSON will be written in {@linkplain Strictness#LENIENT} - * mode. Note that in both cases the old strictness value of the writer will be restored when this method returns. + *
If the {@code Gson} instance has an {@linkplain GsonBuilder#setStrictness(Strictness) explicit strictness setting},
+ * this setting will be used for writing the JSON regardless of the {@linkplain JsonWriter#getStrictness() strictness}
+ * of the provided {@link JsonWriter}. For legacy reasons, if the {@code Gson} instance has no explicit strictness setting
+ * and the writer does not have the strictness {@link Strictness#STRICT}, the JSON will be written in {@link Strictness#LENIENT}
+ * mode.
+ * Note that in all cases the old strictness setting of the writer will be restored when this method returns.
*
*
The 'HTML-safe' and 'serialize {@code null}' settings of this {@code Gson} instance * (configured by the {@link GsonBuilder}) are applied, and the original settings of the * writer are restored once this method returns. * - * @param jsonElement the JSON element to be written. - * @param writer the JSON writer to which the provided element will be written. + * @param jsonElement the JSON element to be written + * @param writer the JSON writer to which the provided element will be written * @throws JsonIOException if there was a problem writing to the writer */ public void toJson(JsonElement jsonElement, JsonWriter writer) throws JsonIOException { @@ -973,7 +978,7 @@ public void toJson(JsonElement jsonElement, JsonWriter writer) throws JsonIOExce if (this.strictness != null) { writer.setStrictness(this.strictness); - } else if (writer.getStrictness() == Strictness.LEGACY_STRICT) { + } else if (writer.getStrictness() != Strictness.STRICT) { writer.setStrictness(Strictness.LENIENT); } @@ -1203,9 +1208,12 @@ private static void assertFullConsumption(Object obj, JsonReader reader) { *
Unlike the other {@code fromJson} methods, no exception is thrown if the JSON data has * multiple top-level JSON elements, or if there is trailing data. * - *
The JSON data is parsed in {@linkplain JsonReader#setStrictness(Strictness) lenient mode}, - * regardless of the strictness setting of the provided reader. The strictness setting - * of the reader is restored once this method returns. + *
If the {@code Gson} instance has an {@linkplain GsonBuilder#setStrictness(Strictness) explicit strictness setting},
+ * this setting will be used for reading the JSON regardless of the {@linkplain JsonReader#getStrictness() strictness}
+ * of the provided {@link JsonReader}. For legacy reasons, if the {@code Gson} instance has no explicit strictness setting
+ * and the reader does not have the strictness {@link Strictness#STRICT}, the JSON will be written in {@link Strictness#LENIENT}
+ * mode. Unlike the other {@code fromJson} methods, no exception is thrown if the JSON data has
* multiple top-level JSON elements, or if there is trailing data.
*
- * If the {@code Gson} instance has a not-null strictness setting, this setting will be used for reading the JSON
- * regardless of the {@linkplain JsonReader#getStrictness() strictness} of the provided {@link JsonReader}. For legacy
- * reasons, if the {@code Gson} instance has {@code null} as its strictness setting and the provided {@link JsonReader}
- * has a strictness of {@link Strictness#LEGACY_STRICT}, the JSON will be read in {@linkplain Strictness#LENIENT}
- * mode. Note that in both cases the old strictness value of the reader will be restored when this method returns.
+ * If the {@code Gson} instance has an {@linkplain GsonBuilder#setStrictness(Strictness) explicit strictness setting},
+ * this setting will be used for reading the JSON regardless of the {@linkplain JsonReader#getStrictness() strictness}
+ * of the provided {@link JsonReader}. For legacy reasons, if the {@code Gson} instance has no explicit strictness setting
+ * and the reader does not have the strictness {@link Strictness#STRICT}, the JSON will be written in {@link Strictness#LENIENT}
+ * mode. Use this builder to construct a {@link Gson} instance when you need to set configuration
* options other than the default. For {@link Gson} with default configuration, it is simpler to
@@ -73,12 +73,16 @@
* .create();
*
*
- * NOTES:
+ * Notes:
* This method has been deprecated. Please use {@link GsonBuilder#setStrictness(Strictness)} instead.
- * Calling this method is equivalent to {@code setStrictness(Strictness.LENIENT)} The JSON string is parsed in {@linkplain JsonReader#setLenient(boolean) lenient mode}.
+ * The JSON string is parsed in {@linkplain JsonReader#setStrictness(Strictness) lenient mode}.
*
* @param json JSON text
* @return a parse tree of {@link JsonElement}s corresponding to the specified JSON
@@ -57,7 +57,7 @@ public static JsonElement parseString(String json) throws JsonSyntaxException {
* An exception is thrown if the JSON string has multiple top-level JSON elements,
* or if there is trailing data.
*
- * The JSON data is parsed in {@linkplain JsonReader#setLenient(boolean) lenient mode}.
+ * The JSON data is parsed in {@linkplain JsonReader#setStrictness(Strictness) lenient mode}.
*
* @param reader JSON text
* @return a parse tree of {@link JsonElement}s corresponding to the specified JSON
@@ -87,8 +87,8 @@ public static JsonElement parseReader(Reader reader) throws JsonIOException, Jso
* Unlike the other {@code parse} methods, no exception is thrown if the JSON data has
* multiple top-level JSON elements, or if there is trailing data.
*
- * The JSON data is parsed in {@linkplain JsonReader#setLenient(boolean) lenient mode},
- * regardless of the lenient mode setting of the provided reader. The lenient mode setting
+ * The JSON data is parsed in {@linkplain JsonReader#setStrictness(Strictness) lenient mode},
+ * regardless of the strictness setting of the provided reader. The strictness setting
* of the reader is restored once this method returns.
*
* @throws JsonParseException if there is an IOException or if the specified
@@ -97,8 +97,8 @@ public static JsonElement parseReader(Reader reader) throws JsonIOException, Jso
*/
public static JsonElement parseReader(JsonReader reader)
throws JsonIOException, JsonSyntaxException {
- boolean lenient = reader.isLenient();
- reader.setLenient(true);
+ Strictness strictness = reader.getStrictness();
+ reader.setStrictness(Strictness.LENIENT);
try {
return Streams.parse(reader);
} catch (StackOverflowError e) {
@@ -106,7 +106,7 @@ public static JsonElement parseReader(JsonReader reader)
} catch (OutOfMemoryError e) {
throw new JsonParseException("Failed parsing JSON source: " + reader + " to Json", e);
} finally {
- reader.setLenient(lenient);
+ reader.setStrictness(strictness);
}
}
diff --git a/gson/src/main/java/com/google/gson/JsonStreamParser.java b/gson/src/main/java/com/google/gson/JsonStreamParser.java
index cbc2883ca8..7d2629368b 100644
--- a/gson/src/main/java/com/google/gson/JsonStreamParser.java
+++ b/gson/src/main/java/com/google/gson/JsonStreamParser.java
@@ -28,7 +28,7 @@
/**
* A streaming parser that allows reading of multiple {@link JsonElement}s from the specified reader
* asynchronously. The JSON data is parsed in lenient mode, see also
- * {@link JsonReader#setLenient(boolean)}.
+ * {@link JsonReader#setStrictness(Strictness)}.
*
* This class is conditionally thread-safe (see Item 70, Effective Java second edition). To
* properly use this class across multiple threads, you will need to add some external
@@ -66,7 +66,7 @@ public JsonStreamParser(String json) {
*/
public JsonStreamParser(Reader reader) {
parser = new JsonReader(reader);
- parser.setLenient(true);
+ parser.setStrictness(Strictness.LENIENT);
lock = new Object();
}
diff --git a/gson/src/main/java/com/google/gson/Strictness.java b/gson/src/main/java/com/google/gson/Strictness.java
index daf086a05f..f3bd3fe08f 100644
--- a/gson/src/main/java/com/google/gson/Strictness.java
+++ b/gson/src/main/java/com/google/gson/Strictness.java
@@ -1,17 +1,17 @@
package com.google.gson;
-import com.google.gson.stream.JsonWriter;
import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
/**
* Modes that indicate how strictly a JSON {@linkplain JsonReader reader} or
* {@linkplain JsonWriter writer} follows the syntax laid out in the
* RFC 8259 JSON specification.
*
- * You can look at {@link JsonWriter#setStrictness(Strictness)} to see how the strictness
- * affects the {@link JsonWriter} and you can look at
- * {@link JsonReader#setStrictness(Strictness)} to see how the strictness
- * affects the {@link JsonReader}. You can look at {@link JsonReader#setStrictness(Strictness)} to see how the strictness
+ * affects the {@link JsonReader} and you can look at
+ * {@link JsonWriter#setStrictness(Strictness)} to see how the strictness
+ * affects the {@link JsonWriter}. No exception is thrown if the JSON data has multiple top-level JSON elements,
* or if there is trailing data.
@@ -270,10 +271,10 @@ public final T fromJson(Reader in) throws IOException {
}
/**
- * Converts the JSON document in {@code json} to a Java object. Unlike Gson's
- * similar {@link Gson#fromJson(String, Class) fromJson} method, this read is
- * strict. Create a {@link JsonReader#setLenient(boolean) lenient} {@code
- * JsonReader} and call {@link #read(JsonReader)} for lenient reading.
+ * 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.
*
* No exception is thrown if the JSON data has multiple top-level JSON elements,
* or if there is trailing data.
diff --git a/gson/src/main/java/com/google/gson/stream/JsonReader.java b/gson/src/main/java/com/google/gson/stream/JsonReader.java
index 8678c8b291..dfa4b6e8f4 100644
--- a/gson/src/main/java/com/google/gson/stream/JsonReader.java
+++ b/gson/src/main/java/com/google/gson/stream/JsonReader.java
@@ -182,7 +182,7 @@
* Prefixing JSON files with
+ * Note that in all cases the old strictness setting of the reader will be restored when this method returns.
*
* @param
+ * Note that in all cases the old strictness setting of the reader will be restored when this method returns.
*
* @param
- *
*
* @author Inderjeet Singh
@@ -525,18 +529,19 @@ public GsonBuilder setFormattingStyle(FormattingStyle formattingStyle) {
/**
* Sets the strictness of this builder to {@link Strictness#LENIENT}.
*
- * ")]}'\n"
makes them non-executable
* by {@code