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

Strict mode for JSON parsing #2323

Merged
merged 51 commits into from May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
9b2d343
Feat #6: Add strict flag to Gson and GsonBuilder
marten-voorberg Feb 23, 2023
fe6dd37
Test #2: Add failing tests for capitalized keywords
marten-voorberg Feb 23, 2023
a78764a
Feat #2: JsonReader does not read (partially) capitalized keywords if…
marten-voorberg Feb 23, 2023
4895e8f
Feat #3: Added implementation and tests for JSONReader not accepting …
Feb 24, 2023
fd3f757
Test #3: Simplify test cases by removing unnecessary array
marten-voorberg Feb 24, 2023
0c28e0b
Feat #3: Improve error by including the illegal character
marten-voorberg Feb 24, 2023
947a312
Feat #5: JsonReader does not allow unespaced control flow characters …
marten-voorberg Feb 24, 2023
0f57cc9
Test #5: Test unespaced control flow characters in strict mode
marten-voorberg Feb 24, 2023
695159b
Feat #4: Disallow espaced newline character in strict mode
marten-voorberg Feb 24, 2023
bda7321
Test #4: Add tests for (dis)allowing newline character depensding on …
marten-voorberg Feb 24, 2023
d438cbd
Test #5: Test case for unescaped control char in non-strict mode
marten-voorberg Feb 24, 2023
23897d2
Test #2: Simplify test cases
marten-voorberg Feb 24, 2023
26463e7
Merge remote-tracking branch 'origin/master'
marten-voorberg Feb 24, 2023
d3950fa
Feat #13: Change leniency API to Strictness enum in JsonReader, Gson,…
marten-voorberg Feb 25, 2023
c303c61
Feat #15: Change JsonWriter API to also use Strictness
marten-voorberg Feb 27, 2023
2b55804
Test #15: Test Strictness in JsonWriter API
marten-voorberg Feb 27, 2023
d2a792b
Doc #15: Add and update documentation for Strictness in JsonWriter API
marten-voorberg Feb 27, 2023
0cd64da
refactor #12: Fixed typos and empty catch brackets in tests
gustajoh Feb 27, 2023
f2abd89
refactor #12: Resolved importing wildcards, made some lines adhere to…
gustajoh Feb 27, 2023
d506d00
#5 Add test case for unescaped control characters
gustajoh Feb 27, 2023
7319777
Doc #17: Add and change javadoc of public methods
marten-voorberg Feb 27, 2023
b29baa0
Test #34: Add tests for setting strictness through GsonBuilder
marten-voorberg Feb 27, 2023
0af517b
Fix: Add Fix broken test
marten-voorberg Feb 27, 2023
e44d1c3
Fix: Invalid JavaDoc in Gson.java
marten-voorberg Feb 27, 2023
15d7db6
Doc #17: update outdated javadoc
marten-voorberg Feb 27, 2023
75f9c8c
#37: Resolve more PR feedback
marten-voorberg Feb 28, 2023
a0a6c36
Refactor #35: Refactor JsonReader#peekKeyword to reduce the amount of…
marten-voorberg Feb 28, 2023
77dff43
Doc #40: Update JavaDoc based on PR feedback
marten-voorberg Mar 1, 2023
664bf1c
Doc #40: Update old RFC in GsonBuilder documentation
marten-voorberg Mar 1, 2023
5e64e3c
Doc #40: Fix formatting error in JavaDoc
marten-voorberg Mar 1, 2023
0d65c62
Doc #40: Add tests for setting strictness and lenient to JsonReaderTest
marten-voorberg Mar 1, 2023
2b812f1
Test #43: Changed tests to make use of assertThrows
gustajoh Mar 2, 2023
d7fff3b
doc #46: Resolved comments in main PR
gustajoh Mar 3, 2023
a821062
Feat #45: Change Gson.fromJson and Gson.toJson to be strict when the …
marten-voorberg Mar 3, 2023
ce04ac0
Fix #45: Small type
marten-voorberg Mar 3, 2023
7f7586d
Merge branch 'google:master' into master
marten-voorberg Mar 3, 2023
c0df07c
Update gson/src/test/java/com/google/gson/stream/JsonReaderTest.java
marten-voorberg Mar 7, 2023
8b63f54
Fix #45: Resolve various comments by Marcono1234
marten-voorberg Mar 7, 2023
eaa0ed5
Update gson/src/main/java/com/google/gson/GsonBuilder.java
marten-voorberg Mar 7, 2023
6a27a65
Fix #45: Resolve various comments by Marcono1234
marten-voorberg Mar 7, 2023
79295c8
Merge remote-tracking branch 'origin/master'
marten-voorberg Mar 7, 2023
d0a4065
Fix #45: Resolve various comments by eamonmcmanus
marten-voorberg Mar 7, 2023
8453738
Strictness mode follow-up
Marcono1234 Apr 8, 2023
8593b57
Update Troubleshooting.md and Gson default lenient mode documentation
Marcono1234 Apr 9, 2023
9a7f33a
Always use GSON strictness when set.
marten-voorberg Apr 18, 2023
4d0ea89
Rename Strictness.DEFAULT to Strictness.LEGACY_STRICT
marten-voorberg Apr 19, 2023
be59dd6
Update JavaDoc with new strictness functionality
marten-voorberg Apr 19, 2023
48bf90c
Replace default with legacy strict for JsonReader javadoc
marten-voorberg Apr 19, 2023
caaad5b
Add JSONReader test cases for U2028 and U2029
marten-voorberg May 13, 2023
39cd9a0
Refactor JSONReader#peekKeyWord() based on @eamonmcmanus's suggestion
marten-voorberg May 13, 2023
4342ef8
Deprecate setLenient in favor of setStrictness
marten-voorberg May 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Troubleshooting.md
Expand Up @@ -129,7 +129,7 @@ Notes:

**Solution:** See [`Gson` class documentation](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/Gson.html) section "Lenient JSON handling"

Note: Even in non-lenient mode Gson deviates slightly from the JSON specification, see [`JsonReader.setLenient`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/stream/JsonReader.html#setLenient(boolean)) for more details.
Note: Even in non-lenient mode Gson deviates slightly from the JSON specification, see [`JsonReader.setStrictness`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/stream/JsonReader.html#setStrictness(Strictness)) for more details.

## `IllegalStateException`: "Expected ... but was ..."

Expand Down
110 changes: 76 additions & 34 deletions gson/src/main/java/com/google/gson/Gson.java
Expand Up @@ -105,10 +105,15 @@
* <p>See the <a href="https://github.com/google/gson/blob/master/UserGuide.md">Gson User Guide</a>
* for a more complete set of examples.</p>
*
* <h2>Lenient JSON handling</h2>
* <h2>JSON Strictness handling</h2>
* For legacy reasons most of the {@code Gson} methods allow JSON data which does not
* comply with the JSON specification, regardless of whether {@link GsonBuilder#setLenient()}
* is used or not. If this behavior is not desired, the following workarounds can be used:
* comply with the JSON specification when the strictness is set to {@code null} (the default value).
* To specify the {@linkplain Strictness strictness} of a {@code Gson} instance, you should set it through
* {@link GsonBuilder#setStrictness(Strictness)}. If the strictness of a {@code Gson} instance is set to a not-null
* value, the strictness will always be enforced.
*
* <p>For older Gson versions which don't have the {@link Strictness} mode API the following
* workarounds can be used:
*
* <h3>Serialization</h3>
* <ol>
Expand Down Expand Up @@ -140,7 +145,7 @@
*/
public final class Gson {
static final boolean DEFAULT_JSON_NON_EXECUTABLE = false;
static final boolean DEFAULT_LENIENT = false;
static final Strictness DEFAULT_STRICTNESS = null;
static final FormattingStyle DEFAULT_FORMATTING_STYLE = null;
static final boolean DEFAULT_ESCAPE_HTML = true;
static final boolean DEFAULT_SERIALIZE_NULLS = false;
Expand Down Expand Up @@ -184,7 +189,7 @@ public final class Gson {
final boolean generateNonExecutableJson;
final boolean htmlSafe;
final FormattingStyle formattingStyle;
final boolean lenient;
final Strictness strictness;
final boolean serializeSpecialFloatingPointValues;
final boolean useJdkUnsafe;
final String datePattern;
Expand Down Expand Up @@ -231,13 +236,14 @@ public final class Gson {
* <li>By default, Gson excludes <code>transient</code> or <code>static</code> fields from
* consideration for serialization and deserialization. You can change this behavior through
* {@link GsonBuilder#excludeFieldsWithModifiers(int...)}.</li>
* <li>The strictness is set to {@code null}.</li>
* </ul>
*/
public Gson() {
this(Excluder.DEFAULT, DEFAULT_FIELD_NAMING_STRATEGY,
Collections.<Type, InstanceCreator<?>>emptyMap(), DEFAULT_SERIALIZE_NULLS,
DEFAULT_COMPLEX_MAP_KEYS, DEFAULT_JSON_NON_EXECUTABLE, DEFAULT_ESCAPE_HTML,
DEFAULT_FORMATTING_STYLE, DEFAULT_LENIENT, DEFAULT_SPECIALIZE_FLOAT_VALUES,
DEFAULT_FORMATTING_STYLE, DEFAULT_STRICTNESS, DEFAULT_SPECIALIZE_FLOAT_VALUES,
DEFAULT_USE_JDK_UNSAFE,
LongSerializationPolicy.DEFAULT, DEFAULT_DATE_PATTERN, DateFormat.DEFAULT, DateFormat.DEFAULT,
Collections.<TypeAdapterFactory>emptyList(), Collections.<TypeAdapterFactory>emptyList(),
Expand All @@ -248,7 +254,7 @@ public Gson() {
Gson(Excluder excluder, FieldNamingStrategy fieldNamingStrategy,
Map<Type, InstanceCreator<?>> instanceCreators, boolean serializeNulls,
boolean complexMapKeySerialization, boolean generateNonExecutableGson, boolean htmlSafe,
FormattingStyle formattingStyle, boolean lenient, boolean serializeSpecialFloatingPointValues,
FormattingStyle formattingStyle, Strictness strictness, boolean serializeSpecialFloatingPointValues,
boolean useJdkUnsafe,
LongSerializationPolicy longSerializationPolicy, String datePattern, int dateStyle,
int timeStyle, List<TypeAdapterFactory> builderFactories,
Expand All @@ -265,7 +271,7 @@ public Gson() {
this.generateNonExecutableJson = generateNonExecutableGson;
this.htmlSafe = htmlSafe;
this.formattingStyle = formattingStyle;
this.lenient = lenient;
this.strictness = strictness;
this.serializeSpecialFloatingPointValues = serializeSpecialFloatingPointValues;
this.useJdkUnsafe = useJdkUnsafe;
this.longSerializationPolicy = longSerializationPolicy;
Expand Down Expand Up @@ -822,24 +828,37 @@ 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}.
*
* <p>The JSON data is written in {@linkplain JsonWriter#setLenient(boolean) lenient mode},
* regardless of the lenient mode setting of the provided writer. The lenient mode setting
* of the writer is restored once this method returns.
<p> 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.
*
* <p>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.
*
* @throws JsonIOException if there was a problem writing to the writer
*/
public void toJson(Object src, Type typeOfSrc, JsonWriter writer) throws JsonIOException {
@SuppressWarnings("unchecked")
TypeAdapter<Object> adapter = (TypeAdapter<Object>) getAdapter(TypeToken.get(typeOfSrc));
boolean oldLenient = writer.isLenient();
writer.setLenient(true);

Strictness oldStrictness = writer.getStrictness();
if (this.strictness != null) {
writer.setStrictness(this.strictness);
} else if (writer.getStrictness() == Strictness.LEGACY_STRICT){
writer.setStrictness(Strictness.LENIENT);
}

boolean oldHtmlSafe = writer.isHtmlSafe();
writer.setHtmlSafe(htmlSafe);
boolean oldSerializeNulls = writer.getSerializeNulls();

writer.setHtmlSafe(htmlSafe);
writer.setSerializeNulls(serializeNulls);
try {
adapter.write(writer, src);
Expand All @@ -848,7 +867,7 @@ public void toJson(Object src, Type typeOfSrc, JsonWriter writer) throws JsonIOE
} catch (AssertionError e) {
throw new AssertionError("AssertionError (GSON " + GsonBuildConfig.VERSION + "): " + e.getMessage(), e);
} finally {
writer.setLenient(oldLenient);
writer.setStrictness(oldStrictness);
writer.setHtmlSafe(oldHtmlSafe);
writer.setSerializeNulls(oldSerializeNulls);
}
Expand Down Expand Up @@ -892,7 +911,9 @@ public void toJson(JsonElement jsonElement, Appendable writer) throws JsonIOExce
* <li>{@link GsonBuilder#disableHtmlEscaping()}</li>
* <li>{@link GsonBuilder#generateNonExecutableJson()}</li>
* <li>{@link GsonBuilder#serializeNulls()}</li>
* <li>{@link GsonBuilder#setLenient()}</li>
* <li>{@link GsonBuilder#setStrictness(Strictness)}. If the strictness of this {@code Gson} instance
* is set to {@code null}, the created writer will have a strictness of {@link Strictness#LEGACY_STRICT}.
* If the strictness is set to a non-null value, this strictness will be used for the created writer.</li>
* <li>{@link GsonBuilder#setPrettyPrinting()}</li>
* <li>{@link GsonBuilder#setPrettyPrinting(FormattingStyle)}</li>
* </ul>
Expand All @@ -904,7 +925,7 @@ public JsonWriter newJsonWriter(Writer writer) throws IOException {
JsonWriter jsonWriter = new JsonWriter(writer);
jsonWriter.setFormattingStyle(formattingStyle);
jsonWriter.setHtmlSafe(htmlSafe);
jsonWriter.setLenient(lenient);
jsonWriter.setStrictness(strictness == null ? Strictness.LEGACY_STRICT : strictness);
jsonWriter.setSerializeNulls(serializeNulls);
return jsonWriter;
}
Expand All @@ -914,43 +935,56 @@ public JsonWriter newJsonWriter(Writer writer) throws IOException {
*
* <p>The following settings are considered:
* <ul>
* <li>{@link GsonBuilder#setLenient()}</li>
* <li>{@link GsonBuilder#setStrictness(Strictness)}. If the strictness of this {@code Gson} instance
* is set to {@code null}, the created reader will have a strictness of {@link Strictness#LEGACY_STRICT}.
* If the strictness is set to a non-null value, this strictness will be used for the created reader.</li>
* </ul>
*/
public JsonReader newJsonReader(Reader reader) {
JsonReader jsonReader = new JsonReader(reader);
jsonReader.setLenient(lenient);
jsonReader.setStrictness(strictness == null ? Strictness.LEGACY_STRICT : strictness);
return jsonReader;
}

/**
* Writes the JSON for {@code jsonElement} to {@code writer}.
*
* <p>The JSON data is written in {@linkplain JsonWriter#setLenient(boolean) lenient mode},
* regardless of the lenient mode setting of the provided writer. The lenient mode setting
* of the writer is restored once this method returns.
* <p> 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.
*
* <p>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.
* @throws JsonIOException if there was a problem writing to the writer
*/
public void toJson(JsonElement jsonElement, JsonWriter writer) throws JsonIOException {
boolean oldLenient = writer.isLenient();
writer.setLenient(true);
Strictness oldStrictness = writer.getStrictness();
boolean oldHtmlSafe = writer.isHtmlSafe();
writer.setHtmlSafe(htmlSafe);
boolean oldSerializeNulls = writer.getSerializeNulls();

writer.setHtmlSafe(htmlSafe);
writer.setSerializeNulls(serializeNulls);

if (this.strictness != null) {
writer.setStrictness(this.strictness);
} else if (writer.getStrictness() == Strictness.LEGACY_STRICT) {
writer.setStrictness(Strictness.LENIENT);
}

try {
Streams.write(jsonElement, writer);
} catch (IOException e) {
throw new JsonIOException(e);
} catch (AssertionError e) {
throw new AssertionError("AssertionError (GSON " + GsonBuildConfig.VERSION + "): " + e.getMessage(), e);
} finally {
writer.setLenient(oldLenient);
writer.setStrictness(oldStrictness);
writer.setHtmlSafe(oldHtmlSafe);
writer.setSerializeNulls(oldSerializeNulls);
}
Expand Down Expand Up @@ -1169,8 +1203,8 @@ private static void assertFullConsumption(Object obj, JsonReader reader) {
* <p>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.
*
* <p>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
* <p>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.
*
* @param <T> the type of the desired object
Expand Down Expand Up @@ -1198,9 +1232,11 @@ public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, J
* <p>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.
*
* <p>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
* of the reader is restored once this method returns.
* <p> 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.
*
* @param <T> the type of the desired object
* @param reader the reader whose next JSON value should be deserialized
Expand All @@ -1220,8 +1256,14 @@ public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, J
*/
public <T> T fromJson(JsonReader reader, TypeToken<T> typeOfT) throws JsonIOException, JsonSyntaxException {
boolean isEmpty = true;
boolean oldLenient = reader.isLenient();
reader.setLenient(true);
Strictness oldStrictness = reader.getStrictness();

if (this.strictness != null) {
reader.setStrictness(this.strictness);
} else if (reader.getStrictness() == Strictness.LEGACY_STRICT){
reader.setStrictness(Strictness.LENIENT);
}

try {
reader.peek();
isEmpty = false;
Expand All @@ -1244,7 +1286,7 @@ public <T> T fromJson(JsonReader reader, TypeToken<T> typeOfT) throws JsonIOExce
} catch (AssertionError e) {
throw new AssertionError("AssertionError (GSON " + GsonBuildConfig.VERSION + "): " + e.getMessage(), e);
} finally {
reader.setLenient(oldLenient);
reader.setStrictness(oldStrictness);
}
}

Expand Down
46 changes: 34 additions & 12 deletions gson/src/main/java/com/google/gson/GsonBuilder.java
Expand Up @@ -21,11 +21,11 @@
import static com.google.gson.Gson.DEFAULT_ESCAPE_HTML;
import static com.google.gson.Gson.DEFAULT_FORMATTING_STYLE;
import static com.google.gson.Gson.DEFAULT_JSON_NON_EXECUTABLE;
import static com.google.gson.Gson.DEFAULT_LENIENT;
import static com.google.gson.Gson.DEFAULT_NUMBER_TO_NUMBER_STRATEGY;
import static com.google.gson.Gson.DEFAULT_OBJECT_TO_NUMBER_STRATEGY;
import static com.google.gson.Gson.DEFAULT_SERIALIZE_NULLS;
import static com.google.gson.Gson.DEFAULT_SPECIALIZE_FLOAT_VALUES;
import static com.google.gson.Gson.DEFAULT_STRICTNESS;
import static com.google.gson.Gson.DEFAULT_USE_JDK_UNSAFE;

import com.google.gson.annotations.Since;
Expand All @@ -50,6 +50,7 @@
import java.util.Map;
import java.util.Objects;


/**
* <p>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
Expand Down Expand Up @@ -100,7 +101,7 @@ public final class GsonBuilder {
private boolean escapeHtmlChars = DEFAULT_ESCAPE_HTML;
private FormattingStyle formattingStyle = DEFAULT_FORMATTING_STYLE;
private boolean generateNonExecutableJson = DEFAULT_JSON_NON_EXECUTABLE;
private boolean lenient = DEFAULT_LENIENT;
private Strictness strictness = DEFAULT_STRICTNESS;
private boolean useJdkUnsafe = DEFAULT_USE_JDK_UNSAFE;
private ToNumberStrategy objectToNumberStrategy = DEFAULT_OBJECT_TO_NUMBER_STRATEGY;
private ToNumberStrategy numberToNumberStrategy = DEFAULT_NUMBER_TO_NUMBER_STRATEGY;
Expand Down Expand Up @@ -130,7 +131,7 @@ public GsonBuilder() {
this.generateNonExecutableJson = gson.generateNonExecutableJson;
this.escapeHtmlChars = gson.htmlSafe;
this.formattingStyle = gson.formattingStyle;
this.lenient = gson.lenient;
this.strictness = gson.strictness;
this.serializeSpecialFloatingPointValues = gson.serializeSpecialFloatingPointValues;
this.longSerializationPolicy = gson.longSerializationPolicy;
this.datePattern = gson.datePattern;
Expand Down Expand Up @@ -502,17 +503,38 @@ public GsonBuilder setPrettyPrinting(FormattingStyle formattingStyle) {
}

/**
* Configures Gson to allow JSON data which does not strictly comply with the JSON specification.
* Sets the strictness of this builder to {@link Strictness#LENIENT}.
*
* <p>Note: Due to legacy reasons most methods of Gson are always lenient, regardless of
* whether this builder method is used.
* <p>This method has been deprecated. Please use {@link GsonBuilder#setStrictness(Strictness)} instead.
* Calling this method is equivalent to {@code setStrictness(Strictness.LENIENT)}</p>
*
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @see JsonReader#setLenient(boolean)
* @see JsonWriter#setLenient(boolean)
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern.
* @see JsonReader#setStrictness(Strictness)
* @see JsonWriter#setStrictness(Strictness)
* @see #setStrictness(Strictness)
*/
@Deprecated
public GsonBuilder setLenient() {
lenient = true;
strictness = Strictness.LENIENT;
return this;
}

/**
* Sets the strictness of this builder to the provided parameter.
*
* <p>This changes how strict the
* <a href="https://www.ietf.org/rfc/rfc8259.txt">RFC 8259 JSON specification</a> is enforced when parsing or
* writing JSON. For details on this, refer to {@link JsonReader#setStrictness(Strictness)} and
* {@link JsonWriter#setStrictness(Strictness)}.</p>
*
* @param strictness the new strictness mode. May not be {@code null}.
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern.
* @see JsonWriter#setStrictness(Strictness)
* @see JsonReader#setStrictness(Strictness)
* @since $next-version$
*/
public GsonBuilder setStrictness(Strictness strictness) {
this.strictness = Objects.requireNonNull(strictness);
return this;
}

Expand Down Expand Up @@ -684,7 +706,7 @@ public GsonBuilder registerTypeHierarchyAdapter(Class<?> baseType, Object typeAd
}

/**
* Section 2.4 of <a href="http://www.ietf.org/rfc/rfc4627.txt">JSON specification</a> disallows
* Section 6 of <a href="https://www.ietf.org/rfc/rfc8259.txt">JSON specification</a> disallows
* special double values (NaN, Infinity, -Infinity). However,
* <a href="http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf">Javascript
* specification</a> (see section 4.3.20, 4.3.22, 4.3.23) allows these values as valid Javascript
Expand Down Expand Up @@ -774,7 +796,7 @@ public Gson create() {

return new Gson(excluder, fieldNamingPolicy, new HashMap<>(instanceCreators),
serializeNulls, complexMapKeySerialization,
generateNonExecutableJson, escapeHtmlChars, formattingStyle, lenient,
generateNonExecutableJson, escapeHtmlChars, formattingStyle, strictness,
serializeSpecialFloatingPointValues, useJdkUnsafe, longSerializationPolicy,
datePattern, dateStyle, timeStyle, new ArrayList<>(this.factories),
new ArrayList<>(this.hierarchyFactories), factories,
Expand Down