Reuse objects in JsonParser implementations
Closes gh-6188
This commit is contained in:
parent
e383752150
commit
25f37da466
|
|
@ -33,6 +33,11 @@ import com.google.gson.reflect.TypeToken;
|
|||
*/
|
||||
public class GsonJsonParser implements JsonParser {
|
||||
|
||||
private static final TypeToken<List<Object>> LIST_TYPE = new TypeToken<List<Object>>() {
|
||||
};
|
||||
private static final TypeToken<Map<String, Object>> MAP_TYPE = new TypeToken<Map<String, Object>>() {
|
||||
};
|
||||
|
||||
private Gson gson = new GsonBuilder().create();
|
||||
|
||||
@Override
|
||||
|
|
@ -40,7 +45,7 @@ public class GsonJsonParser implements JsonParser {
|
|||
if (json != null) {
|
||||
json = json.trim();
|
||||
if (json.startsWith("{")) {
|
||||
return this.gson.fromJson(json, new MapTypeToken().getType());
|
||||
return this.gson.fromJson(json, MAP_TYPE.getType());
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Cannot parse JSON");
|
||||
|
|
@ -51,16 +56,10 @@ public class GsonJsonParser implements JsonParser {
|
|||
if (json != null) {
|
||||
json = json.trim();
|
||||
if (json.startsWith("[")) {
|
||||
TypeToken<List<Object>> type = new TypeToken<List<Object>>() {
|
||||
};
|
||||
return this.gson.fromJson(json, type.getType());
|
||||
return this.gson.fromJson(json, LIST_TYPE.getType());
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Cannot parse JSON");
|
||||
}
|
||||
|
||||
private static final class MapTypeToken extends TypeToken<Map<String, Object>> {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,10 +30,17 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||
*/
|
||||
public class JacksonJsonParser implements JsonParser {
|
||||
|
||||
private static final TypeReference<List<Object>> LIST_TYPE = new TypeReference<List<Object>>() {
|
||||
};
|
||||
private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<Map<String, Object>>() {
|
||||
};
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public Map<String, Object> parseMap(String json) {
|
||||
try {
|
||||
return new ObjectMapper().readValue(json, new MapTypeReference());
|
||||
return this.objectMapper.readValue(json, MAP_TYPE);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalArgumentException("Cannot parse JSON", ex);
|
||||
|
|
@ -43,17 +50,11 @@ public class JacksonJsonParser implements JsonParser {
|
|||
@Override
|
||||
public List<Object> parseList(String json) {
|
||||
try {
|
||||
TypeReference<List<Object>> type = new TypeReference<List<Object>>() {
|
||||
};
|
||||
return new ObjectMapper().readValue(json, type);
|
||||
return this.objectMapper.readValue(json, LIST_TYPE);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalArgumentException("Cannot parse JSON", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static class MapTypeReference extends TypeReference<Map<String, Object>> {
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue