| AndBase开发框架 1.6

- 首页
- 相关页面
- 命名空间
- 类
- 文件

- 类列表
- 类索引
- 类继承关系
- 类成员

全部 类 命名空间 文件 函数 变量 枚举值 页
类 | Public 成员函数 | 包函数 | Private 成员函数 | 静态 Private 成员函数 | Private 属性 | 所有成员列表
com.google.gson.internal.bind.MapTypeAdapterFactory类 参考
类 com.google.gson.internal.bind.MapTypeAdapterFactory 继承关系图:

## 类
class | Adapter< K, V >

## Public 成员函数
| MapTypeAdapterFactory (ConstructorConstructor constructorConstructor, boolean complexMapKeySerialization)

## 包函数
public< T > TypeAdapter< T > | create (Gson gson, TypeToken< T > typeToken)

## Private 成员函数
TypeAdapter<?> | getKeyAdapter (Gson context, Type keyType)

## 静态 Private 成员函数
static< T > JsonElement | toJsonTree (TypeAdapter< T > typeAdapter, T value)

## Private 属性
final ConstructorConstructor | constructorConstructor
final boolean | complexMapKeySerialization

## 详细描述

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

## 构造及析构函数说明

com.google.gson.internal.bind.MapTypeAdapterFactory.MapTypeAdapterFactory | ( | ConstructorConstructor | constructorConstructor,
|  | boolean | complexMapKeySerialization
| ) |  |
| inline

## 成员函数说明

public<T> TypeAdapter<T> com.google.gson.internal.bind.MapTypeAdapterFactory.create | ( | Gson | gson,
|  | TypeToken< T > | type
| ) |  |
| inline package

Returns a type adapter for

type

, or null if this factory doesn't support

type

.

实现了 com.google.gson.TypeAdapterFactory.

TypeAdapter<?> com.google.gson.internal.bind.MapTypeAdapterFactory.getKeyAdapter | ( | Gson | context,
|  | Type | keyType
| ) |  |
| inline private

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

static <T> JsonElement com.google.gson.internal.bind.MapTypeAdapterFactory.toJsonTree | ( | TypeAdapter< T > | typeAdapter,
|  | T | value
| ) |  |
| inline static private

## 类成员变量说明

final boolean com.google.gson.internal.bind.MapTypeAdapterFactory.complexMapKeySerialization
| private

final ConstructorConstructor com.google.gson.internal.bind.MapTypeAdapterFactory.constructorConstructor
| private

该类的文档由以下文件生成:

- src/com/google/gson/internal/bind/MapTypeAdapterFactory.java