回答編集履歴
1
クラス名を質問側に合わせる、他細かいミス
test
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
```lang-java
|
6
6
|
|
7
|
-
@Data public class
|
7
|
+
@Data public class Artistmatches {
|
8
8
|
|
9
9
|
|
10
10
|
|
@@ -18,17 +18,17 @@
|
|
18
18
|
|
19
19
|
```lang-java
|
20
20
|
|
21
|
-
public class
|
21
|
+
public class ArtistmatchesDeserializer implements JsonDeserializer<Artistmatches> {
|
22
22
|
|
23
23
|
|
24
24
|
|
25
|
-
@Override public
|
25
|
+
@Override public Artistmatches deserialize(JsonElement json, Type typeOfT,
|
26
26
|
|
27
27
|
JsonDeserializationContext context) throws JsonParseException {
|
28
28
|
|
29
29
|
|
30
30
|
|
31
|
-
|
31
|
+
Artistmatches artistmatches = new Artistmatches();
|
32
32
|
|
33
33
|
|
34
34
|
|
@@ -38,25 +38,33 @@
|
|
38
38
|
|
39
39
|
JsonElement artistElement = obj.get("artist");
|
40
40
|
|
41
|
+
List<Artist> artists;
|
42
|
+
|
41
43
|
if (artistElement.isJsonObject()) {
|
42
44
|
|
43
|
-
//
|
45
|
+
// objectならデシリアライズ後に要素数1のリストにする
|
44
46
|
|
45
47
|
Artist artist = context.deserialize(artistElement, Artist.class);
|
46
48
|
|
47
|
-
|
49
|
+
artists = Lists.newArrayList(artist);
|
48
50
|
|
49
51
|
} else if (artistElement.isJsonArray()) {
|
50
52
|
|
51
|
-
//
|
53
|
+
// arrayなら普通にデシリアライズ
|
52
54
|
|
53
55
|
Type type = new TypeToken<ArrayList<Artist>>() {
|
54
56
|
|
55
57
|
}.getType();
|
56
58
|
|
57
|
-
|
59
|
+
artists = (ArrayList<Artist>) context.deserialize(artistElement, type);
|
60
|
+
|
61
|
+
} else {
|
62
|
+
|
63
|
+
artists = new ArrayList<>();// or null 好きなように
|
58
64
|
|
59
65
|
}
|
66
|
+
|
67
|
+
artistmatches.setArtists(artists);
|
60
68
|
|
61
69
|
|
62
70
|
|
@@ -64,7 +72,7 @@
|
|
64
72
|
|
65
73
|
|
66
74
|
|
67
|
-
return s
|
75
|
+
return artistmatches;
|
68
76
|
|
69
77
|
}
|
70
78
|
|
@@ -76,9 +84,13 @@
|
|
76
84
|
|
77
85
|
```lang-java
|
78
86
|
|
79
|
-
//
|
87
|
+
// Deserializerを登録
|
80
88
|
|
89
|
+
Gson gson = new GsonBuilder()
|
90
|
+
|
81
|
-
|
91
|
+
.registerTypeAdapter(Artistmatches.class, new ArtistmatchesDeserializer())
|
92
|
+
|
93
|
+
.create();
|
82
94
|
|
83
95
|
```
|
84
96
|
|