Top Description Inners Fields Constructors Methods
com.google.gson.internal.bind

public final Class MapTypeAdapterFactory

extends Object
implements TypeAdapterFactory
Class Inheritance
All Implemented Interfaces
com.google.gson.TypeAdapterFactory
Imports
com.google.gson.Gson, .JsonElement, .JsonPrimitive, .JsonSyntaxException, .TypeAdapter, .TypeAdapterFactory, com.google.gson.internal.ConstructorConstructor, .GsonTypes, .JsonReaderInternalAccess, .ObjectConstructor, .Streams, com.google.gson.reflect.TypeToken, com.google.gson.stream.JsonReader, .JsonToken, .JsonWriter, java.io.IOException, java.lang.reflect.Type, java.util.ArrayList, .List, .Map

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 Java 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
  ...

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();
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.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
pack-priv final boolean
private final ConstructorConstructor

Constructor Summary

AccessConstructor and Description
public
MapTypeAdapterFactory(ConstructorConstructor constructorConstructor, boolean complexMapKeySerialization)

Method Summary

Modifier and TypeMethod and Description
public <T> TypeAdapter<T>
create(Gson gson, TypeToken<T> typeToken)

Implements com.google.gson.TypeAdapterFactory.create.

Returns a type adapter for type, or null if this factory doesn't support type.

private TypeAdapter<?>
getKeyAdapter(Gson context, Type keyType)

Returns a type adapter that writes the value as a string.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

complexMapKeySerializationback to summary
pack-priv final boolean complexMapKeySerialization
constructorConstructorback to summary
private final ConstructorConstructor constructorConstructor

Constructor Detail

MapTypeAdapterFactoryback to summary
public MapTypeAdapterFactory(ConstructorConstructor constructorConstructor, boolean complexMapKeySerialization)

Method Detail

createback to summary
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken)

Implements com.google.gson.TypeAdapterFactory.create.

Doc from com.google.gson.TypeAdapterFactory.create.

Returns a type adapter for type, or null if this factory doesn't support type.

Annotations
@Override
getKeyAdapterback to summary
private TypeAdapter<?> getKeyAdapter(Gson context, Type keyType)

Returns a type adapter that writes the value as a string.

com.google.gson.internal.bind back to summary

private final Class MapTypeAdapterFactory.Adapter<K, V>

extends TypeAdapter<Map<K, V>>
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final ObjectConstructor<? extends Map<K, V>>
private final TypeAdapter<K>
private final TypeAdapter<V>

Constructor Summary

AccessConstructor and Description
pack-priv
Adapter(TypeAdapter<K> keyTypeAdapter, TypeAdapter<V> valueTypeAdapter, ObjectConstructor<? extends Map<K, V>> constructor)

Method Summary

Modifier and TypeMethod and Description
private String
public Map<K, V>
read(JsonReader in)

Implements abstract com.google.gson.TypeAdapter.read.

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.google.gson.TypeAdapter.write.

Writes one JSON value (an array, object, string, number, boolean or null) for value.

Inherited from com.google.gson.TypeAdapter:
fromJsonfromJsonfromJsonTreenullSafetoJsontoJsontoJsonTree

Field Detail

constructorback to summary
private final ObjectConstructor<? extends Map<K, V>> constructor
keyTypeAdapterback to summary
private final TypeAdapter<K> keyTypeAdapter
valueTypeAdapterback to summary
private final TypeAdapter<V> valueTypeAdapter

Constructor Detail

Adapterback to summary
pack-priv Adapter(TypeAdapter<K> keyTypeAdapter, TypeAdapter<V> valueTypeAdapter, ObjectConstructor<? extends Map<K, V>> constructor)

Method Detail

keyToStringback to summary
private String keyToString(JsonElement keyElement)
readback to summary
public Map<K, V> read(JsonReader in) throws IOException

Implements abstract com.google.gson.TypeAdapter.read.

Doc from com.google.gson.TypeAdapter.read.

Reads one JSON value (an array, object, string, number, boolean or null) and converts it to a Java object. Returns the converted object.

Annotations
@Override
writeback to summary
public void write(JsonWriter out, Map<K, V> map) throws IOException

Implements abstract com.google.gson.TypeAdapter.write.

Doc from com.google.gson.TypeAdapter.write.

Writes one JSON value (an array, object, string, number, boolean or null) for value.

Annotations
@Override