- java.lang.Object
-
- com.google.gson.internal.bind.MapTypeAdapterFactory
-
- All Implemented Interfaces:
TypeAdapterFactory
public final class MapTypeAdapterFactory extends java.lang.Object implements TypeAdapterFactory
Adapts maps to either JSON objects or JSON arrays.Maps as JSON objects
For primitive keys or when complex map key serialization is not enabled, this converts JavaMaps
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));
{ "(5,6)": "a", "(8,8)": "b" }
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 ...
Maps as JSON arrays
An alternative approach taken by this type adapter when it is required and complex map key serialization is enabled is to encode maps as arrays of map entries. Each map entry is a two element array containing a key and a value. This approach is more flexible because any type can be used as the map's key; not just strings. But it's also less portable because the receiver of such JSON must be aware of the map entry convention.Register this adapter when you are creating your GSON instance.
Gson gson = new GsonBuilder() .registerTypeAdapter(Map.class, new MapAsArrayTypeAdapter()) .create();
[ [ { "x": 5, "y": 6 }, "a", ], [ { "x": 8, "y": 8 }, "b" ] ]
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
MapTypeAdapterFactory.Adapter<K,V>
-
Field Summary
Fields Modifier and Type Field Description (package private) boolean
complexMapKeySerialization
private ConstructorConstructor
constructorConstructor
-
Constructor Summary
Constructors Constructor Description MapTypeAdapterFactory(ConstructorConstructor constructorConstructor, boolean complexMapKeySerialization)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> TypeAdapter<T>
create(Gson gson, TypeToken<T> typeToken)
Returns a type adapter fortype
, or null if this factory doesn't supporttype
.private TypeAdapter<?>
getKeyAdapter(Gson context, java.lang.reflect.Type keyType)
Returns a type adapter that writes the value as a string.
-
-
-
Field Detail
-
constructorConstructor
private final ConstructorConstructor constructorConstructor
-
complexMapKeySerialization
final boolean complexMapKeySerialization
-
-
Constructor Detail
-
MapTypeAdapterFactory
public MapTypeAdapterFactory(ConstructorConstructor constructorConstructor, boolean complexMapKeySerialization)
-
-
Method Detail
-
create
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken)
Description copied from interface:TypeAdapterFactory
Returns a type adapter fortype
, or null if this factory doesn't supporttype
.- Specified by:
create
in interfaceTypeAdapterFactory
-
getKeyAdapter
private TypeAdapter<?> getKeyAdapter(Gson context, java.lang.reflect.Type keyType)
Returns a type adapter that writes the value as a string.
-
-