gson-comments/gson/src/test/java/com/google/gson/functional/CustomTypeAdaptersTest.java
Inderjeet Singh cc90a68241 Fixed issue 68 by providing support to override default type adapters for
primitive types. Added a visitFieldUsingCustomHandler method to handle this in
the visitors.
2008-10-31 00:19:58 +00:00

256 lines
9.2 KiB
Java

/*
* 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 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.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.common.TestTypes.BagOfPrimitives;
import com.google.gson.common.TestTypes.ClassWithCustomTypeConverter;
import junit.framework.TestCase;
import java.lang.reflect.Type;
/**
* Functional tests for the support of custom serializer and deserializers.
*
* @author Inderjeet Singh
* @author Joel Leitch
*/
public class CustomTypeAdaptersTest extends TestCase {
private GsonBuilder builder;
@Override
protected void setUp() throws Exception {
super.setUp();
builder = new GsonBuilder();
}
public void testCustomSerializers() {
Gson gson = builder.registerTypeAdapter(
ClassWithCustomTypeConverter.class, new JsonSerializer<ClassWithCustomTypeConverter>() {
public JsonElement serialize(ClassWithCustomTypeConverter src, Type typeOfSrc,
JsonSerializationContext context) {
JsonObject json = new JsonObject();
json.addProperty("bag", 5);
json.addProperty("value", 25);
return json;
}
}).create();
ClassWithCustomTypeConverter target = new ClassWithCustomTypeConverter();
assertEquals("{\"bag\":5,\"value\":25}", gson.toJson(target));
}
public void testCustomDeserializers() {
Gson gson = new GsonBuilder().registerTypeAdapter(
ClassWithCustomTypeConverter.class, new JsonDeserializer<ClassWithCustomTypeConverter>() {
public ClassWithCustomTypeConverter deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) {
JsonObject jsonObject = json.getAsJsonObject();
int value = jsonObject.get("bag").getAsInt();
return new ClassWithCustomTypeConverter(new BagOfPrimitives(value,
value, false, ""), value);
}
}).create();
String json = "{\"bag\":5,\"value\":25}";
ClassWithCustomTypeConverter target = gson.fromJson(json, ClassWithCustomTypeConverter.class);
assertEquals(5, target.getBag().getIntValue());
}
public void disable_testCustomSerializersOfSelf() {
Gson gson = createGsonObjectWithFooTypeAdapter();
Gson basicGson = new Gson();
Foo newFooObject = new Foo(1, 2L);
String jsonFromCustomSerializer = gson.toJson(newFooObject);
String jsonFromGson = basicGson.toJson(newFooObject);
assertEquals(jsonFromGson, jsonFromCustomSerializer);
}
public void disable_testCustomDeserializersOfSelf() {
Gson gson = createGsonObjectWithFooTypeAdapter();
Gson basicGson = new Gson();
Foo expectedFoo = new Foo(1, 2L);
String json = basicGson.toJson(expectedFoo);
Foo newFooObject = gson.fromJson(json, Foo.class);
assertEquals(expectedFoo.key, newFooObject.key);
assertEquals(expectedFoo.value, newFooObject.value);
}
public void testCustomNestedSerializers() {
Gson gson = new GsonBuilder().registerTypeAdapter(
BagOfPrimitives.class, new JsonSerializer<BagOfPrimitives>() {
public JsonElement serialize(BagOfPrimitives src, Type typeOfSrc,
JsonSerializationContext context) {
return new JsonPrimitive(6);
}
}).create();
ClassWithCustomTypeConverter target = new ClassWithCustomTypeConverter();
assertEquals("{\"bag\":6,\"value\":10}", gson.toJson(target));
}
public void testCustomNestedDeserializers() {
Gson gson = new GsonBuilder().registerTypeAdapter(
BagOfPrimitives.class, new JsonDeserializer<BagOfPrimitives>() {
public BagOfPrimitives deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
int value = json.getAsInt();
return new BagOfPrimitives(value, value, false, "");
}
}).create();
String json = "{\"bag\":7,\"value\":25}";
ClassWithCustomTypeConverter target = gson.fromJson(json, ClassWithCustomTypeConverter.class);
assertEquals(7, target.getBag().getIntValue());
}
public void testCustomTypeAdapterDoesNotAppliesToSubClasses() {
Gson gson = new GsonBuilder().registerTypeAdapter(Base.class, new JsonSerializer<Base> () {
public JsonElement serialize(Base src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject json = new JsonObject();
json.addProperty("value", src.baseValue);
return json;
}
}).create();
Base b = new Base();
String json = gson.toJson(b);
assertTrue(json.contains("value"));
b = new Derived();
json = gson.toJson(b);
assertTrue(json.contains("derivedValue"));
}
public void testCustomTypeAdapterAppliesToSubClassesSerializedAsBaseClass() {
Gson gson = new GsonBuilder().registerTypeAdapter(Base.class, new JsonSerializer<Base> () {
public JsonElement serialize(Base src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject json = new JsonObject();
json.addProperty("value", src.baseValue);
return json;
}
}).create();
Base b = new Base();
String json = gson.toJson(b);
assertTrue(json.contains("value"));
b = new Derived();
json = gson.toJson(b, Base.class);
assertTrue(json.contains("value"));
assertFalse(json.contains("derivedValue"));
}
private static class Base {
int baseValue = 2;
}
private static class Derived extends Base {
int derivedValue = 3;
}
private Gson createGsonObjectWithFooTypeAdapter() {
return new GsonBuilder().registerTypeAdapter(Foo.class, new FooTypeAdapter()).create();
}
public static class Foo {
private final int key;
private final long value;
public Foo() {
this(0, 0L);
}
public Foo(int key, long value) {
this.key = key;
this.value = value;
}
}
public static class FooTypeAdapter implements JsonSerializer<Foo>, JsonDeserializer<Foo> {
public Foo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return context.deserialize(json, typeOfT);
}
public JsonElement serialize(Foo src, Type typeOfSrc, JsonSerializationContext context) {
return context.serialize(src, typeOfSrc);
}
}
public void testCustomSerializerForLong() {
final ClassWithBooleanField customSerializerInvoked = new ClassWithBooleanField();
customSerializerInvoked.value = false;
Gson gson = new GsonBuilder().registerTypeAdapter(Long.class, new JsonSerializer<Long>() {
public JsonElement serialize(Long src, Type typeOfSrc, JsonSerializationContext context) {
customSerializerInvoked.value = true;
return src == null ? new JsonNull() : new JsonPrimitive(src);
}
}).serializeNulls().create();
ClassWithWrapperLongField src = new ClassWithWrapperLongField();
String json = gson.toJson(src);
assertTrue(json.contains("\"value\":null"));
assertTrue(customSerializerInvoked.value);
customSerializerInvoked.value = false;
src.value = 10L;
json = gson.toJson(src);
assertTrue(json.contains("\"value\":10"));
assertTrue(customSerializerInvoked.value);
}
public void testCustomDeserializerForLong() {
final ClassWithBooleanField customDeserializerInvoked = new ClassWithBooleanField();
customDeserializerInvoked.value = false;
Gson gson = new GsonBuilder().registerTypeAdapter(Long.class, new JsonDeserializer<Long>() {
public Long deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
customDeserializerInvoked.value = true;
if (json == null || json.isJsonNull()) {
return null;
} else {
Number number = json.getAsJsonPrimitive().getAsNumber();
return number == null ? null : number.longValue();
}
}
}).create();
String json = "{'value':null}";
ClassWithWrapperLongField target = gson.fromJson(json, ClassWithWrapperLongField.class);
assertNull(target.value);
assertTrue(customDeserializerInvoked.value);
customDeserializerInvoked.value = false;
json = "{'value':10}";
target = gson.fromJson(json, ClassWithWrapperLongField.class);
assertEquals(10L, target.value.longValue());
assertTrue(customDeserializerInvoked.value);
}
private static class ClassWithWrapperLongField {
Long value;
}
private static class ClassWithBooleanField {
Boolean value;
}
}