From eaaa2a1b4f7daa7cc5c0485fba77bfe58e1ebf75 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Sun, 9 Mar 2014 07:31:02 +0000 Subject: [PATCH] added functional tests for applying JsonAdapter annotation on Fields. --- .../JsonAdapterAnnotationOnFieldsTest.java | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 gson/src/test/java/com/google/gson/functional/JsonAdapterAnnotationOnFieldsTest.java diff --git a/gson/src/test/java/com/google/gson/functional/JsonAdapterAnnotationOnFieldsTest.java b/gson/src/test/java/com/google/gson/functional/JsonAdapterAnnotationOnFieldsTest.java new file mode 100644 index 00000000..d459ab30 --- /dev/null +++ b/gson/src/test/java/com/google/gson/functional/JsonAdapterAnnotationOnFieldsTest.java @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2014 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.io.IOException; + +import junit.framework.TestCase; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.TypeAdapter; +import com.google.gson.annotations.JsonAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +/** + * Functional tests for the {@link com.google.gson.annotations.JsonAdapter} annotation on fields. + */ +public final class JsonAdapterAnnotationOnFieldsTest extends TestCase { + + public void testJsonAdapterInvoked() { + Gson gson = new Gson(); + String json = gson.toJson(new Computer(new User("Inderjeet Singh"))); + assertEquals("{\"user\":{\"firstName\":\"Inderjeet\",\"lastName\":\"Singh\"}}", json); + Computer computer = gson.fromJson("{'user':{'firstName':'Jesse','lastName':'Wilson'}}", Computer.class); + assertEquals("Jesse Wilson", computer.user.name); + } + + public void testRegisteredTypeAdapterOverridesFieldAnnotation() { + Gson gson = new GsonBuilder() + .registerTypeAdapter(Part.class, new TypeAdapter() { + @Override public void write(JsonWriter out, Part user) throws IOException { + out.value("registeredAdapter"); + } + @Override public Part read(JsonReader in) throws IOException { + return new Part(in.nextString()); + } + }).create(); + String json = gson.toJson(new Gadget(new Part("screen"))); + assertEquals("{\"part\":\"registeredAdapter\"}", json); + Gadget gadget = gson.fromJson("{'part':'registeredAdapterValue'}", Gadget.class); + assertEquals("registeredAdapterValue", gadget.part.name); + } + + private static final class Gadget { + @JsonAdapter(PartJsonAdapter.class) + final Part part; + Gadget(Part part) { + this.part = part; + } + } + + private static final class Part { + final String name; + Part(String name) { + this.name = name; + } + } + + private static class PartJsonAdapter extends TypeAdapter { + @Override public void write(JsonWriter out, Part part) throws IOException { + out.value(part.name); + } + @Override public Part read(JsonReader in) throws IOException { + in.nextString(); + return new Part("partJsonAdapter"); + } + } + + private static final class Computer { + @JsonAdapter(UserJsonAdapter.class) + final User user; + Computer(User user) { + this.user = user; + } + } + + @JsonAdapter(UserJsonAdapter.class) + private static class User { + public final String name; + private User(String name) { + this.name = name; + } + } + + private static class UserJsonAdapter extends TypeAdapter { + @Override public void write(JsonWriter out, User user) throws IOException { + // implement write: combine firstName and lastName into name + out.beginObject(); + String[] parts = user.name.split(" "); + out.name("firstName"); + out.value(parts[0]); + out.name("lastName"); + out.value(parts[1]); + out.endObject(); + } + @Override public User read(JsonReader in) throws IOException { + // implement read: split name into firstName and lastName + in.beginObject(); + in.nextName(); + String firstName = in.nextString(); + in.nextName(); + String lastName = in.nextString(); + in.endObject(); + return new User(firstName + " " + lastName); + } + } +}