Maps to JSON Objects. This requires that map keys can be serialized as strings; this
is insufficient for some key types. For example, consider a map whose keys are points on a grid.
The default JSON form encodes reasonably:
Map<Point, String> original = new LinkedHashMap<>();
original.put(new Point(5, 6), "a");
original.put(new Point(8, 8), "b");
System.out.println(gson.toJson(original, type));
The above code prints this JSON object:
{
"(5,6)": "a",
"(8,8)": "b"
}
But GSON is unable to deserialize this value because the JSON string name is just the toString() of the map key. Attempting to convert the above JSON to an object
fails with a parse exception:
com.google.gson.JsonParseException: Expecting object found: "(5,6)" at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler at com.google.gson.ObjectNavigator.navigateClassFields ...
Register this adapter when you are creating your GSON instance.
Gson gson = new GsonBuilder()
.registerTypeAdapter(Map.class, new MapAsArrayTypeAdapter())
.create();
This will change the structure of the JSON emitted by the code above. Now we get an array. In
this case the arrays elements are map entries:
[
[
{
"x": 5,
"y": 6
},
"a",
],
[
{
"x": 8,
"y": 8
},
"b"
]
]
This format will serialize and deserialize just fine as long as this adapter is registered.
| Modifier and Type | Class and Description |
|---|---|
| private class |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final boolean | |
| private final ConstructorConstructor |
| Access | Constructor and Description |
|---|---|
| public | MapTypeAdapterFactory(ConstructorConstructor constructorConstructor, boolean complexMapKeySerialization)
|
| Modifier and Type | Method and Description |
|---|---|
| public <T> TypeAdapter | create(Gson gson, TypeToken<T> typeToken)
Implements com. Returns a type adapter for |
| private TypeAdapter |
| complexMapKeySerialization | back to summary |
|---|---|
| pack-priv final boolean complexMapKeySerialization | |
| constructorConstructor | back to summary |
|---|---|
| private final ConstructorConstructor constructorConstructor | |
| MapTypeAdapterFactory | back to summary |
|---|---|
| public MapTypeAdapterFactory(ConstructorConstructor constructorConstructor, boolean complexMapKeySerialization) | |
| create | back to summary |
|---|---|
| public <T> TypeAdapter Implements com. Doc from com. Returns a type adapter for
| |
| getKeyAdapter | back to summary |
|---|---|
| private TypeAdapter Returns a type adapter that writes the value as a string. | |
| Modifier and Type | Field and Description |
|---|---|
| private final ObjectConstructor | |
| private final TypeAdapter | |
| private final TypeAdapter |
| Access | Constructor and Description |
|---|---|
| pack-priv | Adapter(TypeAdapter<K> keyTypeAdapter, TypeAdapter<V> valueTypeAdapter, ObjectConstructor<? extends Map<K, V>> constructor)
|
| Modifier and Type | Method and Description |
|---|---|
| private String | |
| public Map | read(JsonReader in)
Implements abstract com. Reads one JSON value (an array, object, string, number, boolean or null) and converts it to a Java object. |
| public void | write(JsonWriter out, Map<K, V> map)
Implements abstract com. Writes one JSON value (an array, object, string, number, boolean or null) for |
| constructor | back to summary |
|---|---|
| private final ObjectConstructor<? extends Map<K, V>> constructor | |
| keyTypeAdapter | back to summary |
|---|---|
| private final TypeAdapter<K> keyTypeAdapter | |
| valueTypeAdapter | back to summary |
|---|---|
| private final TypeAdapter<V> valueTypeAdapter | |
| Adapter | back to summary |
|---|---|
| pack-priv Adapter(TypeAdapter<K> keyTypeAdapter, TypeAdapter<V> valueTypeAdapter, ObjectConstructor<? extends Map<K, V>> constructor) | |
| keyToString | back to summary |
|---|---|
| private String keyToString(JsonElement keyElement) | |
| read | back to summary |
|---|---|
| public Map Implements abstract com. Doc from com. Reads one JSON value (an array, object, string, number, boolean or null) and converts it to a Java object. Returns the converted object.
| |
| write | back to summary |
|---|---|
| public void write(JsonWriter out, Map<K, V> map) throws IOException Implements abstract com. Doc from com. Writes one JSON value (an array, object, string, number, boolean or null) for
| |