|
16 | 16 |
|
17 | 17 | package com.google.gson; |
18 | 18 |
|
19 | | -import static com.google.gson.BuilderHelper.atomicLongAdapter; |
20 | | -import static com.google.gson.BuilderHelper.atomicLongArrayAdapter; |
21 | | -import static com.google.gson.BuilderHelper.doubleAdapter; |
22 | | -import static com.google.gson.BuilderHelper.floatAdapter; |
23 | | -import static com.google.gson.BuilderHelper.longAdapter; |
24 | 19 | import static com.google.gson.BuilderHelper.unmodifiableList; |
25 | 20 |
|
26 | 21 | import com.google.gson.annotations.JsonAdapter; |
27 | 22 | import com.google.gson.internal.ConstructorConstructor; |
28 | 23 | import com.google.gson.internal.Excluder; |
29 | 24 | import com.google.gson.internal.GsonBuildConfig; |
30 | | -import com.google.gson.internal.LazilyParsedNumber; |
31 | 25 | import com.google.gson.internal.Primitives; |
32 | 26 | import com.google.gson.internal.Streams; |
33 | | -import com.google.gson.internal.bind.ArrayTypeAdapter; |
34 | | -import com.google.gson.internal.bind.CollectionTypeAdapterFactory; |
35 | | -import com.google.gson.internal.bind.DefaultDateTypeAdapter; |
36 | 27 | import com.google.gson.internal.bind.JsonAdapterAnnotationTypeAdapterFactory; |
37 | 28 | import com.google.gson.internal.bind.JsonTreeReader; |
38 | 29 | import com.google.gson.internal.bind.JsonTreeWriter; |
39 | | -import com.google.gson.internal.bind.MapTypeAdapterFactory; |
40 | | -import com.google.gson.internal.bind.NumberTypeAdapter; |
41 | | -import com.google.gson.internal.bind.ObjectTypeAdapter; |
42 | | -import com.google.gson.internal.bind.ReflectiveTypeAdapterFactory; |
43 | 30 | import com.google.gson.internal.bind.SerializationDelegatingTypeAdapter; |
44 | | -import com.google.gson.internal.bind.TypeAdapters; |
45 | | -import com.google.gson.internal.sql.SqlTypesSupport; |
46 | 31 | import com.google.gson.reflect.TypeToken; |
47 | 32 | import com.google.gson.stream.JsonReader; |
48 | 33 | import com.google.gson.stream.JsonToken; |
|
54 | 39 | import java.io.StringReader; |
55 | 40 | import java.io.Writer; |
56 | 41 | import java.lang.reflect.Type; |
57 | | -import java.math.BigDecimal; |
58 | | -import java.math.BigInteger; |
59 | | -import java.util.ArrayList; |
60 | 42 | import java.util.HashMap; |
61 | 43 | import java.util.List; |
62 | 44 | import java.util.Map; |
63 | 45 | import java.util.Objects; |
64 | 46 | import java.util.concurrent.ConcurrentHashMap; |
65 | 47 | import java.util.concurrent.ConcurrentMap; |
66 | | -import java.util.concurrent.atomic.AtomicLong; |
67 | | -import java.util.concurrent.atomic.AtomicLongArray; |
68 | 48 |
|
69 | 49 | /** |
70 | 50 | * This is the main class for using Gson. Gson is typically used by first constructing a Gson |
@@ -160,11 +140,8 @@ public final class Gson { |
160 | 140 | static final FormattingStyle DEFAULT_FORMATTING_STYLE = FormattingStyle.COMPACT; |
161 | 141 | static final boolean DEFAULT_ESCAPE_HTML = true; |
162 | 142 | static final boolean DEFAULT_SERIALIZE_NULLS = false; |
163 | | - static final boolean DEFAULT_COMPLEX_MAP_KEYS = false; |
164 | 143 | static final boolean DEFAULT_SPECIALIZE_FLOAT_VALUES = false; |
165 | | - static final boolean DEFAULT_USE_JDK_UNSAFE = true; |
166 | 144 | static final String DEFAULT_DATE_PATTERN = null; |
167 | | - static final FieldNamingStrategy DEFAULT_FIELD_NAMING_STRATEGY = FieldNamingPolicy.IDENTITY; |
168 | 145 | static final ToNumberStrategy DEFAULT_OBJECT_TO_NUMBER_STRATEGY = ToNumberPolicy.DOUBLE; |
169 | 146 | static final ToNumberStrategy DEFAULT_NUMBER_TO_NUMBER_STRATEGY = |
170 | 147 | ToNumberPolicy.LAZILY_PARSED_NUMBER; |
@@ -259,145 +236,31 @@ public Gson() { |
259 | 236 | } |
260 | 237 |
|
261 | 238 | Gson(GsonBuilder builder) { |
262 | | - this( |
263 | | - builder.excluder, |
264 | | - builder.fieldNamingPolicy, |
265 | | - new HashMap<>(builder.instanceCreators), |
266 | | - builder.serializeNulls, |
267 | | - builder.complexMapKeySerialization, |
268 | | - builder.generateNonExecutableJson, |
269 | | - builder.escapeHtmlChars, |
270 | | - builder.formattingStyle, |
271 | | - builder.strictness, |
272 | | - builder.serializeSpecialFloatingPointValues, |
273 | | - builder.useJdkUnsafe, |
274 | | - builder.longSerializationPolicy, |
275 | | - builder.datePattern, |
276 | | - builder.dateStyle, |
277 | | - builder.timeStyle, |
278 | | - unmodifiableList(builder.factories), |
279 | | - unmodifiableList(builder.hierarchyFactories), |
280 | | - builder.getAllFactoriesToBeAdded(), |
281 | | - builder.objectToNumberStrategy, |
282 | | - builder.numberToNumberStrategy, |
283 | | - unmodifiableList(builder.reflectionFilters)); |
284 | | - } |
285 | | - |
286 | | - private Gson( |
287 | | - Excluder excluder, |
288 | | - FieldNamingStrategy fieldNamingStrategy, |
289 | | - Map<Type, InstanceCreator<?>> instanceCreators, |
290 | | - boolean serializeNulls, |
291 | | - boolean complexMapKeySerialization, |
292 | | - boolean generateNonExecutableGson, |
293 | | - boolean htmlSafe, |
294 | | - FormattingStyle formattingStyle, |
295 | | - Strictness strictness, |
296 | | - boolean serializeSpecialFloatingPointValues, |
297 | | - boolean useJdkUnsafe, |
298 | | - LongSerializationPolicy longSerializationPolicy, |
299 | | - String datePattern, |
300 | | - int dateStyle, |
301 | | - int timeStyle, |
302 | | - List<TypeAdapterFactory> builderFactories, |
303 | | - List<TypeAdapterFactory> builderHierarchyFactories, |
304 | | - List<TypeAdapterFactory> factoriesToBeAdded, |
305 | | - ToNumberStrategy objectToNumberStrategy, |
306 | | - ToNumberStrategy numberToNumberStrategy, |
307 | | - List<ReflectionAccessFilter> reflectionFilters) { |
308 | | - this.excluder = excluder; |
309 | | - this.fieldNamingStrategy = fieldNamingStrategy; |
310 | | - this.instanceCreators = instanceCreators; |
311 | | - this.serializeNulls = serializeNulls; |
312 | | - this.complexMapKeySerialization = complexMapKeySerialization; |
313 | | - this.generateNonExecutableJson = generateNonExecutableGson; |
314 | | - this.htmlSafe = htmlSafe; |
315 | | - this.formattingStyle = formattingStyle; |
316 | | - this.strictness = strictness; |
317 | | - this.serializeSpecialFloatingPointValues = serializeSpecialFloatingPointValues; |
318 | | - this.useJdkUnsafe = useJdkUnsafe; |
319 | | - this.longSerializationPolicy = longSerializationPolicy; |
320 | | - this.datePattern = datePattern; |
321 | | - this.dateStyle = dateStyle; |
322 | | - this.timeStyle = timeStyle; |
323 | | - this.builderFactories = builderFactories; |
324 | | - this.builderHierarchyFactories = builderHierarchyFactories; |
325 | | - this.objectToNumberStrategy = objectToNumberStrategy; |
326 | | - this.numberToNumberStrategy = numberToNumberStrategy; |
327 | | - this.reflectionFilters = reflectionFilters; |
328 | | - |
329 | | - List<TypeAdapterFactory> factories = new ArrayList<>(); |
330 | | - |
331 | | - // built-in type adapters that cannot be overridden |
332 | | - factories.add(TypeAdapters.JSON_ELEMENT_FACTORY); |
333 | | - factories.add(ObjectTypeAdapter.getFactory(objectToNumberStrategy)); |
334 | | - |
335 | | - // the excluder must precede all adapters that handle user-defined types |
336 | | - factories.add(excluder); |
337 | | - |
338 | | - // users' type adapters |
339 | | - factories.addAll(factoriesToBeAdded); |
340 | | - |
341 | | - // type adapters for basic platform types |
342 | | - factories.add(TypeAdapters.STRING_FACTORY); |
343 | | - factories.add(TypeAdapters.INTEGER_FACTORY); |
344 | | - factories.add(TypeAdapters.BOOLEAN_FACTORY); |
345 | | - factories.add(TypeAdapters.BYTE_FACTORY); |
346 | | - factories.add(TypeAdapters.SHORT_FACTORY); |
347 | | - TypeAdapter<Number> longAdapter = longAdapter(longSerializationPolicy); |
348 | | - factories.add(TypeAdapters.newFactory(long.class, Long.class, longAdapter)); |
349 | | - factories.add( |
350 | | - TypeAdapters.newFactory( |
351 | | - double.class, Double.class, doubleAdapter(serializeSpecialFloatingPointValues))); |
352 | | - factories.add( |
353 | | - TypeAdapters.newFactory( |
354 | | - float.class, Float.class, floatAdapter(serializeSpecialFloatingPointValues))); |
355 | | - factories.add(NumberTypeAdapter.getFactory(numberToNumberStrategy)); |
356 | | - factories.add(TypeAdapters.ATOMIC_INTEGER_FACTORY); |
357 | | - factories.add(TypeAdapters.ATOMIC_BOOLEAN_FACTORY); |
358 | | - factories.add(TypeAdapters.newFactory(AtomicLong.class, atomicLongAdapter(longAdapter))); |
359 | | - factories.add( |
360 | | - TypeAdapters.newFactory(AtomicLongArray.class, atomicLongArrayAdapter(longAdapter))); |
361 | | - factories.add(TypeAdapters.ATOMIC_INTEGER_ARRAY_FACTORY); |
362 | | - factories.add(TypeAdapters.CHARACTER_FACTORY); |
363 | | - factories.add(TypeAdapters.STRING_BUILDER_FACTORY); |
364 | | - factories.add(TypeAdapters.STRING_BUFFER_FACTORY); |
365 | | - factories.add(TypeAdapters.newFactory(BigDecimal.class, TypeAdapters.BIG_DECIMAL)); |
366 | | - factories.add(TypeAdapters.newFactory(BigInteger.class, TypeAdapters.BIG_INTEGER)); |
367 | | - // Add adapter for LazilyParsedNumber because user can obtain it from Gson and then try to |
368 | | - // serialize it again |
369 | | - factories.add( |
370 | | - TypeAdapters.newFactory(LazilyParsedNumber.class, TypeAdapters.LAZILY_PARSED_NUMBER)); |
371 | | - factories.add(TypeAdapters.URL_FACTORY); |
372 | | - factories.add(TypeAdapters.URI_FACTORY); |
373 | | - factories.add(TypeAdapters.UUID_FACTORY); |
374 | | - factories.add(TypeAdapters.CURRENCY_FACTORY); |
375 | | - factories.add(TypeAdapters.LOCALE_FACTORY); |
376 | | - factories.add(TypeAdapters.INET_ADDRESS_FACTORY); |
377 | | - factories.add(TypeAdapters.BIT_SET_FACTORY); |
378 | | - factories.add(DefaultDateTypeAdapter.DEFAULT_STYLE_FACTORY); |
379 | | - factories.add(TypeAdapters.CALENDAR_FACTORY); |
380 | | - factories.addAll(SqlTypesSupport.SQL_TYPE_FACTORIES); |
381 | | - factories.add(ArrayTypeAdapter.FACTORY); |
382 | | - factories.add(TypeAdapters.CLASS_FACTORY); |
383 | | - |
| 239 | + this.excluder = builder.excluder; |
| 240 | + this.fieldNamingStrategy = builder.fieldNamingPolicy; |
| 241 | + this.instanceCreators = new HashMap<>(builder.instanceCreators); |
| 242 | + this.serializeNulls = builder.serializeNulls; |
| 243 | + this.complexMapKeySerialization = builder.complexMapKeySerialization; |
| 244 | + this.generateNonExecutableJson = builder.generateNonExecutableJson; |
| 245 | + this.htmlSafe = builder.escapeHtmlChars; |
| 246 | + this.formattingStyle = builder.formattingStyle; |
| 247 | + this.strictness = builder.strictness; |
| 248 | + this.serializeSpecialFloatingPointValues = builder.serializeSpecialFloatingPointValues; |
| 249 | + this.useJdkUnsafe = builder.useJdkUnsafe; |
| 250 | + this.longSerializationPolicy = builder.longSerializationPolicy; |
| 251 | + this.datePattern = builder.datePattern; |
| 252 | + this.dateStyle = builder.dateStyle; |
| 253 | + this.timeStyle = builder.timeStyle; |
| 254 | + this.builderFactories = unmodifiableList(builder.factories); |
| 255 | + this.builderHierarchyFactories = unmodifiableList(builder.hierarchyFactories); |
| 256 | + this.objectToNumberStrategy = builder.objectToNumberStrategy; |
| 257 | + this.numberToNumberStrategy = builder.numberToNumberStrategy; |
| 258 | + this.reflectionFilters = unmodifiableList(builder.reflectionFilters); |
384 | 259 | this.constructorConstructor = |
385 | 260 | new ConstructorConstructor(instanceCreators, useJdkUnsafe, reflectionFilters); |
386 | | - // type adapters for composite and user-defined types |
387 | | - factories.add(new CollectionTypeAdapterFactory(constructorConstructor)); |
388 | | - factories.add(new MapTypeAdapterFactory(constructorConstructor, complexMapKeySerialization)); |
389 | 261 | this.jsonAdapterFactory = new JsonAdapterAnnotationTypeAdapterFactory(constructorConstructor); |
390 | | - factories.add(jsonAdapterFactory); |
391 | | - factories.add(TypeAdapters.ENUM_FACTORY); |
392 | | - factories.add( |
393 | | - new ReflectiveTypeAdapterFactory( |
394 | | - constructorConstructor, |
395 | | - fieldNamingStrategy, |
396 | | - excluder, |
397 | | - jsonAdapterFactory, |
398 | | - reflectionFilters)); |
399 | | - |
400 | | - this.factories = unmodifiableList(factories); |
| 262 | + this.factories = |
| 263 | + unmodifiableList(builder.createFactories(constructorConstructor, jsonAdapterFactory)); |
401 | 264 | } |
402 | 265 |
|
403 | 266 | /** |
|
0 commit comments