diff --git a/spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java b/spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java index 7ca22a9d0c..86a3cf5b5b 100644 --- a/spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java +++ b/spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java @@ -21,6 +21,7 @@ import java.util.Map; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; /** * Thin wrapper to adapt {@link Gson} to a {@link JsonParser}. @@ -35,24 +36,24 @@ public class GsonJsonParser implements JsonParser { private Gson gson = new GsonBuilder().create(); @Override - @SuppressWarnings("unchecked") public Map parseMap(String json) { if (json != null) { json = json.trim(); if (json.startsWith("{")) { - return this.gson.fromJson(json, Map.class); + TypeToken> type = new TypeToken>() { }; + return this.gson.fromJson(json, type.getType()); } } throw new IllegalArgumentException("Cannot parse JSON"); } @Override - @SuppressWarnings("unchecked") public List parseList(String json) { if (json != null) { json = json.trim(); if (json.startsWith("[")) { - return this.gson.fromJson(json, List.class); + TypeToken> type = new TypeToken>() { }; + return this.gson.fromJson(json, type.getType()); } } throw new IllegalArgumentException("Cannot parse JSON"); diff --git a/spring-boot/src/main/java/org/springframework/boot/json/JacksonJsonParser.java b/spring-boot/src/main/java/org/springframework/boot/json/JacksonJsonParser.java index df54a37fcb..0421979500 100644 --- a/spring-boot/src/main/java/org/springframework/boot/json/JacksonJsonParser.java +++ b/spring-boot/src/main/java/org/springframework/boot/json/JacksonJsonParser.java @@ -19,6 +19,7 @@ package org.springframework.boot.json; import java.util.List; import java.util.Map; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; /** @@ -30,10 +31,10 @@ import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonJsonParser implements JsonParser { @Override - @SuppressWarnings("unchecked") public Map parseMap(String json) { try { - return new ObjectMapper().readValue(json, Map.class); + TypeReference> type = new TypeReference>() { }; + return new ObjectMapper().readValue(json, type); } catch (Exception ex) { throw new IllegalArgumentException("Cannot parse JSON", ex); @@ -41,10 +42,10 @@ public class JacksonJsonParser implements JsonParser { } @Override - @SuppressWarnings("unchecked") public List parseList(String json) { try { - return new ObjectMapper().readValue(json, List.class); + TypeReference> type = new TypeReference>() { }; + return new ObjectMapper().readValue(json, type); } catch (Exception ex) { throw new IllegalArgumentException("Cannot parse JSON", ex);