/*
* Copyright (C) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.gson.internal.bind;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.internal.$Gson$Types;
import com.google.gson.internal.ConstructorConstructor;
import com.google.gson.internal.JsonReaderInternalAccess;
import com.google.gson.internal.ObjectConstructor;
import com.google.gson.internal.Streams;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.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
* {@link Map 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:
*
* {@code
* Map 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:
*
* {@code
* {
* "(5,6)": "a",
* "(8,8)": "b"
* }
* }
*
* But GSON is unable to deserialize this value because the JSON string name is just the {@link
* Object#toString() 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.
*
*
{@code
* 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:
*
* {@code
* [
* [
* {
* "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.
*/
public final class MapTypeAdapterFactory implements TypeAdapterFactory {
private final ConstructorConstructor constructorConstructor;
final boolean complexMapKeySerialization;
public MapTypeAdapterFactory(
ConstructorConstructor constructorConstructor, boolean complexMapKeySerialization) {
this.constructorConstructor = constructorConstructor;
this.complexMapKeySerialization = complexMapKeySerialization;
}
@Override
public TypeAdapter create(Gson gson, TypeToken typeToken) {
Type type = typeToken.getType();
Class super T> rawType = typeToken.getRawType();
if (!Map.class.isAssignableFrom(rawType)) {
return null;
}
Type[] keyAndValueTypes = $Gson$Types.getMapKeyAndValueTypes(type, rawType);
TypeAdapter> keyAdapter = getKeyAdapter(gson, keyAndValueTypes[0]);
TypeAdapter> valueAdapter = gson.getAdapter(TypeToken.get(keyAndValueTypes[1]));
ObjectConstructor constructor = constructorConstructor.get(typeToken);
@SuppressWarnings({"unchecked", "rawtypes"})
// we don't define a type parameter for the key or value types
TypeAdapter result =
new Adapter(
gson, keyAndValueTypes[0], keyAdapter, keyAndValueTypes[1], valueAdapter, constructor);
return result;
}
/** Returns a type adapter that writes the value as a string. */
private TypeAdapter> getKeyAdapter(Gson context, Type keyType) {
return (keyType == boolean.class || keyType == Boolean.class)
? TypeAdapters.BOOLEAN_AS_STRING
: context.getAdapter(TypeToken.get(keyType));
}
private final class Adapter extends TypeAdapter