Mention that GsonBuilder.registerTypeAdapter makes (de-)serializers null-safe (#1704)
This commit is contained in:
parent
5bebf970d1
commit
b0b6834157
@ -534,6 +534,12 @@ public final class GsonBuilder {
|
||||
* types! For example, applications registering {@code boolean.class} should also register {@code
|
||||
* Boolean.class}.
|
||||
*
|
||||
* <p>{@link JsonSerializer} and {@link JsonDeserializer} are made "{@code null}-safe". This
|
||||
* means when trying to serialize {@code null}, Gson will write a JSON {@code null} and the
|
||||
* serializer is not called. Similarly when deserializing a JSON {@code null}, Gson will emit
|
||||
* {@code null} without calling the deserializer. If it is desired to handle {@code null} values,
|
||||
* a {@link TypeAdapter} should be used instead.
|
||||
*
|
||||
* @param type the type definition for the type adapter being registered
|
||||
* @param typeAdapter This object must implement at least one of the {@link TypeAdapter},
|
||||
* {@link InstanceCreator}, {@link JsonSerializer}, and a {@link JsonDeserializer} interfaces.
|
||||
|
@ -53,10 +53,16 @@ public class GsonTypeAdapterTest extends TestCase {
|
||||
fail("Type Adapter should have thrown an exception");
|
||||
} catch (IllegalStateException expected) { }
|
||||
|
||||
// Verify that serializer is made null-safe, i.e. it is not called for null
|
||||
assertEquals("null", gson.toJson(null, AtomicLong.class));
|
||||
|
||||
try {
|
||||
gson.fromJson("123", AtomicLong.class);
|
||||
fail("Type Adapter should have thrown an exception");
|
||||
} catch (JsonParseException expected) { }
|
||||
|
||||
// Verify that deserializer is made null-safe, i.e. it is not called for null
|
||||
assertNull(gson.fromJson(JsonNull.INSTANCE, AtomicLong.class));
|
||||
}
|
||||
|
||||
public void testTypeAdapterProperlyConvertsTypes() throws Exception {
|
||||
|
Loading…
Reference in New Issue
Block a user