File tree Expand file tree Collapse file tree 2 files changed +10
-8
lines changed
spring-boot/src/main/java/org/springframework/boot/json Expand file tree Collapse file tree 2 files changed +10
-8
lines changed Original file line number Diff line number Diff line change 21
21
22
22
import com .google .gson .Gson ;
23
23
import com .google .gson .GsonBuilder ;
24
+ import com .google .gson .reflect .TypeToken ;
24
25
25
26
/**
26
27
* Thin wrapper to adapt {@link Gson} to a {@link JsonParser}.
@@ -35,24 +36,24 @@ public class GsonJsonParser implements JsonParser {
35
36
private Gson gson = new GsonBuilder ().create ();
36
37
37
38
@ Override
38
- @ SuppressWarnings ("unchecked" )
39
39
public Map <String , Object > parseMap (String json ) {
40
40
if (json != null ) {
41
41
json = json .trim ();
42
42
if (json .startsWith ("{" )) {
43
- return this .gson .fromJson (json , Map .class );
43
+ TypeToken <Map <String , Object >> type = new TypeToken <Map <String , Object >>() { };
44
+ return this .gson .fromJson (json , type .getType ());
44
45
}
45
46
}
46
47
throw new IllegalArgumentException ("Cannot parse JSON" );
47
48
}
48
49
49
50
@ Override
50
- @ SuppressWarnings ("unchecked" )
51
51
public List <Object > parseList (String json ) {
52
52
if (json != null ) {
53
53
json = json .trim ();
54
54
if (json .startsWith ("[" )) {
55
- return this .gson .fromJson (json , List .class );
55
+ TypeToken <List <Object >> type = new TypeToken <List <Object >>() { };
56
+ return this .gson .fromJson (json , type .getType ());
56
57
}
57
58
}
58
59
throw new IllegalArgumentException ("Cannot parse JSON" );
Original file line number Diff line number Diff line change 19
19
import java .util .List ;
20
20
import java .util .Map ;
21
21
22
+ import com .fasterxml .jackson .core .type .TypeReference ;
22
23
import com .fasterxml .jackson .databind .ObjectMapper ;
23
24
24
25
/**
30
31
public class JacksonJsonParser implements JsonParser {
31
32
32
33
@ Override
33
- @ SuppressWarnings ("unchecked" )
34
34
public Map <String , Object > parseMap (String json ) {
35
35
try {
36
- return new ObjectMapper ().readValue (json , Map .class );
36
+ TypeReference <Map <String , Object >> type = new TypeReference <Map <String , Object >>() { };
37
+ return new ObjectMapper ().readValue (json , type );
37
38
}
38
39
catch (Exception ex ) {
39
40
throw new IllegalArgumentException ("Cannot parse JSON" , ex );
40
41
}
41
42
}
42
43
43
44
@ Override
44
- @ SuppressWarnings ("unchecked" )
45
45
public List <Object > parseList (String json ) {
46
46
try {
47
- return new ObjectMapper ().readValue (json , List .class );
47
+ TypeReference <List <Object >> type = new TypeReference <List <Object >>() { };
48
+ return new ObjectMapper ().readValue (json , type );
48
49
}
49
50
catch (Exception ex ) {
50
51
throw new IllegalArgumentException ("Cannot parse JSON" , ex );
You can’t perform that action at this time.
0 commit comments