1616
1717package com .google .gson ;
1818
19+ import com .google .gson .internal .Excluder ;
20+ import com .google .gson .internal .bind .ArrayTypeAdapter ;
21+ import com .google .gson .internal .bind .CollectionTypeAdapterFactory ;
22+ import com .google .gson .internal .bind .DefaultDateTypeAdapter ;
23+ import com .google .gson .internal .bind .JsonAdapterAnnotationTypeAdapterFactory ;
24+ import com .google .gson .internal .bind .MapTypeAdapterFactory ;
25+ import com .google .gson .internal .bind .NumberTypeAdapter ;
26+ import com .google .gson .internal .bind .ObjectTypeAdapter ;
27+ import com .google .gson .internal .bind .ReflectiveTypeAdapterFactory ;
1928import com .google .gson .internal .bind .TypeAdapters ;
29+ import com .google .gson .internal .sql .SqlTypesSupport ;
2030import com .google .gson .stream .JsonReader ;
2131import com .google .gson .stream .JsonToken ;
2232import com .google .gson .stream .JsonWriter ;
2333import java .io .IOException ;
34+ import java .text .DateFormat ;
35+ import java .util .ArrayList ;
2436import java .util .Arrays ;
2537import java .util .Collection ;
2638import java .util .Collections ;
3143class BuilderHelper {
3244 private BuilderHelper () {}
3345
46+ static final List <TypeAdapterFactory > DEFAULT_TYPE_ADAPTER_FACTORIES ;
47+
48+ static final int EXPECTED_FACTORIES_SIZE = 45 ;
49+
50+ static {
51+ List <TypeAdapterFactory > factories = new ArrayList <>(EXPECTED_FACTORIES_SIZE );
52+
53+ // built-in type adapters that cannot be overridden
54+ factories .add (TypeAdapters .JSON_ELEMENT_FACTORY );
55+ factories .add (ObjectTypeAdapter .getFactory (Gson .DEFAULT_OBJECT_TO_NUMBER_STRATEGY ));
56+
57+ // the excluder must precede all adapters that handle user-defined types
58+ factories .add (Excluder .DEFAULT );
59+
60+ // dates
61+ addTypeAdaptersForDate (
62+ Gson .DEFAULT_DATE_PATTERN , DateFormat .DEFAULT , DateFormat .DEFAULT , factories );
63+
64+ // type adapters for basic platform types
65+ factories .add (TypeAdapters .STRING_FACTORY );
66+ factories .add (TypeAdapters .INTEGER_FACTORY );
67+ factories .add (TypeAdapters .BOOLEAN_FACTORY );
68+ factories .add (TypeAdapters .BYTE_FACTORY );
69+ factories .add (TypeAdapters .SHORT_FACTORY );
70+ TypeAdapter <Number > longAdapter = LongSerializationPolicy .DEFAULT .typeAdapter ();
71+ factories .add (TypeAdapters .newFactory (long .class , Long .class , longAdapter ));
72+ factories .add (
73+ TypeAdapters .newFactory (
74+ double .class , Double .class , doubleAdapter (Gson .DEFAULT_SPECIALIZE_FLOAT_VALUES )));
75+ factories .add (
76+ TypeAdapters .newFactory (
77+ float .class , Float .class , floatAdapter (Gson .DEFAULT_SPECIALIZE_FLOAT_VALUES )));
78+ factories .add (NumberTypeAdapter .getFactory (Gson .DEFAULT_NUMBER_TO_NUMBER_STRATEGY ));
79+ factories .add (TypeAdapters .ATOMIC_INTEGER_FACTORY );
80+ factories .add (TypeAdapters .ATOMIC_BOOLEAN_FACTORY );
81+ factories .add (TypeAdapters .newFactory (AtomicLong .class , atomicLongAdapter (longAdapter )));
82+ factories .add (
83+ TypeAdapters .newFactory (AtomicLongArray .class , atomicLongArrayAdapter (longAdapter )));
84+ factories .add (TypeAdapters .ATOMIC_INTEGER_ARRAY_FACTORY );
85+ factories .add (TypeAdapters .CHARACTER_FACTORY );
86+ factories .add (TypeAdapters .STRING_BUILDER_FACTORY );
87+ factories .add (TypeAdapters .STRING_BUFFER_FACTORY );
88+ factories .add (TypeAdapters .BIG_DECIMAL_FACTORY );
89+ factories .add (TypeAdapters .BIG_INTEGER_FACTORY );
90+ // Add adapter for LazilyParsedNumber because user can obtain it from Gson and then try to
91+ // serialize it again
92+ factories .add (TypeAdapters .LAZILY_PARSED_NUMBER_FACTORY );
93+ factories .add (TypeAdapters .URL_FACTORY );
94+ factories .add (TypeAdapters .URI_FACTORY );
95+ factories .add (TypeAdapters .UUID_FACTORY );
96+ factories .add (TypeAdapters .CURRENCY_FACTORY );
97+ factories .add (TypeAdapters .LOCALE_FACTORY );
98+ factories .add (TypeAdapters .INET_ADDRESS_FACTORY );
99+ factories .add (TypeAdapters .BIT_SET_FACTORY );
100+ factories .add (DefaultDateTypeAdapter .DEFAULT_STYLE_FACTORY );
101+ factories .add (TypeAdapters .CALENDAR_FACTORY );
102+ factories .addAll (SqlTypesSupport .SQL_TYPE_FACTORIES );
103+ factories .add (ArrayTypeAdapter .FACTORY );
104+ factories .add (TypeAdapters .CLASS_FACTORY );
105+
106+ // type adapters for composite and user-defined types
107+ factories .add (CollectionTypeAdapterFactory .DEFAULT );
108+ factories .add (MapTypeAdapterFactory .DEFAULT );
109+ factories .add (JsonAdapterAnnotationTypeAdapterFactory .DEFAULT );
110+ factories .add (TypeAdapters .ENUM_FACTORY );
111+ factories .add (ReflectiveTypeAdapterFactory .DEFAULT );
112+
113+ DEFAULT_TYPE_ADAPTER_FACTORIES = immutableList (factories );
114+ }
115+
34116 @ SuppressWarnings ("unchecked" )
35- static <E > List <E > unmodifiableList (Collection <E > collection ) {
117+ static <E > List <E > immutableList (Collection <E > collection ) {
36118 if (collection .isEmpty ()) {
37119 return Collections .emptyList ();
38120 }
@@ -49,7 +131,7 @@ static <E> List<E> unmodifiableList(Collection<E> collection) {
49131 : Arrays .asList (collection .toArray ()));
50132 }
51133
52- private static final TypeAdapter <Number > DOUBLE_WITH_CHECK =
134+ private static final TypeAdapter <Number > DOUBLE_STRICT =
53135 new TypeAdapter <Number >() {
54136 @ Override
55137 public Double read (JsonReader in ) throws IOException {
@@ -72,7 +154,7 @@ public void write(JsonWriter out, Number value) throws IOException {
72154 }
73155 };
74156
75- private static final TypeAdapter <Number > FLOAT_WITH_CHECK =
157+ private static final TypeAdapter <Number > FLOAT_STRICT =
76158 new TypeAdapter <Number >() {
77159 @ Override
78160 public Float read (JsonReader in ) throws IOException {
@@ -99,11 +181,11 @@ public void write(JsonWriter out, Number value) throws IOException {
99181 };
100182
101183 static TypeAdapter <Number > doubleAdapter (boolean serializeSpecialFloatingPointValues ) {
102- return serializeSpecialFloatingPointValues ? TypeAdapters .DOUBLE : DOUBLE_WITH_CHECK ;
184+ return serializeSpecialFloatingPointValues ? TypeAdapters .DOUBLE : DOUBLE_STRICT ;
103185 }
104186
105187 static TypeAdapter <Number > floatAdapter (boolean serializeSpecialFloatingPointValues ) {
106- return serializeSpecialFloatingPointValues ? TypeAdapters .FLOAT : FLOAT_WITH_CHECK ;
188+ return serializeSpecialFloatingPointValues ? TypeAdapters .FLOAT : FLOAT_STRICT ;
107189 }
108190
109191 private static void checkValidFloatingPoint (double value ) {
@@ -162,4 +244,40 @@ public AtomicLongArray read(JsonReader in) throws IOException {
162244 }
163245 }.nullSafe ();
164246 }
247+
248+ static void addTypeAdaptersForDate (
249+ String datePattern , int dateStyle , int timeStyle , List <TypeAdapterFactory > factories ) {
250+ TypeAdapterFactory dateAdapterFactory ;
251+ boolean sqlTypesSupported = SqlTypesSupport .SUPPORTS_SQL_TYPES ;
252+ TypeAdapterFactory sqlTimestampAdapterFactory = null ;
253+ TypeAdapterFactory sqlDateAdapterFactory = null ;
254+
255+ if (datePattern != null && !datePattern .trim ().isEmpty ()) {
256+ dateAdapterFactory = DefaultDateTypeAdapter .DateType .DATE .createAdapterFactory (datePattern );
257+
258+ if (sqlTypesSupported ) {
259+ sqlTimestampAdapterFactory =
260+ SqlTypesSupport .TIMESTAMP_DATE_TYPE .createAdapterFactory (datePattern );
261+ sqlDateAdapterFactory = SqlTypesSupport .DATE_DATE_TYPE .createAdapterFactory (datePattern );
262+ }
263+ } else if (dateStyle != DateFormat .DEFAULT || timeStyle != DateFormat .DEFAULT ) {
264+ dateAdapterFactory =
265+ DefaultDateTypeAdapter .DateType .DATE .createAdapterFactory (dateStyle , timeStyle );
266+
267+ if (sqlTypesSupported ) {
268+ sqlTimestampAdapterFactory =
269+ SqlTypesSupport .TIMESTAMP_DATE_TYPE .createAdapterFactory (dateStyle , timeStyle );
270+ sqlDateAdapterFactory =
271+ SqlTypesSupport .DATE_DATE_TYPE .createAdapterFactory (dateStyle , timeStyle );
272+ }
273+ } else {
274+ return ;
275+ }
276+
277+ factories .add (dateAdapterFactory );
278+ if (sqlTypesSupported ) {
279+ factories .add (sqlTimestampAdapterFactory );
280+ factories .add (sqlDateAdapterFactory );
281+ }
282+ }
165283}
0 commit comments