File tree 2 files changed +16
-16
lines changed
spring-boot/src/main/java/org/springframework/boot/json
2 files changed +16
-16
lines changed Original file line number Diff line number Diff line change 33
33
*/
34
34
public class GsonJsonParser implements JsonParser {
35
35
36
+ private static final TypeToken <List <Object >> LIST_TYPE = new TypeToken <List <Object >>() {
37
+ };
38
+ private static final TypeToken <Map <String , Object >> MAP_TYPE = new TypeToken <Map <String , Object >>() {
39
+ };
40
+
36
41
private Gson gson = new GsonBuilder ().create ();
37
42
38
43
@ Override
39
44
public Map <String , Object > parseMap (String json ) {
40
45
if (json != null ) {
41
46
json = json .trim ();
42
47
if (json .startsWith ("{" )) {
43
- return this .gson .fromJson (json , new MapTypeToken () .getType ());
48
+ return this .gson .fromJson (json , MAP_TYPE .getType ());
44
49
}
45
50
}
46
51
throw new IllegalArgumentException ("Cannot parse JSON" );
@@ -51,16 +56,10 @@ public List<Object> parseList(String json) {
51
56
if (json != null ) {
52
57
json = json .trim ();
53
58
if (json .startsWith ("[" )) {
54
- TypeToken <List <Object >> type = new TypeToken <List <Object >>() {
55
- };
56
- return this .gson .fromJson (json , type .getType ());
59
+ return this .gson .fromJson (json , LIST_TYPE .getType ());
57
60
}
58
61
}
59
62
throw new IllegalArgumentException ("Cannot parse JSON" );
60
63
}
61
64
62
- private static final class MapTypeToken extends TypeToken <Map <String , Object >> {
63
-
64
- }
65
-
66
65
}
Original file line number Diff line number Diff line change 30
30
*/
31
31
public class JacksonJsonParser implements JsonParser {
32
32
33
+ private static final TypeReference <List <Object >> LIST_TYPE = new TypeReference <List <Object >>() {
34
+ };
35
+ private static final TypeReference <Map <String , Object >> MAP_TYPE = new TypeReference <Map <String , Object >>() {
36
+ };
37
+
38
+ private final ObjectMapper objectMapper = new ObjectMapper ();
39
+
33
40
@ Override
34
41
public Map <String , Object > parseMap (String json ) {
35
42
try {
36
- return new ObjectMapper (). readValue (json , new MapTypeReference () );
43
+ return this . objectMapper . readValue (json , MAP_TYPE );
37
44
}
38
45
catch (Exception ex ) {
39
46
throw new IllegalArgumentException ("Cannot parse JSON" , ex );
@@ -43,17 +50,11 @@ public Map<String, Object> parseMap(String json) {
43
50
@ Override
44
51
public List <Object > parseList (String json ) {
45
52
try {
46
- TypeReference <List <Object >> type = new TypeReference <List <Object >>() {
47
- };
48
- return new ObjectMapper ().readValue (json , type );
53
+ return this .objectMapper .readValue (json , LIST_TYPE );
49
54
}
50
55
catch (Exception ex ) {
51
56
throw new IllegalArgumentException ("Cannot parse JSON" , ex );
52
57
}
53
58
}
54
59
55
- private static class MapTypeReference extends TypeReference <Map <String , Object >> {
56
-
57
- };
58
-
59
60
}
You can’t perform that action at this time.
0 commit comments