This commit is contained in:
Phillip Webb
2016-05-16 12:00:03 -07:00
parent 46134b58b8
commit 00fbb5c3d9
6 changed files with 48 additions and 31 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,8 +40,7 @@ public class GsonJsonParser implements JsonParser {
if (json != null) {
json = json.trim();
if (json.startsWith("{")) {
TypeToken<Map<String, Object>> type = new TypeToken<Map<String, Object>>() { };
return this.gson.fromJson(json, type.getType());
return this.gson.fromJson(json, new MapTypeToken().getType());
}
}
throw new IllegalArgumentException("Cannot parse JSON");
@@ -52,11 +51,16 @@ public class GsonJsonParser implements JsonParser {
if (json != null) {
json = json.trim();
if (json.startsWith("[")) {
TypeToken<List<Object>> type = new TypeToken<List<Object>>() { };
TypeToken<List<Object>> type = new TypeToken<List<Object>>() {
};
return this.gson.fromJson(json, type.getType());
}
}
throw new IllegalArgumentException("Cannot parse JSON");
}
private static final class MapTypeToken extends TypeToken<Map<String, Object>> {
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,8 +33,7 @@ public class JacksonJsonParser implements JsonParser {
@Override
public Map<String, Object> parseMap(String json) {
try {
TypeReference<Map<String, Object>> type = new TypeReference<Map<String, Object>>() { };
return new ObjectMapper().readValue(json, type);
return new ObjectMapper().readValue(json, new MapTypeReference());
}
catch (Exception ex) {
throw new IllegalArgumentException("Cannot parse JSON", ex);
@@ -44,7 +43,8 @@ public class JacksonJsonParser implements JsonParser {
@Override
public List<Object> parseList(String json) {
try {
TypeReference<List<Object>> type = new TypeReference<List<Object>>() { };
TypeReference<List<Object>> type = new TypeReference<List<Object>>() {
};
return new ObjectMapper().readValue(json, type);
}
catch (Exception ex) {
@@ -52,4 +52,8 @@ public class JacksonJsonParser implements JsonParser {
}
}
private static class MapTypeReference extends TypeReference<Map<String, Object>> {
};
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,11 +17,11 @@
package org.springframework.boot.json;
/**
* Tests for {@link JsonParser}.
* Tests for {@link JacksonJsonParser}.
*
* @author Dave Syer
*/
public class JacksonParserTests extends AbstractJsonParserTests {
public class JacksonJsonParserTests extends AbstractJsonParserTests {
@Override
protected JsonParser getParser() {