-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathGenericClasses.java
66 lines (54 loc) · 1.35 KB
/
GenericClasses.java
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
package com.example;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
public class GenericClasses {
static class GenericClass<T> {
@SerializedName("t")
T t;
@Override
public String toString() {
return "{t=" + t + "}";
}
}
static class UsingGenericClass {
@SerializedName("g")
GenericClass<DummyClass> g;
@Override
public String toString() {
return "{g=" + g + "}";
}
}
static class GenericUsingGenericClass<T> {
@SerializedName("g")
GenericClass<T> g;
@Override
public String toString() {
return "{g=" + g + "}";
}
}
@JsonAdapter(DummyClass.Adapter.class)
static class DummyClass {
String s;
DummyClass(String s) {
this.s = s;
}
@Override
public String toString() {
return s;
}
static class Adapter extends TypeAdapter<DummyClass> {
@Override
public DummyClass read(JsonReader in) throws IOException {
return new DummyClass("read-" + in.nextInt());
}
@Override
public void write(JsonWriter out, DummyClass value) throws IOException {
throw new UnsupportedOperationException();
}
}
}
}