【Android】【框架】【JSON】【Gson】

整体架构

【Android】【框架】【JSON】【Gson】_第1张图片
image.png

核心在于TypeAdapter,它定义了类型和字节之间的相互转换

TypeAdapter

【Android】【框架】【JSON】【Gson】_第2张图片
image.png
【Android】【框架】【JSON】【Gson】_第3张图片
image.png
public abstract class TypeAdapter {

  public abstract void write(JsonWriter out, T value) throws IOException;

  public abstract T read(JsonReader in) throws IOException;

  ...
}

看Json代码示例

{
            "test1": "wx9fdb8ble7ce3c68f",
            "test2": 123456789,
            "testData1": {   
                "testdatason1": "97895455"
                "testdatason2":3,
             }
}

解析Json,当解析到value的时候,会去获取对应的TypeAdapter,具体是到一个List按序匹配,如果没有名字,采用反射

【Android】【框架】【JSON】【Gson】_第4张图片
image.png
  Gson(final Excluder excluder, final FieldNamingStrategy fieldNamingStrategy,
      final Map> instanceCreators, boolean serializeNulls,
      boolean complexMapKeySerialization, boolean generateNonExecutableGson, boolean htmlSafe,
      boolean prettyPrinting, boolean lenient, boolean serializeSpecialFloatingPointValues,
      LongSerializationPolicy longSerializationPolicy, String datePattern, int dateStyle,
      int timeStyle, List builderFactories,
      List builderHierarchyFactories,
      List factoriesToBeAdded) {
    ...
    ...
    ...

    List factories = new ArrayList();

    // built-in type adapters that cannot be overridden
    factories.add(TypeAdapters.JSON_ELEMENT_FACTORY);
    factories.add(ObjectTypeAdapter.FACTORY);

    // the excluder must precede all adapters that handle user-defined types
    factories.add(excluder);

    // users type adapters
    factories.addAll(factoriesToBeAdded);

    // type adapters for basic platform types
    factories.add(TypeAdapters.STRING_FACTORY);
    factories.add(TypeAdapters.INTEGER_FACTORY);
    factories.add(TypeAdapters.BOOLEAN_FACTORY);
    factories.add(TypeAdapters.BYTE_FACTORY);
    factories.add(TypeAdapters.SHORT_FACTORY);
    TypeAdapter longAdapter = longAdapter(longSerializationPolicy);
    factories.add(TypeAdapters.newFactory(long.class, Long.class, longAdapter));
    factories.add(TypeAdapters.newFactory(double.class, Double.class,
            doubleAdapter(serializeSpecialFloatingPointValues)));
    factories.add(TypeAdapters.newFactory(float.class, Float.class,
            floatAdapter(serializeSpecialFloatingPointValues)));
    factories.add(TypeAdapters.NUMBER_FACTORY);
    factories.add(TypeAdapters.ATOMIC_INTEGER_FACTORY);
    factories.add(TypeAdapters.ATOMIC_BOOLEAN_FACTORY);
    factories.add(TypeAdapters.newFactory(AtomicLong.class, atomicLongAdapter(longAdapter)));
    factories.add(TypeAdapters.newFactory(AtomicLongArray.class, atomicLongArrayAdapter(longAdapter)));
    factories.add(TypeAdapters.ATOMIC_INTEGER_ARRAY_FACTORY);
    factories.add(TypeAdapters.CHARACTER_FACTORY);
    factories.add(TypeAdapters.STRING_BUILDER_FACTORY);
    factories.add(TypeAdapters.STRING_BUFFER_FACTORY);
    factories.add(TypeAdapters.newFactory(BigDecimal.class, TypeAdapters.BIG_DECIMAL));
    factories.add(TypeAdapters.newFactory(BigInteger.class, TypeAdapters.BIG_INTEGER));
    factories.add(TypeAdapters.URL_FACTORY);
    factories.add(TypeAdapters.URI_FACTORY);
    factories.add(TypeAdapters.UUID_FACTORY);
    factories.add(TypeAdapters.CURRENCY_FACTORY);
    factories.add(TypeAdapters.LOCALE_FACTORY);
    factories.add(TypeAdapters.INET_ADDRESS_FACTORY);
    factories.add(TypeAdapters.BIT_SET_FACTORY);
    factories.add(DateTypeAdapter.FACTORY);
    factories.add(TypeAdapters.CALENDAR_FACTORY);
    factories.add(TimeTypeAdapter.FACTORY);
    factories.add(SqlDateTypeAdapter.FACTORY);
    factories.add(TypeAdapters.TIMESTAMP_FACTORY);
    factories.add(ArrayTypeAdapter.FACTORY);
    factories.add(TypeAdapters.CLASS_FACTORY);

    // type adapters for composite and user-defined types
    factories.add(new CollectionTypeAdapterFactory(constructorConstructor));
    factories.add(new MapTypeAdapterFactory(constructorConstructor, complexMapKeySerialization));
    this.jsonAdapterFactory = new JsonAdapterAnnotationTypeAdapterFactory(constructorConstructor);
    factories.add(jsonAdapterFactory);
    factories.add(TypeAdapters.ENUM_FACTORY);
    // 注意,ReflectiveTypeAdapterFactor是要最后添加的
    factories.add(new ReflectiveTypeAdapterFactory(
        constructorConstructor, fieldNamingStrategy, excluder, jsonAdapterFactory));

    this.factories = Collections.unmodifiableList(factories);
  }

对于自定义对象其实就是通过ReflectiveTypeAdapterFactory来做的
内部其实还是调用的基本数据类型TypeAdapter

总结

1、整体分为2部分,一个是TypeAdapter,定义写入写出;一个是容器,用于存放JSON
2、对于自定义对象解析,需要反射获取对象字段,然后挑选具体的TypeAdapter
3、对于自定义对象循环嵌套的情况,使用FutureTypeAdapter,单个对象只需解析一次
4、对于多线程情况,用ThreadLocal支持

后记

有什么写得错误、让人费解或遗漏的地方,希望可以不吝赐教,我会马上更改

学习自

https://mp.weixin.qq.com/s/j-klYVIjJxeWWIGfdeBsEw
https://blog.csdn.net/chunqiuwei/article/details/49401733

你可能感兴趣的:(【Android】【框架】【JSON】【Gson】)