Skip to content

Commit 62a41ae

Browse files
committed
Merge pull request spring-projects#6188 from izeye:json
* pr/6188: Reuse objects in JsonParser implementations
2 parents e383752 + 25f37da commit 62a41ae

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,19 @@
3333
*/
3434
public class GsonJsonParser implements JsonParser {
3535

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+
3641
private Gson gson = new GsonBuilder().create();
3742

3843
@Override
3944
public Map<String, Object> parseMap(String json) {
4045
if (json != null) {
4146
json = json.trim();
4247
if (json.startsWith("{")) {
43-
return this.gson.fromJson(json, new MapTypeToken().getType());
48+
return this.gson.fromJson(json, MAP_TYPE.getType());
4449
}
4550
}
4651
throw new IllegalArgumentException("Cannot parse JSON");
@@ -51,16 +56,10 @@ public List<Object> parseList(String json) {
5156
if (json != null) {
5257
json = json.trim();
5358
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());
5760
}
5861
}
5962
throw new IllegalArgumentException("Cannot parse JSON");
6063
}
6164

62-
private static final class MapTypeToken extends TypeToken<Map<String, Object>> {
63-
64-
}
65-
6665
}

spring-boot/src/main/java/org/springframework/boot/json/JacksonJsonParser.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,17 @@
3030
*/
3131
public class JacksonJsonParser implements JsonParser {
3232

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+
3340
@Override
3441
public Map<String, Object> parseMap(String json) {
3542
try {
36-
return new ObjectMapper().readValue(json, new MapTypeReference());
43+
return this.objectMapper.readValue(json, MAP_TYPE);
3744
}
3845
catch (Exception ex) {
3946
throw new IllegalArgumentException("Cannot parse JSON", ex);
@@ -43,17 +50,11 @@ public Map<String, Object> parseMap(String json) {
4350
@Override
4451
public List<Object> parseList(String json) {
4552
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);
4954
}
5055
catch (Exception ex) {
5156
throw new IllegalArgumentException("Cannot parse JSON", ex);
5257
}
5358
}
5459

55-
private static class MapTypeReference extends TypeReference<Map<String, Object>> {
56-
57-
};
58-
5960
}

0 commit comments

Comments
 (0)