gson-comments/gson/src/test/java/com/google/gson/functional/CustomTypeAdaptersTest.java

183 lines
6.4 KiB
Java
Raw Normal View History

2008-09-01 05:13:32 +02:00
/*
* 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.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());
}
2008-09-16 19:53:43 +02:00
public void disable_testCustomSerializersOfSelf() {
2008-09-16 19:53:43 +02:00
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() {
2008-09-16 19:53:43 +02:00
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);
}
2008-09-01 05:13:32 +02:00
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());
}
2008-09-16 19:53:43 +02:00
2008-10-14 20:54:34 +02:00
public void testCustomTypeAdapterAppliesToSubClasses() {
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;
}
2008-09-16 19:53:43 +02:00
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);
}
}
2008-09-01 05:13:32 +02:00
}