publicGson(){this(Excluder.DEFAULT,FieldNamingPolicy.IDENTITY,Collections.<Type,InstanceCreator<?>>emptyMap(),DEFAULT_SERIALIZE_NULLS,DEFAULT_COMPLEX_MAP_KEYS,DEFAULT_JSON_NON_EXECUTABLE,DEFAULT_ESCAPE_HTML,DEFAULT_PRETTY_PRINT,DEFAULT_LENIENT,DEFAULT_SPECIALIZE_FLOAT_VALUES,LongSerializationPolicy.DEFAULT,null,DateFormat.DEFAULT,DateFormat.DEFAULT,Collections.<TypeAdapterFactory>emptyList(),Collections.<TypeAdapterFactory>emptyList(),Collections.<TypeAdapterFactory>emptyList());}//实际的Gson(Excluderexcluder,FieldNamingStrategyfieldNamingStrategy,Map<Type,InstanceCreator<?>>instanceCreators,booleanserializeNulls,booleancomplexMapKeySerialization,booleangenerateNonExecutableGson,booleanhtmlSafe,booleanprettyPrinting,booleanlenient,booleanserializeSpecialFloatingPointValues,LongSerializationPolicylongSerializationPolicy,StringdatePattern,intdateStyle,inttimeStyle,List<TypeAdapterFactory>builderFactories,List<TypeAdapterFactory>builderHierarchyFactories,List<TypeAdapterFactory>factoriesToBeAdded){// 执行排查的字段和类型 this.excluder=excluder;// 字段命名机制。比如是 aName 后者是 a_namethis.fieldNamingStrategy=fieldNamingStrategy;// Gson默认是需要有不带参数的构造函数,但是碰到没有无参数构造函数且没有权限修改第三方类的情况下,可以通过InstanceCreator来解决this.instanceCreators=instanceCreators;// 通过构造器创建指定的类型this.constructorConstructor=newConstructorConstructor(instanceCreators);// 是否序列化nullthis.serializeNulls=serializeNulls;this.complexMapKeySerialization=complexMapKeySerialization;// 是否输出不可执行的json。如果为true,会在开头先输出一个")]}'\n"this.generateNonExecutableJson=generateNonExecutableGson;// 是否输出Html安全的JSON. 如果为true,在输出前会转义 < > & =this.htmlSafe=htmlSafe;this.prettyPrinting=prettyPrinting;// 是否使用宽松的模式。如果为true,则会忽略某些限制this.lenient=lenient;this.serializeSpecialFloatingPointValues=serializeSpecialFloatingPointValues;// 对long和Long类型的序列化策略this.longSerializationPolicy=longSerializationPolicy;// 日期的模式this.datePattern=datePattern;this.dateStyle=dateStyle;this.timeStyle=timeStyle;// 创建TypeAdapter的工厂类this.builderFactories=builderFactories;this.builderHierarchyFactories=builderHierarchyFactories;List<TypeAdapterFactory>factories=newArrayList<TypeAdapterFactory>();// built-in type adapters that cannot be overriddenfactories.add(TypeAdapters.JSON_ELEMENT_FACTORY);factories.add(ObjectTypeAdapter.FACTORY);// the excluder must precede all adapters that handle user-defined typesfactories.add(excluder);// users' type adaptersfactories.addAll(factoriesToBeAdded);// type adapters for basic platform typesfactories.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<Number>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 typesfactories.add(newCollectionTypeAdapterFactory(constructorConstructor));factories.add(newMapTypeAdapterFactory(constructorConstructor,complexMapKeySerialization));this.jsonAdapterFactory=newJsonAdapterAnnotationTypeAdapterFactory(constructorConstructor);factories.add(jsonAdapterFactory);factories.add(TypeAdapters.ENUM_FACTORY);//在通常情况下,我们自定义的类型会通过这个 ReflectiveTypeAdapter 来做转换。factories.add(newReflectiveTypeAdapterFactory(constructorConstructor,fieldNamingStrategy,excluder,jsonAdapterFactory));this.factories=Collections.unmodifiableList(factories);}
public<T>TypeAdapter<T>getAdapter(TypeToken<T>type){// 首先从本地内存获取TypeAdapter<?>cached=typeTokenCache.get(type==null?NULL_KEY_SURROGATE:type);if(cached!=null){return(TypeAdapter<T>)cached;}// 接着从ThreadLocal中获取Map<TypeToken<?>,FutureTypeAdapter<?>>threadCalls=calls.get();booleanrequiresThreadLocalCleanup=false;if(threadCalls==null){threadCalls=newHashMap<TypeToken<?>,FutureTypeAdapter<?>>();calls.set(threadCalls);requiresThreadLocalCleanup=true;}// the key and value type parameters always agreeFutureTypeAdapter<T>ongoingCall=(FutureTypeAdapter<T>)threadCalls.get(type);if(ongoingCall!=null){returnongoingCall;}try{FutureTypeAdapter<T>call=newFutureTypeAdapter<T>();threadCalls.put(type,call);// 循环左右的 TypeAdapterFactory 找到可以处理当前类型的第一个 Factoryfor(TypeAdapterFactoryfactory:factories){TypeAdapter<T>candidate=factory.create(this,type);if(candidate!=null){call.setDelegate(candidate);typeTokenCache.put(type,candidate);returncandidate;}}thrownewIllegalArgumentException("GSON ("+GsonBuildConfig.VERSION+") cannot handle "+type);}finally{threadCalls.remove(type);if(requiresThreadLocalCleanup){calls.remove();}}}