From 2236c95c37e4ac882c20ac080e4ebc68b85b296c Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Wed, 28 Sep 2011 18:00:34 +0000 Subject: [PATCH] Death to serializeDefault and deserializeDefault --- .../gson/JsonDeserializationContext.java | 4 - .../google/gson/JsonSerializationContext.java | 4 - .../SystemOnlyTypeAdaptersTest.java | 77 ------------------- 3 files changed, 85 deletions(-) delete mode 100644 gson/src/test/java/com/google/gson/functional/SystemOnlyTypeAdaptersTest.java diff --git a/gson/src/main/java/com/google/gson/JsonDeserializationContext.java b/gson/src/main/java/com/google/gson/JsonDeserializationContext.java index da0e2dbb..1e4263f8 100644 --- a/gson/src/main/java/com/google/gson/JsonDeserializationContext.java +++ b/gson/src/main/java/com/google/gson/JsonDeserializationContext.java @@ -60,8 +60,4 @@ public class JsonDeserializationContext { public T deserialize(JsonElement json, Type typeOfT) throws JsonParseException { return gson.fromJson(json, typeOfT); } - - public T deserializeDefault(JsonElement json, Type typeOfT) throws JsonParseException { - throw new UnsupportedOperationException(); - } } \ No newline at end of file diff --git a/gson/src/main/java/com/google/gson/JsonSerializationContext.java b/gson/src/main/java/com/google/gson/JsonSerializationContext.java index e5fee93d..83f88776 100644 --- a/gson/src/main/java/com/google/gson/JsonSerializationContext.java +++ b/gson/src/main/java/com/google/gson/JsonSerializationContext.java @@ -56,8 +56,4 @@ public class JsonSerializationContext { public JsonElement serialize(Object src, Type typeOfSrc) { return gson.toJsonTree(src, typeOfSrc); } - - public JsonElement serializeDefault(Object src, Type typeOfSrc) { - throw new UnsupportedOperationException(); - } } \ No newline at end of file diff --git a/gson/src/test/java/com/google/gson/functional/SystemOnlyTypeAdaptersTest.java b/gson/src/test/java/com/google/gson/functional/SystemOnlyTypeAdaptersTest.java deleted file mode 100644 index f76728fd..00000000 --- a/gson/src/test/java/com/google/gson/functional/SystemOnlyTypeAdaptersTest.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * 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.functional; - -import java.lang.reflect.Type; - -import junit.framework.TestCase; - -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.JsonSerializationContext; -import com.google.gson.JsonSerializer; - -/** - * Functional tests for serialize default behavior where a custom type adapter is allowed to invoke - * context.serialize on self. - * - * @author Inderjeet Singh - */ -public class SystemOnlyTypeAdaptersTest extends TestCase { - - private Gson gson; - @Override - protected void setUp() throws Exception { - super.setUp(); - this.gson = new GsonBuilder().registerTypeAdapter(Foo.class, new FooTypeAdapter()).create(); - } - - public void testSerializeDefault() { - String json = gson.toJson(new Foo()); - assertEquals("{\"a\":10,\"secret-key\":\"abracadabra\"}", json); - } - - public void testDeserializeDefault() { - String json = "{a:5,'secret-key':'abracadabra'}"; - Foo foo = gson.fromJson(json, Foo.class); - assertEquals(5, foo.a); - } - - private static class Foo { - int a = 10; - } - - private static class FooTypeAdapter implements JsonSerializer, JsonDeserializer { - public JsonElement serialize(Foo src, Type typeOfSrc, JsonSerializationContext context) { - JsonObject json = context.serializeDefault(src, typeOfSrc).getAsJsonObject(); - json.addProperty("secret-key", "abracadabra"); - return json; - } - - public Foo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - if (!"abracadabra".equals(json.getAsJsonObject().get("secret-key").getAsString())) { - throw new IllegalArgumentException("invalid key"); - } - return context.deserializeDefault(json, typeOfT); - } - } -}