|
16 | 16 |
|
17 | 17 | package com.google.gson; |
18 | 18 |
|
19 | | -import static com.google.gson.internal.bind.TypeAdapters.atomicLongAdapter; |
20 | | -import static com.google.gson.internal.bind.TypeAdapters.atomicLongArrayAdapter; |
| 19 | +import static com.google.gson.GsonBuilder.newImmutableList; |
21 | 20 |
|
22 | 21 | import com.google.gson.annotations.JsonAdapter; |
23 | 22 | import com.google.gson.internal.ConstructorConstructor; |
24 | 23 | import com.google.gson.internal.Excluder; |
25 | 24 | import com.google.gson.internal.GsonBuildConfig; |
26 | 25 | import com.google.gson.internal.Primitives; |
27 | 26 | import com.google.gson.internal.Streams; |
28 | | -import com.google.gson.internal.bind.ArrayTypeAdapter; |
29 | | -import com.google.gson.internal.bind.CollectionTypeAdapterFactory; |
30 | | -import com.google.gson.internal.bind.DefaultDateTypeAdapter; |
31 | 27 | import com.google.gson.internal.bind.JsonAdapterAnnotationTypeAdapterFactory; |
32 | 28 | import com.google.gson.internal.bind.JsonTreeReader; |
33 | 29 | import com.google.gson.internal.bind.JsonTreeWriter; |
34 | | -import com.google.gson.internal.bind.MapTypeAdapterFactory; |
35 | | -import com.google.gson.internal.bind.NumberTypeAdapter; |
36 | | -import com.google.gson.internal.bind.ObjectTypeAdapter; |
37 | | -import com.google.gson.internal.bind.ReflectiveTypeAdapterFactory; |
38 | 30 | import com.google.gson.internal.bind.SerializationDelegatingTypeAdapter; |
39 | | -import com.google.gson.internal.bind.TypeAdapters; |
40 | | -import com.google.gson.internal.sql.SqlTypesSupport; |
41 | 31 | import com.google.gson.reflect.TypeToken; |
42 | 32 | import com.google.gson.stream.JsonReader; |
43 | 33 | import com.google.gson.stream.JsonToken; |
|
49 | 39 | import java.io.StringReader; |
50 | 40 | import java.io.Writer; |
51 | 41 | import java.lang.reflect.Type; |
52 | | -import java.text.DateFormat; |
53 | | -import java.util.ArrayList; |
54 | | -import java.util.Collections; |
55 | 42 | import java.util.HashMap; |
56 | 43 | import java.util.List; |
57 | 44 | import java.util.Map; |
58 | 45 | import java.util.Objects; |
59 | 46 | import java.util.concurrent.ConcurrentHashMap; |
60 | 47 | import java.util.concurrent.ConcurrentMap; |
61 | | -import java.util.concurrent.atomic.AtomicLong; |
62 | | -import java.util.concurrent.atomic.AtomicLongArray; |
63 | 48 |
|
64 | 49 | /** |
65 | 50 | * This is the main class for using Gson. Gson is typically used by first constructing a Gson |
|
149 | 134 | */ |
150 | 135 | public final class Gson { |
151 | 136 |
|
152 | | - static final boolean DEFAULT_JSON_NON_EXECUTABLE = false; |
153 | | - // Strictness of `null` is the legacy mode where some Gson APIs are always lenient |
154 | | - static final Strictness DEFAULT_STRICTNESS = null; |
155 | | - static final FormattingStyle DEFAULT_FORMATTING_STYLE = FormattingStyle.COMPACT; |
156 | | - static final boolean DEFAULT_ESCAPE_HTML = true; |
157 | | - static final boolean DEFAULT_SERIALIZE_NULLS = false; |
158 | | - static final boolean DEFAULT_COMPLEX_MAP_KEYS = false; |
159 | | - static final boolean DEFAULT_SPECIALIZE_FLOAT_VALUES = false; |
160 | | - static final boolean DEFAULT_USE_JDK_UNSAFE = true; |
161 | | - static final String DEFAULT_DATE_PATTERN = null; |
162 | | - static final FieldNamingStrategy DEFAULT_FIELD_NAMING_STRATEGY = FieldNamingPolicy.IDENTITY; |
163 | | - static final ToNumberStrategy DEFAULT_OBJECT_TO_NUMBER_STRATEGY = ToNumberPolicy.DOUBLE; |
164 | | - static final ToNumberStrategy DEFAULT_NUMBER_TO_NUMBER_STRATEGY = |
165 | | - ToNumberPolicy.LAZILY_PARSED_NUMBER; |
166 | | - |
167 | 137 | private static final String JSON_NON_EXECUTABLE_PREFIX = ")]}'\n"; |
168 | 138 |
|
169 | 139 | /** |
@@ -250,144 +220,40 @@ public final class Gson { |
250 | 220 | * </ul> |
251 | 221 | */ |
252 | 222 | public Gson() { |
253 | | - this( |
254 | | - Excluder.DEFAULT, |
255 | | - DEFAULT_FIELD_NAMING_STRATEGY, |
256 | | - Collections.emptyMap(), |
257 | | - DEFAULT_SERIALIZE_NULLS, |
258 | | - DEFAULT_COMPLEX_MAP_KEYS, |
259 | | - DEFAULT_JSON_NON_EXECUTABLE, |
260 | | - DEFAULT_ESCAPE_HTML, |
261 | | - DEFAULT_FORMATTING_STYLE, |
262 | | - DEFAULT_STRICTNESS, |
263 | | - DEFAULT_SPECIALIZE_FLOAT_VALUES, |
264 | | - DEFAULT_USE_JDK_UNSAFE, |
265 | | - LongSerializationPolicy.DEFAULT, |
266 | | - DEFAULT_DATE_PATTERN, |
267 | | - DateFormat.DEFAULT, |
268 | | - DateFormat.DEFAULT, |
269 | | - Collections.emptyList(), |
270 | | - Collections.emptyList(), |
271 | | - Collections.emptyList(), |
272 | | - DEFAULT_OBJECT_TO_NUMBER_STRATEGY, |
273 | | - DEFAULT_NUMBER_TO_NUMBER_STRATEGY, |
274 | | - Collections.emptyList()); |
| 223 | + this(GsonBuilder.DEFAULT); |
275 | 224 | } |
276 | 225 |
|
277 | | - Gson( |
278 | | - Excluder excluder, |
279 | | - FieldNamingStrategy fieldNamingStrategy, |
280 | | - Map<Type, InstanceCreator<?>> instanceCreators, |
281 | | - boolean serializeNulls, |
282 | | - boolean complexMapKeySerialization, |
283 | | - boolean generateNonExecutableGson, |
284 | | - boolean htmlSafe, |
285 | | - FormattingStyle formattingStyle, |
286 | | - Strictness strictness, |
287 | | - boolean serializeSpecialFloatingPointValues, |
288 | | - boolean useJdkUnsafe, |
289 | | - LongSerializationPolicy longSerializationPolicy, |
290 | | - String datePattern, |
291 | | - int dateStyle, |
292 | | - int timeStyle, |
293 | | - List<TypeAdapterFactory> builderFactories, |
294 | | - List<TypeAdapterFactory> builderHierarchyFactories, |
295 | | - List<TypeAdapterFactory> factoriesToBeAdded, |
296 | | - ToNumberStrategy objectToNumberStrategy, |
297 | | - ToNumberStrategy numberToNumberStrategy, |
298 | | - List<ReflectionAccessFilter> reflectionFilters) { |
299 | | - this.excluder = excluder; |
300 | | - this.fieldNamingStrategy = fieldNamingStrategy; |
301 | | - this.instanceCreators = instanceCreators; |
302 | | - this.constructorConstructor = |
303 | | - new ConstructorConstructor(instanceCreators, useJdkUnsafe, reflectionFilters); |
304 | | - this.serializeNulls = serializeNulls; |
305 | | - this.complexMapKeySerialization = complexMapKeySerialization; |
306 | | - this.generateNonExecutableJson = generateNonExecutableGson; |
307 | | - this.htmlSafe = htmlSafe; |
308 | | - this.formattingStyle = formattingStyle; |
309 | | - this.strictness = strictness; |
310 | | - this.serializeSpecialFloatingPointValues = serializeSpecialFloatingPointValues; |
311 | | - this.useJdkUnsafe = useJdkUnsafe; |
312 | | - this.longSerializationPolicy = longSerializationPolicy; |
313 | | - this.datePattern = datePattern; |
314 | | - this.dateStyle = dateStyle; |
315 | | - this.timeStyle = timeStyle; |
316 | | - this.builderFactories = builderFactories; |
317 | | - this.builderHierarchyFactories = builderHierarchyFactories; |
318 | | - this.objectToNumberStrategy = objectToNumberStrategy; |
319 | | - this.numberToNumberStrategy = numberToNumberStrategy; |
320 | | - this.reflectionFilters = reflectionFilters; |
321 | | - |
322 | | - List<TypeAdapterFactory> factories = new ArrayList<>(); |
323 | | - |
324 | | - // built-in type adapters that cannot be overridden |
325 | | - factories.add(TypeAdapters.JSON_ELEMENT_FACTORY); |
326 | | - factories.add(ObjectTypeAdapter.getFactory(objectToNumberStrategy)); |
327 | | - |
328 | | - // the excluder must precede all adapters that handle user-defined types |
329 | | - factories.add(excluder); |
330 | | - |
331 | | - // users' type adapters |
332 | | - factories.addAll(factoriesToBeAdded); |
333 | | - |
334 | | - // type adapters for basic platform types |
335 | | - factories.add(TypeAdapters.STRING_FACTORY); |
336 | | - factories.add(TypeAdapters.INTEGER_FACTORY); |
337 | | - factories.add(TypeAdapters.BOOLEAN_FACTORY); |
338 | | - factories.add(TypeAdapters.BYTE_FACTORY); |
339 | | - factories.add(TypeAdapters.SHORT_FACTORY); |
340 | | - TypeAdapter<Number> longAdapter = longSerializationPolicy.typeAdapter(); |
341 | | - factories.add(TypeAdapters.newFactory(long.class, Long.class, longAdapter)); |
342 | | - factories.add(TypeAdapters.newFactory(double.class, Double.class, doubleAdapter())); |
343 | | - factories.add(TypeAdapters.newFactory(float.class, Float.class, floatAdapter())); |
344 | | - factories.add(NumberTypeAdapter.getFactory(numberToNumberStrategy)); |
345 | | - factories.add(TypeAdapters.ATOMIC_INTEGER_FACTORY); |
346 | | - factories.add(TypeAdapters.ATOMIC_BOOLEAN_FACTORY); |
347 | | - factories.add(TypeAdapters.newFactory(AtomicLong.class, atomicLongAdapter(longAdapter))); |
348 | | - factories.add( |
349 | | - TypeAdapters.newFactory(AtomicLongArray.class, atomicLongArrayAdapter(longAdapter))); |
350 | | - factories.add(TypeAdapters.ATOMIC_INTEGER_ARRAY_FACTORY); |
351 | | - factories.add(TypeAdapters.CHARACTER_FACTORY); |
352 | | - factories.add(TypeAdapters.STRING_BUILDER_FACTORY); |
353 | | - factories.add(TypeAdapters.STRING_BUFFER_FACTORY); |
354 | | - factories.add(TypeAdapters.BIG_DECIMAL_FACTORY); |
355 | | - factories.add(TypeAdapters.BIG_INTEGER_FACTORY); |
356 | | - // Add adapter for LazilyParsedNumber because user can obtain it from Gson and then try to |
357 | | - // serialize it again |
358 | | - factories.add(TypeAdapters.LAZILY_PARSED_NUMBER_FACTORY); |
359 | | - factories.add(TypeAdapters.URL_FACTORY); |
360 | | - factories.add(TypeAdapters.URI_FACTORY); |
361 | | - factories.add(TypeAdapters.UUID_FACTORY); |
362 | | - factories.add(TypeAdapters.CURRENCY_FACTORY); |
363 | | - factories.add(TypeAdapters.LOCALE_FACTORY); |
364 | | - factories.add(TypeAdapters.INET_ADDRESS_FACTORY); |
365 | | - factories.add(TypeAdapters.BIT_SET_FACTORY); |
366 | | - factories.add(DefaultDateTypeAdapter.DEFAULT_STYLE_FACTORY); |
367 | | - factories.add(TypeAdapters.CALENDAR_FACTORY); |
368 | | - factories.addAll(SqlTypesSupport.SQL_TYPE_FACTORIES); |
369 | | - TypeAdapterFactory javaTimeFactory = TypeAdapters.javaTimeTypeAdapterFactory(); |
370 | | - if (javaTimeFactory != null) { |
371 | | - factories.add(javaTimeFactory); |
| 226 | + Gson(GsonBuilder builder) { |
| 227 | + this.excluder = builder.excluder; |
| 228 | + this.fieldNamingStrategy = builder.fieldNamingPolicy; |
| 229 | + this.instanceCreators = new HashMap<>(builder.instanceCreators); |
| 230 | + this.serializeNulls = builder.serializeNulls; |
| 231 | + this.complexMapKeySerialization = builder.complexMapKeySerialization; |
| 232 | + this.generateNonExecutableJson = builder.generateNonExecutableJson; |
| 233 | + this.htmlSafe = builder.escapeHtmlChars; |
| 234 | + this.formattingStyle = builder.formattingStyle; |
| 235 | + this.strictness = builder.strictness; |
| 236 | + this.serializeSpecialFloatingPointValues = builder.serializeSpecialFloatingPointValues; |
| 237 | + this.useJdkUnsafe = builder.useJdkUnsafe; |
| 238 | + this.longSerializationPolicy = builder.longSerializationPolicy; |
| 239 | + this.datePattern = builder.datePattern; |
| 240 | + this.dateStyle = builder.dateStyle; |
| 241 | + this.timeStyle = builder.timeStyle; |
| 242 | + this.builderFactories = newImmutableList(builder.factories); |
| 243 | + this.builderHierarchyFactories = newImmutableList(builder.hierarchyFactories); |
| 244 | + this.objectToNumberStrategy = builder.objectToNumberStrategy; |
| 245 | + this.numberToNumberStrategy = builder.numberToNumberStrategy; |
| 246 | + this.reflectionFilters = newImmutableList(builder.reflectionFilters); |
| 247 | + if (builder == GsonBuilder.DEFAULT) { |
| 248 | + this.constructorConstructor = GsonBuilder.DEFAULT_CONSTRUCTOR_CONSTRUCTOR; |
| 249 | + this.jsonAdapterFactory = GsonBuilder.DEFAULT_JSON_ADAPTER_ANNOTATION_TYPE_ADAPTER_FACTORY; |
| 250 | + this.factories = GsonBuilder.DEFAULT_TYPE_ADAPTER_FACTORIES; |
| 251 | + } else { |
| 252 | + this.constructorConstructor = |
| 253 | + new ConstructorConstructor(instanceCreators, useJdkUnsafe, reflectionFilters); |
| 254 | + this.jsonAdapterFactory = new JsonAdapterAnnotationTypeAdapterFactory(constructorConstructor); |
| 255 | + this.factories = builder.createFactories(constructorConstructor, jsonAdapterFactory); |
372 | 256 | } |
373 | | - factories.add(ArrayTypeAdapter.FACTORY); |
374 | | - factories.add(TypeAdapters.CLASS_FACTORY); |
375 | | - |
376 | | - // type adapters for composite and user-defined types |
377 | | - factories.add(new CollectionTypeAdapterFactory(constructorConstructor)); |
378 | | - factories.add(new MapTypeAdapterFactory(constructorConstructor, complexMapKeySerialization)); |
379 | | - this.jsonAdapterFactory = new JsonAdapterAnnotationTypeAdapterFactory(constructorConstructor); |
380 | | - factories.add(jsonAdapterFactory); |
381 | | - factories.add(TypeAdapters.ENUM_FACTORY); |
382 | | - factories.add( |
383 | | - new ReflectiveTypeAdapterFactory( |
384 | | - constructorConstructor, |
385 | | - fieldNamingStrategy, |
386 | | - excluder, |
387 | | - jsonAdapterFactory, |
388 | | - reflectionFilters)); |
389 | | - |
390 | | - this.factories = Collections.unmodifiableList(factories); |
391 | 257 | } |
392 | 258 |
|
393 | 259 | /** |
@@ -439,14 +305,6 @@ public boolean htmlSafe() { |
439 | 305 | return htmlSafe; |
440 | 306 | } |
441 | 307 |
|
442 | | - private TypeAdapter<Number> floatAdapter() { |
443 | | - return serializeSpecialFloatingPointValues ? TypeAdapters.FLOAT : TypeAdapters.FLOAT_STRICT; |
444 | | - } |
445 | | - |
446 | | - private TypeAdapter<Number> doubleAdapter() { |
447 | | - return serializeSpecialFloatingPointValues ? TypeAdapters.DOUBLE : TypeAdapters.DOUBLE_STRICT; |
448 | | - } |
449 | | - |
450 | 308 | /** |
451 | 309 | * Returns the type adapter for {@code type}. |
452 | 310 | * |
|
0 commit comments