Fix build
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Johannes Frohnmeyer 2023-09-19 20:14:48 +02:00
parent c46cbf85d4
commit 7d030fad60
Signed by: Johannes
GPG Key ID: E76429612C2929F4
45 changed files with 181 additions and 422 deletions

View File

@ -1,31 +0,0 @@
<!--
Thank you for your contribution!
Please see the contributing guide: https://github.com/google/.github/blob/master/CONTRIBUTING.md
Keep in mind that Gson is in maintenance mode. If you want to add a new feature, please first search for existing GitHub issues, or create a new one to discuss the feature and get feedback.
-->
### Purpose
<!-- Describe the purpose of this pull request, for example which new feature it adds or which bug it fixes -->
<!-- If this pull request closes a GitHub issue, please write "Closes #<issue>", for example "Closes #123" -->
### Description
<!-- If necessary provide more information, for example relevant implementation details or corner cases which are not covered yet -->
<!-- If there are related issues or pull requests, link to them by referencing their number, for example "pull request #123" -->
### Checklist
<!-- The following checklist is mainly intended for yourself to verify that you did not miss anything -->
- [ ] New code follows the [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html)
- [ ] If necessary, new public API validates arguments, for example rejects `null`
- [ ] New public API has Javadoc
- [ ] Javadoc uses `@since $next-version$`
(`$next-version$` is a special placeholder which is automatically replaced during release)
- [ ] If necessary, new unit tests have been added
- [ ] Assertions in unit tests use [Truth](https://truth.dev/), see existing tests
- [ ] No JUnit 3 features are used (such as extending class `TestCase`)
- [ ] If this pull request fixes a bug, a new test was added for a situation which failed previously and is now fixed
- [ ] `mvn clean verify javadoc:jar` passes without errors

View File

@ -1,29 +0,0 @@
# For security reasons this is a separate GitHub workflow, see https://github.com/google/gson/issues/2429#issuecomment-1622522842
# Once https://github.com/mojohaus/animal-sniffer/issues/252 or https://github.com/mojohaus/animal-sniffer/pull/253
# are resolved, can consider adjusting pom.xml to include this as part of normal Maven build
name: Check Android compatibility
on: [push, pull_request]
permissions:
contents: read # to fetch code (actions/checkout)
jobs:
check-android-compatibility:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '11'
cache: 'maven'
- name: Check Android compatibility
run: |
# Run 'test' phase because plugin normally expects to be executed after tests have been compiled
mvn --batch-mode --no-transfer-progress test animal-sniffer:check@check-android-compatibility -DskipTests

View File

@ -20,7 +20,7 @@
<parent> <parent>
<groupId>io.gitlab.jfronny</groupId> <groupId>io.gitlab.jfronny</groupId>
<artifactId>gson-parent</artifactId> <artifactId>gson-parent</artifactId>
<version>2.10.2-SNAPSHOT</version> <version>2.10.3-SNAPSHOT</version>
</parent> </parent>
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
@ -34,16 +34,12 @@
</licenses> </licenses>
<dependencies> <dependencies>
<!-- This dependency can be considered optional; omitting it during runtime will most likely <!--
not cause any issues. However, it is not declared with `<optional>true</optional>` because errorprone was removed due to JPMS:
that can lead to cryptic compiler warnings for consumers, and to be on the safe side in case It complained that the module didn't exist when included in module-info.java,
there are actually issues which could occur when the dependency is missing at runtime. otherwise it complained about it existing but not being referenced.
See also discussion at https://github.com/google/gson/pull/2320#issuecomment-1455233938 --> In other words, it just didn't work.
<dependency> -->
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_annotations</artifactId>
<version>2.21.1</version>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
@ -193,7 +189,7 @@
<plugin> <plugin>
<groupId>org.moditect</groupId> <groupId>org.moditect</groupId>
<artifactId>moditect-maven-plugin</artifactId> <artifactId>moditect-maven-plugin</artifactId>
<version>1.0.0.Final</version> <version>1.0.0.RC2</version>
<executions> <executions>
<execution> <execution>
<id>add-module-info</id> <id>add-module-info</id>

View File

@ -18,6 +18,7 @@ package com.google.gson;
import com.google.gson.annotations.JsonAdapter; import com.google.gson.annotations.JsonAdapter;
import com.google.gson.internal.ConstructorConstructor; import com.google.gson.internal.ConstructorConstructor;
import com.google.gson.internal.DefaultConfig;
import com.google.gson.internal.Excluder; import com.google.gson.internal.Excluder;
import com.google.gson.internal.GsonBuildConfig; import com.google.gson.internal.GsonBuildConfig;
import com.google.gson.internal.LazilyParsedNumber; import com.google.gson.internal.LazilyParsedNumber;
@ -235,7 +236,7 @@ public final class Gson {
this(Excluder.DEFAULT, DefaultConfig.DEFAULT_FIELD_NAMING_STRATEGY, this(Excluder.DEFAULT, DefaultConfig.DEFAULT_FIELD_NAMING_STRATEGY,
Collections.<Type, InstanceCreator<?>>emptyMap(), DefaultConfig.DEFAULT_SERIALIZE_NULLS, Collections.<Type, InstanceCreator<?>>emptyMap(), DefaultConfig.DEFAULT_SERIALIZE_NULLS,
DefaultConfig.DEFAULT_COMPLEX_MAP_KEYS, DefaultConfig.DEFAULT_DUPLICATE_MAP_KEYS, DefaultConfig.DEFAULT_JSON_NON_EXECUTABLE, DefaultConfig.DEFAULT_ESCAPE_HTML, DefaultConfig.DEFAULT_COMPLEX_MAP_KEYS, DefaultConfig.DEFAULT_DUPLICATE_MAP_KEYS, DefaultConfig.DEFAULT_JSON_NON_EXECUTABLE, DefaultConfig.DEFAULT_ESCAPE_HTML,
DefaultConfig.DEFAULT_FORMATTING_STYLE, DefaultConfig.DEFAULT_STRICTNESS, DefaultConfig.DEFAULT_OMIT_QUOTES, DefaultConfig.DEFAULT_SPECIALIZE_FLOAT_VALUES, DefaultConfig.DEFAULT_FORMATTING_STYLE, Strictness.LENIENT, DefaultConfig.DEFAULT_OMIT_QUOTES, DefaultConfig.DEFAULT_SPECIALIZE_FLOAT_VALUES,
DefaultConfig.DEFAULT_USE_JDK_UNSAFE, DefaultConfig.DEFAULT_USE_JDK_UNSAFE,
LongSerializationPolicy.DEFAULT, DefaultConfig.DEFAULT_DATE_PATTERN, DateFormat.DEFAULT, DateFormat.DEFAULT, LongSerializationPolicy.DEFAULT, DefaultConfig.DEFAULT_DATE_PATTERN, DateFormat.DEFAULT, DateFormat.DEFAULT,
Collections.<TypeAdapterFactory>emptyList(), Collections.<TypeAdapterFactory>emptyList(), Collections.<TypeAdapterFactory>emptyList(), Collections.<TypeAdapterFactory>emptyList(),

View File

@ -18,8 +18,6 @@ package com.google.gson;
import static com.google.gson.internal.DefaultConfig.*; import static com.google.gson.internal.DefaultConfig.*;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.InlineMe;
import com.google.gson.annotations.Since; import com.google.gson.annotations.Since;
import com.google.gson.annotations.Until; import com.google.gson.annotations.Until;
import com.google.gson.internal.$Gson$Preconditions; import com.google.gson.internal.$Gson$Preconditions;
@ -158,7 +156,6 @@ public final class GsonBuilder {
* @see Since * @see Since
* @see Until * @see Until
*/ */
@CanIgnoreReturnValue
public GsonBuilder setVersion(double version) { public GsonBuilder setVersion(double version) {
if (Double.isNaN(version) || version < 0.0) { if (Double.isNaN(version) || version < 0.0) {
throw new IllegalArgumentException("Invalid version: " + version); throw new IllegalArgumentException("Invalid version: " + version);
@ -181,7 +178,6 @@ public final class GsonBuilder {
* {@link java.lang.reflect.Modifier#STATIC}. * {@link java.lang.reflect.Modifier#STATIC}.
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
*/ */
@CanIgnoreReturnValue
public GsonBuilder excludeFieldsWithModifiers(int... modifiers) { public GsonBuilder excludeFieldsWithModifiers(int... modifiers) {
Objects.requireNonNull(modifiers); Objects.requireNonNull(modifiers);
excluder = excluder.withModifiers(modifiers); excluder = excluder.withModifiers(modifiers);
@ -197,7 +193,6 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.3 * @since 1.3
*/ */
@CanIgnoreReturnValue
public GsonBuilder generateNonExecutableJson() { public GsonBuilder generateNonExecutableJson() {
this.generateNonExecutableJson = true; this.generateNonExecutableJson = true;
return this; return this;
@ -212,7 +207,6 @@ public final class GsonBuilder {
* *
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
*/ */
@CanIgnoreReturnValue
public GsonBuilder excludeFieldsWithoutExposeAnnotation() { public GsonBuilder excludeFieldsWithoutExposeAnnotation() {
excluder = excluder.excludeFieldsWithoutExposeAnnotation(); excluder = excluder.excludeFieldsWithoutExposeAnnotation();
return this; return this;
@ -225,7 +219,6 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.2 * @since 1.2
*/ */
@CanIgnoreReturnValue
public GsonBuilder serializeNulls() { public GsonBuilder serializeNulls() {
this.serializeNulls = true; this.serializeNulls = true;
return this; return this;
@ -310,7 +303,6 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.7 * @since 1.7
*/ */
@CanIgnoreReturnValue
public GsonBuilder enableComplexMapKeySerialization() { public GsonBuilder enableComplexMapKeySerialization() {
complexMapKeySerialization = true; complexMapKeySerialization = true;
return this; return this;
@ -351,7 +343,6 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.3 * @since 1.3
*/ */
@CanIgnoreReturnValue
public GsonBuilder disableInnerClassSerialization() { public GsonBuilder disableInnerClassSerialization() {
excluder = excluder.disableInnerClassSerialization(); excluder = excluder.disableInnerClassSerialization();
return this; return this;
@ -365,7 +356,6 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.3 * @since 1.3
*/ */
@CanIgnoreReturnValue
public GsonBuilder setLongSerializationPolicy(LongSerializationPolicy serializationPolicy) { public GsonBuilder setLongSerializationPolicy(LongSerializationPolicy serializationPolicy) {
this.longSerializationPolicy = Objects.requireNonNull(serializationPolicy); this.longSerializationPolicy = Objects.requireNonNull(serializationPolicy);
return this; return this;
@ -377,7 +367,6 @@ public final class GsonBuilder {
* *
* <p>This method just delegates to {@link #setFieldNamingStrategy(FieldNamingStrategy)}. * <p>This method just delegates to {@link #setFieldNamingStrategy(FieldNamingStrategy)}.
*/ */
@CanIgnoreReturnValue
public GsonBuilder setFieldNamingPolicy(FieldNamingPolicy namingConvention) { public GsonBuilder setFieldNamingPolicy(FieldNamingPolicy namingConvention) {
return setFieldNamingStrategy(namingConvention); return setFieldNamingStrategy(namingConvention);
} }
@ -394,7 +383,6 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.3 * @since 1.3
*/ */
@CanIgnoreReturnValue
public GsonBuilder setFieldNamingStrategy(FieldNamingStrategy fieldNamingStrategy) { public GsonBuilder setFieldNamingStrategy(FieldNamingStrategy fieldNamingStrategy) {
this.fieldNamingPolicy = Objects.requireNonNull(fieldNamingStrategy); this.fieldNamingPolicy = Objects.requireNonNull(fieldNamingStrategy);
return this; return this;
@ -408,7 +396,6 @@ public final class GsonBuilder {
* @see ToNumberPolicy#DOUBLE The default object-to-number strategy * @see ToNumberPolicy#DOUBLE The default object-to-number strategy
* @since 2.8.9 * @since 2.8.9
*/ */
@CanIgnoreReturnValue
public GsonBuilder setObjectToNumberStrategy(ToNumberStrategy objectToNumberStrategy) { public GsonBuilder setObjectToNumberStrategy(ToNumberStrategy objectToNumberStrategy) {
this.objectToNumberStrategy = Objects.requireNonNull(objectToNumberStrategy); this.objectToNumberStrategy = Objects.requireNonNull(objectToNumberStrategy);
return this; return this;
@ -422,7 +409,6 @@ public final class GsonBuilder {
* @see ToNumberPolicy#LAZILY_PARSED_NUMBER The default number-to-number strategy * @see ToNumberPolicy#LAZILY_PARSED_NUMBER The default number-to-number strategy
* @since 2.8.9 * @since 2.8.9
*/ */
@CanIgnoreReturnValue
public GsonBuilder setNumberToNumberStrategy(ToNumberStrategy numberToNumberStrategy) { public GsonBuilder setNumberToNumberStrategy(ToNumberStrategy numberToNumberStrategy) {
this.numberToNumberStrategy = Objects.requireNonNull(numberToNumberStrategy); this.numberToNumberStrategy = Objects.requireNonNull(numberToNumberStrategy);
return this; return this;
@ -454,7 +440,6 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.4 * @since 1.4
*/ */
@CanIgnoreReturnValue
public GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies) { public GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies) {
Objects.requireNonNull(strategies); Objects.requireNonNull(strategies);
for (ExclusionStrategy strategy : strategies) { for (ExclusionStrategy strategy : strategies) {
@ -478,7 +463,6 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.7 * @since 1.7
*/ */
@CanIgnoreReturnValue
public GsonBuilder addSerializationExclusionStrategy(ExclusionStrategy strategy) { public GsonBuilder addSerializationExclusionStrategy(ExclusionStrategy strategy) {
Objects.requireNonNull(strategy); Objects.requireNonNull(strategy);
excluder = excluder.withExclusionStrategy(strategy, true, false); excluder = excluder.withExclusionStrategy(strategy, true, false);
@ -500,7 +484,6 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.7 * @since 1.7
*/ */
@CanIgnoreReturnValue
public GsonBuilder addDeserializationExclusionStrategy(ExclusionStrategy strategy) { public GsonBuilder addDeserializationExclusionStrategy(ExclusionStrategy strategy) {
Objects.requireNonNull(strategy); Objects.requireNonNull(strategy);
excluder = excluder.withExclusionStrategy(strategy, false, true); excluder = excluder.withExclusionStrategy(strategy, false, true);
@ -516,7 +499,6 @@ public final class GsonBuilder {
* *
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
*/ */
@CanIgnoreReturnValue
public GsonBuilder setPrettyPrinting() { public GsonBuilder setPrettyPrinting() {
return setFormattingStyle(FormattingStyle.PRETTY); return setFormattingStyle(FormattingStyle.PRETTY);
} }
@ -529,7 +511,6 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since $next-version$ * @since $next-version$
*/ */
@CanIgnoreReturnValue
public GsonBuilder setFormattingStyle(FormattingStyle formattingStyle) { public GsonBuilder setFormattingStyle(FormattingStyle formattingStyle) {
this.formattingStyle = Objects.requireNonNull(formattingStyle); this.formattingStyle = Objects.requireNonNull(formattingStyle);
return this; return this;
@ -547,8 +528,6 @@ public final class GsonBuilder {
* @see #setStrictness(Strictness) * @see #setStrictness(Strictness)
*/ */
@Deprecated @Deprecated
@InlineMe(replacement = "this.setStrictness(Strictness.LENIENT)", imports = "com.google.gson.Strictness")
@CanIgnoreReturnValue
public GsonBuilder setLenient() { public GsonBuilder setLenient() {
return setStrictness(Strictness.LENIENT); return setStrictness(Strictness.LENIENT);
} }
@ -567,7 +546,6 @@ public final class GsonBuilder {
* @see JsonWriter#setStrictness(Strictness) * @see JsonWriter#setStrictness(Strictness)
* @since $next-version$ * @since $next-version$
*/ */
@CanIgnoreReturnValue
public GsonBuilder setStrictness(Strictness strictness) { public GsonBuilder setStrictness(Strictness strictness) {
this.strictness = Objects.requireNonNull(strictness); this.strictness = Objects.requireNonNull(strictness);
return this; return this;
@ -589,7 +567,6 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.3 * @since 1.3
*/ */
@CanIgnoreReturnValue
public GsonBuilder disableHtmlEscaping() { public GsonBuilder disableHtmlEscaping() {
this.escapeHtmlChars = false; this.escapeHtmlChars = false;
return this; return this;
@ -611,7 +588,6 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.2 * @since 1.2
*/ */
@CanIgnoreReturnValue
public GsonBuilder setDateFormat(String pattern) { public GsonBuilder setDateFormat(String pattern) {
// TODO(Joel): Make this fail fast if it is an invalid date format // TODO(Joel): Make this fail fast if it is an invalid date format
this.datePattern = pattern; this.datePattern = pattern;
@ -632,7 +608,6 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.2 * @since 1.2
*/ */
@CanIgnoreReturnValue
public GsonBuilder setDateFormat(int style) { public GsonBuilder setDateFormat(int style) {
this.dateStyle = style; this.dateStyle = style;
this.datePattern = null; this.datePattern = null;
@ -654,7 +629,6 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.2 * @since 1.2
*/ */
@CanIgnoreReturnValue
public GsonBuilder setDateFormat(int dateStyle, int timeStyle) { public GsonBuilder setDateFormat(int dateStyle, int timeStyle) {
this.dateStyle = dateStyle; this.dateStyle = dateStyle;
this.timeStyle = timeStyle; this.timeStyle = timeStyle;
@ -684,7 +658,6 @@ public final class GsonBuilder {
* {@link InstanceCreator}, {@link JsonSerializer}, and a {@link JsonDeserializer} interfaces. * {@link InstanceCreator}, {@link JsonSerializer}, and a {@link JsonDeserializer} interfaces.
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
*/ */
@CanIgnoreReturnValue
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) { public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
Objects.requireNonNull(type); Objects.requireNonNull(type);
$Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?> $Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
@ -718,7 +691,6 @@ public final class GsonBuilder {
* *
* @since 2.1 * @since 2.1
*/ */
@CanIgnoreReturnValue
public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) { public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) {
Objects.requireNonNull(factory); Objects.requireNonNull(factory);
factories.add(factory); factories.add(factory);
@ -739,7 +711,6 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.7 * @since 1.7
*/ */
@CanIgnoreReturnValue
public GsonBuilder registerTypeHierarchyAdapter(Class<?> baseType, Object typeAdapter) { public GsonBuilder registerTypeHierarchyAdapter(Class<?> baseType, Object typeAdapter) {
Objects.requireNonNull(baseType); Objects.requireNonNull(baseType);
$Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?> $Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
@ -776,7 +747,6 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.3 * @since 1.3
*/ */
@CanIgnoreReturnValue
public GsonBuilder serializeSpecialFloatingPointValues() { public GsonBuilder serializeSpecialFloatingPointValues() {
this.serializeSpecialFloatingPointValues = true; this.serializeSpecialFloatingPointValues = true;
return this; return this;
@ -798,7 +768,6 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 2.9.0 * @since 2.9.0
*/ */
@CanIgnoreReturnValue
public GsonBuilder disableJdkUnsafe() { public GsonBuilder disableJdkUnsafe() {
this.useJdkUnsafe = false; this.useJdkUnsafe = false;
return this; return this;
@ -824,7 +793,6 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 2.9.1 * @since 2.9.1
*/ */
@CanIgnoreReturnValue
public GsonBuilder addReflectionAccessFilter(ReflectionAccessFilter filter) { public GsonBuilder addReflectionAccessFilter(ReflectionAccessFilter filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
reflectionFilters.addFirst(filter); reflectionFilters.addFirst(filter);

View File

@ -16,7 +16,6 @@
package com.google.gson; package com.google.gson;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.internal.NonNullElementWrapperList; import com.google.gson.internal.NonNullElementWrapperList;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
@ -146,7 +145,6 @@ public final class JsonArray extends JsonElement implements Iterable<JsonElement
* @return the element previously at the specified position * @return the element previously at the specified position
* @throws IndexOutOfBoundsException if the specified index is outside the array bounds * @throws IndexOutOfBoundsException if the specified index is outside the array bounds
*/ */
@CanIgnoreReturnValue
public JsonElement set(int index, JsonElement element) { public JsonElement set(int index, JsonElement element) {
return elements.set(index, element == null ? JsonNull.INSTANCE : element); return elements.set(index, element == null ? JsonNull.INSTANCE : element);
} }
@ -159,7 +157,6 @@ public final class JsonArray extends JsonElement implements Iterable<JsonElement
* @return true if this array contained the specified element, false otherwise * @return true if this array contained the specified element, false otherwise
* @since 2.3 * @since 2.3
*/ */
@CanIgnoreReturnValue
public boolean remove(JsonElement element) { public boolean remove(JsonElement element) {
return elements.remove(element); return elements.remove(element);
} }
@ -174,7 +171,6 @@ public final class JsonArray extends JsonElement implements Iterable<JsonElement
* @throws IndexOutOfBoundsException if the specified index is outside the array bounds * @throws IndexOutOfBoundsException if the specified index is outside the array bounds
* @since 2.3 * @since 2.3
*/ */
@CanIgnoreReturnValue
public JsonElement remove(int index) { public JsonElement remove(int index) {
return elements.remove(index); return elements.remove(index);
} }

View File

@ -16,7 +16,6 @@
package com.google.gson; package com.google.gson;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.internal.Streams; import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
import java.io.IOException; import java.io.IOException;
@ -144,7 +143,6 @@ public abstract class JsonElement {
* @throws IllegalStateException if this element is of another type. * @throws IllegalStateException if this element is of another type.
* @since 1.2 * @since 1.2
*/ */
@CanIgnoreReturnValue // When this method is used only to verify that the value is JsonNull
public JsonNull getAsJsonNull() { public JsonNull getAsJsonNull() {
if (isJsonNull()) { if (isJsonNull()) {
return (JsonNull) this; return (JsonNull) this;

View File

@ -16,7 +16,6 @@
package com.google.gson; package com.google.gson;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.internal.LinkedTreeMap; import com.google.gson.internal.LinkedTreeMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -78,7 +77,6 @@ public final class JsonObject extends JsonElement {
* member with this name exists. * member with this name exists.
* @since 1.3 * @since 1.3
*/ */
@CanIgnoreReturnValue
public JsonElement remove(String property) { public JsonElement remove(String property) {
return members.remove(property); return members.remove(property);
} }

View File

@ -15,7 +15,6 @@
*/ */
package com.google.gson; package com.google.gson;
import com.google.errorprone.annotations.InlineMe;
import com.google.gson.internal.Streams; import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonToken;
@ -112,21 +111,18 @@ public final class JsonParser {
/** @deprecated Use {@link JsonParser#parseString} */ /** @deprecated Use {@link JsonParser#parseString} */
@Deprecated @Deprecated
@InlineMe(replacement = "JsonParser.parseString(json)", imports = "com.google.gson.JsonParser")
public JsonElement parse(String json) throws JsonSyntaxException { public JsonElement parse(String json) throws JsonSyntaxException {
return parseString(json); return parseString(json);
} }
/** @deprecated Use {@link JsonParser#parseReader(Reader)} */ /** @deprecated Use {@link JsonParser#parseReader(Reader)} */
@Deprecated @Deprecated
@InlineMe(replacement = "JsonParser.parseReader(json)", imports = "com.google.gson.JsonParser")
public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException { public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException {
return parseReader(json); return parseReader(json);
} }
/** @deprecated Use {@link JsonParser#parseReader(JsonReader)} */ /** @deprecated Use {@link JsonParser#parseReader(JsonReader)} */
@Deprecated @Deprecated
@InlineMe(replacement = "JsonParser.parseReader(json)", imports = "com.google.gson.JsonParser")
public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException { public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException {
return parseReader(json); return parseReader(json);
} }

View File

@ -5,9 +5,9 @@ import com.google.gson.reflect.*;
public class DefaultConfig { public class DefaultConfig {
public static final boolean DEFAULT_JSON_NON_EXECUTABLE = false; public static final boolean DEFAULT_JSON_NON_EXECUTABLE = false;
public static final boolean DEFAULT_LENIENT = false; public static final Strictness DEFAULT_STRICTNESS = Strictness.LEGACY_STRICT;
public static final boolean DEFAULT_OMIT_QUOTES = false; public static final boolean DEFAULT_OMIT_QUOTES = false;
public static final FormattingStyle DEFAULT_FORMATTING_STYLE = null; public static final FormattingStyle DEFAULT_FORMATTING_STYLE = FormattingStyle.COMPACT;
public static final boolean DEFAULT_ESCAPE_HTML = true; public static final boolean DEFAULT_ESCAPE_HTML = true;
public static final boolean DEFAULT_SERIALIZE_NULLS = false; public static final boolean DEFAULT_SERIALIZE_NULLS = false;
public static final boolean DEFAULT_COMPLEX_MAP_KEYS = false; public static final boolean DEFAULT_COMPLEX_MAP_KEYS = false;

View File

@ -17,7 +17,6 @@
package com.google.gson.internal; package com.google.gson.internal;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.IOException; import java.io.IOException;
import java.io.InvalidObjectException; import java.io.InvalidObjectException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
@ -108,7 +107,6 @@ public final class LinkedTreeMap<K, V> extends AbstractMap<K, V> implements Seri
return findByObject(key) != null; return findByObject(key) != null;
} }
@CanIgnoreReturnValue
@Override public V put(K key, V value) { @Override public V put(K key, V value) {
if (key == null) { if (key == null) {
throw new NullPointerException("key == null"); throw new NullPointerException("key == null");

View File

@ -17,6 +17,7 @@
package com.google.gson.internal.bind; package com.google.gson.internal.bind;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.Strictness;
import com.google.gson.TypeAdapter; import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory; import com.google.gson.TypeAdapterFactory;
import com.google.gson.internal.$Gson$Types; import com.google.gson.internal.$Gson$Types;
@ -65,7 +66,7 @@ public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
in.nextNull(); in.nextNull();
return null; return null;
} }
if (in.isLenient() && in.peek() != JsonToken.BEGIN_ARRAY) { if (in.getStrictness() == Strictness.LENIENT && in.peek() != JsonToken.BEGIN_ARRAY) {
// Coerce // Coerce
Object array = Array.newInstance(componentType, 1); Object array = Array.newInstance(componentType, 1);
Array.set(array, 0, componentTypeAdapter.read(in)); Array.set(array, 0, componentTypeAdapter.read(in));

View File

@ -17,6 +17,7 @@
package com.google.gson.internal.bind; package com.google.gson.internal.bind;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.Strictness;
import com.google.gson.TypeAdapter; import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory; import com.google.gson.TypeAdapterFactory;
import com.google.gson.internal.$Gson$Types; import com.google.gson.internal.$Gson$Types;
@ -77,7 +78,7 @@ public final class CollectionTypeAdapterFactory implements TypeAdapterFactory {
} }
Collection<E> collection = constructor.construct(); Collection<E> collection = constructor.construct();
if (!in.isLenient() || in.peek() == JsonToken.BEGIN_ARRAY) { if (in.getStrictness() != Strictness.LENIENT || in.peek() == JsonToken.BEGIN_ARRAY) {
in.beginArray(); in.beginArray();
while (in.hasNext()) { while (in.hasNext()) {
E instance = elementTypeAdapter.read(in); E instance = elementTypeAdapter.read(in);

View File

@ -524,12 +524,8 @@ public final class TypeAdapters {
public static final TypeAdapter<URL> URL = new TypeAdapter<URL>() { public static final TypeAdapter<URL> URL = new TypeAdapter<URL>() {
@Override @Override
public URL read(JsonReader in) throws IOException { public URL read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) { URI uri = URI.read(in);
in.nextNull(); return uri == null ? null : uri.toURL();
return null;
}
String nextString = in.nextString();
return "null".equals(nextString) ? null : new URL(nextString);
} }
@Override @Override
public void write(JsonWriter out, URL value) throws IOException { public void write(JsonWriter out, URL value) throws IOException {

View File

@ -19,9 +19,9 @@ package com.google.gson.stream;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.Strictness; import com.google.gson.Strictness;
import com.google.gson.internal.DefaultConfig;
import com.google.gson.internal.JsonReaderInternalAccess; import com.google.gson.internal.JsonReaderInternalAccess;
import com.google.gson.internal.TroubleshootingGuide; import com.google.gson.internal.TroubleshootingGuide;
import com.google.gson.internal.bind.JsonTreeReader;
import java.io.Closeable; import java.io.Closeable;
import java.io.EOFException; import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
@ -391,6 +391,7 @@ public class JsonReader implements Closeable {
public final void setStrictness(Strictness strictness) { public final void setStrictness(Strictness strictness) {
Objects.requireNonNull(strictness); Objects.requireNonNull(strictness);
this.strictness = strictness; this.strictness = strictness;
if (strictness == Strictness.LENIENT) setSerializeSpecialFloatingPointValues(true);
} }
/** /**

View File

@ -16,7 +16,6 @@
package com.google.gson.stream; package com.google.gson.stream;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonNull; import com.google.gson.JsonNull;
@ -154,7 +153,6 @@ public final class JsonTreeReader extends JsonReader {
return stack[stackSize - 1]; return stack[stackSize - 1];
} }
@CanIgnoreReturnValue
private Object popStack() { private Object popStack() {
Object result = stack[--stackSize]; Object result = stack[--stackSize];
stack[stackSize] = null; stack[stackSize] = null;

View File

@ -16,7 +16,6 @@
package com.google.gson.stream; package com.google.gson.stream;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonNull; import com.google.gson.JsonNull;
@ -98,7 +97,6 @@ public final class JsonTreeWriter extends JsonWriter {
} }
} }
@CanIgnoreReturnValue
@Override public JsonWriter beginArray() throws IOException { @Override public JsonWriter beginArray() throws IOException {
JsonArray array = new JsonArray(); JsonArray array = new JsonArray();
put(array); put(array);
@ -106,7 +104,6 @@ public final class JsonTreeWriter extends JsonWriter {
return this; return this;
} }
@CanIgnoreReturnValue
@Override public JsonWriter endArray() throws IOException { @Override public JsonWriter endArray() throws IOException {
if (stack.isEmpty() || pendingName != null) { if (stack.isEmpty() || pendingName != null) {
throw new IllegalStateException(); throw new IllegalStateException();
@ -119,7 +116,6 @@ public final class JsonTreeWriter extends JsonWriter {
throw new IllegalStateException(); throw new IllegalStateException();
} }
@CanIgnoreReturnValue
@Override public JsonWriter beginObject() throws IOException { @Override public JsonWriter beginObject() throws IOException {
JsonObject object = new JsonObject(); JsonObject object = new JsonObject();
put(object); put(object);
@ -127,7 +123,6 @@ public final class JsonTreeWriter extends JsonWriter {
return this; return this;
} }
@CanIgnoreReturnValue
@Override public JsonWriter endObject() throws IOException { @Override public JsonWriter endObject() throws IOException {
if (stack.isEmpty() || pendingName != null) { if (stack.isEmpty() || pendingName != null) {
throw new IllegalStateException(); throw new IllegalStateException();
@ -140,7 +135,6 @@ public final class JsonTreeWriter extends JsonWriter {
throw new IllegalStateException(); throw new IllegalStateException();
} }
@CanIgnoreReturnValue
@Override public JsonWriter name(String name) throws IOException { @Override public JsonWriter name(String name) throws IOException {
Objects.requireNonNull(name, "name == null"); Objects.requireNonNull(name, "name == null");
if (stack.isEmpty() || pendingName != null) { if (stack.isEmpty() || pendingName != null) {
@ -154,7 +148,6 @@ public final class JsonTreeWriter extends JsonWriter {
throw new IllegalStateException("Please begin an object before writing a name."); throw new IllegalStateException("Please begin an object before writing a name.");
} }
@CanIgnoreReturnValue
@Override public JsonWriter value(String value) throws IOException { @Override public JsonWriter value(String value) throws IOException {
if (value == null) { if (value == null) {
return nullValue(); return nullValue();
@ -167,19 +160,16 @@ public final class JsonTreeWriter extends JsonWriter {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@CanIgnoreReturnValue
@Override public JsonWriter nullValue() throws IOException { @Override public JsonWriter nullValue() throws IOException {
put(JsonNull.INSTANCE); put(JsonNull.INSTANCE);
return this; return this;
} }
@CanIgnoreReturnValue
@Override public JsonWriter value(boolean value) throws IOException { @Override public JsonWriter value(boolean value) throws IOException {
put(new JsonPrimitive(value)); put(new JsonPrimitive(value));
return this; return this;
} }
@CanIgnoreReturnValue
@Override public JsonWriter value(Boolean value) throws IOException { @Override public JsonWriter value(Boolean value) throws IOException {
if (value == null) { if (value == null) {
return nullValue(); return nullValue();
@ -188,7 +178,6 @@ public final class JsonTreeWriter extends JsonWriter {
return this; return this;
} }
@CanIgnoreReturnValue
@Override public JsonWriter value(float value) throws IOException { @Override public JsonWriter value(float value) throws IOException {
if (!isLenient() && (Float.isNaN(value) || Float.isInfinite(value))) { if (!isLenient() && (Float.isNaN(value) || Float.isInfinite(value))) {
throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value); throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value);
@ -197,7 +186,6 @@ public final class JsonTreeWriter extends JsonWriter {
return this; return this;
} }
@CanIgnoreReturnValue
@Override public JsonWriter value(double value) throws IOException { @Override public JsonWriter value(double value) throws IOException {
if (!isLenient() && (Double.isNaN(value) || Double.isInfinite(value))) { if (!isLenient() && (Double.isNaN(value) || Double.isInfinite(value))) {
throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value); throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value);
@ -206,13 +194,11 @@ public final class JsonTreeWriter extends JsonWriter {
return this; return this;
} }
@CanIgnoreReturnValue
@Override public JsonWriter value(long value) throws IOException { @Override public JsonWriter value(long value) throws IOException {
put(new JsonPrimitive(value)); put(new JsonPrimitive(value));
return this; return this;
} }
@CanIgnoreReturnValue
@Override public JsonWriter value(Number value) throws IOException { @Override public JsonWriter value(Number value) throws IOException {
if (value == null) { if (value == null) {
return nullValue(); return nullValue();

View File

@ -26,7 +26,6 @@ import static com.google.gson.stream.JsonScope.NONEMPTY_OBJECT;
import com.google.gson.internal.*; import com.google.gson.internal.*;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.FormattingStyle; import com.google.gson.FormattingStyle;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
@ -415,7 +414,6 @@ public class JsonWriter implements Closeable, Flushable {
* *
* @return this writer. * @return this writer.
*/ */
@CanIgnoreReturnValue
public JsonWriter beginArray() throws IOException { public JsonWriter beginArray() throws IOException {
writeDeferredName(); writeDeferredName();
return open(EMPTY_ARRAY, '['); return open(EMPTY_ARRAY, '[');
@ -426,7 +424,6 @@ public class JsonWriter implements Closeable, Flushable {
* *
* @return this writer. * @return this writer.
*/ */
@CanIgnoreReturnValue
public JsonWriter endArray() throws IOException { public JsonWriter endArray() throws IOException {
return close(EMPTY_ARRAY, NONEMPTY_ARRAY, ']'); return close(EMPTY_ARRAY, NONEMPTY_ARRAY, ']');
} }
@ -437,7 +434,6 @@ public class JsonWriter implements Closeable, Flushable {
* *
* @return this writer. * @return this writer.
*/ */
@CanIgnoreReturnValue
public JsonWriter beginObject() throws IOException { public JsonWriter beginObject() throws IOException {
writeDeferredName(); writeDeferredName();
return open(EMPTY_OBJECT, '{'); return open(EMPTY_OBJECT, '{');
@ -448,7 +444,6 @@ public class JsonWriter implements Closeable, Flushable {
* *
* @return this writer. * @return this writer.
*/ */
@CanIgnoreReturnValue
public JsonWriter endObject() throws IOException { public JsonWriter endObject() throws IOException {
return close(EMPTY_OBJECT, NONEMPTY_OBJECT, '}'); return close(EMPTY_OBJECT, NONEMPTY_OBJECT, '}');
} }
@ -457,7 +452,6 @@ public class JsonWriter implements Closeable, Flushable {
* Enters a new scope by appending any necessary whitespace and the given * Enters a new scope by appending any necessary whitespace and the given
* bracket. * bracket.
*/ */
@CanIgnoreReturnValue
private JsonWriter open(int empty, char openBracket) throws IOException { private JsonWriter open(int empty, char openBracket) throws IOException {
beforeValue(); beforeValue();
push(empty); push(empty);
@ -469,7 +463,6 @@ public class JsonWriter implements Closeable, Flushable {
* Closes the current scope by appending any necessary whitespace and the * Closes the current scope by appending any necessary whitespace and the
* given bracket. * given bracket.
*/ */
@CanIgnoreReturnValue
private JsonWriter close(int empty, int nonempty, char closeBracket) private JsonWriter close(int empty, int nonempty, char closeBracket)
throws IOException { throws IOException {
int context = peek(); int context = peek();
@ -524,7 +517,7 @@ public class JsonWriter implements Closeable, Flushable {
* This writer MUST be lenient to use this * This writer MUST be lenient to use this
*/ */
public JsonWriter comment(String comment) throws IOException { public JsonWriter comment(String comment) throws IOException {
if (!lenient) throw new MalformedJsonException("Cannot write comment in non-lenient JsonWriter."); if (strictness != Strictness.LENIENT) throw new MalformedJsonException("Cannot write comment in non-lenient JsonWriter.");
if (comment == null || comment.isBlank()) return this; if (comment == null || comment.isBlank()) return this;
String[] parts = comment.split("\n"); String[] parts = comment.split("\n");
Collections.addAll(deferredComment, parts); Collections.addAll(deferredComment, parts);
@ -557,7 +550,6 @@ public class JsonWriter implements Closeable, Flushable {
* @param name the name of the forthcoming value. May not be {@code null}. * @param name the name of the forthcoming value. May not be {@code null}.
* @return this writer. * @return this writer.
*/ */
@CanIgnoreReturnValue
public JsonWriter name(String name) throws IOException { public JsonWriter name(String name) throws IOException {
Objects.requireNonNull(name, "name == null"); Objects.requireNonNull(name, "name == null");
if (deferredName != null) { if (deferredName != null) {
@ -590,7 +582,6 @@ public class JsonWriter implements Closeable, Flushable {
* @param value the literal string value, or null to encode a null literal. * @param value the literal string value, or null to encode a null literal.
* @return this writer. * @return this writer.
*/ */
@CanIgnoreReturnValue
public JsonWriter value(String value) throws IOException { public JsonWriter value(String value) throws IOException {
if (value == null) { if (value == null) {
return nullValue(); return nullValue();
@ -612,7 +603,6 @@ public class JsonWriter implements Closeable, Flushable {
* writing raw JSON values. * writing raw JSON values.
* @since 2.4 * @since 2.4
*/ */
@CanIgnoreReturnValue
public JsonWriter jsonValue(String value) throws IOException { public JsonWriter jsonValue(String value) throws IOException {
if (value == null) { if (value == null) {
return nullValue(); return nullValue();
@ -628,7 +618,6 @@ public class JsonWriter implements Closeable, Flushable {
* *
* @return this writer. * @return this writer.
*/ */
@CanIgnoreReturnValue
public JsonWriter nullValue() throws IOException { public JsonWriter nullValue() throws IOException {
if (deferredName != null) { if (deferredName != null) {
if (serializeNulls) { if (serializeNulls) {
@ -648,7 +637,6 @@ public class JsonWriter implements Closeable, Flushable {
* *
* @return this writer. * @return this writer.
*/ */
@CanIgnoreReturnValue
public JsonWriter value(boolean value) throws IOException { public JsonWriter value(boolean value) throws IOException {
writeDeferredName(); writeDeferredName();
beforeValue(); beforeValue();
@ -662,7 +650,6 @@ public class JsonWriter implements Closeable, Flushable {
* @return this writer. * @return this writer.
* @since 2.7 * @since 2.7
*/ */
@CanIgnoreReturnValue
public JsonWriter value(Boolean value) throws IOException { public JsonWriter value(Boolean value) throws IOException {
if (value == null) { if (value == null) {
return nullValue(); return nullValue();
@ -684,7 +671,6 @@ public class JsonWriter implements Closeable, Flushable {
* #setStrictness(Strictness) lenient}. * #setStrictness(Strictness) lenient}.
* @since 2.9.1 * @since 2.9.1
*/ */
@CanIgnoreReturnValue
public JsonWriter value(float value) throws IOException { public JsonWriter value(float value) throws IOException {
writeDeferredName(); writeDeferredName();
if (!serializeSpecialFloatingPointValues && (Float.isNaN(value) || Float.isInfinite(value))) { if (!serializeSpecialFloatingPointValues && (Float.isNaN(value) || Float.isInfinite(value))) {
@ -704,7 +690,6 @@ public class JsonWriter implements Closeable, Flushable {
* @throws IllegalArgumentException if the value is NaN or Infinity and this writer is * @throws IllegalArgumentException if the value is NaN or Infinity and this writer is
* not {@link #setStrictness(Strictness) lenient}. * not {@link #setStrictness(Strictness) lenient}.
*/ */
@CanIgnoreReturnValue
public JsonWriter value(double value) throws IOException { public JsonWriter value(double value) throws IOException {
writeDeferredName(); writeDeferredName();
if (!serializeSpecialFloatingPointValues && (Double.isNaN(value) || Double.isInfinite(value))) { if (!serializeSpecialFloatingPointValues && (Double.isNaN(value) || Double.isInfinite(value))) {
@ -720,7 +705,6 @@ public class JsonWriter implements Closeable, Flushable {
* *
* @return this writer. * @return this writer.
*/ */
@CanIgnoreReturnValue
public JsonWriter value(long value) throws IOException { public JsonWriter value(long value) throws IOException {
writeDeferredName(); writeDeferredName();
beforeValue(); beforeValue();
@ -750,7 +734,6 @@ public class JsonWriter implements Closeable, Flushable {
* not {@link #setStrictness(Strictness) lenient}; or if the {@code toString()} result is not a * not {@link #setStrictness(Strictness) lenient}; or if the {@code toString()} result is not a
* valid JSON number. * valid JSON number.
*/ */
@CanIgnoreReturnValue
public JsonWriter value(Number value) throws IOException { public JsonWriter value(Number value) throws IOException {
if (value == null) { if (value == null) {
return nullValue(); return nullValue();

View File

@ -25,9 +25,6 @@ module io.gitlab.jfronny.gson {
exports io.gitlab.jfronny.gson.stream; exports io.gitlab.jfronny.gson.stream;
exports io.gitlab.jfronny.gson.util; exports io.gitlab.jfronny.gson.util;
// Dependency on Error Prone Annotations
requires static com.google.errorprone.annotations;
// Optional dependency on java.sql // Optional dependency on java.sql
requires static java.sql; requires static java.sql;

View File

@ -61,13 +61,13 @@ public final class GsonTest {
@Test @Test
public void testStrictnessDefault() { public void testStrictnessDefault() {
assertThat(new Gson().strictness).isNull(); assertThat(new Gson().strictness).isEqualTo(Strictness.LENIENT);
} }
@Test @Test
public void testOverridesDefaultExcluder() { public void testOverridesDefaultExcluder() {
Gson gson = new Gson(CUSTOM_EXCLUDER, CUSTOM_FIELD_NAMING_STRATEGY, Gson gson = new Gson(CUSTOM_EXCLUDER, CUSTOM_FIELD_NAMING_STRATEGY,
new HashMap<Type, InstanceCreator<?>>(), true, false, true, false, new HashMap<Type, InstanceCreator<?>>(), true, false, true, false, false,
FormattingStyle.PRETTY, Strictness.LENIENT, false, false, true, FormattingStyle.PRETTY, Strictness.LENIENT, false, false, true,
LongSerializationPolicy.DEFAULT, null, DateFormat.DEFAULT, LongSerializationPolicy.DEFAULT, null, DateFormat.DEFAULT,
DateFormat.DEFAULT, new ArrayList<TypeAdapterFactory>(), DateFormat.DEFAULT, new ArrayList<TypeAdapterFactory>(),
@ -84,7 +84,7 @@ public final class GsonTest {
@Test @Test
public void testClonedTypeAdapterFactoryListsAreIndependent() { public void testClonedTypeAdapterFactoryListsAreIndependent() {
Gson original = new Gson(CUSTOM_EXCLUDER, CUSTOM_FIELD_NAMING_STRATEGY, Gson original = new Gson(CUSTOM_EXCLUDER, CUSTOM_FIELD_NAMING_STRATEGY,
new HashMap<Type, InstanceCreator<?>>(), true, false, true, false, new HashMap<Type, InstanceCreator<?>>(), true, false, true, false, false,
FormattingStyle.PRETTY, Strictness.LENIENT, false, false, true, FormattingStyle.PRETTY, Strictness.LENIENT, false, false, true,
LongSerializationPolicy.DEFAULT, null, DateFormat.DEFAULT, LongSerializationPolicy.DEFAULT, null, DateFormat.DEFAULT,
DateFormat.DEFAULT, new ArrayList<TypeAdapterFactory>(), DateFormat.DEFAULT, new ArrayList<TypeAdapterFactory>(),

View File

@ -222,7 +222,7 @@ public final class MixedStreamTest {
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(writer); JsonWriter jsonWriter = new JsonWriter(writer);
new GsonBuilder().setLenient().serializeSpecialFloatingPointValues().create() new GsonBuilder().setStrictness(Strictness.LENIENT).serializeSpecialFloatingPointValues().create()
.toJson(doubles, type, jsonWriter); .toJson(doubles, type, jsonWriter);
assertThat(writer.toString()).isEqualTo("[NaN,-Infinity,Infinity,-0.0,0.5,0.0]"); assertThat(writer.toString()).isEqualTo("[NaN,-Infinity,Infinity,-0.0,0.5,0.0]");

View File

@ -98,19 +98,19 @@ public class ToNumberPolicyTest {
@Test @Test
public void testNullsAreNeverExpected() throws IOException { public void testNullsAreNeverExpected() throws IOException {
IllegalStateException e = assertThrows(IllegalStateException.class, () -> ToNumberPolicy.DOUBLE.readNumber(fromString("null"))); IllegalStateException e = assertThrows(IllegalStateException.class, () -> ToNumberPolicy.DOUBLE.readNumber(fromString("null")));
assertThat(e).hasMessageThat().isEqualTo("Expected a double but was NULL at line 1 column 5 path $" assertThat(e).hasMessageThat().isEqualTo("Expected a double but was NULL at line 1 column 5 (char '\0') path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#adapter-not-null-safe"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#adapter-not-null-safe");
e = assertThrows(IllegalStateException.class, () -> ToNumberPolicy.LAZILY_PARSED_NUMBER.readNumber(fromString("null"))); e = assertThrows(IllegalStateException.class, () -> ToNumberPolicy.LAZILY_PARSED_NUMBER.readNumber(fromString("null")));
assertThat(e).hasMessageThat().isEqualTo("Expected a string but was NULL at line 1 column 5 path $" assertThat(e).hasMessageThat().isEqualTo("Expected a string but was NULL at line 1 column 5 (char '\0') path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#adapter-not-null-safe"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#adapter-not-null-safe");
e = assertThrows(IllegalStateException.class, () -> ToNumberPolicy.LONG_OR_DOUBLE.readNumber(fromString("null"))); e = assertThrows(IllegalStateException.class, () -> ToNumberPolicy.LONG_OR_DOUBLE.readNumber(fromString("null")));
assertThat(e).hasMessageThat().isEqualTo("Expected a string but was NULL at line 1 column 5 path $" assertThat(e).hasMessageThat().isEqualTo("Expected a string but was NULL at line 1 column 5 (char '\0') path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#adapter-not-null-safe"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#adapter-not-null-safe");
e = assertThrows(IllegalStateException.class, () -> ToNumberPolicy.BIG_DECIMAL.readNumber(fromString("null"))); e = assertThrows(IllegalStateException.class, () -> ToNumberPolicy.BIG_DECIMAL.readNumber(fromString("null")));
assertThat(e).hasMessageThat().isEqualTo("Expected a string but was NULL at line 1 column 5 path $" assertThat(e).hasMessageThat().isEqualTo("Expected a string but was NULL at line 1 column 5 (char '\0') path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#adapter-not-null-safe"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#adapter-not-null-safe");
} }

View File

@ -18,7 +18,6 @@ package com.google.gson;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import com.google.errorprone.annotations.Keep;
import com.google.gson.annotations.Since; import com.google.gson.annotations.Since;
import com.google.gson.annotations.Until; import com.google.gson.annotations.Until;
import com.google.gson.internal.Excluder; import com.google.gson.internal.Excluder;
@ -76,7 +75,6 @@ public class VersionExclusionStrategyTest {
private static class MockClassSince { private static class MockClassSince {
@Since(VERSION) @Since(VERSION)
@Keep
public final int someField = 0; public final int someField = 0;
} }
@ -84,7 +82,6 @@ public class VersionExclusionStrategyTest {
private static class MockClassUntil { private static class MockClassUntil {
@Until(VERSION) @Until(VERSION)
@Keep
public final int someField = 0; public final int someField = 0;
} }
@ -94,7 +91,6 @@ public class VersionExclusionStrategyTest {
@Since(VERSION) @Since(VERSION)
@Until(VERSION + 2) @Until(VERSION + 2)
@Keep
public final int someField = 0; public final int someField = 0;
} }
} }

View File

@ -18,13 +18,7 @@ package com.google.gson.functional;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import com.google.gson.Gson; import com.google.gson.*;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.common.TestTypes.Base; import com.google.gson.common.TestTypes.Base;
import com.google.gson.common.TestTypes.ClassWithBaseField; import com.google.gson.common.TestTypes.ClassWithBaseField;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@ -116,7 +110,7 @@ public class CustomDeserializerTest {
public void testJsonTypeFieldBasedDeserialization() { public void testJsonTypeFieldBasedDeserialization() {
String json = "{field1:'abc',field2:'def',__type__:'SUB_TYPE1'}"; String json = "{field1:'abc',field2:'def',__type__:'SUB_TYPE1'}";
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeAdapter(MyBase.class, (JsonDeserializer<MyBase>) (json1, pojoType, context) -> { .registerTypeAdapter(MyBase.class, (JsonDeserializer<MyBase>) (json1, pojoType, context) -> {
String type = json1.getAsJsonObject().get(MyBase.TYPE_ACCESS).getAsString(); String type = json1.getAsJsonObject().get(MyBase.TYPE_ACCESS).getAsString();
return context.deserialize(json1, SubTypes.valueOf(type).getSubclass()); return context.deserialize(json1, SubTypes.valueOf(type).getSubclass());
@ -154,7 +148,7 @@ public class CustomDeserializerTest {
@Test @Test
public void testCustomDeserializerReturnsNullForTopLevelObject() { public void testCustomDeserializerReturnsNullForTopLevelObject() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeAdapter(Base.class, (JsonDeserializer<Base>) (json, typeOfT, context) -> null).create(); .registerTypeAdapter(Base.class, (JsonDeserializer<Base>) (json, typeOfT, context) -> null).create();
String json = "{baseName:'Base',subName:'SubRevised'}"; String json = "{baseName:'Base',subName:'SubRevised'}";
Base target = gson.fromJson(json, Base.class); Base target = gson.fromJson(json, Base.class);
@ -164,7 +158,7 @@ public class CustomDeserializerTest {
@Test @Test
public void testCustomDeserializerReturnsNull() { public void testCustomDeserializerReturnsNull() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeAdapter(Base.class, (JsonDeserializer<Base>) (json, typeOfT, context) -> null).create(); .registerTypeAdapter(Base.class, (JsonDeserializer<Base>) (json, typeOfT, context) -> null).create();
String json = "{base:{baseName:'Base',subName:'SubRevised'}}"; String json = "{base:{baseName:'Base',subName:'SubRevised'}}";
ClassWithBaseField target = gson.fromJson(json, ClassWithBaseField.class); ClassWithBaseField target = gson.fromJson(json, ClassWithBaseField.class);
@ -174,7 +168,7 @@ public class CustomDeserializerTest {
@Test @Test
public void testCustomDeserializerReturnsNullForArrayElements() { public void testCustomDeserializerReturnsNullForArrayElements() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeAdapter(Base.class, (JsonDeserializer<Base>) (json, typeOfT, context) -> null).create(); .registerTypeAdapter(Base.class, (JsonDeserializer<Base>) (json, typeOfT, context) -> null).create();
String json = "[{baseName:'Base'},{baseName:'Base'}]"; String json = "[{baseName:'Base'},{baseName:'Base'}]";
Base[] target = gson.fromJson(json, Base[].class); Base[] target = gson.fromJson(json, Base[].class);
@ -185,7 +179,7 @@ public class CustomDeserializerTest {
@Test @Test
public void testCustomDeserializerReturnsNullForArrayElementsForArrayField() { public void testCustomDeserializerReturnsNullForArrayElementsForArrayField() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeAdapter(Base.class, (JsonDeserializer<Base>) (json, typeOfT, context) -> null).create(); .registerTypeAdapter(Base.class, (JsonDeserializer<Base>) (json, typeOfT, context) -> null).create();
String json = "{bases:[{baseName:'Base'},{baseName:'Base'}]}"; String json = "{bases:[{baseName:'Base'},{baseName:'Base'}]}";
ClassWithBaseArray target = gson.fromJson(json, ClassWithBaseArray.class); ClassWithBaseArray target = gson.fromJson(json, ClassWithBaseArray.class);

View File

@ -18,17 +18,7 @@ package com.google.gson.functional;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.gson.Gson; import com.google.gson.*;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.common.TestTypes.BagOfPrimitives; import com.google.gson.common.TestTypes.BagOfPrimitives;
import com.google.gson.common.TestTypes.ClassWithCustomTypeConverter; import com.google.gson.common.TestTypes.ClassWithCustomTypeConverter;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
@ -418,7 +408,7 @@ public class CustomTypeAdaptersTest {
@Test @Test
public void testEnsureCustomDeserializerNotInvokedForNullValues() { public void testEnsureCustomDeserializerNotInvokedForNullValues() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeAdapter(DataHolder.class, new DataHolderDeserializer()) .registerTypeAdapter(DataHolder.class, new DataHolderDeserializer())
.create(); .create();
String json = "{wrappedData:null}"; String json = "{wrappedData:null}";

View File

@ -18,18 +18,7 @@ package com.google.gson.functional;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import com.google.gson.Gson; import com.google.gson.*;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;
import com.google.gson.TypeAdapter;
import com.google.gson.internal.JavaVersion; import com.google.gson.internal.JavaVersion;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
@ -113,7 +102,7 @@ public class DefaultTypeAdaptersTest {
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#java-lang-class-unsupported"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#java-lang-class-unsupported");
} }
// Override with a custom type adapter for class. // Override with a custom type adapter for class.
gson = new GsonBuilder().setLenient().registerTypeAdapter(Class.class, new MyClassTypeAdapter()).create(); gson = new GsonBuilder().setStrictness(Strictness.LENIENT).registerTypeAdapter(Class.class, new MyClassTypeAdapter()).create();
assertThat(gson.fromJson("java.lang.String", Class.class)).isAssignableTo(String.class); assertThat(gson.fromJson("java.lang.String", Class.class)).isAssignableTo(String.class);
} }

View File

@ -22,6 +22,7 @@ import com.google.errorprone.annotations.Keep;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator; import com.google.gson.InstanceCreator;
import com.google.gson.Strictness;
import com.google.gson.annotations.Expose; import com.google.gson.annotations.Expose;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import org.junit.Before; import org.junit.Before;
@ -39,7 +40,7 @@ public class ExposeFieldsTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
gson = new GsonBuilder() gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.excludeFieldsWithoutExposeAnnotation() .excludeFieldsWithoutExposeAnnotation()
.registerTypeAdapter(SomeInterface.class, new SomeInterfaceInstanceCreator()) .registerTypeAdapter(SomeInterface.class, new SomeInterfaceInstanceCreator())
.create(); .create();

View File

@ -71,7 +71,7 @@ public class FormattingStyleTest {
@Test @Test
public void testVariousCombinationsParse() { public void testVariousCombinationsParse() {
// Mixing various indent and newline styles in the same string, to be parsed. // Mixing various indent and newline styles in the same string, to be parsed.
String jsonStringMix = "{\r\t'a':\r\n[ 1,2\t]\n}"; String jsonStringMix = "{\r\t\"a\":\r\n[ 1,2\t]\n}";
TypeToken<Map<String, List<Integer>>> inputType = new TypeToken<Map<String, List<Integer>>>() {}; TypeToken<Map<String, List<Integer>>> inputType = new TypeToken<Map<String, List<Integer>>>() {};
Map<String, List<Integer>> actualParsed; Map<String, List<Integer>> actualParsed;

View File

@ -21,6 +21,7 @@ import static com.google.common.truth.Truth.assertThat;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator; import com.google.gson.InstanceCreator;
import com.google.gson.Strictness;
import com.google.gson.common.TestTypes.Base; import com.google.gson.common.TestTypes.Base;
import com.google.gson.common.TestTypes.ClassWithBaseField; import com.google.gson.common.TestTypes.ClassWithBaseField;
import com.google.gson.common.TestTypes.Sub; import com.google.gson.common.TestTypes.Sub;
@ -43,7 +44,7 @@ public class InstanceCreatorTest {
@Test @Test
public void testInstanceCreatorReturnsBaseType() { public void testInstanceCreatorReturnsBaseType() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeAdapter(Base.class, (InstanceCreator<Base>) type -> new Base()) .registerTypeAdapter(Base.class, (InstanceCreator<Base>) type -> new Base())
.create(); .create();
String json = "{baseName:'BaseRevised',subName:'Sub'}"; String json = "{baseName:'BaseRevised',subName:'Sub'}";
@ -54,7 +55,7 @@ public class InstanceCreatorTest {
@Test @Test
public void testInstanceCreatorReturnsSubTypeForTopLevelObject() { public void testInstanceCreatorReturnsSubTypeForTopLevelObject() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeAdapter(Base.class, (InstanceCreator<Base>) type -> new Sub()) .registerTypeAdapter(Base.class, (InstanceCreator<Base>) type -> new Sub())
.create(); .create();
@ -70,7 +71,7 @@ public class InstanceCreatorTest {
@Test @Test
public void testInstanceCreatorReturnsSubTypeForField() { public void testInstanceCreatorReturnsSubTypeForField() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeAdapter(Base.class, (InstanceCreator<Base>) type -> new Sub()) .registerTypeAdapter(Base.class, (InstanceCreator<Base>) type -> new Sub())
.create(); .create();
String json = "{base:{baseName:'Base',subName:'SubRevised'}}"; String json = "{base:{baseName:'Base',subName:'SubRevised'}}";

View File

@ -20,18 +20,7 @@ import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.gson.Gson; import com.google.gson.*;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.annotations.JsonAdapter; import com.google.gson.annotations.JsonAdapter;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
@ -105,7 +94,7 @@ public final class JsonAdapterAnnotationOnClassesTest {
} }
}; };
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeAdapter(A.class, serializer) .registerTypeAdapter(A.class, serializer)
.create(); .create();
String json = gson.toJson(new A("abcd")); String json = gson.toJson(new A("abcd"));
@ -126,7 +115,7 @@ public final class JsonAdapterAnnotationOnClassesTest {
} }
}; };
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeAdapter(A.class, deserializer) .registerTypeAdapter(A.class, deserializer)
.create(); .create();
String json = gson.toJson(new A("abcd")); String json = gson.toJson(new A("abcd"));

View File

@ -18,18 +18,7 @@ package com.google.gson.functional;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import com.google.gson.ExclusionStrategy; import com.google.gson.*;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.annotations.JsonAdapter; import com.google.gson.annotations.JsonAdapter;
import com.google.gson.internal.bind.ReflectiveTypeAdapterFactory; import com.google.gson.internal.bind.ReflectiveTypeAdapterFactory;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
@ -78,7 +67,7 @@ public final class JsonAdapterAnnotationOnFieldsTest {
@Test @Test
public void testFieldAnnotationTakesPrecedenceOverRegisteredTypeAdapter() { public void testFieldAnnotationTakesPrecedenceOverRegisteredTypeAdapter() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeAdapter(Part.class, new TypeAdapter<Part>() { .registerTypeAdapter(Part.class, new TypeAdapter<Part>() {
@Override public void write(JsonWriter out, Part part) { @Override public void write(JsonWriter out, Part part) {
throw new AssertionError(); throw new AssertionError();

View File

@ -22,6 +22,7 @@ import static org.junit.Assert.fail;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException; import com.google.gson.JsonSyntaxException;
import com.google.gson.Strictness;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.HashMap; import java.util.HashMap;
@ -116,7 +117,7 @@ public class MapAsArrayTypeAdapterTest {
@Test @Test
public void testMapWithTypeVariableDeserialization() { public void testMapWithTypeVariableDeserialization() {
Gson gson = new GsonBuilder().setLenient().enableComplexMapKeySerialization().create(); Gson gson = new GsonBuilder().setStrictness(Strictness.LENIENT).enableComplexMapKeySerialization().create();
String json = "{map:[[{x:2,y:3},{x:4,y:5}]]}"; String json = "{map:[[{x:2,y:3},{x:4,y:5}]]}";
Type type = new TypeToken<PointWithProperty<Point>>(){}.getType(); Type type = new TypeToken<PointWithProperty<Point>>(){}.getType();
PointWithProperty<Point> map = gson.fromJson(json, type); PointWithProperty<Point> map = gson.fromJson(json, type);

View File

@ -18,15 +18,7 @@ package com.google.gson.functional;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import com.google.gson.Gson; import com.google.gson.*;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.common.TestTypes.BagOfPrimitives; import com.google.gson.common.TestTypes.BagOfPrimitives;
import com.google.gson.common.TestTypes.ClassWithObjects; import com.google.gson.common.TestTypes.ClassWithObjects;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@ -240,7 +232,7 @@ public class NullObjectAndFieldTest {
@Test @Test
public void testCustomTypeAdapterPassesNullDesrialization() { public void testCustomTypeAdapterPassesNullDesrialization() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeAdapter(ObjectWithField.class, (JsonDeserializer<ObjectWithField>) (json, type, context) -> context.deserialize(null, type)).create(); .registerTypeAdapter(ObjectWithField.class, (JsonDeserializer<ObjectWithField>) (json, type, context) -> context.deserialize(null, type)).create();
String json = "{value:'value1'}"; String json = "{value:'value1'}";
ObjectWithField target = gson.fromJson(json, ObjectWithField.class); ObjectWithField target = gson.fromJson(json, ObjectWithField.class);

View File

@ -544,7 +544,7 @@ public class PrimitiveTest {
@Test @Test
public void testDoubleNaNSerialization() { public void testDoubleNaNSerialization() {
Gson gson = new GsonBuilder().setLenient().serializeSpecialFloatingPointValues().create(); Gson gson = new GsonBuilder().setStrictness(Strictness.LENIENT).serializeSpecialFloatingPointValues().create();
double nan = Double.NaN; double nan = Double.NaN;
assertThat(gson.toJson(nan)).isEqualTo("NaN"); assertThat(gson.toJson(nan)).isEqualTo("NaN");
assertThat(gson.toJson(Double.NaN)).isEqualTo("NaN"); assertThat(gson.toJson(Double.NaN)).isEqualTo("NaN");
@ -573,7 +573,7 @@ public class PrimitiveTest {
@Test @Test
public void testFloatNaNSerialization() { public void testFloatNaNSerialization() {
Gson gson = new GsonBuilder().setLenient().serializeSpecialFloatingPointValues().create(); Gson gson = new GsonBuilder().setStrictness(Strictness.LENIENT).serializeSpecialFloatingPointValues().create();
float nan = Float.NaN; float nan = Float.NaN;
assertThat(gson.toJson(nan)).isEqualTo("NaN"); assertThat(gson.toJson(nan)).isEqualTo("NaN");
assertThat(gson.toJson(Float.NaN)).isEqualTo("NaN"); assertThat(gson.toJson(Float.NaN)).isEqualTo("NaN");
@ -611,7 +611,7 @@ public class PrimitiveTest {
@Test @Test
public void testDoubleInfinitySerialization() { public void testDoubleInfinitySerialization() {
Gson gson = new GsonBuilder().setLenient().serializeSpecialFloatingPointValues().create(); Gson gson = new GsonBuilder().setStrictness(Strictness.LENIENT).serializeSpecialFloatingPointValues().create();
double infinity = Double.POSITIVE_INFINITY; double infinity = Double.POSITIVE_INFINITY;
assertThat(gson.toJson(infinity)).isEqualTo("Infinity"); assertThat(gson.toJson(infinity)).isEqualTo("Infinity");
assertThat(gson.toJson(Double.POSITIVE_INFINITY)).isEqualTo("Infinity"); assertThat(gson.toJson(Double.POSITIVE_INFINITY)).isEqualTo("Infinity");
@ -640,7 +640,7 @@ public class PrimitiveTest {
@Test @Test
public void testFloatInfinitySerialization() { public void testFloatInfinitySerialization() {
Gson gson = new GsonBuilder().setLenient().serializeSpecialFloatingPointValues().create(); Gson gson = new GsonBuilder().setStrictness(Strictness.LENIENT).serializeSpecialFloatingPointValues().create();
float infinity = Float.POSITIVE_INFINITY; float infinity = Float.POSITIVE_INFINITY;
assertThat(gson.toJson(infinity)).isEqualTo("Infinity"); assertThat(gson.toJson(infinity)).isEqualTo("Infinity");
assertThat(gson.toJson(Float.POSITIVE_INFINITY)).isEqualTo("Infinity"); assertThat(gson.toJson(Float.POSITIVE_INFINITY)).isEqualTo("Infinity");
@ -678,7 +678,7 @@ public class PrimitiveTest {
@Test @Test
public void testNegativeInfinitySerialization() { public void testNegativeInfinitySerialization() {
Gson gson = new GsonBuilder().setLenient().serializeSpecialFloatingPointValues().create(); Gson gson = new GsonBuilder().setStrictness(Strictness.LENIENT).serializeSpecialFloatingPointValues().create();
double negativeInfinity = Double.NEGATIVE_INFINITY; double negativeInfinity = Double.NEGATIVE_INFINITY;
assertThat(gson.toJson(negativeInfinity)).isEqualTo("-Infinity"); assertThat(gson.toJson(negativeInfinity)).isEqualTo("-Infinity");
assertThat(gson.toJson(Double.NEGATIVE_INFINITY)).isEqualTo("-Infinity"); assertThat(gson.toJson(Double.NEGATIVE_INFINITY)).isEqualTo("-Infinity");
@ -707,7 +707,7 @@ public class PrimitiveTest {
@Test @Test
public void testNegativeInfinityFloatSerialization() { public void testNegativeInfinityFloatSerialization() {
Gson gson = new GsonBuilder().setLenient().serializeSpecialFloatingPointValues().create(); Gson gson = new GsonBuilder().setStrictness(Strictness.LENIENT).serializeSpecialFloatingPointValues().create();
float negativeInfinity = Float.NEGATIVE_INFINITY; float negativeInfinity = Float.NEGATIVE_INFINITY;
assertThat(gson.toJson(negativeInfinity)).isEqualTo("-Infinity"); assertThat(gson.toJson(negativeInfinity)).isEqualTo("-Infinity");
assertThat(gson.toJson(Float.NEGATIVE_INFINITY)).isEqualTo("-Infinity"); assertThat(gson.toJson(Float.NEGATIVE_INFINITY)).isEqualTo("-Infinity");

View File

@ -44,6 +44,20 @@ public class ReflectionAccessTest {
} }
} }
private static JsonIOException assertInaccessibleException(String json, Class<?> toDeserialize) {
Gson gson = new Gson();
try {
gson.fromJson(json, toDeserialize);
throw new AssertionError("Missing exception; test has to be run with `--illegal-access=deny`");
} catch (JsonSyntaxException e) {
throw new AssertionError("Unexpected exception; test has to be run with `--illegal-access=deny`", e);
} catch (JsonIOException expected) {
assertThat(expected).hasMessageThat().endsWith("\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#reflection-inaccessible");
// Return exception for further assertions
return expected;
}
}
/** /**
* Test serializing an instance of a non-accessible internal class, but where * Test serializing an instance of a non-accessible internal class, but where
* Gson supports serializing one of its superinterfaces. * Gson supports serializing one of its superinterfaces.

View File

@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.Strictness;
import com.google.gson.common.TestTypes.BagOfPrimitives; import com.google.gson.common.TestTypes.BagOfPrimitives;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -39,7 +40,7 @@ public class SecurityTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
gsonBuilder = new GsonBuilder().setLenient(); gsonBuilder = new GsonBuilder().setStrictness(Strictness.LENIENT);
} }
@Test @Test

View File

@ -20,13 +20,7 @@ import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.gson.Gson; import com.google.gson.*;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
@ -179,7 +173,7 @@ public final class StreamingTypeAdaptersTest {
out.value(person.name + "," + person.age); out.value(person.name + "," + person.age);
} }
}; };
Gson gson = new GsonBuilder().setLenient().registerTypeAdapter( Gson gson = new GsonBuilder().setStrictness(Strictness.LENIENT).registerTypeAdapter(
Person.class, typeAdapter).create(); Person.class, typeAdapter).create();
Truck truck = new Truck(); Truck truck = new Truck();
truck.horsePower = 1.0D; truck.horsePower = 1.0D;
@ -195,10 +189,10 @@ public final class StreamingTypeAdaptersTest {
gson.fromJson(json, Truck.class); gson.fromJson(json, Truck.class);
fail(); fail();
} catch (JsonSyntaxException expected) { } catch (JsonSyntaxException expected) {
assertThat(expected).hasMessageThat().isEqualTo("java.lang.IllegalStateException: Expected a string but was NULL at line 1 column 33 path $.passengers[0]" assertThat(expected).hasMessageThat().isEqualTo("java.lang.IllegalStateException: Expected a string but was NULL at line 1 column 33 (char ',') path $.passengers[0]"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#adapter-not-null-safe"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#adapter-not-null-safe");
} }
gson = new GsonBuilder().setLenient().registerTypeAdapter(Person.class, typeAdapter.nullSafe()).create(); gson = new GsonBuilder().setStrictness(Strictness.LENIENT).registerTypeAdapter(Person.class, typeAdapter.nullSafe()).create();
assertThat(gson.toJson(truck, Truck.class)) assertThat(gson.toJson(truck, Truck.class))
.isEqualTo("{\"horsePower\":1.0,\"passengers\":[null,\"jesse,30\"]}"); .isEqualTo("{\"horsePower\":1.0,\"passengers\":[null,\"jesse,30\"]}");
truck = gson.fromJson(json, Truck.class); truck = gson.fromJson(json, Truck.class);

View File

@ -18,15 +18,7 @@ package com.google.gson.functional;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import com.google.gson.Gson; import com.google.gson.*;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
@ -55,7 +47,7 @@ public class TreeTypeAdaptersTest {
@Before @Before
public void setUp() { public void setUp() {
gson = new GsonBuilder() gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeAdapter(Id.class, new IdTreeTypeAdapter()) .registerTypeAdapter(Id.class, new IdTreeTypeAdapter())
.create(); .create();
course = new Course<>(COURSE_ID, 4, course = new Course<>(COURSE_ID, 4,

View File

@ -18,15 +18,7 @@ package com.google.gson.functional;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import com.google.gson.Gson; import com.google.gson.*;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
import java.io.IOException; import java.io.IOException;
@ -37,7 +29,7 @@ public final class TypeAdapterPrecedenceTest {
@Test @Test
public void testNonstreamingFollowedByNonstreaming() { public void testNonstreamingFollowedByNonstreaming() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeAdapter(Foo.class, newSerializer("serializer 1")) .registerTypeAdapter(Foo.class, newSerializer("serializer 1"))
.registerTypeAdapter(Foo.class, newSerializer("serializer 2")) .registerTypeAdapter(Foo.class, newSerializer("serializer 2"))
.registerTypeAdapter(Foo.class, newDeserializer("deserializer 1")) .registerTypeAdapter(Foo.class, newDeserializer("deserializer 1"))
@ -50,7 +42,7 @@ public final class TypeAdapterPrecedenceTest {
@Test @Test
public void testStreamingFollowedByStreaming() { public void testStreamingFollowedByStreaming() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeAdapter(Foo.class, newTypeAdapter("type adapter 1")) .registerTypeAdapter(Foo.class, newTypeAdapter("type adapter 1"))
.registerTypeAdapter(Foo.class, newTypeAdapter("type adapter 2")) .registerTypeAdapter(Foo.class, newTypeAdapter("type adapter 2"))
.create(); .create();
@ -61,7 +53,7 @@ public final class TypeAdapterPrecedenceTest {
@Test @Test
public void testSerializeNonstreamingTypeAdapterFollowedByStreamingTypeAdapter() { public void testSerializeNonstreamingTypeAdapterFollowedByStreamingTypeAdapter() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeAdapter(Foo.class, newSerializer("serializer")) .registerTypeAdapter(Foo.class, newSerializer("serializer"))
.registerTypeAdapter(Foo.class, newDeserializer("deserializer")) .registerTypeAdapter(Foo.class, newDeserializer("deserializer"))
.registerTypeAdapter(Foo.class, newTypeAdapter("type adapter")) .registerTypeAdapter(Foo.class, newTypeAdapter("type adapter"))
@ -73,7 +65,7 @@ public final class TypeAdapterPrecedenceTest {
@Test @Test
public void testStreamingFollowedByNonstreaming() { public void testStreamingFollowedByNonstreaming() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeAdapter(Foo.class, newTypeAdapter("type adapter")) .registerTypeAdapter(Foo.class, newTypeAdapter("type adapter"))
.registerTypeAdapter(Foo.class, newSerializer("serializer")) .registerTypeAdapter(Foo.class, newSerializer("serializer"))
.registerTypeAdapter(Foo.class, newDeserializer("deserializer")) .registerTypeAdapter(Foo.class, newDeserializer("deserializer"))
@ -85,7 +77,7 @@ public final class TypeAdapterPrecedenceTest {
@Test @Test
public void testStreamingHierarchicalFollowedByNonstreaming() { public void testStreamingHierarchicalFollowedByNonstreaming() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeHierarchyAdapter(Foo.class, newTypeAdapter("type adapter")) .registerTypeHierarchyAdapter(Foo.class, newTypeAdapter("type adapter"))
.registerTypeAdapter(Foo.class, newSerializer("serializer")) .registerTypeAdapter(Foo.class, newSerializer("serializer"))
.registerTypeAdapter(Foo.class, newDeserializer("deserializer")) .registerTypeAdapter(Foo.class, newDeserializer("deserializer"))
@ -108,7 +100,7 @@ public final class TypeAdapterPrecedenceTest {
@Test @Test
public void testStreamingHierarchicalFollowedByNonstreamingHierarchical() { public void testStreamingHierarchicalFollowedByNonstreamingHierarchical() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeHierarchyAdapter(Foo.class, newSerializer("serializer")) .registerTypeHierarchyAdapter(Foo.class, newSerializer("serializer"))
.registerTypeHierarchyAdapter(Foo.class, newDeserializer("deserializer")) .registerTypeHierarchyAdapter(Foo.class, newDeserializer("deserializer"))
.registerTypeHierarchyAdapter(Foo.class, newTypeAdapter("type adapter")) .registerTypeHierarchyAdapter(Foo.class, newTypeAdapter("type adapter"))
@ -120,7 +112,7 @@ public final class TypeAdapterPrecedenceTest {
@Test @Test
public void testNonstreamingHierarchicalFollowedByNonstreaming() { public void testNonstreamingHierarchicalFollowedByNonstreaming() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setLenient() .setStrictness(Strictness.LENIENT)
.registerTypeHierarchyAdapter(Foo.class, newSerializer("hierarchical")) .registerTypeHierarchyAdapter(Foo.class, newSerializer("hierarchical"))
.registerTypeHierarchyAdapter(Foo.class, newDeserializer("hierarchical")) .registerTypeHierarchyAdapter(Foo.class, newDeserializer("hierarchical"))
.registerTypeAdapter(Foo.class, newSerializer("non hierarchical")) .registerTypeAdapter(Foo.class, newSerializer("non hierarchical"))

View File

@ -17,6 +17,7 @@
package com.google.gson.jf; package com.google.gson.jf;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.Strictness;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
import org.junit.Test; import org.junit.Test;
@ -47,7 +48,7 @@ public final class CommentsTest {
+ " \"c\"\n" + " \"c\"\n"
+ "]"; + "]";
List<String> abc = new GsonBuilder().setLenient().create().fromJson(json, new TypeToken<List<String>>() {}.getType()); List<String> abc = new GsonBuilder().setStrictness(Strictness.LENIENT).create().fromJson(json, new TypeToken<List<String>>() {}.getType());
assertThat(abc).isEqualTo(Arrays.asList("a", "b", "c")); assertThat(abc).isEqualTo(Arrays.asList("a", "b", "c"));
} }
@ -70,7 +71,7 @@ public final class CommentsTest {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
JsonWriter jw = new JsonWriter(sw); JsonWriter jw = new JsonWriter(sw);
jw.setLenient(true); jw.setStrictness(Strictness.LENIENT);
jw.setIndent(" "); jw.setIndent(" ");
jw.comment("comment at file head") jw.comment("comment at file head")

View File

@ -2,6 +2,7 @@ package com.google.gson.jf;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.Strictness;
import org.junit.Test; import org.junit.Test;
import java.util.List; import java.util.List;
@ -9,7 +10,7 @@ import java.util.List;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
public class LenientCollectionCommaTest { public class LenientCollectionCommaTest {
private Gson lenient = new GsonBuilder().setLenient().create(); private Gson lenient = new GsonBuilder().setStrictness(Strictness.LENIENT).create();
private static final String json = "{\"collection\": [1, 2,, 3, , , 4, 5,]}"; private static final String json = "{\"collection\": [1, 2,, 3, , , 4, 5,]}";
@Test @Test

View File

@ -3,15 +3,18 @@ package com.google.gson.jf;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import com.google.gson.Strictness;
import org.junit.Test; import org.junit.Test;
import java.lang.reflect.Field;
import java.util.List; import java.util.List;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertThrows;
public class LenientCollectionTest { public class LenientCollectionTest {
private Gson lenient = new GsonBuilder().setLenient().create(); private Gson lenient = new GsonBuilder().setStrictness(Strictness.LENIENT).create();
private Gson strict = new GsonBuilder().create(); private Gson strict = new GsonBuilder().create();
private static final String json = "{\"collection\": \"example\"}"; private static final String json = "{\"collection\": \"example\"}";
@ -28,7 +31,7 @@ public class LenientCollectionTest {
} }
@Test @Test
public void testStrictArray() { public void testStrictArray() throws NoSuchFieldException, IllegalAccessException {
assertThrows("Strict Gson should not deserialize array without brackets", assertThrows("Strict Gson should not deserialize array without brackets",
JsonParseException.class, JsonParseException.class,
() -> strict.fromJson(json, Box.StringArray.class)); () -> strict.fromJson(json, Box.StringArray.class));

View File

@ -2,6 +2,7 @@ package com.google.gson.jf;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.Strictness;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import org.junit.Test; import org.junit.Test;
@ -14,7 +15,7 @@ import static com.google.common.truth.Truth.assertThat;
public class ReadNullTest { public class ReadNullTest {
@Test @Test
public void testReadNull() throws IOException { public void testReadNull() throws IOException {
Gson gson = new GsonBuilder().serializeNulls().setLenient().create(); Gson gson = new GsonBuilder().serializeNulls().setStrictness(Strictness.LENIENT).create();
String exampleFile = "{\n // Yes\n \"value1\": 1024,\n \"value2\": null,\n \"value3\": 10\n}"; String exampleFile = "{\n // Yes\n \"value1\": 1024,\n \"value2\": null,\n \"value3\": 10\n}";
try (Reader r = new StringReader(exampleFile); JsonReader jr = gson.newJsonReader(r)) { try (Reader r = new StringReader(exampleFile); JsonReader jr = gson.newJsonReader(r)) {
jr.beginObject(); jr.beginObject();

View File

@ -134,14 +134,14 @@ public final class JsonReaderTest {
IOException expected = assertThrows(IOException.class, reader::nextBoolean); IOException expected = assertThrows(IOException.class, reader::nextBoolean);
assertThat(expected).hasMessageThat().startsWith("Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed" + assertThat(expected).hasMessageThat().startsWith("Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed" +
" JSON at line 1 column 1 path $"); " JSON at line 1 column 1 (char 'T') path $");
reader = new JsonReader(reader("True")); reader = new JsonReader(reader("True"));
reader.setStrictness(Strictness.STRICT); reader.setStrictness(Strictness.STRICT);
expected = assertThrows(IOException.class, reader::nextBoolean); expected = assertThrows(IOException.class, reader::nextBoolean);
assertThat(expected).hasMessageThat().startsWith("Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed" + assertThat(expected).hasMessageThat().startsWith("Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed" +
" JSON at line 1 column 1 path $"); " JSON at line 1 column 1 (char 'T') path $");
} }
@Test @Test
@ -151,14 +151,14 @@ public final class JsonReaderTest {
IOException expected = assertThrows(IOException.class, reader::nextBoolean); IOException expected = assertThrows(IOException.class, reader::nextBoolean);
assertThat(expected).hasMessageThat().startsWith("Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed" + assertThat(expected).hasMessageThat().startsWith("Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed" +
" JSON at line 1 column 1 path $"); " JSON at line 1 column 1 (char 'F') path $");
reader = new JsonReader(reader("FaLse")); reader = new JsonReader(reader("FaLse"));
reader.setStrictness(Strictness.STRICT); reader.setStrictness(Strictness.STRICT);
expected = assertThrows(IOException.class, reader::nextBoolean); expected = assertThrows(IOException.class, reader::nextBoolean);
assertThat(expected).hasMessageThat().startsWith("Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed" + assertThat(expected).hasMessageThat().startsWith("Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed" +
" JSON at line 1 column 1 path $"); " JSON at line 1 column 1 (char 'F') path $");
} }
@Test @Test
@ -168,14 +168,14 @@ public final class JsonReaderTest {
IOException expected = assertThrows(IOException.class, reader::nextNull); IOException expected = assertThrows(IOException.class, reader::nextNull);
assertThat(expected).hasMessageThat().startsWith("Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed" + assertThat(expected).hasMessageThat().startsWith("Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed" +
" JSON at line 1 column 1 path $"); " JSON at line 1 column 1 (char 'N') path $");
reader = new JsonReader(reader("nulL")); reader = new JsonReader(reader("nulL"));
reader.setStrictness(Strictness.STRICT); reader.setStrictness(Strictness.STRICT);
expected = assertThrows(IOException.class, reader::nextNull); expected = assertThrows(IOException.class, reader::nextNull);
assertThat(expected).hasMessageThat().startsWith("Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed" + assertThat(expected).hasMessageThat().startsWith("Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed" +
" JSON at line 1 column 1 path $"); " JSON at line 1 column 1 (char 'n') path $");
} }
@Test @Test
@ -413,7 +413,7 @@ public final class JsonReaderTest {
reader.nextName(); reader.nextName();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Invalid escape sequence at line 2 column 8 path $." assertThat(expected).hasMessageThat().isEqualTo("Invalid escape sequence at line 2 column 8 (char 'l') path $."
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
} }
} }
@ -542,7 +542,7 @@ public final class JsonReaderTest {
reader.nextString(); reader.nextString();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Malformed Unicode escape \\u000g at line 1 column 5 path $[0]" assertThat(expected).hasMessageThat().isEqualTo("Malformed Unicode escape \\u000g at line 1 column 5 (char '0') path $[0]"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
} }
} }
@ -556,7 +556,7 @@ public final class JsonReaderTest {
reader.nextString(); reader.nextString();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Unterminated escape sequence at line 1 column 5 path $[0]" assertThat(expected).hasMessageThat().isEqualTo("Unterminated escape sequence at line 1 column 5 (char '0') path $[0]"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
} }
} }
@ -570,7 +570,7 @@ public final class JsonReaderTest {
reader.nextString(); reader.nextString();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Unterminated escape sequence at line 1 column 4 path $[0]" assertThat(expected).hasMessageThat().isEqualTo("Unterminated escape sequence at line 1 column 4 (char '[') path $[0]"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
} }
} }
@ -637,7 +637,7 @@ public final class JsonReaderTest {
reader.nextDouble(); reader.nextDouble();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 2 path $[0]"); assertStrictError(expected, "line 1 column 2 (char 'N') path $[0]");
} }
} }
@ -650,7 +650,7 @@ public final class JsonReaderTest {
reader.nextDouble(); reader.nextDouble();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertThat(expected).hasMessageThat().isEqualTo("JSON forbids NaN and infinities: NaN at line 1 column 7 path $[0]" assertThat(expected).hasMessageThat().isEqualTo("JSON forbids NaN and infinities: NaN at line 1 column 7 (char ']') path $[0]"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
} }
} }
@ -688,7 +688,7 @@ public final class JsonReaderTest {
reader.skipValue(); reader.skipValue();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 2 path $[0]"); assertStrictError(expected, "line 1 column 2 (char 'N') path $[0]");
} }
} }
@ -781,7 +781,7 @@ public final class JsonReaderTest {
reader.nextBoolean(); reader.nextBoolean();
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
assertUnexpectedStructureError(expected, "a boolean", "STRING", "line 1 column 2 path $[0]"); assertUnexpectedStructureError(expected, "a boolean", "STRING", "line 1 column 2 (char 't') path $[0]");
} }
assertThat(reader.nextString()).isEqualTo("truey"); assertThat(reader.nextString()).isEqualTo("truey");
reader.endArray(); reader.endArray();
@ -1018,7 +1018,7 @@ public final class JsonReaderTest {
reader.nextString(); reader.nextString();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Expected value at line 1 column 6 path $.a" assertThat(expected).hasMessageThat().isEqualTo("Expected value at line 1 column 6 (char '}') path $.a"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
} }
} }
@ -1078,63 +1078,63 @@ public final class JsonReaderTest {
String unused = reader.nextString(); String unused = reader.nextString();
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
assertUnexpectedStructureError(expected, "a string", "NAME", "line 1 column 3 path $."); assertUnexpectedStructureError(expected, "a string", "NAME", "line 1 column 3 (char 'a') path $.");
} }
assertThat(reader.nextName()).isEqualTo("a"); assertThat(reader.nextName()).isEqualTo("a");
try { try {
String unused = reader.nextName(); String unused = reader.nextName();
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
assertUnexpectedStructureError(expected, "a name", "BOOLEAN", "line 1 column 10 path $.a"); assertUnexpectedStructureError(expected, "a name", "BOOLEAN", "line 1 column 10 (char '}') path $.a");
} }
try { try {
reader.beginArray(); reader.beginArray();
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
assertUnexpectedStructureError(expected, "BEGIN_ARRAY", "BOOLEAN", "line 1 column 10 path $.a"); assertUnexpectedStructureError(expected, "BEGIN_ARRAY", "BOOLEAN", "line 1 column 10 (char '}') path $.a");
} }
try { try {
reader.endArray(); reader.endArray();
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
assertUnexpectedStructureError(expected, "END_ARRAY", "BOOLEAN", "line 1 column 10 path $.a"); assertUnexpectedStructureError(expected, "END_ARRAY", "BOOLEAN", "line 1 column 10 (char '}') path $.a");
} }
try { try {
reader.beginObject(); reader.beginObject();
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
assertUnexpectedStructureError(expected, "BEGIN_OBJECT", "BOOLEAN", "line 1 column 10 path $.a"); assertUnexpectedStructureError(expected, "BEGIN_OBJECT", "BOOLEAN", "line 1 column 10 (char '}') path $.a");
} }
try { try {
reader.endObject(); reader.endObject();
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
assertUnexpectedStructureError(expected, "END_OBJECT", "BOOLEAN", "line 1 column 10 path $.a"); assertUnexpectedStructureError(expected, "END_OBJECT", "BOOLEAN", "line 1 column 10 (char '}') path $.a");
} }
assertThat(reader.nextBoolean()).isTrue(); assertThat(reader.nextBoolean()).isTrue();
try { try {
reader.nextString(); reader.nextString();
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
assertUnexpectedStructureError(expected, "a string", "END_OBJECT", "line 1 column 11 path $.a"); assertUnexpectedStructureError(expected, "a string", "END_OBJECT", "line 1 column 11 (char '\0') path $.a");
} }
try { try {
reader.nextName(); reader.nextName();
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
assertUnexpectedStructureError(expected, "a name", "END_OBJECT", "line 1 column 11 path $.a"); assertUnexpectedStructureError(expected, "a name", "END_OBJECT", "line 1 column 11 (char '\0') path $.a");
} }
try { try {
reader.beginArray(); reader.beginArray();
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
assertUnexpectedStructureError(expected, "BEGIN_ARRAY", "END_OBJECT", "line 1 column 11 path $.a"); assertUnexpectedStructureError(expected, "BEGIN_ARRAY", "END_OBJECT", "line 1 column 11 (char '\0') path $.a");
} }
try { try {
reader.endArray(); reader.endArray();
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
assertUnexpectedStructureError(expected, "END_ARRAY", "END_OBJECT", "line 1 column 11 path $.a"); assertUnexpectedStructureError(expected, "END_ARRAY", "END_OBJECT", "line 1 column 11 (char '\0') path $.a");
} }
reader.endObject(); reader.endObject();
assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT); assertThat(reader.peek()).isEqualTo(JsonToken.END_DOCUMENT);
@ -1162,7 +1162,7 @@ public final class JsonReaderTest {
reader.nextNull(); reader.nextNull();
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
assertUnexpectedStructureError(expected, "null", "STRING", "line 1 column 3 path $[0]"); assertUnexpectedStructureError(expected, "null", "STRING", "line 1 column 3 (char 'n') path $[0]");
} }
} }
@ -1174,7 +1174,7 @@ public final class JsonReaderTest {
reader.nextString(); reader.nextString();
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
assertUnexpectedStructureError(expected, "a string", "NULL", "line 1 column 6 path $[0]"); assertUnexpectedStructureError(expected, "a string", "NULL", "line 1 column 6 (char ']') path $[0]");
} }
} }
@ -1187,7 +1187,7 @@ public final class JsonReaderTest {
reader.nextBoolean(); reader.nextBoolean();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 6 path $.a"); assertStrictError(expected, "line 1 column 6 (char 't') path $.a");
} }
reader = new JsonReader(reader("{\"a\"=>true}")); reader = new JsonReader(reader("{\"a\"=>true}"));
@ -1197,7 +1197,7 @@ public final class JsonReaderTest {
reader.nextBoolean(); reader.nextBoolean();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 6 path $.a"); assertStrictError(expected, "line 1 column 6 (char '>') path $.a");
} }
} }
@ -1225,7 +1225,7 @@ public final class JsonReaderTest {
reader.skipValue(); reader.skipValue();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 6 path $.a"); assertStrictError(expected, "line 1 column 6 (char 't') path $.a");
} }
reader = new JsonReader(reader("{\"a\"=>true}")); reader = new JsonReader(reader("{\"a\"=>true}"));
@ -1235,7 +1235,7 @@ public final class JsonReaderTest {
reader.skipValue(); reader.skipValue();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 6 path $.a"); assertStrictError(expected, "line 1 column 6 (char '>') path $.a");
} }
} }
@ -1267,7 +1267,7 @@ public final class JsonReaderTest {
reader.nextBoolean(); reader.nextBoolean();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 3 path $[0]"); assertStrictError(expected, "line 1 column 3 (char '/') path $[0]");
} }
reader = new JsonReader(reader("[# comment \n true]")); reader = new JsonReader(reader("[# comment \n true]"));
@ -1276,7 +1276,7 @@ public final class JsonReaderTest {
reader.nextBoolean(); reader.nextBoolean();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 3 path $[0]"); assertStrictError(expected, "line 1 column 3 (char ' ') path $[0]");
} }
reader = new JsonReader(reader("[/* comment */ true]")); reader = new JsonReader(reader("[/* comment */ true]"));
@ -1285,7 +1285,7 @@ public final class JsonReaderTest {
reader.nextBoolean(); reader.nextBoolean();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 3 path $[0]"); assertStrictError(expected, "line 1 column 3 (char '*') path $[0]");
} }
} }
@ -1315,7 +1315,7 @@ public final class JsonReaderTest {
reader.skipValue(); reader.skipValue();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 3 path $[0]"); assertStrictError(expected, "line 1 column 3 (char '/') path $[0]");
} }
reader = new JsonReader(reader("[# comment \n true]")); reader = new JsonReader(reader("[# comment \n true]"));
@ -1324,7 +1324,7 @@ public final class JsonReaderTest {
reader.skipValue(); reader.skipValue();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 3 path $[0]"); assertStrictError(expected, "line 1 column 3 (char ' ') path $[0]");
} }
reader = new JsonReader(reader("[/* comment */ true]")); reader = new JsonReader(reader("[/* comment */ true]"));
@ -1333,7 +1333,7 @@ public final class JsonReaderTest {
reader.skipValue(); reader.skipValue();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 3 path $[0]"); assertStrictError(expected, "line 1 column 3 (char '*') path $[0]");
} }
} }
@ -1345,7 +1345,7 @@ public final class JsonReaderTest {
reader.nextName(); reader.nextName();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 3 path $."); assertStrictError(expected, "line 1 column 3 (char ':') path $.");
} }
} }
@ -1365,7 +1365,7 @@ public final class JsonReaderTest {
reader.skipValue(); reader.skipValue();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 3 path $."); assertStrictError(expected, "line 1 column 3 (char ':') path $.");
} }
} }
@ -1377,7 +1377,7 @@ public final class JsonReaderTest {
reader.nextName(); reader.nextName();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 3 path $."); assertStrictError(expected, "line 1 column 3 (char 'a') path $.");
} }
} }
@ -1397,7 +1397,7 @@ public final class JsonReaderTest {
reader.skipValue(); reader.skipValue();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 3 path $."); assertStrictError(expected, "line 1 column 3 (char 'a') path $.");
} }
} }
@ -1409,7 +1409,7 @@ public final class JsonReaderTest {
reader.nextString(); reader.nextString();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 2 path $[0]"); assertStrictError(expected, "line 1 column 2 (char 'a') path $[0]");
} }
} }
@ -1421,7 +1421,7 @@ public final class JsonReaderTest {
reader.skipValue(); reader.skipValue();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 2 path $[0]"); assertStrictError(expected, "line 1 column 2 (char 'a') path $[0]");
} }
} }
@ -1441,7 +1441,7 @@ public final class JsonReaderTest {
reader.nextString(); reader.nextString();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 3 path $[0]"); assertStrictError(expected, "line 1 column 3 (char 'a') path $[0]");
} }
} }
@ -1461,7 +1461,7 @@ public final class JsonReaderTest {
reader.skipValue(); reader.skipValue();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 3 path $[0]"); assertStrictError(expected, "line 1 column 3 (char 'a') path $[0]");
} }
} }
@ -1473,7 +1473,7 @@ public final class JsonReaderTest {
boolean unused = reader.nextBoolean(); boolean unused = reader.nextBoolean();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 2 path $[0]"); assertStrictError(expected, "line 1 column 2 (char 't') path $[0]");
} }
} }
@ -1494,7 +1494,7 @@ public final class JsonReaderTest {
reader.skipValue(); reader.skipValue();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 2 path $[0]"); assertStrictError(expected, "line 1 column 2 (char 't') path $[0]");
} }
} }
@ -1507,7 +1507,7 @@ public final class JsonReaderTest {
boolean unused = reader.nextBoolean(); boolean unused = reader.nextBoolean();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 6 path $.a"); assertStrictError(expected, "line 1 column 6 (char 't') path $.a");
} }
} }
@ -1530,7 +1530,7 @@ public final class JsonReaderTest {
reader.skipValue(); reader.skipValue();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 6 path $.a"); assertStrictError(expected, "line 1 column 6 (char 't') path $.a");
} }
} }
@ -1543,7 +1543,7 @@ public final class JsonReaderTest {
reader.nextNull(); reader.nextNull();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 8 path $[1]"); assertStrictError(expected, "line 1 column 8 (char 't') path $[1]");
} }
reader = new JsonReader(reader("[,true]")); reader = new JsonReader(reader("[,true]"));
@ -1552,7 +1552,7 @@ public final class JsonReaderTest {
reader.nextNull(); reader.nextNull();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 3 path $[0]"); assertStrictError(expected, "line 1 column 3 (char 't') path $[0]");
} }
reader = new JsonReader(reader("[true,]")); reader = new JsonReader(reader("[true,]"));
@ -1561,8 +1561,8 @@ public final class JsonReaderTest {
try { try {
reader.nextNull(); reader.nextNull();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (IllegalStateException expected) {
assertStrictError(expected, "line 1 column 8 path $[1]"); assertThat(expected).hasMessageThat().startsWith("Expected null but was END_ARRAY at line 1 column 8 (char '\0') path $[1]");
} }
reader = new JsonReader(reader("[,]")); reader = new JsonReader(reader("[,]"));
@ -1571,7 +1571,7 @@ public final class JsonReaderTest {
reader.nextNull(); reader.nextNull();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 3 path $[0]"); assertStrictError(expected, "line 1 column 3 (char ']') path $[0]");
} }
} }
@ -1616,7 +1616,7 @@ public final class JsonReaderTest {
reader.skipValue(); reader.skipValue();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 8 path $[1]"); assertStrictError(expected, "line 1 column 8 (char 't') path $[1]");
} }
reader = new JsonReader(reader("[,true]")); reader = new JsonReader(reader("[,true]"));
@ -1625,7 +1625,7 @@ public final class JsonReaderTest {
reader.skipValue(); reader.skipValue();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 3 path $[0]"); assertStrictError(expected, "line 1 column 3 (char 't') path $[0]");
} }
reader = new JsonReader(reader("[true,]")); reader = new JsonReader(reader("[true,]"));
@ -1634,8 +1634,8 @@ public final class JsonReaderTest {
try { try {
reader.skipValue(); reader.skipValue();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (IllegalStateException expected) {
assertStrictError(expected, "line 1 column 8 path $[1]"); assertThat(expected).hasMessageThat().isEqualTo("Attempt to skip led outside its parent");
} }
reader = new JsonReader(reader("[,]")); reader = new JsonReader(reader("[,]"));
@ -1644,7 +1644,7 @@ public final class JsonReaderTest {
reader.skipValue(); reader.skipValue();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 3 path $[0]"); assertStrictError(expected, "line 1 column 3 (char ']') path $[0]");
} }
} }
@ -1657,7 +1657,7 @@ public final class JsonReaderTest {
reader.peek(); reader.peek();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 5 path $"); assertStrictError(expected, "line 1 column 5 (char ']') path $");
} }
} }
@ -1682,7 +1682,7 @@ public final class JsonReaderTest {
reader.skipValue(); reader.skipValue();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 5 path $"); assertStrictError(expected, "line 1 column 5 (char ']') path $");
} }
} }
@ -1728,7 +1728,7 @@ public final class JsonReaderTest {
reader.beginArray(); reader.beginArray();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 1 path $"); assertStrictError(expected, "line 1 column 1 (char ')') path $");
} }
} }
@ -1739,7 +1739,7 @@ public final class JsonReaderTest {
reader.skipValue(); reader.skipValue();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 1 path $"); assertStrictError(expected, "line 1 column 1 (char ')') path $");
} }
} }
@ -1770,7 +1770,7 @@ public final class JsonReaderTest {
reader.nextString(); reader.nextString();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Unexpected value at line 1 column 3 path $" assertThat(expected).hasMessageThat().isEqualTo("Unexpected value at line 1 column 3 (char '}') path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
} }
} }
@ -1790,7 +1790,7 @@ public final class JsonReaderTest {
reader.endArray(); reader.endArray();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 2 path $[0]"); assertStrictError(expected, "line 1 column 2 (char '\uFEFF') path $[0]");
} }
} }
@ -1888,7 +1888,7 @@ public final class JsonReaderTest {
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertThat(expected).hasMessageThat().isEqualTo( assertThat(expected).hasMessageThat().isEqualTo(
"Expected value at line 1 column 14 path $[1].a[2]" "Expected value at line 1 column 14 (char '}') path $[1].a[2]"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
} }
} }
@ -1901,7 +1901,7 @@ public final class JsonReaderTest {
reader.nextDouble(); reader.nextDouble();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertStrictError(expected, "line 1 column 2 path $[0]"); assertStrictError(expected, "line 1 column 2 (char '0') path $[0]");
} }
} }
@ -1974,7 +1974,7 @@ public final class JsonReaderTest {
reader.peek(); reader.peek();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Expected value at line 1 column 1 path $" assertThat(expected).hasMessageThat().isEqualTo("Expected value at line 1 column 1 (char '/') path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
} }
} }
@ -1987,7 +1987,7 @@ public final class JsonReaderTest {
reader.peek(); reader.peek();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Expected value at line 1 column 10 path $" assertThat(expected).hasMessageThat().isEqualTo("Expected value at line 1 column 10 (char '/') path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
} }
} }
@ -2000,7 +2000,7 @@ public final class JsonReaderTest {
reader.peek(); reader.peek();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Expected value at line 1 column 1 path $" assertThat(expected).hasMessageThat().isEqualTo("Expected value at line 1 column 1 (char '/') path $"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
} }
} }
@ -2016,7 +2016,7 @@ public final class JsonReaderTest {
reader.peek(); reader.peek();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Unterminated object at line 1 column 16 path $.a" assertThat(expected).hasMessageThat().isEqualTo("Unterminated object at x at line 1 column 16 (char '\0') path $.a"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
} }
} }
@ -2145,7 +2145,7 @@ public final class JsonReaderTest {
reader.peek(); reader.peek();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Expected name at line 1 column 11 path $.a" assertThat(expected).hasMessageThat().isEqualTo("Expected name at line 1 column 11 (char '\0') path $.a"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
} }
} }
@ -2161,7 +2161,7 @@ public final class JsonReaderTest {
reader.peek(); reader.peek();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Expected name at line 1 column 11 path $.a" assertThat(expected).hasMessageThat().isEqualTo("Expected name at line 1 column 11 (char '\0') path $.a"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
} }
} }
@ -2229,7 +2229,7 @@ public final class JsonReaderTest {
reader.nextString(); reader.nextString();
fail(); fail();
} catch (MalformedJsonException expected) { } catch (MalformedJsonException expected) {
assertThat(expected).hasMessageThat().isEqualTo("Unterminated string at line 1 column 9 path $[0]" assertThat(expected).hasMessageThat().isEqualTo("Unterminated string at line 1 column 9 (char '[') path $[0]"
+ "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json"); + "\nSee https://github.com/google/gson/blob/main/Troubleshooting.md#malformed-json");
} }
} }

44
pom.xml
View File

@ -20,7 +20,7 @@
<groupId>io.gitlab.jfronny</groupId> <groupId>io.gitlab.jfronny</groupId>
<artifactId>gson-parent</artifactId> <artifactId>gson-parent</artifactId>
<version>2.10.2-SNAPSHOT</version> <version>2.10.3-SNAPSHOT</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<name>Gson Parent</name> <name>Gson Parent</name>
@ -99,36 +99,6 @@
</dependencyManagement> </dependencyManagement>
<build> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<id>enforce-versions</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireMavenVersion>
<!-- Usage of `.mvn/jvm.config` for Error Prone requires at least Maven 3.3.1 -->
<version>[3.3.1,)</version>
</requireMavenVersion>
<!-- Enforce that correct JDK version is used to avoid cryptic build errors -->
<requireJavaVersion>
<!-- Other plugins of this build require at least JDK 11 -->
<version>[11,)</version>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement> <pluginManagement>
<plugins> <plugins>
<plugin> <plugin>
@ -153,15 +123,14 @@
<configuration> <configuration>
<!-- Specify newer JDK as target to allow linking to newer Java API, and to generate <!-- Specify newer JDK as target to allow linking to newer Java API, and to generate
module overview in Javadoc for Gson's module descriptor --> module overview in Javadoc for Gson's module descriptor -->
<release>11</release> <release>17</release>
<!-- Exclude `missing` group because some tags have been omitted when they are redundant --> <!-- Exclude `missing` group because some tags have been omitted when they are redundant -->
<doclint>all,-missing</doclint> <doclint>all,-missing</doclint>
<!-- Link against newer Java API Javadoc because most users likely <!-- Link against newer Java API Javadoc because most users likely
use a newer Java version than the one used for building this project --> use a newer Java version than the one used for building this project -->
<detectJavaApiLink>false</detectJavaApiLink> <detectJavaApiLink>false</detectJavaApiLink>
<links> <links>
<link>https://docs.oracle.com/en/java/javase/11/docs/api/</link> <link>https://docs.oracle.com/en/java/javase/17/docs/api/</link>
<link>https://errorprone.info/api/latest/</link>
</links> </links>
<!-- Disable detection of offline links between Maven modules: <!-- Disable detection of offline links between Maven modules:
(1) Only `gson` module is published, so for other modules Javadoc links don't (1) Only `gson` module is published, so for other modules Javadoc links don't
@ -184,15 +153,10 @@
<artifactId>maven-jar-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version> <version>3.3.0</version>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId> <artifactId>maven-source-plugin</artifactId>
<version>3.3.0</version> <version>3.2.1</version>
</plugin> </plugin>
</plugins> </plugins>
</pluginManagement> </pluginManagement>