|
| 1 | +/* |
| 2 | + * Copyright (C) 2016 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.gson.functional; |
| 18 | + |
| 19 | +import java.lang.reflect.Type; |
| 20 | + |
| 21 | +import com.google.gson.Gson; |
| 22 | +import com.google.gson.JsonDeserializationContext; |
| 23 | +import com.google.gson.JsonDeserializer; |
| 24 | +import com.google.gson.JsonElement; |
| 25 | +import com.google.gson.JsonParseException; |
| 26 | +import com.google.gson.JsonPrimitive; |
| 27 | +import com.google.gson.JsonSerializationContext; |
| 28 | +import com.google.gson.JsonSerializer; |
| 29 | +import com.google.gson.annotations.JsonAdapter; |
| 30 | + |
| 31 | +import junit.framework.TestCase; |
| 32 | + |
| 33 | +/** |
| 34 | + * Functional tests for the {@link JsonAdapter} annotation on fields where the value is of |
| 35 | + * type {@link JsonSerializer} or {@link JsonDeserializer}. |
| 36 | + */ |
| 37 | +public final class JsonAdapterSerializerDeserializerTest extends TestCase { |
| 38 | + |
| 39 | + public void testJsonSerializerDeserializerBasedJsonAdapterOnFields() { |
| 40 | + Gson gson = new Gson(); |
| 41 | + String json = gson.toJson(new Computer(new User("Inderjeet Singh"), null, new User("Jesse Wilson"))); |
| 42 | + assertEquals("{\"user1\":\"UserSerializer\",\"user3\":\"UserSerializerDeserializer\"}", json); |
| 43 | + Computer computer = gson.fromJson("{'user2':'Jesse Wilson','user3':'Jake Wharton'}", Computer.class); |
| 44 | + assertEquals("UserSerializer", computer.user2.name); |
| 45 | + assertEquals("UserSerializerDeserializer", computer.user3.name); |
| 46 | + } |
| 47 | + |
| 48 | + private static final class Computer { |
| 49 | + @JsonAdapter(UserSerializer.class) final User user1; |
| 50 | + @JsonAdapter(UserDeserializer.class) final User user2; |
| 51 | + @JsonAdapter(UserSerializerDeserializer.class) final User user3; |
| 52 | + Computer(User user1, User user2, User user3) { |
| 53 | + this.user1 = user1; |
| 54 | + this.user2 = user2; |
| 55 | + this.user3 = user3; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private static final class User { |
| 60 | + public final String name; |
| 61 | + private User(String name) { |
| 62 | + this.name = name; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static final class UserSerializer implements JsonSerializer<User> { |
| 67 | + @Override |
| 68 | + public JsonElement serialize(User src, Type typeOfSrc, JsonSerializationContext context) { |
| 69 | + return new JsonPrimitive("UserSerializer"); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private static final class UserDeserializer implements JsonDeserializer<User> { |
| 74 | + @Override |
| 75 | + public User deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) |
| 76 | + throws JsonParseException { |
| 77 | + return new User("UserSerializer"); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private static final class UserSerializerDeserializer implements JsonSerializer<User>, JsonDeserializer<User> { |
| 82 | + @Override |
| 83 | + public JsonElement serialize(User src, Type typeOfSrc, JsonSerializationContext context) { |
| 84 | + return new JsonPrimitive("UserSerializerDeserializer"); |
| 85 | + } |
| 86 | + @Override |
| 87 | + public User deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) |
| 88 | + throws JsonParseException { |
| 89 | + return new User("UserSerializerDeserializer"); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public void testJsonSerializerDeserializerBasedJsonAdapterOnClass() { |
| 94 | + Gson gson = new Gson(); |
| 95 | + String json = gson.toJson(new Computer2(new User2("Inderjeet Singh"))); |
| 96 | + assertEquals("{\"user\":\"UserSerializerDeserializer2\"}", json); |
| 97 | + Computer2 computer = gson.fromJson("{'user':'Inderjeet Singh'}", Computer2.class); |
| 98 | + assertEquals("UserSerializerDeserializer2", computer.user.name); |
| 99 | + } |
| 100 | + |
| 101 | + private static final class Computer2 { |
| 102 | + final User2 user; |
| 103 | + Computer2(User2 user) { |
| 104 | + this.user = user; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @JsonAdapter(UserSerializerDeserializer2.class) |
| 109 | + private static final class User2 { |
| 110 | + public final String name; |
| 111 | + private User2(String name) { |
| 112 | + this.name = name; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private static final class UserSerializerDeserializer2 implements JsonSerializer<User2>, JsonDeserializer<User2> { |
| 117 | + @Override |
| 118 | + public JsonElement serialize(User2 src, Type typeOfSrc, JsonSerializationContext context) { |
| 119 | + return new JsonPrimitive("UserSerializerDeserializer2"); |
| 120 | + } |
| 121 | + @Override |
| 122 | + public User2 deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) |
| 123 | + throws JsonParseException { |
| 124 | + return new User2("UserSerializerDeserializer2"); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public void testDifferentJsonAdaptersForGenericFieldsOfSameRawType() { |
| 129 | + Container c = new Container("Foo", 10); |
| 130 | + Gson gson = new Gson(); |
| 131 | + String json = gson.toJson(c); |
| 132 | + assertTrue(json.contains("\"a\":\"BaseStringAdapter\"")); |
| 133 | + assertTrue(json.contains("\"b\":\"BaseIntegerAdapter\"")); |
| 134 | + } |
| 135 | + |
| 136 | + private static final class Container { |
| 137 | + @JsonAdapter(BaseStringAdapter.class) Base<String> a; |
| 138 | + @JsonAdapter(BaseIntegerAdapter.class) Base<Integer> b; |
| 139 | + Container(String a, int b) { |
| 140 | + this.a = new Base<String>(a); |
| 141 | + this.b = new Base<Integer>(b); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private static final class Base<T> { |
| 146 | + @SuppressWarnings("unused") |
| 147 | + T value; |
| 148 | + Base(T value) { |
| 149 | + this.value = value; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private static final class BaseStringAdapter implements JsonSerializer<Base<String>> { |
| 154 | + @Override public JsonElement serialize(Base<String> src, Type typeOfSrc, JsonSerializationContext context) { |
| 155 | + return new JsonPrimitive("BaseStringAdapter"); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private static final class BaseIntegerAdapter implements JsonSerializer<Base<Integer>> { |
| 160 | + @Override public JsonElement serialize(Base<Integer> src, Type typeOfSrc, JsonSerializationContext context) { |
| 161 | + return new JsonPrimitive("BaseIntegerAdapter"); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments