/* * 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() { 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() { 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() { 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() { 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()); } 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, JsonDeserializer { 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); } } }