aboutsummaryrefslogtreecommitdiff
path: root/gson/src/test/java/com/google/gson/functional/JsonAdapterSerializerDeserializerTest.java
blob: f539884366ab96ec8a8dd3ca8b6d0b1951938a1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright (C) 2016 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 com.google.gson.Gson;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.annotations.JsonAdapter;

import junit.framework.TestCase;

/**
 * Functional tests for the {@link JsonAdapter} annotation on fields where the value is of
 * type {@link JsonSerializer} or {@link JsonDeserializer}.
 */
public final class JsonAdapterSerializerDeserializerTest extends TestCase {

  public void testJsonSerializerDeserializerBasedJsonAdapterOnFields() {
    Gson gson = new Gson();
    String json = gson.toJson(new Computer(new User("Inderjeet Singh"), null, new User("Jesse Wilson")));
    assertEquals("{\"user1\":\"UserSerializer\",\"user3\":\"UserSerializerDeserializer\"}", json);
    Computer computer = gson.fromJson("{'user2':'Jesse Wilson','user3':'Jake Wharton'}", Computer.class);
    assertEquals("UserSerializer", computer.user2.name);
    assertEquals("UserSerializerDeserializer", computer.user3.name);
  }

  private static final class Computer {
    @JsonAdapter(UserSerializer.class) final User user1;
    @JsonAdapter(UserDeserializer.class) final User user2;
    @JsonAdapter(UserSerializerDeserializer.class) final User user3;
    Computer(User user1, User user2, User user3) {
      this.user1 = user1;
      this.user2 = user2;
      this.user3 = user3;
    }
  }

  private static final class User {
    public final String name;
    private User(String name) {
      this.name = name;
    }
  }

  private static final class UserSerializer implements JsonSerializer<User> {
    @Override
    public JsonElement serialize(User src, Type typeOfSrc, JsonSerializationContext context) {
      return new JsonPrimitive("UserSerializer");
    }
  }

  private static final class UserDeserializer implements JsonDeserializer<User> {
    @Override
    public User deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
      return new User("UserSerializer");
    }
  }

  private static final class UserSerializerDeserializer implements JsonSerializer<User>, JsonDeserializer<User> {
    @Override
    public JsonElement serialize(User src, Type typeOfSrc, JsonSerializationContext context) {
      return new JsonPrimitive("UserSerializerDeserializer");
    }
    @Override
    public User deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
      return new User("UserSerializerDeserializer");
    }
  }

  public void testJsonSerializerDeserializerBasedJsonAdapterOnClass() {
    Gson gson = new Gson();
    String json = gson.toJson(new Computer2(new User2("Inderjeet Singh")));
    assertEquals("{\"user\":\"UserSerializerDeserializer2\"}", json);
    Computer2 computer = gson.fromJson("{'user':'Inderjeet Singh'}", Computer2.class);
    assertEquals("UserSerializerDeserializer2", computer.user.name);
  }

  private static final class Computer2 {
    final User2 user;
    Computer2(User2 user) {
      this.user = user;
    }
  }

  @JsonAdapter(UserSerializerDeserializer2.class)
  private static final class User2 {
    public final String name;
    private User2(String name) {
      this.name = name;
    }
  }

  private static final class UserSerializerDeserializer2 implements JsonSerializer<User2>, JsonDeserializer<User2> {
    @Override
    public JsonElement serialize(User2 src, Type typeOfSrc, JsonSerializationContext context) {
      return new JsonPrimitive("UserSerializerDeserializer2");
    }
    @Override
    public User2 deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
      return new User2("UserSerializerDeserializer2");
    }
  }

  public void testDifferentJsonAdaptersForGenericFieldsOfSameRawType() {
    Container c = new Container("Foo", 10);
    Gson gson = new Gson();
    String json = gson.toJson(c);
    assertTrue(json.contains("\"a\":\"BaseStringAdapter\""));
    assertTrue(json.contains("\"b\":\"BaseIntegerAdapter\""));
  }

  private static final class Container {
    @JsonAdapter(BaseStringAdapter.class) Base<String> a;
    @JsonAdapter(BaseIntegerAdapter.class) Base<Integer> b;
    Container(String a, int b) {
      this.a = new Base<>(a);
      this.b = new Base<>(b);
    }
  }

  private static final class Base<T> {
    @SuppressWarnings("unused")
    T value;
    Base(T value) {
      this.value = value;
    }
  }

  private static final class BaseStringAdapter implements JsonSerializer<Base<String>> {
    @Override public JsonElement serialize(Base<String> src, Type typeOfSrc, JsonSerializationContext context) {
      return new JsonPrimitive("BaseStringAdapter");
    }
  }

  private static final class BaseIntegerAdapter implements JsonSerializer<Base<Integer>> {
    @Override public JsonElement serialize(Base<Integer> src, Type typeOfSrc, JsonSerializationContext context) {
      return new JsonPrimitive("BaseIntegerAdapter");
    }
  }

  public void testJsonAdapterNullSafe() {
    Gson gson = new Gson();
    String json = gson.toJson(new Computer3(null, null));
    assertEquals("{\"user1\":\"UserSerializerDeserializer\"}", json);
    Computer3 computer3 = gson.fromJson("{\"user1\":null, \"user2\":null}", Computer3.class);
    assertEquals("UserSerializerDeserializer", computer3.user1.name);
    assertNull(computer3.user2);
  }

  private static final class Computer3 {
    @JsonAdapter(value = UserSerializerDeserializer.class, nullSafe = false) final User user1;
    @JsonAdapter(value = UserSerializerDeserializer.class) final User user2;
    Computer3(User user1, User user2) {
      this.user1 = user1;
      this.user2 = user2;
    }
  }
}