From c2a0e4634a2100494159add78db2ee06f5eb9be6 Mon Sep 17 00:00:00 2001 From: Maicol <79454487+MaicolAntali@users.noreply.github.com> Date: Fri, 10 Nov 2023 21:36:42 +0100 Subject: [PATCH] Updates the line separator from `CRLF` to `LF` (#2536) --- Troubleshooting.md | 714 +++++++++--------- .../main/java/com/google/gson/JsonNull.java | 130 ++-- .../main/java/com/google/gson/JsonParser.java | 278 +++---- .../gson/internal/$Gson$Preconditions.java | 114 +-- .../bind/TypeAdapterRuntimeTypeWrapper.java | 206 ++--- .../java/com/google/gson/GsonBuilderTest.java | 616 +++++++-------- .../gson/functional/ConcurrencyTest.java | 298 ++++---- .../functional/InternationalizationTest.java | 166 ++-- .../functional/NullObjectAndFieldTest.java | 534 ++++++------- .../gson/internal/LazilyParsedNumberTest.java | 108 +-- 10 files changed, 1582 insertions(+), 1582 deletions(-) diff --git a/Troubleshooting.md b/Troubleshooting.md index 8289c93c..be4000f4 100644 --- a/Troubleshooting.md +++ b/Troubleshooting.md @@ -1,357 +1,357 @@ -# Troubleshooting Guide - -This guide describes how to troubleshoot common issues when using Gson. - - - - -## `ClassCastException` when using deserialized object - -**Symptom:** `ClassCastException` is thrown when accessing an object deserialized by Gson - -**Reason:** Your code is most likely not type-safe - -**Solution:** Make sure your code adheres to the following: - -- Avoid raw types: Instead of calling `fromJson(..., List.class)`, create for example a `TypeToken>`. - See the [user guide](UserGuide.md#collections-examples) for more information. -- When using `TypeToken` prefer the `Gson.fromJson` overloads with `TypeToken` parameter such as [`fromJson(Reader, TypeToken)`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/Gson.html#fromJson(java.io.Reader,com.google.gson.reflect.TypeToken)). - The overloads with `Type` parameter do not provide any type-safety guarantees. -- When using `TypeToken` make sure you don't capture a type variable. For example avoid something like `new TypeToken>()` (where `T` is a type variable). Due to Java [type erasure](https://dev.java/learn/generics/type-erasure/) the actual type of `T` is not available at runtime. Refactor your code to pass around `TypeToken` instances or use [`TypeToken.getParameterized(...)`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/reflect/TypeToken.html#getParameterized(java.lang.reflect.Type,java.lang.reflect.Type...)), for example `TypeToken.getParameterized(List.class, elementType)` where `elementType` is a type you have to provide separately. - -## `InaccessibleObjectException`: 'module ... does not "opens ..." to unnamed module' - -**Symptom:** An exception with a message in the form 'module ... does not "opens ..." to unnamed module' is thrown - -**Reason:** You use Gson by accident to access internal fields of third-party classes - -**Solution:** Write custom Gson [`TypeAdapter`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/TypeAdapter.html) implementations for the affected classes or change the type of your data. If this occurs for a field in one of your classes which you did not actually want to serialize or deserialize in the first place, you can exclude that field, see the [user guide](UserGuide.md#excluding-fields-from-serialization-and-deserialization). - -**Explanation:** - -When no built-in adapter for a type exists and no custom adapter has been registered, Gson falls back to using reflection to access the fields of a class (including `private` ones). Most likely you are seeing this error because you (by accident) rely on the reflection-based adapter for third-party classes. That should be avoided because you make yourself dependent on the implementation details of these classes which could change at any point. For the JDK it is also not possible anymore to access internal fields using reflection starting with JDK 17, see [JEP 403](https://openjdk.org/jeps/403). - -If you want to prevent using reflection on third-party classes in the future you can write your own [`ReflectionAccessFilter`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/ReflectionAccessFilter.html) or use one of the predefined ones, such as `ReflectionAccessFilter.BLOCK_ALL_PLATFORM`. - -## `InaccessibleObjectException`: 'module ... does not "opens ..." to module com.google.gson' - -**Symptom:** An exception with a message in the form 'module ... does not "opens ..." to module com.google.gson' is thrown - -**Reason:** - -- If the reported package is your own package then you have not configured the module declaration of your project to allow Gson to use reflection on your classes. -- If the reported package is from a third party library or the JDK see [this troubleshooting point](#inaccessibleobjectexception-module--does-not-opens--to-unnamed-module). - -**Solution:** Make sure the `module-info.java` file of your project allows Gson to use reflection on your classes, for example: - -```java -module mymodule { - requires com.google.gson; - - opens mypackage to com.google.gson; -} -``` - -Or in case this occurs for a field in one of your classes which you did not actually want to serialize or deserialize in the first place, you can exclude that field, see the [user guide](UserGuide.md#excluding-fields-from-serialization-and-deserialization). - -## Android app not working in Release mode; random property names - -**Symptom:** Your Android app is working fine in Debug mode but fails in Release mode and the JSON properties have seemingly random names such as `a`, `b`, ... - -**Reason:** You probably have not configured ProGuard / R8 correctly - -**Solution:** Make sure you have configured ProGuard / R8 correctly to preserve the names of your fields. See the [Android example](examples/android-proguard-example/README.md) for more information. - -## Android app unable to parse JSON after app update - -**Symptom:** You released a new version of your Android app and it fails to parse JSON data created by the previous version of your app - -**Reason:** You probably have not configured ProGuard / R8 correctly; probably the field names are being obfuscated and their naming changed between the versions of your app - -**Solution:** Make sure you have configured ProGuard / R8 correctly to preserve the names of your fields. See the [Android example](examples/android-proguard-example/README.md) for more information. - -If you want to preserve backward compatibility for you app you can use [`@SerializedName`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/annotations/SerializedName.html) on the fields to specify the obfuscated name as alternate, for example: `@SerializedName(value = "myprop", alternate = "a")` - -Normally ProGuard and R8 produce a mapping file, this makes it easier to find out the obfuscated field names instead of having to find them out through trial and error or other means. See the [Android Studio user guide](https://developer.android.com/studio/build/shrink-code.html#retracing) for more information. - -## Default field values not present after deserialization - -**Symptom:** You have assign default values to fields but after deserialization the fields have their standard value (such as `null` or `0`) - -**Reason:** Gson cannot invoke the constructor of your class and falls back to JDK `Unsafe` (or similar means) - -**Solution:** Make sure that the class: - -- is `static` (explicitly or implicitly when it is a top-level class) -- has a no-args constructor - -Otherwise Gson will by default try to use JDK `Unsafe` or similar means to create an instance of your class without invoking the constructor and without running any initializers. You can also disable that behavior through [`GsonBuilder.disableJdkUnsafe()`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/GsonBuilder.html#disableJdkUnsafe()) to notice such issues early on. - -## `null` values for anonymous and local classes - -**Symptom:** Objects of a class are always serialized as JSON `null` / always deserialized as Java `null` - -**Reason:** The class you are serializing or deserializing is an anonymous or a local class (or you have specified a custom `ExclusionStrategy`) - -**Solution:** Convert the class to a `static` nested class. If the class is already `static` make sure you have not specified a Gson `ExclusionStrategy` which might exclude the class. - -Notes: - -- "double brace-initialization" also creates anonymous classes -- Local record classes (feature added in Java 16) are supported by Gson and are not affected by this - -## Map keys having unexpected format in JSON - -**Symptom:** JSON output for `Map` keys is unexpected / cannot be deserialized again - -**Reason:** The `Map` key type is 'complex' and you have not configured the `GsonBuilder` properly - -**Solution:** Use [`GsonBuilder.enableComplexMapKeySerialization()`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/GsonBuilder.html#enableComplexMapKeySerialization()). See also the [user guide](UserGuide.md#maps-examples) for more information. - -## Parsing JSON fails with `MalformedJsonException` - -**Symptom:** JSON parsing fails with `MalformedJsonException` - -**Reason:** The JSON data is actually malformed - -**Solution:** During debugging, log the JSON data right before calling Gson methods or set a breakpoint to inspect the data and make sure it has the expected format. Sometimes APIs might return HTML error pages (instead of JSON data) when reaching rate limits or when other errors occur. Also read the location information of the `MalformedJsonException` exception message, it indicates where exactly in the document the malformed data was detected, including the [JSONPath](https://goessner.net/articles/JsonPath/). - -For example, let's assume you want to deserialize the following JSON data: - -```json -{ - "languages": [ - "English", - "French", - ] -} -``` - -This will fail with an exception similar to this one: `MalformedJsonException: Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed JSON at line 5 column 4 path $.languages[2]` -The problem here is the trailing comma (`,`) after `"French"`, trailing commas are not allowed by the JSON specification. The location information "line 5 column 4" points to the `]` in the JSON data (with some slight inaccuracies) because Gson expected another value after `,` instead of the closing `]`. The JSONPath `$.languages[2]` in the exception message also points there: `$.` refers to the root object, `languages` refers to its member of that name and `[2]` refers to the (missing) third value in the JSON array value of that member (numbering starts at 0, so it is `[2]` instead of `[3]`). -The proper solution here is to fix the malformed JSON data. - -To spot syntax errors in the JSON data easily you can open it in an editor with support for JSON, for example Visual Studio Code. It will highlight within the JSON data the error location and show why the JSON data is considered invalid. - -## Integral JSON number is parsed as `double` - -**Symptom:** JSON data contains an integral number such as `45` but Gson returns it as `double` - -**Reason:** When parsing a JSON number as `Object`, Gson will by default always return a `double` - -**Solution:** Use [`GsonBuilder.setObjectToNumberStrategy`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/GsonBuilder.html#setObjectToNumberStrategy(com.google.gson.ToNumberStrategy)) to specify what type of number should be returned - -## Malformed JSON not rejected - -**Symptom:** Gson parses malformed JSON without throwing any exceptions - -**Reason:** Due to legacy reasons Gson performs parsing by default in lenient mode - -**Solution:** If you are using Gson 2.11.0 or newer, call [`GsonBuilder.setStrictness`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/GsonBuilder.html#setStrictness(com.google.gson.Strictness)), -[`JsonReader.setStrictness`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/stream/JsonReader.html#setStrictness(com.google.gson.Strictness)) -and [`JsonWriter.setStrictness`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/stream/JsonWriter.html#setStrictness(com.google.gson.Strictness)) -with `Strictness.STRICT` to overwrite the default lenient behavior of `Gson` and make these classes strictly adhere to the JSON specification. -Otherwise if you are using an older Gson version, see the [`Gson` class documentation](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/Gson.html#default-lenient) -section "JSON Strictness handling" for alternative solutions. - -## `IllegalStateException`: "Expected ... but was ..." - -**Symptom:** An `IllegalStateException` with a message in the form "Expected ... but was ..." is thrown - -**Reason:** The JSON data does not have the correct format - -**Solution:** Make sure that your classes correctly model the JSON data. Also during debugging log the JSON data right before calling Gson methods or set a breakpoint to inspect the data and make sure it has the expected format. Read the location information of the exception message, it indicates where exactly in the document the error occurred, including the [JSONPath](https://goessner.net/articles/JsonPath/). - -For example, let's assume you have the following Java class: - -```java -class WebPage { - String languages; -} -``` - -And you want to deserialize the following JSON data: - -```json -{ - "languages": ["English", "French"] -} -``` - -This will fail with an exception similar to this one: `IllegalStateException: Expected a string but was BEGIN_ARRAY at line 2 column 17 path $.languages` -This means Gson expected a JSON string value but found the beginning of a JSON array (`[`). The location information "line 2 column 17" points to the `[` in the JSON data (with some slight inaccuracies), so does the JSONPath `$.languages` in the exception message. It refers to the `languages` member of the root object (`$.`). -The solution here is to change in the `WebPage` class the field `String languages` to `List languages`. - -## `IllegalStateException`: "Expected ... but was NULL" - -**Symptom:** An `IllegalStateException` with a message in the form "Expected ... but was NULL" is thrown - -**Reason:** - -- A built-in adapter does not support JSON null values -- You have written a custom `TypeAdapter` which does not properly handle JSON null values - -**Solution:** If this occurs for a custom adapter you wrote, add code similar to the following at the beginning of its `read` method: - -```java -@Override -public MyClass read(JsonReader in) throws IOException { - if (in.peek() == JsonToken.NULL) { - in.nextNull(); - return null; - } - - ... -} -``` - -Alternatively you can call [`nullSafe()`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/TypeAdapter.html#nullSafe()) on the adapter instance you created. - -## Properties missing in JSON - -**Symptom:** Properties are missing in the JSON output - -**Reason:** Gson by default omits JSON null from the output (or: ProGuard / R8 is not configured correctly and removed unused fields) - -**Solution:** Use [`GsonBuilder.serializeNulls()`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/GsonBuilder.html#serializeNulls()) - -Note: Gson does not support anonymous and local classes and will serialize them as JSON null, see the [related troubleshooting point](#null-values-for-anonymous-and-local-classes). - -## JSON output changes for newer Android versions - -**Symptom:** The JSON output differs when running on newer Android versions - -**Reason:** You use Gson by accident to access internal fields of Android classes - -**Solution:** Write custom Gson [`TypeAdapter`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/TypeAdapter.html) implementations for the affected classes or change the type of your data - -**Explanation:** - -When no built-in adapter for a type exists and no custom adapter has been registered, Gson falls back to using reflection to access the fields of a class (including `private` ones). Most likely you are experiencing this issue because you (by accident) rely on the reflection-based adapter for Android classes. That should be avoided because you make yourself dependent on the implementation details of these classes which could change at any point. - -If you want to prevent using reflection on third-party classes in the future you can write your own [`ReflectionAccessFilter`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/ReflectionAccessFilter.html) or use one of the predefined ones, such as `ReflectionAccessFilter.BLOCK_ALL_PLATFORM`. - -## JSON output contains values of `static` fields - -**Symptom:** The JSON output contains values of `static` fields - -**Reason:** You used `GsonBuilder.excludeFieldsWithModifiers` to overwrite the default excluded modifiers - -**Solution:** When calling `GsonBuilder.excludeFieldsWithModifiers` you overwrite the default excluded modifiers. Therefore, you have to explicitly exclude `static` fields if desired. This can be done by adding `Modifier.STATIC` as additional argument. - -## `NoSuchMethodError` when calling Gson methods - -**Symptom:** A `java.lang.NoSuchMethodError` is thrown when trying to call certain Gson methods - -**Reason:** - -- You have multiple versions of Gson on your classpath -- Or, the Gson version you compiled against is different from the one on your classpath -- Or, you are using a code shrinking tool such as ProGuard or R8 which removed methods from Gson - -**Solution:** First disable any code shrinking tools such as ProGuard or R8 and check if the issue persists. If not, you have to tweak the configuration of that tool to not modify Gson classes. Otherwise verify that the Gson JAR on your classpath is the same you are compiling against, and that there is only one Gson JAR on your classpath. See [this Stack Overflow question](https://stackoverflow.com/q/227486) to find out where a class is loaded from. For example, for debugging you could include the following code: - -```java -System.out.println(Gson.class.getProtectionDomain().getCodeSource().getLocation()); -``` - -If that fails with a `NullPointerException` you have to try one of the other ways to find out where a class is loaded from. - -## `IllegalArgumentException`: 'Class ... declares multiple JSON fields named '...'' - -**Symptom:** An exception with the message 'Class ... declares multiple JSON fields named '...'' is thrown - -**Reason:** - -- The name you have specified with a [`@SerializedName`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/annotations/SerializedName.html) annotation for a field collides with the name of another field -- The [`FieldNamingStrategy`](https://javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/FieldNamingStrategy.html) you have specified produces conflicting field names -- A field of your class has the same name as the field of a superclass - -Gson prevents multiple fields with the same name because during deserialization it would be ambiguous for which field the JSON data should be deserialized. For serialization it would cause the same field to appear multiple times in JSON. While the JSON specification permits this, it is likely that the application parsing the JSON data will not handle it correctly. - -**Solution:** First identify the fields with conflicting names based on the exception message. Then decide if you want to rename one of them using the [`@SerializedName`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/annotations/SerializedName.html) annotation, or if you want to [exclude](UserGuide.md#excluding-fields-from-serialization-and-deserialization) one of them. When excluding one of the fields you have to include it for both serialization and deserialization (even if your application only performs one of these actions) because the duplicate field check cannot differentiate between these actions. - -## `UnsupportedOperationException` when serializing or deserializing `java.lang.Class` - -**Symptom:** An `UnsupportedOperationException` is thrown when trying to serialize or deserialize `java.lang.Class` - -**Reason:** Gson intentionally does not permit serializing and deserializing `java.lang.Class` for security reasons. Otherwise a malicious user could make your application load an arbitrary class from the classpath and, depending on what your application does with the `Class`, in the worst case perform a remote code execution attack. - -**Solution:** First check if you really need to serialize or deserialize a `Class`. Often it is possible to use string aliases and then map them to the known `Class`; you could write a custom [`TypeAdapter`](https://javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/TypeAdapter.html) to do this. If the `Class` values are not known in advance, try to introduce a common base class or interface for all these classes and then verify that the deserialized class is a subclass. For example assuming the base class is called `MyBaseClass`, your custom `TypeAdapter` should load the class like this: - -```java -Class.forName(jsonString, false, getClass().getClassLoader()).asSubclass(MyBaseClass.class) -``` - -This will not initialize arbitrary classes, and it will throw a `ClassCastException` if the loaded class is not the same as or a subclass of `MyBaseClass`. - -## `IllegalStateException`: 'TypeToken must be created with a type argument'
`RuntimeException`: 'Missing type parameter' - -**Symptom:** An `IllegalStateException` with the message 'TypeToken must be created with a type argument' is thrown. -For older Gson versions a `RuntimeException` with message 'Missing type parameter' is thrown. - -**Reason:** - -- You created a `TypeToken` without type argument, for example `new TypeToken() {}` (note the missing `<...>`). You always have to provide the type argument, for example like this: `new TypeToken>() {}`. Normally the compiler will also emit a 'raw types' warning when you forget the `<...>`. -- You are using a code shrinking tool such as ProGuard or R8 (Android app builds normally have this enabled by default) but have not configured it correctly for usage with Gson. - -**Solution:** When you are using a code shrinking tool such as ProGuard or R8 you have to adjust your configuration to include the following rules: - -``` -# Keep generic signatures; needed for correct type resolution --keepattributes Signature - -# Keep class TypeToken (respectively its generic signature) --keep class com.google.gson.reflect.TypeToken { *; } - -# Keep any (anonymous) classes extending TypeToken --keep class * extends com.google.gson.reflect.TypeToken -``` - -See also the [Android example](examples/android-proguard-example/README.md) for more information. - -Note: For newer Gson versions these rules might be applied automatically; make sure you are using the latest Gson version and the latest version of the code shrinking tool. - -## `JsonIOException`: 'Abstract classes can't be instantiated!' (R8) - -**Symptom:** A `JsonIOException` with the message 'Abstract classes can't be instantiated!' is thrown; the class mentioned in the exception message is not actually `abstract` in your source code, and you are using the code shrinking tool R8 (Android app builds normally have this configured by default). - -Note: If the class which you are trying to deserialize is actually abstract, then this exception is probably unrelated to R8 and you will have to implement a custom [`InstanceCreator`](https://javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/InstanceCreator.html) or [`TypeAdapter`](https://javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/TypeAdapter.html) which creates an instance of a non-abstract subclass of the class. - -**Reason:** The code shrinking tool R8 performs optimizations where it removes the no-args constructor from a class and makes the class `abstract`. Due to this Gson cannot create an instance of the class. - -**Solution:** Make sure the class has a no-args constructor, then adjust your R8 configuration file to keep the constructor of the class. For example: - -``` -# Keep the no-args constructor of the deserialized class --keepclassmembers class com.example.MyClass { - (); -} -``` - -You can also use `(...);` to keep all constructors of that class, but then you might actually rely on `sun.misc.Unsafe` on both JDK and Android to create classes without no-args constructor, see [`GsonBuilder.disableJdkUnsafe()`](https://javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/GsonBuilder.html#disableJdkUnsafe()) for more information. - -For Android you can add this rule to the `proguard-rules.pro` file, see also the [Android documentation](https://developer.android.com/build/shrink-code#keep-code). In case the class name in the exception message is obfuscated, see the Android documentation about [retracing](https://developer.android.com/build/shrink-code#retracing). - -For Android you can alternatively use the [`@Keep` annotation](https://developer.android.com/studio/write/annotations#keep) on the class or constructor you want to keep. That might be easier than having to maintain a custom R8 configuration. - -Note that the latest Gson versions (> 2.10.1) specify a default R8 configuration. If your class is a top-level class or is `static`, has a no-args constructor and its fields are annotated with Gson's [`@SerializedName`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/annotations/SerializedName.html), you might not have to perform any additional R8 configuration. - -## `IllegalArgumentException`: 'TypeToken type argument must not contain a type variable' - -**Symptom:** An exception with the message 'TypeToken type argument must not contain a type variable' is thrown - -**Reason:** This exception is thrown when you create an anonymous `TypeToken` subclass which captures a type variable, for example `new TypeToken>() {}` (where `T` is a type variable). At compile time such code looks safe and you can use the type `List` without any warnings. However, this code is not actually type-safe because at runtime due to [type erasure](https://dev.java/learn/generics/type-erasure/) only the upper bound of the type variable is available. For the previous example that would be `List`. When using such a `TypeToken` with any Gson methods performing deserialization this would lead to confusing and difficult to debug `ClassCastException`s. For serialization it can in some cases also lead to undesired results. - -Note: Earlier version of Gson unfortunately did not prevent capturing type variables, which caused many users to unwittingly write type-unsafe code. - -**Solution:** - -- Use [`TypeToken.getParameterized(...)`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/reflect/TypeToken.html#getParameterized(java.lang.reflect.Type,java.lang.reflect.Type...)), for example `TypeToken.getParameterized(List.class, elementType)` where `elementType` is a type you have to provide separately. -- For Kotlin users: Use [`reified` type parameters](https://kotlinlang.org/docs/inline-functions.html#reified-type-parameters), that means change `` to ``, if possible. If you have a chain of functions with type parameters you will probably have to make all of them `reified`. -- If you don't actually use Gson's `TypeToken` for any Gson method, use a general purpose 'type token' implementation provided by a different library instead, for example Guava's [`com.google.common.reflect.TypeToken`](https://javadoc.io/doc/com.google.guava/guava/latest/com/google/common/reflect/TypeToken.html). - -For backward compatibility it is possible to restore Gson's old behavior of allowing `TypeToken` to capture type variables by setting the [system property](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/System.html#setProperty(java.lang.String,java.lang.String)) `gson.allowCapturingTypeVariables` to `"true"`, **however**: - -- This does not solve any of the type-safety problems mentioned above; in the long term you should prefer one of the other solutions listed above. This system property might be removed in future Gson versions. -- You should only ever set the property to `"true"`, but never to any other value or manually clear it. Otherwise this might counteract any libraries you are using which might have deliberately set the system property because they rely on its behavior. +# Troubleshooting Guide + +This guide describes how to troubleshoot common issues when using Gson. + + + + +## `ClassCastException` when using deserialized object + +**Symptom:** `ClassCastException` is thrown when accessing an object deserialized by Gson + +**Reason:** Your code is most likely not type-safe + +**Solution:** Make sure your code adheres to the following: + +- Avoid raw types: Instead of calling `fromJson(..., List.class)`, create for example a `TypeToken>`. + See the [user guide](UserGuide.md#collections-examples) for more information. +- When using `TypeToken` prefer the `Gson.fromJson` overloads with `TypeToken` parameter such as [`fromJson(Reader, TypeToken)`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/Gson.html#fromJson(java.io.Reader,com.google.gson.reflect.TypeToken)). + The overloads with `Type` parameter do not provide any type-safety guarantees. +- When using `TypeToken` make sure you don't capture a type variable. For example avoid something like `new TypeToken>()` (where `T` is a type variable). Due to Java [type erasure](https://dev.java/learn/generics/type-erasure/) the actual type of `T` is not available at runtime. Refactor your code to pass around `TypeToken` instances or use [`TypeToken.getParameterized(...)`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/reflect/TypeToken.html#getParameterized(java.lang.reflect.Type,java.lang.reflect.Type...)), for example `TypeToken.getParameterized(List.class, elementType)` where `elementType` is a type you have to provide separately. + +## `InaccessibleObjectException`: 'module ... does not "opens ..." to unnamed module' + +**Symptom:** An exception with a message in the form 'module ... does not "opens ..." to unnamed module' is thrown + +**Reason:** You use Gson by accident to access internal fields of third-party classes + +**Solution:** Write custom Gson [`TypeAdapter`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/TypeAdapter.html) implementations for the affected classes or change the type of your data. If this occurs for a field in one of your classes which you did not actually want to serialize or deserialize in the first place, you can exclude that field, see the [user guide](UserGuide.md#excluding-fields-from-serialization-and-deserialization). + +**Explanation:** + +When no built-in adapter for a type exists and no custom adapter has been registered, Gson falls back to using reflection to access the fields of a class (including `private` ones). Most likely you are seeing this error because you (by accident) rely on the reflection-based adapter for third-party classes. That should be avoided because you make yourself dependent on the implementation details of these classes which could change at any point. For the JDK it is also not possible anymore to access internal fields using reflection starting with JDK 17, see [JEP 403](https://openjdk.org/jeps/403). + +If you want to prevent using reflection on third-party classes in the future you can write your own [`ReflectionAccessFilter`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/ReflectionAccessFilter.html) or use one of the predefined ones, such as `ReflectionAccessFilter.BLOCK_ALL_PLATFORM`. + +## `InaccessibleObjectException`: 'module ... does not "opens ..." to module com.google.gson' + +**Symptom:** An exception with a message in the form 'module ... does not "opens ..." to module com.google.gson' is thrown + +**Reason:** + +- If the reported package is your own package then you have not configured the module declaration of your project to allow Gson to use reflection on your classes. +- If the reported package is from a third party library or the JDK see [this troubleshooting point](#inaccessibleobjectexception-module--does-not-opens--to-unnamed-module). + +**Solution:** Make sure the `module-info.java` file of your project allows Gson to use reflection on your classes, for example: + +```java +module mymodule { + requires com.google.gson; + + opens mypackage to com.google.gson; +} +``` + +Or in case this occurs for a field in one of your classes which you did not actually want to serialize or deserialize in the first place, you can exclude that field, see the [user guide](UserGuide.md#excluding-fields-from-serialization-and-deserialization). + +## Android app not working in Release mode; random property names + +**Symptom:** Your Android app is working fine in Debug mode but fails in Release mode and the JSON properties have seemingly random names such as `a`, `b`, ... + +**Reason:** You probably have not configured ProGuard / R8 correctly + +**Solution:** Make sure you have configured ProGuard / R8 correctly to preserve the names of your fields. See the [Android example](examples/android-proguard-example/README.md) for more information. + +## Android app unable to parse JSON after app update + +**Symptom:** You released a new version of your Android app and it fails to parse JSON data created by the previous version of your app + +**Reason:** You probably have not configured ProGuard / R8 correctly; probably the field names are being obfuscated and their naming changed between the versions of your app + +**Solution:** Make sure you have configured ProGuard / R8 correctly to preserve the names of your fields. See the [Android example](examples/android-proguard-example/README.md) for more information. + +If you want to preserve backward compatibility for you app you can use [`@SerializedName`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/annotations/SerializedName.html) on the fields to specify the obfuscated name as alternate, for example: `@SerializedName(value = "myprop", alternate = "a")` + +Normally ProGuard and R8 produce a mapping file, this makes it easier to find out the obfuscated field names instead of having to find them out through trial and error or other means. See the [Android Studio user guide](https://developer.android.com/studio/build/shrink-code.html#retracing) for more information. + +## Default field values not present after deserialization + +**Symptom:** You have assign default values to fields but after deserialization the fields have their standard value (such as `null` or `0`) + +**Reason:** Gson cannot invoke the constructor of your class and falls back to JDK `Unsafe` (or similar means) + +**Solution:** Make sure that the class: + +- is `static` (explicitly or implicitly when it is a top-level class) +- has a no-args constructor + +Otherwise Gson will by default try to use JDK `Unsafe` or similar means to create an instance of your class without invoking the constructor and without running any initializers. You can also disable that behavior through [`GsonBuilder.disableJdkUnsafe()`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/GsonBuilder.html#disableJdkUnsafe()) to notice such issues early on. + +## `null` values for anonymous and local classes + +**Symptom:** Objects of a class are always serialized as JSON `null` / always deserialized as Java `null` + +**Reason:** The class you are serializing or deserializing is an anonymous or a local class (or you have specified a custom `ExclusionStrategy`) + +**Solution:** Convert the class to a `static` nested class. If the class is already `static` make sure you have not specified a Gson `ExclusionStrategy` which might exclude the class. + +Notes: + +- "double brace-initialization" also creates anonymous classes +- Local record classes (feature added in Java 16) are supported by Gson and are not affected by this + +## Map keys having unexpected format in JSON + +**Symptom:** JSON output for `Map` keys is unexpected / cannot be deserialized again + +**Reason:** The `Map` key type is 'complex' and you have not configured the `GsonBuilder` properly + +**Solution:** Use [`GsonBuilder.enableComplexMapKeySerialization()`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/GsonBuilder.html#enableComplexMapKeySerialization()). See also the [user guide](UserGuide.md#maps-examples) for more information. + +## Parsing JSON fails with `MalformedJsonException` + +**Symptom:** JSON parsing fails with `MalformedJsonException` + +**Reason:** The JSON data is actually malformed + +**Solution:** During debugging, log the JSON data right before calling Gson methods or set a breakpoint to inspect the data and make sure it has the expected format. Sometimes APIs might return HTML error pages (instead of JSON data) when reaching rate limits or when other errors occur. Also read the location information of the `MalformedJsonException` exception message, it indicates where exactly in the document the malformed data was detected, including the [JSONPath](https://goessner.net/articles/JsonPath/). + +For example, let's assume you want to deserialize the following JSON data: + +```json +{ + "languages": [ + "English", + "French", + ] +} +``` + +This will fail with an exception similar to this one: `MalformedJsonException: Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed JSON at line 5 column 4 path $.languages[2]` +The problem here is the trailing comma (`,`) after `"French"`, trailing commas are not allowed by the JSON specification. The location information "line 5 column 4" points to the `]` in the JSON data (with some slight inaccuracies) because Gson expected another value after `,` instead of the closing `]`. The JSONPath `$.languages[2]` in the exception message also points there: `$.` refers to the root object, `languages` refers to its member of that name and `[2]` refers to the (missing) third value in the JSON array value of that member (numbering starts at 0, so it is `[2]` instead of `[3]`). +The proper solution here is to fix the malformed JSON data. + +To spot syntax errors in the JSON data easily you can open it in an editor with support for JSON, for example Visual Studio Code. It will highlight within the JSON data the error location and show why the JSON data is considered invalid. + +## Integral JSON number is parsed as `double` + +**Symptom:** JSON data contains an integral number such as `45` but Gson returns it as `double` + +**Reason:** When parsing a JSON number as `Object`, Gson will by default always return a `double` + +**Solution:** Use [`GsonBuilder.setObjectToNumberStrategy`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/GsonBuilder.html#setObjectToNumberStrategy(com.google.gson.ToNumberStrategy)) to specify what type of number should be returned + +## Malformed JSON not rejected + +**Symptom:** Gson parses malformed JSON without throwing any exceptions + +**Reason:** Due to legacy reasons Gson performs parsing by default in lenient mode + +**Solution:** If you are using Gson 2.11.0 or newer, call [`GsonBuilder.setStrictness`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/GsonBuilder.html#setStrictness(com.google.gson.Strictness)), +[`JsonReader.setStrictness`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/stream/JsonReader.html#setStrictness(com.google.gson.Strictness)) +and [`JsonWriter.setStrictness`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/stream/JsonWriter.html#setStrictness(com.google.gson.Strictness)) +with `Strictness.STRICT` to overwrite the default lenient behavior of `Gson` and make these classes strictly adhere to the JSON specification. +Otherwise if you are using an older Gson version, see the [`Gson` class documentation](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/Gson.html#default-lenient) +section "JSON Strictness handling" for alternative solutions. + +## `IllegalStateException`: "Expected ... but was ..." + +**Symptom:** An `IllegalStateException` with a message in the form "Expected ... but was ..." is thrown + +**Reason:** The JSON data does not have the correct format + +**Solution:** Make sure that your classes correctly model the JSON data. Also during debugging log the JSON data right before calling Gson methods or set a breakpoint to inspect the data and make sure it has the expected format. Read the location information of the exception message, it indicates where exactly in the document the error occurred, including the [JSONPath](https://goessner.net/articles/JsonPath/). + +For example, let's assume you have the following Java class: + +```java +class WebPage { + String languages; +} +``` + +And you want to deserialize the following JSON data: + +```json +{ + "languages": ["English", "French"] +} +``` + +This will fail with an exception similar to this one: `IllegalStateException: Expected a string but was BEGIN_ARRAY at line 2 column 17 path $.languages` +This means Gson expected a JSON string value but found the beginning of a JSON array (`[`). The location information "line 2 column 17" points to the `[` in the JSON data (with some slight inaccuracies), so does the JSONPath `$.languages` in the exception message. It refers to the `languages` member of the root object (`$.`). +The solution here is to change in the `WebPage` class the field `String languages` to `List languages`. + +## `IllegalStateException`: "Expected ... but was NULL" + +**Symptom:** An `IllegalStateException` with a message in the form "Expected ... but was NULL" is thrown + +**Reason:** + +- A built-in adapter does not support JSON null values +- You have written a custom `TypeAdapter` which does not properly handle JSON null values + +**Solution:** If this occurs for a custom adapter you wrote, add code similar to the following at the beginning of its `read` method: + +```java +@Override +public MyClass read(JsonReader in) throws IOException { + if (in.peek() == JsonToken.NULL) { + in.nextNull(); + return null; + } + + ... +} +``` + +Alternatively you can call [`nullSafe()`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/TypeAdapter.html#nullSafe()) on the adapter instance you created. + +## Properties missing in JSON + +**Symptom:** Properties are missing in the JSON output + +**Reason:** Gson by default omits JSON null from the output (or: ProGuard / R8 is not configured correctly and removed unused fields) + +**Solution:** Use [`GsonBuilder.serializeNulls()`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/GsonBuilder.html#serializeNulls()) + +Note: Gson does not support anonymous and local classes and will serialize them as JSON null, see the [related troubleshooting point](#null-values-for-anonymous-and-local-classes). + +## JSON output changes for newer Android versions + +**Symptom:** The JSON output differs when running on newer Android versions + +**Reason:** You use Gson by accident to access internal fields of Android classes + +**Solution:** Write custom Gson [`TypeAdapter`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/TypeAdapter.html) implementations for the affected classes or change the type of your data + +**Explanation:** + +When no built-in adapter for a type exists and no custom adapter has been registered, Gson falls back to using reflection to access the fields of a class (including `private` ones). Most likely you are experiencing this issue because you (by accident) rely on the reflection-based adapter for Android classes. That should be avoided because you make yourself dependent on the implementation details of these classes which could change at any point. + +If you want to prevent using reflection on third-party classes in the future you can write your own [`ReflectionAccessFilter`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/ReflectionAccessFilter.html) or use one of the predefined ones, such as `ReflectionAccessFilter.BLOCK_ALL_PLATFORM`. + +## JSON output contains values of `static` fields + +**Symptom:** The JSON output contains values of `static` fields + +**Reason:** You used `GsonBuilder.excludeFieldsWithModifiers` to overwrite the default excluded modifiers + +**Solution:** When calling `GsonBuilder.excludeFieldsWithModifiers` you overwrite the default excluded modifiers. Therefore, you have to explicitly exclude `static` fields if desired. This can be done by adding `Modifier.STATIC` as additional argument. + +## `NoSuchMethodError` when calling Gson methods + +**Symptom:** A `java.lang.NoSuchMethodError` is thrown when trying to call certain Gson methods + +**Reason:** + +- You have multiple versions of Gson on your classpath +- Or, the Gson version you compiled against is different from the one on your classpath +- Or, you are using a code shrinking tool such as ProGuard or R8 which removed methods from Gson + +**Solution:** First disable any code shrinking tools such as ProGuard or R8 and check if the issue persists. If not, you have to tweak the configuration of that tool to not modify Gson classes. Otherwise verify that the Gson JAR on your classpath is the same you are compiling against, and that there is only one Gson JAR on your classpath. See [this Stack Overflow question](https://stackoverflow.com/q/227486) to find out where a class is loaded from. For example, for debugging you could include the following code: + +```java +System.out.println(Gson.class.getProtectionDomain().getCodeSource().getLocation()); +``` + +If that fails with a `NullPointerException` you have to try one of the other ways to find out where a class is loaded from. + +## `IllegalArgumentException`: 'Class ... declares multiple JSON fields named '...'' + +**Symptom:** An exception with the message 'Class ... declares multiple JSON fields named '...'' is thrown + +**Reason:** + +- The name you have specified with a [`@SerializedName`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/annotations/SerializedName.html) annotation for a field collides with the name of another field +- The [`FieldNamingStrategy`](https://javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/FieldNamingStrategy.html) you have specified produces conflicting field names +- A field of your class has the same name as the field of a superclass + +Gson prevents multiple fields with the same name because during deserialization it would be ambiguous for which field the JSON data should be deserialized. For serialization it would cause the same field to appear multiple times in JSON. While the JSON specification permits this, it is likely that the application parsing the JSON data will not handle it correctly. + +**Solution:** First identify the fields with conflicting names based on the exception message. Then decide if you want to rename one of them using the [`@SerializedName`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/annotations/SerializedName.html) annotation, or if you want to [exclude](UserGuide.md#excluding-fields-from-serialization-and-deserialization) one of them. When excluding one of the fields you have to include it for both serialization and deserialization (even if your application only performs one of these actions) because the duplicate field check cannot differentiate between these actions. + +## `UnsupportedOperationException` when serializing or deserializing `java.lang.Class` + +**Symptom:** An `UnsupportedOperationException` is thrown when trying to serialize or deserialize `java.lang.Class` + +**Reason:** Gson intentionally does not permit serializing and deserializing `java.lang.Class` for security reasons. Otherwise a malicious user could make your application load an arbitrary class from the classpath and, depending on what your application does with the `Class`, in the worst case perform a remote code execution attack. + +**Solution:** First check if you really need to serialize or deserialize a `Class`. Often it is possible to use string aliases and then map them to the known `Class`; you could write a custom [`TypeAdapter`](https://javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/TypeAdapter.html) to do this. If the `Class` values are not known in advance, try to introduce a common base class or interface for all these classes and then verify that the deserialized class is a subclass. For example assuming the base class is called `MyBaseClass`, your custom `TypeAdapter` should load the class like this: + +```java +Class.forName(jsonString, false, getClass().getClassLoader()).asSubclass(MyBaseClass.class) +``` + +This will not initialize arbitrary classes, and it will throw a `ClassCastException` if the loaded class is not the same as or a subclass of `MyBaseClass`. + +## `IllegalStateException`: 'TypeToken must be created with a type argument'
`RuntimeException`: 'Missing type parameter' + +**Symptom:** An `IllegalStateException` with the message 'TypeToken must be created with a type argument' is thrown. +For older Gson versions a `RuntimeException` with message 'Missing type parameter' is thrown. + +**Reason:** + +- You created a `TypeToken` without type argument, for example `new TypeToken() {}` (note the missing `<...>`). You always have to provide the type argument, for example like this: `new TypeToken>() {}`. Normally the compiler will also emit a 'raw types' warning when you forget the `<...>`. +- You are using a code shrinking tool such as ProGuard or R8 (Android app builds normally have this enabled by default) but have not configured it correctly for usage with Gson. + +**Solution:** When you are using a code shrinking tool such as ProGuard or R8 you have to adjust your configuration to include the following rules: + +``` +# Keep generic signatures; needed for correct type resolution +-keepattributes Signature + +# Keep class TypeToken (respectively its generic signature) +-keep class com.google.gson.reflect.TypeToken { *; } + +# Keep any (anonymous) classes extending TypeToken +-keep class * extends com.google.gson.reflect.TypeToken +``` + +See also the [Android example](examples/android-proguard-example/README.md) for more information. + +Note: For newer Gson versions these rules might be applied automatically; make sure you are using the latest Gson version and the latest version of the code shrinking tool. + +## `JsonIOException`: 'Abstract classes can't be instantiated!' (R8) + +**Symptom:** A `JsonIOException` with the message 'Abstract classes can't be instantiated!' is thrown; the class mentioned in the exception message is not actually `abstract` in your source code, and you are using the code shrinking tool R8 (Android app builds normally have this configured by default). + +Note: If the class which you are trying to deserialize is actually abstract, then this exception is probably unrelated to R8 and you will have to implement a custom [`InstanceCreator`](https://javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/InstanceCreator.html) or [`TypeAdapter`](https://javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/TypeAdapter.html) which creates an instance of a non-abstract subclass of the class. + +**Reason:** The code shrinking tool R8 performs optimizations where it removes the no-args constructor from a class and makes the class `abstract`. Due to this Gson cannot create an instance of the class. + +**Solution:** Make sure the class has a no-args constructor, then adjust your R8 configuration file to keep the constructor of the class. For example: + +``` +# Keep the no-args constructor of the deserialized class +-keepclassmembers class com.example.MyClass { + (); +} +``` + +You can also use `(...);` to keep all constructors of that class, but then you might actually rely on `sun.misc.Unsafe` on both JDK and Android to create classes without no-args constructor, see [`GsonBuilder.disableJdkUnsafe()`](https://javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/GsonBuilder.html#disableJdkUnsafe()) for more information. + +For Android you can add this rule to the `proguard-rules.pro` file, see also the [Android documentation](https://developer.android.com/build/shrink-code#keep-code). In case the class name in the exception message is obfuscated, see the Android documentation about [retracing](https://developer.android.com/build/shrink-code#retracing). + +For Android you can alternatively use the [`@Keep` annotation](https://developer.android.com/studio/write/annotations#keep) on the class or constructor you want to keep. That might be easier than having to maintain a custom R8 configuration. + +Note that the latest Gson versions (> 2.10.1) specify a default R8 configuration. If your class is a top-level class or is `static`, has a no-args constructor and its fields are annotated with Gson's [`@SerializedName`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/annotations/SerializedName.html), you might not have to perform any additional R8 configuration. + +## `IllegalArgumentException`: 'TypeToken type argument must not contain a type variable' + +**Symptom:** An exception with the message 'TypeToken type argument must not contain a type variable' is thrown + +**Reason:** This exception is thrown when you create an anonymous `TypeToken` subclass which captures a type variable, for example `new TypeToken>() {}` (where `T` is a type variable). At compile time such code looks safe and you can use the type `List` without any warnings. However, this code is not actually type-safe because at runtime due to [type erasure](https://dev.java/learn/generics/type-erasure/) only the upper bound of the type variable is available. For the previous example that would be `List`. When using such a `TypeToken` with any Gson methods performing deserialization this would lead to confusing and difficult to debug `ClassCastException`s. For serialization it can in some cases also lead to undesired results. + +Note: Earlier version of Gson unfortunately did not prevent capturing type variables, which caused many users to unwittingly write type-unsafe code. + +**Solution:** + +- Use [`TypeToken.getParameterized(...)`](https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/reflect/TypeToken.html#getParameterized(java.lang.reflect.Type,java.lang.reflect.Type...)), for example `TypeToken.getParameterized(List.class, elementType)` where `elementType` is a type you have to provide separately. +- For Kotlin users: Use [`reified` type parameters](https://kotlinlang.org/docs/inline-functions.html#reified-type-parameters), that means change `` to ``, if possible. If you have a chain of functions with type parameters you will probably have to make all of them `reified`. +- If you don't actually use Gson's `TypeToken` for any Gson method, use a general purpose 'type token' implementation provided by a different library instead, for example Guava's [`com.google.common.reflect.TypeToken`](https://javadoc.io/doc/com.google.guava/guava/latest/com/google/common/reflect/TypeToken.html). + +For backward compatibility it is possible to restore Gson's old behavior of allowing `TypeToken` to capture type variables by setting the [system property](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/System.html#setProperty(java.lang.String,java.lang.String)) `gson.allowCapturingTypeVariables` to `"true"`, **however**: + +- This does not solve any of the type-safety problems mentioned above; in the long term you should prefer one of the other solutions listed above. This system property might be removed in future Gson versions. +- You should only ever set the property to `"true"`, but never to any other value or manually clear it. Otherwise this might counteract any libraries you are using which might have deliberately set the system property because they rely on its behavior. diff --git a/gson/src/main/java/com/google/gson/JsonNull.java b/gson/src/main/java/com/google/gson/JsonNull.java index 7109fbfa..1cc78ff1 100644 --- a/gson/src/main/java/com/google/gson/JsonNull.java +++ b/gson/src/main/java/com/google/gson/JsonNull.java @@ -1,65 +1,65 @@ -/* - * Copyright (C) 2008 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.gson; - -/** - * A class representing a JSON {@code null} value. - * - * @author Inderjeet Singh - * @author Joel Leitch - * @since 1.2 - */ -public final class JsonNull extends JsonElement { - /** - * Singleton for {@code JsonNull}. - * - * @since 1.8 - */ - public static final JsonNull INSTANCE = new JsonNull(); - - /** - * Creates a new {@code JsonNull} object. - * - * @deprecated Deprecated since Gson version 1.8, use {@link #INSTANCE} instead. - */ - @Deprecated - public JsonNull() { - // Do nothing - } - - /** - * Returns the same instance since it is an immutable value. - * - * @since 2.8.2 - */ - @Override - public JsonNull deepCopy() { - return INSTANCE; - } - - /** All instances of {@code JsonNull} have the same hash code since they are indistinguishable. */ - @Override - public int hashCode() { - return JsonNull.class.hashCode(); - } - - /** All instances of {@code JsonNull} are considered equal. */ - @Override - public boolean equals(Object other) { - return other instanceof JsonNull; - } -} +/* + * Copyright (C) 2008 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gson; + +/** + * A class representing a JSON {@code null} value. + * + * @author Inderjeet Singh + * @author Joel Leitch + * @since 1.2 + */ +public final class JsonNull extends JsonElement { + /** + * Singleton for {@code JsonNull}. + * + * @since 1.8 + */ + public static final JsonNull INSTANCE = new JsonNull(); + + /** + * Creates a new {@code JsonNull} object. + * + * @deprecated Deprecated since Gson version 1.8, use {@link #INSTANCE} instead. + */ + @Deprecated + public JsonNull() { + // Do nothing + } + + /** + * Returns the same instance since it is an immutable value. + * + * @since 2.8.2 + */ + @Override + public JsonNull deepCopy() { + return INSTANCE; + } + + /** All instances of {@code JsonNull} have the same hash code since they are indistinguishable. */ + @Override + public int hashCode() { + return JsonNull.class.hashCode(); + } + + /** All instances of {@code JsonNull} are considered equal. */ + @Override + public boolean equals(Object other) { + return other instanceof JsonNull; + } +} diff --git a/gson/src/main/java/com/google/gson/JsonParser.java b/gson/src/main/java/com/google/gson/JsonParser.java index ee987775..e9cfa5ec 100644 --- a/gson/src/main/java/com/google/gson/JsonParser.java +++ b/gson/src/main/java/com/google/gson/JsonParser.java @@ -1,139 +1,139 @@ -/* - * Copyright (C) 2009 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.gson; - -import com.google.errorprone.annotations.InlineMe; -import com.google.gson.internal.Streams; -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonToken; -import com.google.gson.stream.MalformedJsonException; -import java.io.IOException; -import java.io.Reader; -import java.io.StringReader; - -/** - * A parser to parse JSON into a parse tree of {@link JsonElement}s. - * - * @author Inderjeet Singh - * @author Joel Leitch - * @since 1.3 - */ -public final class JsonParser { - /** - * @deprecated No need to instantiate this class, use the static methods instead. - */ - @Deprecated - public JsonParser() {} - - /** - * Parses the specified JSON string into a parse tree. An exception is thrown if the JSON string - * has multiple top-level JSON elements, or if there is trailing data. - * - *

The JSON string is parsed in {@linkplain JsonReader#setStrictness(Strictness) lenient mode}. - * - * @param json JSON text - * @return a parse tree of {@link JsonElement}s corresponding to the specified JSON - * @throws JsonParseException if the specified text is not valid JSON - * @since 2.8.6 - */ - public static JsonElement parseString(String json) throws JsonSyntaxException { - return parseReader(new StringReader(json)); - } - - /** - * Parses the complete JSON string provided by the reader into a parse tree. An exception is - * thrown if the JSON string has multiple top-level JSON elements, or if there is trailing data. - * - *

The JSON data is parsed in {@linkplain JsonReader#setStrictness(Strictness) lenient mode}. - * - * @param reader JSON text - * @return a parse tree of {@link JsonElement}s corresponding to the specified JSON - * @throws JsonParseException if there is an IOException or if the specified text is not valid - * JSON - * @since 2.8.6 - */ - public static JsonElement parseReader(Reader reader) throws JsonIOException, JsonSyntaxException { - try { - JsonReader jsonReader = new JsonReader(reader); - JsonElement element = parseReader(jsonReader); - if (!element.isJsonNull() && jsonReader.peek() != JsonToken.END_DOCUMENT) { - throw new JsonSyntaxException("Did not consume the entire document."); - } - return element; - } catch (MalformedJsonException e) { - throw new JsonSyntaxException(e); - } catch (IOException e) { - throw new JsonIOException(e); - } catch (NumberFormatException e) { - throw new JsonSyntaxException(e); - } - } - - /** - * Returns the next value from the JSON stream as a parse tree. Unlike the other {@code parse} - * methods, no exception is thrown if the JSON data has multiple top-level JSON elements, or if - * there is trailing data. - * - *

The JSON data is parsed in {@linkplain JsonReader#setStrictness(Strictness) lenient mode}, - * regardless of the strictness setting of the provided reader. The strictness setting of the - * reader is restored once this method returns. - * - * @throws JsonParseException if there is an IOException or if the specified text is not valid - * JSON - * @since 2.8.6 - */ - public static JsonElement parseReader(JsonReader reader) - throws JsonIOException, JsonSyntaxException { - Strictness strictness = reader.getStrictness(); - reader.setStrictness(Strictness.LENIENT); - try { - return Streams.parse(reader); - } catch (StackOverflowError e) { - throw new JsonParseException("Failed parsing JSON source: " + reader + " to Json", e); - } catch (OutOfMemoryError e) { - throw new JsonParseException("Failed parsing JSON source: " + reader + " to Json", e); - } finally { - reader.setStrictness(strictness); - } - } - - /** - * @deprecated Use {@link JsonParser#parseString} - */ - @Deprecated - @InlineMe(replacement = "JsonParser.parseString(json)", imports = "com.google.gson.JsonParser") - public JsonElement parse(String json) throws JsonSyntaxException { - return parseString(json); - } - - /** - * @deprecated Use {@link JsonParser#parseReader(Reader)} - */ - @Deprecated - @InlineMe(replacement = "JsonParser.parseReader(json)", imports = "com.google.gson.JsonParser") - public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException { - return parseReader(json); - } - - /** - * @deprecated Use {@link JsonParser#parseReader(JsonReader)} - */ - @Deprecated - @InlineMe(replacement = "JsonParser.parseReader(json)", imports = "com.google.gson.JsonParser") - public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException { - return parseReader(json); - } -} +/* + * Copyright (C) 2009 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.gson; + +import com.google.errorprone.annotations.InlineMe; +import com.google.gson.internal.Streams; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonToken; +import com.google.gson.stream.MalformedJsonException; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; + +/** + * A parser to parse JSON into a parse tree of {@link JsonElement}s. + * + * @author Inderjeet Singh + * @author Joel Leitch + * @since 1.3 + */ +public final class JsonParser { + /** + * @deprecated No need to instantiate this class, use the static methods instead. + */ + @Deprecated + public JsonParser() {} + + /** + * Parses the specified JSON string into a parse tree. An exception is thrown if the JSON string + * has multiple top-level JSON elements, or if there is trailing data. + * + *

The JSON string is parsed in {@linkplain JsonReader#setStrictness(Strictness) lenient mode}. + * + * @param json JSON text + * @return a parse tree of {@link JsonElement}s corresponding to the specified JSON + * @throws JsonParseException if the specified text is not valid JSON + * @since 2.8.6 + */ + public static JsonElement parseString(String json) throws JsonSyntaxException { + return parseReader(new StringReader(json)); + } + + /** + * Parses the complete JSON string provided by the reader into a parse tree. An exception is + * thrown if the JSON string has multiple top-level JSON elements, or if there is trailing data. + * + *

The JSON data is parsed in {@linkplain JsonReader#setStrictness(Strictness) lenient mode}. + * + * @param reader JSON text + * @return a parse tree of {@link JsonElement}s corresponding to the specified JSON + * @throws JsonParseException if there is an IOException or if the specified text is not valid + * JSON + * @since 2.8.6 + */ + public static JsonElement parseReader(Reader reader) throws JsonIOException, JsonSyntaxException { + try { + JsonReader jsonReader = new JsonReader(reader); + JsonElement element = parseReader(jsonReader); + if (!element.isJsonNull() && jsonReader.peek() != JsonToken.END_DOCUMENT) { + throw new JsonSyntaxException("Did not consume the entire document."); + } + return element; + } catch (MalformedJsonException e) { + throw new JsonSyntaxException(e); + } catch (IOException e) { + throw new JsonIOException(e); + } catch (NumberFormatException e) { + throw new JsonSyntaxException(e); + } + } + + /** + * Returns the next value from the JSON stream as a parse tree. Unlike the other {@code parse} + * methods, no exception is thrown if the JSON data has multiple top-level JSON elements, or if + * there is trailing data. + * + *

The JSON data is parsed in {@linkplain JsonReader#setStrictness(Strictness) lenient mode}, + * regardless of the strictness setting of the provided reader. The strictness setting of the + * reader is restored once this method returns. + * + * @throws JsonParseException if there is an IOException or if the specified text is not valid + * JSON + * @since 2.8.6 + */ + public static JsonElement parseReader(JsonReader reader) + throws JsonIOException, JsonSyntaxException { + Strictness strictness = reader.getStrictness(); + reader.setStrictness(Strictness.LENIENT); + try { + return Streams.parse(reader); + } catch (StackOverflowError e) { + throw new JsonParseException("Failed parsing JSON source: " + reader + " to Json", e); + } catch (OutOfMemoryError e) { + throw new JsonParseException("Failed parsing JSON source: " + reader + " to Json", e); + } finally { + reader.setStrictness(strictness); + } + } + + /** + * @deprecated Use {@link JsonParser#parseString} + */ + @Deprecated + @InlineMe(replacement = "JsonParser.parseString(json)", imports = "com.google.gson.JsonParser") + public JsonElement parse(String json) throws JsonSyntaxException { + return parseString(json); + } + + /** + * @deprecated Use {@link JsonParser#parseReader(Reader)} + */ + @Deprecated + @InlineMe(replacement = "JsonParser.parseReader(json)", imports = "com.google.gson.JsonParser") + public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException { + return parseReader(json); + } + + /** + * @deprecated Use {@link JsonParser#parseReader(JsonReader)} + */ + @Deprecated + @InlineMe(replacement = "JsonParser.parseReader(json)", imports = "com.google.gson.JsonParser") + public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException { + return parseReader(json); + } +} diff --git a/gson/src/main/java/com/google/gson/internal/$Gson$Preconditions.java b/gson/src/main/java/com/google/gson/internal/$Gson$Preconditions.java index ae39ba51..76c24165 100644 --- a/gson/src/main/java/com/google/gson/internal/$Gson$Preconditions.java +++ b/gson/src/main/java/com/google/gson/internal/$Gson$Preconditions.java @@ -1,57 +1,57 @@ -/* - * Copyright (C) 2008 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.gson.internal; - -import java.util.Objects; - -/** - * A simple utility class used to check method Preconditions. - * - *

- * public long divideBy(long value) {
- *   Preconditions.checkArgument(value != 0);
- *   return this.value / value;
- * }
- * 
- * - * @author Inderjeet Singh - * @author Joel Leitch - */ -public final class $Gson$Preconditions { - private $Gson$Preconditions() { - throw new UnsupportedOperationException(); - } - - /** - * @deprecated This is an internal Gson method. Use {@link Objects#requireNonNull(Object)} - * instead. - */ - // Only deprecated for now because external projects might be using this by accident - @Deprecated - public static T checkNotNull(T obj) { - if (obj == null) { - throw new NullPointerException(); - } - return obj; - } - - public static void checkArgument(boolean condition) { - if (!condition) { - throw new IllegalArgumentException(); - } - } -} +/* + * Copyright (C) 2008 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gson.internal; + +import java.util.Objects; + +/** + * A simple utility class used to check method Preconditions. + * + *
+ * public long divideBy(long value) {
+ *   Preconditions.checkArgument(value != 0);
+ *   return this.value / value;
+ * }
+ * 
+ * + * @author Inderjeet Singh + * @author Joel Leitch + */ +public final class $Gson$Preconditions { + private $Gson$Preconditions() { + throw new UnsupportedOperationException(); + } + + /** + * @deprecated This is an internal Gson method. Use {@link Objects#requireNonNull(Object)} + * instead. + */ + // Only deprecated for now because external projects might be using this by accident + @Deprecated + public static T checkNotNull(T obj) { + if (obj == null) { + throw new NullPointerException(); + } + return obj; + } + + public static void checkArgument(boolean condition) { + if (!condition) { + throw new IllegalArgumentException(); + } + } +} diff --git a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapterRuntimeTypeWrapper.java b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapterRuntimeTypeWrapper.java index 049d8114..20698e42 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapterRuntimeTypeWrapper.java +++ b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapterRuntimeTypeWrapper.java @@ -1,103 +1,103 @@ -/* - * Copyright (C) 2011 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.gson.internal.bind; - -import com.google.gson.Gson; -import com.google.gson.TypeAdapter; -import com.google.gson.reflect.TypeToken; -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonWriter; -import java.io.IOException; -import java.lang.reflect.Type; -import java.lang.reflect.TypeVariable; - -final class TypeAdapterRuntimeTypeWrapper extends TypeAdapter { - private final Gson context; - private final TypeAdapter delegate; - private final Type type; - - TypeAdapterRuntimeTypeWrapper(Gson context, TypeAdapter delegate, Type type) { - this.context = context; - this.delegate = delegate; - this.type = type; - } - - @Override - public T read(JsonReader in) throws IOException { - return delegate.read(in); - } - - @Override - public void write(JsonWriter out, T value) throws IOException { - // Order of preference for choosing type adapters - // First preference: a type adapter registered for the runtime type - // Second preference: a type adapter registered for the declared type - // Third preference: reflective type adapter for the runtime type (if it is a subclass of the - // declared type) - // Fourth preference: reflective type adapter for the declared type - - TypeAdapter chosen = delegate; - Type runtimeType = getRuntimeTypeIfMoreSpecific(type, value); - if (runtimeType != type) { - @SuppressWarnings("unchecked") - TypeAdapter runtimeTypeAdapter = - (TypeAdapter) context.getAdapter(TypeToken.get(runtimeType)); - // For backward compatibility only check ReflectiveTypeAdapterFactory.Adapter here but not any - // other wrapping adapters, see - // https://github.com/google/gson/pull/1787#issuecomment-1222175189 - if (!(runtimeTypeAdapter instanceof ReflectiveTypeAdapterFactory.Adapter)) { - // The user registered a type adapter for the runtime type, so we will use that - chosen = runtimeTypeAdapter; - } else if (!isReflective(delegate)) { - // The user registered a type adapter for Base class, so we prefer it over the - // reflective type adapter for the runtime type - chosen = delegate; - } else { - // Use the type adapter for runtime type - chosen = runtimeTypeAdapter; - } - } - chosen.write(out, value); - } - - /** - * Returns whether the type adapter uses reflection. - * - * @param typeAdapter the type adapter to check. - */ - private static boolean isReflective(TypeAdapter typeAdapter) { - // Run this in loop in case multiple delegating adapters are nested - while (typeAdapter instanceof SerializationDelegatingTypeAdapter) { - TypeAdapter delegate = - ((SerializationDelegatingTypeAdapter) typeAdapter).getSerializationDelegate(); - // Break if adapter does not delegate serialization - if (delegate == typeAdapter) { - break; - } - typeAdapter = delegate; - } - - return typeAdapter instanceof ReflectiveTypeAdapterFactory.Adapter; - } - - /** Finds a compatible runtime type if it is more specific */ - private static Type getRuntimeTypeIfMoreSpecific(Type type, Object value) { - if (value != null && (type instanceof Class || type instanceof TypeVariable)) { - type = value.getClass(); - } - return type; - } -} +/* + * Copyright (C) 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.gson.internal.bind; + +import com.google.gson.Gson; +import com.google.gson.TypeAdapter; +import com.google.gson.reflect.TypeToken; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; +import java.io.IOException; +import java.lang.reflect.Type; +import java.lang.reflect.TypeVariable; + +final class TypeAdapterRuntimeTypeWrapper extends TypeAdapter { + private final Gson context; + private final TypeAdapter delegate; + private final Type type; + + TypeAdapterRuntimeTypeWrapper(Gson context, TypeAdapter delegate, Type type) { + this.context = context; + this.delegate = delegate; + this.type = type; + } + + @Override + public T read(JsonReader in) throws IOException { + return delegate.read(in); + } + + @Override + public void write(JsonWriter out, T value) throws IOException { + // Order of preference for choosing type adapters + // First preference: a type adapter registered for the runtime type + // Second preference: a type adapter registered for the declared type + // Third preference: reflective type adapter for the runtime type (if it is a subclass of the + // declared type) + // Fourth preference: reflective type adapter for the declared type + + TypeAdapter chosen = delegate; + Type runtimeType = getRuntimeTypeIfMoreSpecific(type, value); + if (runtimeType != type) { + @SuppressWarnings("unchecked") + TypeAdapter runtimeTypeAdapter = + (TypeAdapter) context.getAdapter(TypeToken.get(runtimeType)); + // For backward compatibility only check ReflectiveTypeAdapterFactory.Adapter here but not any + // other wrapping adapters, see + // https://github.com/google/gson/pull/1787#issuecomment-1222175189 + if (!(runtimeTypeAdapter instanceof ReflectiveTypeAdapterFactory.Adapter)) { + // The user registered a type adapter for the runtime type, so we will use that + chosen = runtimeTypeAdapter; + } else if (!isReflective(delegate)) { + // The user registered a type adapter for Base class, so we prefer it over the + // reflective type adapter for the runtime type + chosen = delegate; + } else { + // Use the type adapter for runtime type + chosen = runtimeTypeAdapter; + } + } + chosen.write(out, value); + } + + /** + * Returns whether the type adapter uses reflection. + * + * @param typeAdapter the type adapter to check. + */ + private static boolean isReflective(TypeAdapter typeAdapter) { + // Run this in loop in case multiple delegating adapters are nested + while (typeAdapter instanceof SerializationDelegatingTypeAdapter) { + TypeAdapter delegate = + ((SerializationDelegatingTypeAdapter) typeAdapter).getSerializationDelegate(); + // Break if adapter does not delegate serialization + if (delegate == typeAdapter) { + break; + } + typeAdapter = delegate; + } + + return typeAdapter instanceof ReflectiveTypeAdapterFactory.Adapter; + } + + /** Finds a compatible runtime type if it is more specific */ + private static Type getRuntimeTypeIfMoreSpecific(Type type, Object value) { + if (value != null && (type instanceof Class || type instanceof TypeVariable)) { + type = value.getClass(); + } + return type; + } +} diff --git a/gson/src/test/java/com/google/gson/GsonBuilderTest.java b/gson/src/test/java/com/google/gson/GsonBuilderTest.java index 37a11105..a5f826db 100644 --- a/gson/src/test/java/com/google/gson/GsonBuilderTest.java +++ b/gson/src/test/java/com/google/gson/GsonBuilderTest.java @@ -1,308 +1,308 @@ -/* - * Copyright (C) 2008 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.gson; - -import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.assertThrows; -import static org.junit.Assert.fail; - -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonWriter; -import java.io.IOException; -import java.io.StringReader; -import java.io.StringWriter; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.lang.reflect.Type; -import org.junit.Test; - -/** - * Unit tests for {@link GsonBuilder}. - * - * @author Inderjeet Singh - */ -public class GsonBuilderTest { - private static final TypeAdapter NULL_TYPE_ADAPTER = - new TypeAdapter() { - @Override - public void write(JsonWriter out, Object value) { - throw new AssertionError(); - } - - @Override - public Object read(JsonReader in) { - throw new AssertionError(); - } - }; - - @Test - public void testCreatingMoreThanOnce() { - GsonBuilder builder = new GsonBuilder(); - Gson gson = builder.create(); - assertThat(gson).isNotNull(); - assertThat(builder.create()).isNotNull(); - - builder.setFieldNamingStrategy( - new FieldNamingStrategy() { - @Override - public String translateName(Field f) { - return "test"; - } - }); - - Gson otherGson = builder.create(); - assertThat(otherGson).isNotNull(); - // Should be different instances because builder has been modified in the meantime - assertThat(gson).isNotSameInstanceAs(otherGson); - } - - /** - * Gson instances should not be affected by subsequent modification of GsonBuilder which created - * them. - */ - @Test - public void testModificationAfterCreate() { - GsonBuilder gsonBuilder = new GsonBuilder(); - Gson gson = gsonBuilder.create(); - - // Modifications of `gsonBuilder` should not affect `gson` object - gsonBuilder.registerTypeAdapter( - CustomClass1.class, - new TypeAdapter() { - @Override - public CustomClass1 read(JsonReader in) { - throw new UnsupportedOperationException(); - } - - @Override - public void write(JsonWriter out, CustomClass1 value) throws IOException { - out.value("custom-adapter"); - } - }); - gsonBuilder.registerTypeHierarchyAdapter( - CustomClass2.class, - new JsonSerializer() { - @Override - public JsonElement serialize( - CustomClass2 src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive("custom-hierarchy-adapter"); - } - }); - gsonBuilder.registerTypeAdapter( - CustomClass3.class, - new InstanceCreator() { - @Override - public CustomClass3 createInstance(Type type) { - return new CustomClass3("custom-instance"); - } - }); - - assertDefaultGson(gson); - // New GsonBuilder created from `gson` should not have been affected by changes - // to `gsonBuilder` either - assertDefaultGson(gson.newBuilder().create()); - - // New Gson instance from modified GsonBuilder should be affected by changes - assertCustomGson(gsonBuilder.create()); - } - - private static void assertDefaultGson(Gson gson) { - // Should use default reflective adapter - String json1 = gson.toJson(new CustomClass1()); - assertThat(json1).isEqualTo("{}"); - - // Should use default reflective adapter - String json2 = gson.toJson(new CustomClass2()); - assertThat(json2).isEqualTo("{}"); - - // Should use default instance creator - CustomClass3 customClass3 = gson.fromJson("{}", CustomClass3.class); - assertThat(customClass3.s).isEqualTo(CustomClass3.NO_ARG_CONSTRUCTOR_VALUE); - } - - private static void assertCustomGson(Gson gson) { - String json1 = gson.toJson(new CustomClass1()); - assertThat(json1).isEqualTo("\"custom-adapter\""); - - String json2 = gson.toJson(new CustomClass2()); - assertThat(json2).isEqualTo("\"custom-hierarchy-adapter\""); - - CustomClass3 customClass3 = gson.fromJson("{}", CustomClass3.class); - assertThat(customClass3.s).isEqualTo("custom-instance"); - } - - static class CustomClass1 {} - - static class CustomClass2 {} - - static class CustomClass3 { - static final String NO_ARG_CONSTRUCTOR_VALUE = "default instance"; - - final String s; - - public CustomClass3(String s) { - this.s = s; - } - - public CustomClass3() { - this(NO_ARG_CONSTRUCTOR_VALUE); - } - } - - @Test - public void testExcludeFieldsWithModifiers() { - Gson gson = - new GsonBuilder().excludeFieldsWithModifiers(Modifier.VOLATILE, Modifier.PRIVATE).create(); - assertThat(gson.toJson(new HasModifiers())).isEqualTo("{\"d\":\"d\"}"); - } - - @SuppressWarnings("unused") - static class HasModifiers { - private String a = "a"; - volatile String b = "b"; - private volatile String c = "c"; - String d = "d"; - } - - @Test - public void testTransientFieldExclusion() { - Gson gson = new GsonBuilder().excludeFieldsWithModifiers().create(); - assertThat(gson.toJson(new HasTransients())).isEqualTo("{\"a\":\"a\"}"); - } - - static class HasTransients { - transient String a = "a"; - } - - @Test - public void testRegisterTypeAdapterForCoreType() { - Type[] types = { - byte.class, int.class, double.class, Short.class, Long.class, String.class, - }; - for (Type type : types) { - new GsonBuilder().registerTypeAdapter(type, NULL_TYPE_ADAPTER); - } - } - - @Test - public void testDisableJdkUnsafe() { - Gson gson = new GsonBuilder().disableJdkUnsafe().create(); - try { - gson.fromJson("{}", ClassWithoutNoArgsConstructor.class); - fail("Expected exception"); - } catch (JsonIOException expected) { - assertThat(expected) - .hasMessageThat() - .isEqualTo( - "Unable to create instance of class" - + " com.google.gson.GsonBuilderTest$ClassWithoutNoArgsConstructor; usage of JDK" - + " Unsafe is disabled. Registering an InstanceCreator or a TypeAdapter for this" - + " type, adding a no-args constructor, or enabling usage of JDK Unsafe may fix" - + " this problem."); - } - } - - private static class ClassWithoutNoArgsConstructor { - @SuppressWarnings("unused") - public ClassWithoutNoArgsConstructor(String s) {} - } - - @Test - public void testSetVersionInvalid() { - GsonBuilder builder = new GsonBuilder(); - try { - builder.setVersion(Double.NaN); - fail(); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().isEqualTo("Invalid version: NaN"); - } - - try { - builder.setVersion(-0.1); - fail(); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessageThat().isEqualTo("Invalid version: -0.1"); - } - } - - @Test - public void testDefaultStrictness() throws IOException { - GsonBuilder builder = new GsonBuilder(); - Gson gson = builder.create(); - assertThat(gson.newJsonReader(new StringReader("{}")).getStrictness()) - .isEqualTo(Strictness.LEGACY_STRICT); - assertThat(gson.newJsonWriter(new StringWriter()).getStrictness()) - .isEqualTo(Strictness.LEGACY_STRICT); - } - - @SuppressWarnings({"deprecation", "InlineMeInliner"}) // for GsonBuilder.setLenient - @Test - public void testSetLenient() throws IOException { - GsonBuilder builder = new GsonBuilder(); - builder.setLenient(); - Gson gson = builder.create(); - assertThat(gson.newJsonReader(new StringReader("{}")).getStrictness()) - .isEqualTo(Strictness.LENIENT); - assertThat(gson.newJsonWriter(new StringWriter()).getStrictness()) - .isEqualTo(Strictness.LENIENT); - } - - @Test - public void testSetStrictness() throws IOException { - final Strictness STRICTNESS = Strictness.STRICT; - GsonBuilder builder = new GsonBuilder(); - builder.setStrictness(STRICTNESS); - Gson gson = builder.create(); - assertThat(gson.newJsonReader(new StringReader("{}")).getStrictness()).isEqualTo(STRICTNESS); - assertThat(gson.newJsonWriter(new StringWriter()).getStrictness()).isEqualTo(STRICTNESS); - } - - @Test - public void testRegisterTypeAdapterForObjectAndJsonElements() { - final String ERROR_MESSAGE = "Cannot override built-in adapter for "; - Type[] types = { - Object.class, JsonElement.class, JsonArray.class, - }; - GsonBuilder gsonBuilder = new GsonBuilder(); - for (Type type : types) { - IllegalArgumentException e = - assertThrows( - IllegalArgumentException.class, - () -> gsonBuilder.registerTypeAdapter(type, NULL_TYPE_ADAPTER)); - assertThat(e).hasMessageThat().isEqualTo(ERROR_MESSAGE + type); - } - } - - @Test - public void testRegisterTypeHierarchyAdapterJsonElements() { - final String ERROR_MESSAGE = "Cannot override built-in adapter for "; - Class[] types = { - JsonElement.class, JsonArray.class, - }; - GsonBuilder gsonBuilder = new GsonBuilder(); - for (Class type : types) { - IllegalArgumentException e = - assertThrows( - IllegalArgumentException.class, - () -> gsonBuilder.registerTypeHierarchyAdapter(type, NULL_TYPE_ADAPTER)); - - assertThat(e).hasMessageThat().isEqualTo(ERROR_MESSAGE + type); - } - // But registering type hierarchy adapter for Object should be allowed - gsonBuilder.registerTypeHierarchyAdapter(Object.class, NULL_TYPE_ADAPTER); - } -} +/* + * Copyright (C) 2008 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gson; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.fail; + +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.lang.reflect.Type; +import org.junit.Test; + +/** + * Unit tests for {@link GsonBuilder}. + * + * @author Inderjeet Singh + */ +public class GsonBuilderTest { + private static final TypeAdapter NULL_TYPE_ADAPTER = + new TypeAdapter() { + @Override + public void write(JsonWriter out, Object value) { + throw new AssertionError(); + } + + @Override + public Object read(JsonReader in) { + throw new AssertionError(); + } + }; + + @Test + public void testCreatingMoreThanOnce() { + GsonBuilder builder = new GsonBuilder(); + Gson gson = builder.create(); + assertThat(gson).isNotNull(); + assertThat(builder.create()).isNotNull(); + + builder.setFieldNamingStrategy( + new FieldNamingStrategy() { + @Override + public String translateName(Field f) { + return "test"; + } + }); + + Gson otherGson = builder.create(); + assertThat(otherGson).isNotNull(); + // Should be different instances because builder has been modified in the meantime + assertThat(gson).isNotSameInstanceAs(otherGson); + } + + /** + * Gson instances should not be affected by subsequent modification of GsonBuilder which created + * them. + */ + @Test + public void testModificationAfterCreate() { + GsonBuilder gsonBuilder = new GsonBuilder(); + Gson gson = gsonBuilder.create(); + + // Modifications of `gsonBuilder` should not affect `gson` object + gsonBuilder.registerTypeAdapter( + CustomClass1.class, + new TypeAdapter() { + @Override + public CustomClass1 read(JsonReader in) { + throw new UnsupportedOperationException(); + } + + @Override + public void write(JsonWriter out, CustomClass1 value) throws IOException { + out.value("custom-adapter"); + } + }); + gsonBuilder.registerTypeHierarchyAdapter( + CustomClass2.class, + new JsonSerializer() { + @Override + public JsonElement serialize( + CustomClass2 src, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive("custom-hierarchy-adapter"); + } + }); + gsonBuilder.registerTypeAdapter( + CustomClass3.class, + new InstanceCreator() { + @Override + public CustomClass3 createInstance(Type type) { + return new CustomClass3("custom-instance"); + } + }); + + assertDefaultGson(gson); + // New GsonBuilder created from `gson` should not have been affected by changes + // to `gsonBuilder` either + assertDefaultGson(gson.newBuilder().create()); + + // New Gson instance from modified GsonBuilder should be affected by changes + assertCustomGson(gsonBuilder.create()); + } + + private static void assertDefaultGson(Gson gson) { + // Should use default reflective adapter + String json1 = gson.toJson(new CustomClass1()); + assertThat(json1).isEqualTo("{}"); + + // Should use default reflective adapter + String json2 = gson.toJson(new CustomClass2()); + assertThat(json2).isEqualTo("{}"); + + // Should use default instance creator + CustomClass3 customClass3 = gson.fromJson("{}", CustomClass3.class); + assertThat(customClass3.s).isEqualTo(CustomClass3.NO_ARG_CONSTRUCTOR_VALUE); + } + + private static void assertCustomGson(Gson gson) { + String json1 = gson.toJson(new CustomClass1()); + assertThat(json1).isEqualTo("\"custom-adapter\""); + + String json2 = gson.toJson(new CustomClass2()); + assertThat(json2).isEqualTo("\"custom-hierarchy-adapter\""); + + CustomClass3 customClass3 = gson.fromJson("{}", CustomClass3.class); + assertThat(customClass3.s).isEqualTo("custom-instance"); + } + + static class CustomClass1 {} + + static class CustomClass2 {} + + static class CustomClass3 { + static final String NO_ARG_CONSTRUCTOR_VALUE = "default instance"; + + final String s; + + public CustomClass3(String s) { + this.s = s; + } + + public CustomClass3() { + this(NO_ARG_CONSTRUCTOR_VALUE); + } + } + + @Test + public void testExcludeFieldsWithModifiers() { + Gson gson = + new GsonBuilder().excludeFieldsWithModifiers(Modifier.VOLATILE, Modifier.PRIVATE).create(); + assertThat(gson.toJson(new HasModifiers())).isEqualTo("{\"d\":\"d\"}"); + } + + @SuppressWarnings("unused") + static class HasModifiers { + private String a = "a"; + volatile String b = "b"; + private volatile String c = "c"; + String d = "d"; + } + + @Test + public void testTransientFieldExclusion() { + Gson gson = new GsonBuilder().excludeFieldsWithModifiers().create(); + assertThat(gson.toJson(new HasTransients())).isEqualTo("{\"a\":\"a\"}"); + } + + static class HasTransients { + transient String a = "a"; + } + + @Test + public void testRegisterTypeAdapterForCoreType() { + Type[] types = { + byte.class, int.class, double.class, Short.class, Long.class, String.class, + }; + for (Type type : types) { + new GsonBuilder().registerTypeAdapter(type, NULL_TYPE_ADAPTER); + } + } + + @Test + public void testDisableJdkUnsafe() { + Gson gson = new GsonBuilder().disableJdkUnsafe().create(); + try { + gson.fromJson("{}", ClassWithoutNoArgsConstructor.class); + fail("Expected exception"); + } catch (JsonIOException expected) { + assertThat(expected) + .hasMessageThat() + .isEqualTo( + "Unable to create instance of class" + + " com.google.gson.GsonBuilderTest$ClassWithoutNoArgsConstructor; usage of JDK" + + " Unsafe is disabled. Registering an InstanceCreator or a TypeAdapter for this" + + " type, adding a no-args constructor, or enabling usage of JDK Unsafe may fix" + + " this problem."); + } + } + + private static class ClassWithoutNoArgsConstructor { + @SuppressWarnings("unused") + public ClassWithoutNoArgsConstructor(String s) {} + } + + @Test + public void testSetVersionInvalid() { + GsonBuilder builder = new GsonBuilder(); + try { + builder.setVersion(Double.NaN); + fail(); + } catch (IllegalArgumentException e) { + assertThat(e).hasMessageThat().isEqualTo("Invalid version: NaN"); + } + + try { + builder.setVersion(-0.1); + fail(); + } catch (IllegalArgumentException e) { + assertThat(e).hasMessageThat().isEqualTo("Invalid version: -0.1"); + } + } + + @Test + public void testDefaultStrictness() throws IOException { + GsonBuilder builder = new GsonBuilder(); + Gson gson = builder.create(); + assertThat(gson.newJsonReader(new StringReader("{}")).getStrictness()) + .isEqualTo(Strictness.LEGACY_STRICT); + assertThat(gson.newJsonWriter(new StringWriter()).getStrictness()) + .isEqualTo(Strictness.LEGACY_STRICT); + } + + @SuppressWarnings({"deprecation", "InlineMeInliner"}) // for GsonBuilder.setLenient + @Test + public void testSetLenient() throws IOException { + GsonBuilder builder = new GsonBuilder(); + builder.setLenient(); + Gson gson = builder.create(); + assertThat(gson.newJsonReader(new StringReader("{}")).getStrictness()) + .isEqualTo(Strictness.LENIENT); + assertThat(gson.newJsonWriter(new StringWriter()).getStrictness()) + .isEqualTo(Strictness.LENIENT); + } + + @Test + public void testSetStrictness() throws IOException { + final Strictness STRICTNESS = Strictness.STRICT; + GsonBuilder builder = new GsonBuilder(); + builder.setStrictness(STRICTNESS); + Gson gson = builder.create(); + assertThat(gson.newJsonReader(new StringReader("{}")).getStrictness()).isEqualTo(STRICTNESS); + assertThat(gson.newJsonWriter(new StringWriter()).getStrictness()).isEqualTo(STRICTNESS); + } + + @Test + public void testRegisterTypeAdapterForObjectAndJsonElements() { + final String ERROR_MESSAGE = "Cannot override built-in adapter for "; + Type[] types = { + Object.class, JsonElement.class, JsonArray.class, + }; + GsonBuilder gsonBuilder = new GsonBuilder(); + for (Type type : types) { + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, + () -> gsonBuilder.registerTypeAdapter(type, NULL_TYPE_ADAPTER)); + assertThat(e).hasMessageThat().isEqualTo(ERROR_MESSAGE + type); + } + } + + @Test + public void testRegisterTypeHierarchyAdapterJsonElements() { + final String ERROR_MESSAGE = "Cannot override built-in adapter for "; + Class[] types = { + JsonElement.class, JsonArray.class, + }; + GsonBuilder gsonBuilder = new GsonBuilder(); + for (Class type : types) { + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, + () -> gsonBuilder.registerTypeHierarchyAdapter(type, NULL_TYPE_ADAPTER)); + + assertThat(e).hasMessageThat().isEqualTo(ERROR_MESSAGE + type); + } + // But registering type hierarchy adapter for Object should be allowed + gsonBuilder.registerTypeHierarchyAdapter(Object.class, NULL_TYPE_ADAPTER); + } +} diff --git a/gson/src/test/java/com/google/gson/functional/ConcurrencyTest.java b/gson/src/test/java/com/google/gson/functional/ConcurrencyTest.java index 3bdb67f2..3c5d5e5f 100644 --- a/gson/src/test/java/com/google/gson/functional/ConcurrencyTest.java +++ b/gson/src/test/java/com/google/gson/functional/ConcurrencyTest.java @@ -1,149 +1,149 @@ -/* - * Copyright (C) 2008 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.gson.functional; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.gson.Gson; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.atomic.AtomicBoolean; -import org.junit.Before; -import org.junit.Test; - -/** - * Tests for ensuring Gson thread-safety. - * - * @author Inderjeet Singh - * @author Joel Leitch - */ -public class ConcurrencyTest { - private Gson gson; - - @Before - public void setUp() throws Exception { - gson = new Gson(); - } - - /** - * Source-code based on - * http://groups.google.com/group/google-gson/browse_thread/thread/563bb51ee2495081 - */ - @Test - public void testSingleThreadSerialization() { - MyObject myObj = new MyObject(); - for (int i = 0; i < 10; i++) { - String unused = gson.toJson(myObj); - } - } - - /** - * Source-code based on - * http://groups.google.com/group/google-gson/browse_thread/thread/563bb51ee2495081 - */ - @Test - public void testSingleThreadDeserialization() { - for (int i = 0; i < 10; i++) { - MyObject unused = gson.fromJson("{'a':'hello','b':'world','i':1}", MyObject.class); - } - } - - /** - * Source-code based on - * http://groups.google.com/group/google-gson/browse_thread/thread/563bb51ee2495081 - */ - @Test - public void testMultiThreadSerialization() throws InterruptedException { - final CountDownLatch startLatch = new CountDownLatch(1); - final CountDownLatch finishedLatch = new CountDownLatch(10); - final AtomicBoolean failed = new AtomicBoolean(false); - ExecutorService executor = Executors.newFixedThreadPool(10); - for (int taskCount = 0; taskCount < 10; taskCount++) { - executor.execute( - new Runnable() { - @Override - public void run() { - MyObject myObj = new MyObject(); - try { - startLatch.await(); - for (int i = 0; i < 10; i++) { - String unused = gson.toJson(myObj); - } - } catch (Throwable t) { - failed.set(true); - } finally { - finishedLatch.countDown(); - } - } - }); - } - startLatch.countDown(); - finishedLatch.await(); - assertThat(failed.get()).isFalse(); - } - - /** - * Source-code based on - * http://groups.google.com/group/google-gson/browse_thread/thread/563bb51ee2495081 - */ - @Test - public void testMultiThreadDeserialization() throws InterruptedException { - final CountDownLatch startLatch = new CountDownLatch(1); - final CountDownLatch finishedLatch = new CountDownLatch(10); - final AtomicBoolean failed = new AtomicBoolean(false); - ExecutorService executor = Executors.newFixedThreadPool(10); - for (int taskCount = 0; taskCount < 10; taskCount++) { - executor.execute( - new Runnable() { - @Override - public void run() { - try { - startLatch.await(); - for (int i = 0; i < 10; i++) { - MyObject unused = - gson.fromJson("{'a':'hello','b':'world','i':1}", MyObject.class); - } - } catch (Throwable t) { - failed.set(true); - } finally { - finishedLatch.countDown(); - } - } - }); - } - startLatch.countDown(); - finishedLatch.await(); - assertThat(failed.get()).isFalse(); - } - - @SuppressWarnings("unused") - private static class MyObject { - String a; - String b; - int i; - - MyObject() { - this("hello", "world", 42); - } - - public MyObject(String a, String b, int i) { - this.a = a; - this.b = b; - this.i = i; - } - } -} +/* + * Copyright (C) 2008 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.gson.functional; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.gson.Gson; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicBoolean; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests for ensuring Gson thread-safety. + * + * @author Inderjeet Singh + * @author Joel Leitch + */ +public class ConcurrencyTest { + private Gson gson; + + @Before + public void setUp() throws Exception { + gson = new Gson(); + } + + /** + * Source-code based on + * http://groups.google.com/group/google-gson/browse_thread/thread/563bb51ee2495081 + */ + @Test + public void testSingleThreadSerialization() { + MyObject myObj = new MyObject(); + for (int i = 0; i < 10; i++) { + String unused = gson.toJson(myObj); + } + } + + /** + * Source-code based on + * http://groups.google.com/group/google-gson/browse_thread/thread/563bb51ee2495081 + */ + @Test + public void testSingleThreadDeserialization() { + for (int i = 0; i < 10; i++) { + MyObject unused = gson.fromJson("{'a':'hello','b':'world','i':1}", MyObject.class); + } + } + + /** + * Source-code based on + * http://groups.google.com/group/google-gson/browse_thread/thread/563bb51ee2495081 + */ + @Test + public void testMultiThreadSerialization() throws InterruptedException { + final CountDownLatch startLatch = new CountDownLatch(1); + final CountDownLatch finishedLatch = new CountDownLatch(10); + final AtomicBoolean failed = new AtomicBoolean(false); + ExecutorService executor = Executors.newFixedThreadPool(10); + for (int taskCount = 0; taskCount < 10; taskCount++) { + executor.execute( + new Runnable() { + @Override + public void run() { + MyObject myObj = new MyObject(); + try { + startLatch.await(); + for (int i = 0; i < 10; i++) { + String unused = gson.toJson(myObj); + } + } catch (Throwable t) { + failed.set(true); + } finally { + finishedLatch.countDown(); + } + } + }); + } + startLatch.countDown(); + finishedLatch.await(); + assertThat(failed.get()).isFalse(); + } + + /** + * Source-code based on + * http://groups.google.com/group/google-gson/browse_thread/thread/563bb51ee2495081 + */ + @Test + public void testMultiThreadDeserialization() throws InterruptedException { + final CountDownLatch startLatch = new CountDownLatch(1); + final CountDownLatch finishedLatch = new CountDownLatch(10); + final AtomicBoolean failed = new AtomicBoolean(false); + ExecutorService executor = Executors.newFixedThreadPool(10); + for (int taskCount = 0; taskCount < 10; taskCount++) { + executor.execute( + new Runnable() { + @Override + public void run() { + try { + startLatch.await(); + for (int i = 0; i < 10; i++) { + MyObject unused = + gson.fromJson("{'a':'hello','b':'world','i':1}", MyObject.class); + } + } catch (Throwable t) { + failed.set(true); + } finally { + finishedLatch.countDown(); + } + } + }); + } + startLatch.countDown(); + finishedLatch.await(); + assertThat(failed.get()).isFalse(); + } + + @SuppressWarnings("unused") + private static class MyObject { + String a; + String b; + int i; + + MyObject() { + this("hello", "world", 42); + } + + public MyObject(String a, String b, int i) { + this.a = a; + this.b = b; + this.i = i; + } + } +} diff --git a/gson/src/test/java/com/google/gson/functional/InternationalizationTest.java b/gson/src/test/java/com/google/gson/functional/InternationalizationTest.java index b9510ad0..775c6ffd 100644 --- a/gson/src/test/java/com/google/gson/functional/InternationalizationTest.java +++ b/gson/src/test/java/com/google/gson/functional/InternationalizationTest.java @@ -1,83 +1,83 @@ -/* - * Copyright (C) 2008 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.gson.functional; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.gson.Gson; -import org.junit.Before; -import org.junit.Test; - -/** - * Functional tests for internationalized strings. - * - * @author Inderjeet Singh - */ -public class InternationalizationTest { - private Gson gson; - - @Before - public void setUp() throws Exception { - gson = new Gson(); - } - - @Test - public void testStringsWithUnicodeChineseCharactersSerialization() { - String target = "\u597d\u597d\u597d"; - String json = gson.toJson(target); - String expected = '"' + target + '"'; - assertThat(json).isEqualTo(expected); - } - - @Test - public void testStringsWithUnicodeChineseCharactersDeserialization() { - String expected = "\u597d\u597d\u597d"; - String json = '"' + expected + '"'; - String actual = gson.fromJson(json, String.class); - assertThat(actual).isEqualTo(expected); - } - - @Test - public void testStringsWithUnicodeChineseCharactersEscapedDeserialization() { - String actual = gson.fromJson("'\\u597d\\u597d\\u597d'", String.class); - assertThat(actual).isEqualTo("\u597d\u597d\u597d"); - } - - @Test - public void testSupplementaryUnicodeSerialization() { - // Supplementary code point U+1F60A - String supplementaryCodePoint = new String(new int[] {0x1F60A}, 0, 1); - String json = gson.toJson(supplementaryCodePoint); - assertThat(json).isEqualTo('"' + supplementaryCodePoint + '"'); - } - - @Test - public void testSupplementaryUnicodeDeserialization() { - // Supplementary code point U+1F60A - String supplementaryCodePoint = new String(new int[] {0x1F60A}, 0, 1); - String actual = gson.fromJson('"' + supplementaryCodePoint + '"', String.class); - assertThat(actual).isEqualTo(supplementaryCodePoint); - } - - @Test - public void testSupplementaryUnicodeEscapedDeserialization() { - // Supplementary code point U+1F60A - String supplementaryCodePoint = new String(new int[] {0x1F60A}, 0, 1); - String actual = gson.fromJson("\"\\uD83D\\uDE0A\"", String.class); - assertThat(actual).isEqualTo(supplementaryCodePoint); - } -} +/* + * Copyright (C) 2008 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gson.functional; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.gson.Gson; +import org.junit.Before; +import org.junit.Test; + +/** + * Functional tests for internationalized strings. + * + * @author Inderjeet Singh + */ +public class InternationalizationTest { + private Gson gson; + + @Before + public void setUp() throws Exception { + gson = new Gson(); + } + + @Test + public void testStringsWithUnicodeChineseCharactersSerialization() { + String target = "\u597d\u597d\u597d"; + String json = gson.toJson(target); + String expected = '"' + target + '"'; + assertThat(json).isEqualTo(expected); + } + + @Test + public void testStringsWithUnicodeChineseCharactersDeserialization() { + String expected = "\u597d\u597d\u597d"; + String json = '"' + expected + '"'; + String actual = gson.fromJson(json, String.class); + assertThat(actual).isEqualTo(expected); + } + + @Test + public void testStringsWithUnicodeChineseCharactersEscapedDeserialization() { + String actual = gson.fromJson("'\\u597d\\u597d\\u597d'", String.class); + assertThat(actual).isEqualTo("\u597d\u597d\u597d"); + } + + @Test + public void testSupplementaryUnicodeSerialization() { + // Supplementary code point U+1F60A + String supplementaryCodePoint = new String(new int[] {0x1F60A}, 0, 1); + String json = gson.toJson(supplementaryCodePoint); + assertThat(json).isEqualTo('"' + supplementaryCodePoint + '"'); + } + + @Test + public void testSupplementaryUnicodeDeserialization() { + // Supplementary code point U+1F60A + String supplementaryCodePoint = new String(new int[] {0x1F60A}, 0, 1); + String actual = gson.fromJson('"' + supplementaryCodePoint + '"', String.class); + assertThat(actual).isEqualTo(supplementaryCodePoint); + } + + @Test + public void testSupplementaryUnicodeEscapedDeserialization() { + // Supplementary code point U+1F60A + String supplementaryCodePoint = new String(new int[] {0x1F60A}, 0, 1); + String actual = gson.fromJson("\"\\uD83D\\uDE0A\"", String.class); + assertThat(actual).isEqualTo(supplementaryCodePoint); + } +} diff --git a/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java b/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java index 36e4d3b8..38983601 100644 --- a/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java +++ b/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java @@ -1,267 +1,267 @@ -/* - * Copyright (C) 2008 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.gson.functional; - -import static com.google.common.truth.Truth.assertThat; - -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.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.ClassWithObjects; -import java.lang.reflect.Type; -import java.util.Collection; -import org.junit.Before; -import org.junit.Test; - -/** - * Functional tests for the different cases for serializing (or ignoring) null fields and object. - * - * @author Inderjeet Singh - * @author Joel Leitch - */ -public class NullObjectAndFieldTest { - private GsonBuilder gsonBuilder; - - @Before - public void setUp() throws Exception { - gsonBuilder = new GsonBuilder().serializeNulls(); - } - - @Test - public void testTopLevelNullObjectSerialization() { - Gson gson = gsonBuilder.create(); - String actual = gson.toJson(null); - assertThat(actual).isEqualTo("null"); - - actual = gson.toJson(null, String.class); - assertThat(actual).isEqualTo("null"); - } - - @Test - public void testTopLevelNullObjectDeserialization() { - Gson gson = gsonBuilder.create(); - String actual = gson.fromJson("null", String.class); - assertThat(actual).isNull(); - } - - @Test - public void testExplicitSerializationOfNulls() { - Gson gson = gsonBuilder.create(); - ClassWithObjects target = new ClassWithObjects(null); - String actual = gson.toJson(target); - String expected = "{\"bag\":null}"; - assertThat(actual).isEqualTo(expected); - } - - @Test - public void testExplicitDeserializationOfNulls() { - Gson gson = gsonBuilder.create(); - ClassWithObjects target = gson.fromJson("{\"bag\":null}", ClassWithObjects.class); - assertThat(target.bag).isNull(); - } - - @Test - public void testExplicitSerializationOfNullArrayMembers() { - Gson gson = gsonBuilder.create(); - ClassWithMembers target = new ClassWithMembers(); - String json = gson.toJson(target); - assertThat(json).contains("\"array\":null"); - } - - /** Added to verify http://code.google.com/p/google-gson/issues/detail?id=68 */ - @Test - public void testNullWrappedPrimitiveMemberSerialization() { - Gson gson = gsonBuilder.serializeNulls().create(); - ClassWithNullWrappedPrimitive target = new ClassWithNullWrappedPrimitive(); - String json = gson.toJson(target); - assertThat(json).contains("\"value\":null"); - } - - /** Added to verify http://code.google.com/p/google-gson/issues/detail?id=68 */ - @Test - public void testNullWrappedPrimitiveMemberDeserialization() { - Gson gson = gsonBuilder.create(); - String json = "{'value':null}"; - ClassWithNullWrappedPrimitive target = gson.fromJson(json, ClassWithNullWrappedPrimitive.class); - assertThat(target.value).isNull(); - } - - @Test - public void testExplicitSerializationOfNullCollectionMembers() { - Gson gson = gsonBuilder.create(); - ClassWithMembers target = new ClassWithMembers(); - String json = gson.toJson(target); - assertThat(json).contains("\"col\":null"); - } - - @Test - public void testExplicitSerializationOfNullStringMembers() { - Gson gson = gsonBuilder.create(); - ClassWithMembers target = new ClassWithMembers(); - String json = gson.toJson(target); - assertThat(json).contains("\"str\":null"); - } - - @Test - public void testCustomSerializationOfNulls() { - gsonBuilder.registerTypeAdapter(ClassWithObjects.class, new ClassWithObjectsSerializer()); - Gson gson = gsonBuilder.create(); - ClassWithObjects target = new ClassWithObjects(new BagOfPrimitives()); - String actual = gson.toJson(target); - String expected = "{\"bag\":null}"; - assertThat(actual).isEqualTo(expected); - } - - @Test - public void testPrintPrintingObjectWithNulls() { - gsonBuilder = new GsonBuilder(); - Gson gson = gsonBuilder.create(); - String result = gson.toJson(new ClassWithMembers()); - assertThat(result).isEqualTo("{}"); - - gson = gsonBuilder.serializeNulls().create(); - result = gson.toJson(new ClassWithMembers()); - assertThat(result).contains("\"str\":null"); - } - - @Test - public void testPrintPrintingArraysWithNulls() { - gsonBuilder = new GsonBuilder(); - Gson gson = gsonBuilder.create(); - String result = gson.toJson(new String[] {"1", null, "3"}); - assertThat(result).isEqualTo("[\"1\",null,\"3\"]"); - - gson = gsonBuilder.serializeNulls().create(); - result = gson.toJson(new String[] {"1", null, "3"}); - assertThat(result).isEqualTo("[\"1\",null,\"3\"]"); - } - - // test for issue 389 - @Test - public void testAbsentJsonElementsAreSetToNull() { - Gson gson = new Gson(); - ClassWithInitializedMembers target = - gson.fromJson("{array:[1,2,3]}", ClassWithInitializedMembers.class); - assertThat(target.array).hasLength(3); - assertThat(target.array[1]).isEqualTo(2); - assertThat(target.str1).isEqualTo(ClassWithInitializedMembers.MY_STRING_DEFAULT); - assertThat(target.str2).isNull(); - assertThat(target.int1).isEqualTo(ClassWithInitializedMembers.MY_INT_DEFAULT); - // test the default value of a primitive int field per JVM spec - assertThat(target.int2).isEqualTo(0); - assertThat(target.bool1).isEqualTo(ClassWithInitializedMembers.MY_BOOLEAN_DEFAULT); - // test the default value of a primitive boolean field per JVM spec - assertThat(target.bool2).isFalse(); - } - - public static class ClassWithInitializedMembers { - // Using a mix of no-args constructor and field initializers - // Also, some fields are intialized and some are not (so initialized per JVM spec) - public static final String MY_STRING_DEFAULT = "string"; - private static final int MY_INT_DEFAULT = 2; - private static final boolean MY_BOOLEAN_DEFAULT = true; - int[] array; - String str1, str2; - int int1 = MY_INT_DEFAULT; - int int2; - boolean bool1 = MY_BOOLEAN_DEFAULT; - boolean bool2; - - public ClassWithInitializedMembers() { - str1 = MY_STRING_DEFAULT; - } - } - - private static class ClassWithNullWrappedPrimitive { - private Long value; - } - - @SuppressWarnings("unused") - private static class ClassWithMembers { - String str; - int[] array; - Collection col; - } - - private static class ClassWithObjectsSerializer implements JsonSerializer { - @Override - public JsonElement serialize( - ClassWithObjects src, Type typeOfSrc, JsonSerializationContext context) { - JsonObject obj = new JsonObject(); - obj.add("bag", JsonNull.INSTANCE); - return obj; - } - } - - @Test - public void testExplicitNullSetsFieldToNullDuringDeserialization() { - Gson gson = new Gson(); - String json = "{value:null}"; - ObjectWithField obj = gson.fromJson(json, ObjectWithField.class); - assertThat(obj.value).isNull(); - } - - @Test - public void testCustomTypeAdapterPassesNullSerialization() { - Gson gson = - new GsonBuilder() - .registerTypeAdapter( - ObjectWithField.class, - new JsonSerializer() { - @Override - public JsonElement serialize( - ObjectWithField src, Type typeOfSrc, JsonSerializationContext context) { - return context.serialize(null); - } - }) - .create(); - ObjectWithField target = new ObjectWithField(); - target.value = "value1"; - String json = gson.toJson(target); - assertThat(json).doesNotContain("value1"); - } - - @Test - public void testCustomTypeAdapterPassesNullDesrialization() { - Gson gson = - new GsonBuilder() - .registerTypeAdapter( - ObjectWithField.class, - new JsonDeserializer() { - @Override - public ObjectWithField deserialize( - JsonElement json, Type type, JsonDeserializationContext context) { - return context.deserialize(null, type); - } - }) - .create(); - String json = "{value:'value1'}"; - ObjectWithField target = gson.fromJson(json, ObjectWithField.class); - assertThat(target).isNull(); - } - - private static class ObjectWithField { - String value = ""; - } -} +/* + * Copyright (C) 2008 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gson.functional; + +import static com.google.common.truth.Truth.assertThat; + +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.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.ClassWithObjects; +import java.lang.reflect.Type; +import java.util.Collection; +import org.junit.Before; +import org.junit.Test; + +/** + * Functional tests for the different cases for serializing (or ignoring) null fields and object. + * + * @author Inderjeet Singh + * @author Joel Leitch + */ +public class NullObjectAndFieldTest { + private GsonBuilder gsonBuilder; + + @Before + public void setUp() throws Exception { + gsonBuilder = new GsonBuilder().serializeNulls(); + } + + @Test + public void testTopLevelNullObjectSerialization() { + Gson gson = gsonBuilder.create(); + String actual = gson.toJson(null); + assertThat(actual).isEqualTo("null"); + + actual = gson.toJson(null, String.class); + assertThat(actual).isEqualTo("null"); + } + + @Test + public void testTopLevelNullObjectDeserialization() { + Gson gson = gsonBuilder.create(); + String actual = gson.fromJson("null", String.class); + assertThat(actual).isNull(); + } + + @Test + public void testExplicitSerializationOfNulls() { + Gson gson = gsonBuilder.create(); + ClassWithObjects target = new ClassWithObjects(null); + String actual = gson.toJson(target); + String expected = "{\"bag\":null}"; + assertThat(actual).isEqualTo(expected); + } + + @Test + public void testExplicitDeserializationOfNulls() { + Gson gson = gsonBuilder.create(); + ClassWithObjects target = gson.fromJson("{\"bag\":null}", ClassWithObjects.class); + assertThat(target.bag).isNull(); + } + + @Test + public void testExplicitSerializationOfNullArrayMembers() { + Gson gson = gsonBuilder.create(); + ClassWithMembers target = new ClassWithMembers(); + String json = gson.toJson(target); + assertThat(json).contains("\"array\":null"); + } + + /** Added to verify http://code.google.com/p/google-gson/issues/detail?id=68 */ + @Test + public void testNullWrappedPrimitiveMemberSerialization() { + Gson gson = gsonBuilder.serializeNulls().create(); + ClassWithNullWrappedPrimitive target = new ClassWithNullWrappedPrimitive(); + String json = gson.toJson(target); + assertThat(json).contains("\"value\":null"); + } + + /** Added to verify http://code.google.com/p/google-gson/issues/detail?id=68 */ + @Test + public void testNullWrappedPrimitiveMemberDeserialization() { + Gson gson = gsonBuilder.create(); + String json = "{'value':null}"; + ClassWithNullWrappedPrimitive target = gson.fromJson(json, ClassWithNullWrappedPrimitive.class); + assertThat(target.value).isNull(); + } + + @Test + public void testExplicitSerializationOfNullCollectionMembers() { + Gson gson = gsonBuilder.create(); + ClassWithMembers target = new ClassWithMembers(); + String json = gson.toJson(target); + assertThat(json).contains("\"col\":null"); + } + + @Test + public void testExplicitSerializationOfNullStringMembers() { + Gson gson = gsonBuilder.create(); + ClassWithMembers target = new ClassWithMembers(); + String json = gson.toJson(target); + assertThat(json).contains("\"str\":null"); + } + + @Test + public void testCustomSerializationOfNulls() { + gsonBuilder.registerTypeAdapter(ClassWithObjects.class, new ClassWithObjectsSerializer()); + Gson gson = gsonBuilder.create(); + ClassWithObjects target = new ClassWithObjects(new BagOfPrimitives()); + String actual = gson.toJson(target); + String expected = "{\"bag\":null}"; + assertThat(actual).isEqualTo(expected); + } + + @Test + public void testPrintPrintingObjectWithNulls() { + gsonBuilder = new GsonBuilder(); + Gson gson = gsonBuilder.create(); + String result = gson.toJson(new ClassWithMembers()); + assertThat(result).isEqualTo("{}"); + + gson = gsonBuilder.serializeNulls().create(); + result = gson.toJson(new ClassWithMembers()); + assertThat(result).contains("\"str\":null"); + } + + @Test + public void testPrintPrintingArraysWithNulls() { + gsonBuilder = new GsonBuilder(); + Gson gson = gsonBuilder.create(); + String result = gson.toJson(new String[] {"1", null, "3"}); + assertThat(result).isEqualTo("[\"1\",null,\"3\"]"); + + gson = gsonBuilder.serializeNulls().create(); + result = gson.toJson(new String[] {"1", null, "3"}); + assertThat(result).isEqualTo("[\"1\",null,\"3\"]"); + } + + // test for issue 389 + @Test + public void testAbsentJsonElementsAreSetToNull() { + Gson gson = new Gson(); + ClassWithInitializedMembers target = + gson.fromJson("{array:[1,2,3]}", ClassWithInitializedMembers.class); + assertThat(target.array).hasLength(3); + assertThat(target.array[1]).isEqualTo(2); + assertThat(target.str1).isEqualTo(ClassWithInitializedMembers.MY_STRING_DEFAULT); + assertThat(target.str2).isNull(); + assertThat(target.int1).isEqualTo(ClassWithInitializedMembers.MY_INT_DEFAULT); + // test the default value of a primitive int field per JVM spec + assertThat(target.int2).isEqualTo(0); + assertThat(target.bool1).isEqualTo(ClassWithInitializedMembers.MY_BOOLEAN_DEFAULT); + // test the default value of a primitive boolean field per JVM spec + assertThat(target.bool2).isFalse(); + } + + public static class ClassWithInitializedMembers { + // Using a mix of no-args constructor and field initializers + // Also, some fields are intialized and some are not (so initialized per JVM spec) + public static final String MY_STRING_DEFAULT = "string"; + private static final int MY_INT_DEFAULT = 2; + private static final boolean MY_BOOLEAN_DEFAULT = true; + int[] array; + String str1, str2; + int int1 = MY_INT_DEFAULT; + int int2; + boolean bool1 = MY_BOOLEAN_DEFAULT; + boolean bool2; + + public ClassWithInitializedMembers() { + str1 = MY_STRING_DEFAULT; + } + } + + private static class ClassWithNullWrappedPrimitive { + private Long value; + } + + @SuppressWarnings("unused") + private static class ClassWithMembers { + String str; + int[] array; + Collection col; + } + + private static class ClassWithObjectsSerializer implements JsonSerializer { + @Override + public JsonElement serialize( + ClassWithObjects src, Type typeOfSrc, JsonSerializationContext context) { + JsonObject obj = new JsonObject(); + obj.add("bag", JsonNull.INSTANCE); + return obj; + } + } + + @Test + public void testExplicitNullSetsFieldToNullDuringDeserialization() { + Gson gson = new Gson(); + String json = "{value:null}"; + ObjectWithField obj = gson.fromJson(json, ObjectWithField.class); + assertThat(obj.value).isNull(); + } + + @Test + public void testCustomTypeAdapterPassesNullSerialization() { + Gson gson = + new GsonBuilder() + .registerTypeAdapter( + ObjectWithField.class, + new JsonSerializer() { + @Override + public JsonElement serialize( + ObjectWithField src, Type typeOfSrc, JsonSerializationContext context) { + return context.serialize(null); + } + }) + .create(); + ObjectWithField target = new ObjectWithField(); + target.value = "value1"; + String json = gson.toJson(target); + assertThat(json).doesNotContain("value1"); + } + + @Test + public void testCustomTypeAdapterPassesNullDesrialization() { + Gson gson = + new GsonBuilder() + .registerTypeAdapter( + ObjectWithField.class, + new JsonDeserializer() { + @Override + public ObjectWithField deserialize( + JsonElement json, Type type, JsonDeserializationContext context) { + return context.deserialize(null, type); + } + }) + .create(); + String json = "{value:'value1'}"; + ObjectWithField target = gson.fromJson(json, ObjectWithField.class); + assertThat(target).isNull(); + } + + private static class ObjectWithField { + String value = ""; + } +} diff --git a/gson/src/test/java/com/google/gson/internal/LazilyParsedNumberTest.java b/gson/src/test/java/com/google/gson/internal/LazilyParsedNumberTest.java index e47f2ee0..5dce3790 100644 --- a/gson/src/test/java/com/google/gson/internal/LazilyParsedNumberTest.java +++ b/gson/src/test/java/com/google/gson/internal/LazilyParsedNumberTest.java @@ -1,54 +1,54 @@ -/* - * Copyright (C) 2015 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.gson.internal; - -import static com.google.common.truth.Truth.assertThat; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.math.BigDecimal; -import org.junit.Test; - -public class LazilyParsedNumberTest { - @Test - public void testHashCode() { - LazilyParsedNumber n1 = new LazilyParsedNumber("1"); - LazilyParsedNumber n1Another = new LazilyParsedNumber("1"); - assertThat(n1Another.hashCode()).isEqualTo(n1.hashCode()); - } - - @Test - public void testEquals() { - LazilyParsedNumber n1 = new LazilyParsedNumber("1"); - LazilyParsedNumber n1Another = new LazilyParsedNumber("1"); - assertThat(n1.equals(n1Another)).isTrue(); - } - - @Test - public void testJavaSerialization() throws IOException, ClassNotFoundException { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ObjectOutputStream objOut = new ObjectOutputStream(out); - objOut.writeObject(new LazilyParsedNumber("123")); - objOut.close(); - - ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())); - Number deserialized = (Number) objIn.readObject(); - assertThat(deserialized).isEqualTo(new BigDecimal("123")); - } -} +/* + * Copyright (C) 2015 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.gson.internal; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.math.BigDecimal; +import org.junit.Test; + +public class LazilyParsedNumberTest { + @Test + public void testHashCode() { + LazilyParsedNumber n1 = new LazilyParsedNumber("1"); + LazilyParsedNumber n1Another = new LazilyParsedNumber("1"); + assertThat(n1Another.hashCode()).isEqualTo(n1.hashCode()); + } + + @Test + public void testEquals() { + LazilyParsedNumber n1 = new LazilyParsedNumber("1"); + LazilyParsedNumber n1Another = new LazilyParsedNumber("1"); + assertThat(n1.equals(n1Another)).isTrue(); + } + + @Test + public void testJavaSerialization() throws IOException, ClassNotFoundException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ObjectOutputStream objOut = new ObjectOutputStream(out); + objOut.writeObject(new LazilyParsedNumber("123")); + objOut.close(); + + ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())); + Number deserialized = (Number) objIn.readObject(); + assertThat(deserialized).isEqualTo(new BigDecimal("123")); + } +}