Mention in User Guide alternative for Gson versions without fromJson(..., TypeToken) (#2209)

This commit is contained in:
Marcono1234 2022-09-30 16:57:02 +02:00 committed by GitHub
parent 9c9cafcf9d
commit ea7ab7cd52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 0 deletions

View File

@ -226,6 +226,8 @@ String json = gson.toJson(ints); // ==> json is [1,2,3,4,5]
// Deserialization
TypeToken<Collection<Integer>> collectionType = new TypeToken<Collection<Integer>>(){};
// Note: For older Gson versions it is necessary to use `collectionType.getType()` as argument below,
// this is however not type-safe and care must be taken to specify the correct type for the local variable
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
// ==> ints2 is same as ints
```
@ -267,6 +269,8 @@ TypeToken<Map<String, String>> mapType = new TypeToken<Map<String, String>>(){};
String json = "{\"key\": \"value\"}";
// Deserialization
// Note: For older Gson versions it is necessary to use `mapType.getType()` as argument below,
// this is however not type-safe and care must be taken to specify the correct type for the local variable
Map<String, String> stringMap = gson.fromJson(json, mapType);
// ==> stringMap is {key=value}
```