Skip to content

Commit 286843d

Browse files
refactor: move derived adapters from Gson to TypeAdapters (#2951)
Co-authored-by: Marcono1234 <[email protected]>
1 parent ae9604c commit 286843d

File tree

5 files changed

+191
-183
lines changed

5 files changed

+191
-183
lines changed

gson/src/main/java/com/google/gson/Gson.java

Lines changed: 14 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
package com.google.gson;
1818

19+
import static com.google.gson.internal.bind.TypeAdapters.atomicLongAdapter;
20+
import static com.google.gson.internal.bind.TypeAdapters.atomicLongArrayAdapter;
21+
1922
import com.google.gson.annotations.JsonAdapter;
2023
import com.google.gson.internal.ConstructorConstructor;
2124
import com.google.gson.internal.Excluder;
2225
import com.google.gson.internal.GsonBuildConfig;
23-
import com.google.gson.internal.LazilyParsedNumber;
2426
import com.google.gson.internal.Primitives;
2527
import com.google.gson.internal.Streams;
2628
import com.google.gson.internal.bind.ArrayTypeAdapter;
@@ -47,8 +49,6 @@
4749
import java.io.StringReader;
4850
import java.io.Writer;
4951
import java.lang.reflect.Type;
50-
import java.math.BigDecimal;
51-
import java.math.BigInteger;
5252
import java.text.DateFormat;
5353
import java.util.ArrayList;
5454
import java.util.Collections;
@@ -337,14 +337,10 @@ public Gson() {
337337
factories.add(TypeAdapters.BOOLEAN_FACTORY);
338338
factories.add(TypeAdapters.BYTE_FACTORY);
339339
factories.add(TypeAdapters.SHORT_FACTORY);
340-
TypeAdapter<Number> longAdapter = longAdapter(longSerializationPolicy);
340+
TypeAdapter<Number> longAdapter = longSerializationPolicy.typeAdapter();
341341
factories.add(TypeAdapters.newFactory(long.class, Long.class, longAdapter));
342-
factories.add(
343-
TypeAdapters.newFactory(
344-
double.class, Double.class, doubleAdapter(serializeSpecialFloatingPointValues)));
345-
factories.add(
346-
TypeAdapters.newFactory(
347-
float.class, Float.class, floatAdapter(serializeSpecialFloatingPointValues)));
342+
factories.add(TypeAdapters.newFactory(double.class, Double.class, doubleAdapter()));
343+
factories.add(TypeAdapters.newFactory(float.class, Float.class, floatAdapter()));
348344
factories.add(NumberTypeAdapter.getFactory(numberToNumberStrategy));
349345
factories.add(TypeAdapters.ATOMIC_INTEGER_FACTORY);
350346
factories.add(TypeAdapters.ATOMIC_BOOLEAN_FACTORY);
@@ -355,12 +351,11 @@ public Gson() {
355351
factories.add(TypeAdapters.CHARACTER_FACTORY);
356352
factories.add(TypeAdapters.STRING_BUILDER_FACTORY);
357353
factories.add(TypeAdapters.STRING_BUFFER_FACTORY);
358-
factories.add(TypeAdapters.newFactory(BigDecimal.class, TypeAdapters.BIG_DECIMAL));
359-
factories.add(TypeAdapters.newFactory(BigInteger.class, TypeAdapters.BIG_INTEGER));
354+
factories.add(TypeAdapters.BIG_DECIMAL_FACTORY);
355+
factories.add(TypeAdapters.BIG_INTEGER_FACTORY);
360356
// Add adapter for LazilyParsedNumber because user can obtain it from Gson and then try to
361357
// serialize it again
362-
factories.add(
363-
TypeAdapters.newFactory(LazilyParsedNumber.class, TypeAdapters.LAZILY_PARSED_NUMBER));
358+
factories.add(TypeAdapters.LAZILY_PARSED_NUMBER_FACTORY);
364359
factories.add(TypeAdapters.URL_FACTORY);
365360
factories.add(TypeAdapters.URI_FACTORY);
366361
factories.add(TypeAdapters.UUID_FACTORY);
@@ -370,17 +365,11 @@ public Gson() {
370365
factories.add(TypeAdapters.BIT_SET_FACTORY);
371366
factories.add(DefaultDateTypeAdapter.DEFAULT_STYLE_FACTORY);
372367
factories.add(TypeAdapters.CALENDAR_FACTORY);
368+
factories.addAll(SqlTypesSupport.SQL_TYPE_FACTORIES);
373369
TypeAdapterFactory javaTimeFactory = TypeAdapters.javaTimeTypeAdapterFactory();
374370
if (javaTimeFactory != null) {
375371
factories.add(javaTimeFactory);
376372
}
377-
378-
if (SqlTypesSupport.SUPPORTS_SQL_TYPES) {
379-
factories.add(SqlTypesSupport.TIME_FACTORY);
380-
factories.add(SqlTypesSupport.DATE_FACTORY);
381-
factories.add(SqlTypesSupport.TIMESTAMP_FACTORY);
382-
}
383-
384373
factories.add(ArrayTypeAdapter.FACTORY);
385374
factories.add(TypeAdapters.CLASS_FACTORY);
386375

@@ -450,141 +439,12 @@ public boolean htmlSafe() {
450439
return htmlSafe;
451440
}
452441

453-
private TypeAdapter<Number> doubleAdapter(boolean serializeSpecialFloatingPointValues) {
454-
if (serializeSpecialFloatingPointValues) {
455-
return TypeAdapters.DOUBLE;
456-
}
457-
return new TypeAdapter<Number>() {
458-
@Override
459-
public Double read(JsonReader in) throws IOException {
460-
if (in.peek() == JsonToken.NULL) {
461-
in.nextNull();
462-
return null;
463-
}
464-
return in.nextDouble();
465-
}
466-
467-
@Override
468-
public void write(JsonWriter out, Number value) throws IOException {
469-
if (value == null) {
470-
out.nullValue();
471-
return;
472-
}
473-
double doubleValue = value.doubleValue();
474-
checkValidFloatingPoint(doubleValue);
475-
out.value(doubleValue);
476-
}
477-
};
478-
}
479-
480-
private TypeAdapter<Number> floatAdapter(boolean serializeSpecialFloatingPointValues) {
481-
if (serializeSpecialFloatingPointValues) {
482-
return TypeAdapters.FLOAT;
483-
}
484-
return new TypeAdapter<Number>() {
485-
@Override
486-
public Float read(JsonReader in) throws IOException {
487-
if (in.peek() == JsonToken.NULL) {
488-
in.nextNull();
489-
return null;
490-
}
491-
return (float) in.nextDouble();
492-
}
493-
494-
@Override
495-
public void write(JsonWriter out, Number value) throws IOException {
496-
if (value == null) {
497-
out.nullValue();
498-
return;
499-
}
500-
float floatValue = value.floatValue();
501-
checkValidFloatingPoint(floatValue);
502-
// For backward compatibility don't call `JsonWriter.value(float)` because that method has
503-
// been newly added and not all custom JsonWriter implementations might override it yet
504-
Number floatNumber = value instanceof Float ? value : floatValue;
505-
out.value(floatNumber);
506-
}
507-
};
508-
}
509-
510-
static void checkValidFloatingPoint(double value) {
511-
if (Double.isNaN(value) || Double.isInfinite(value)) {
512-
throw new IllegalArgumentException(
513-
value
514-
+ " is not a valid double value as per JSON specification. To override this"
515-
+ " behavior, use GsonBuilder.serializeSpecialFloatingPointValues() method.");
516-
}
517-
}
518-
519-
private static TypeAdapter<Number> longAdapter(LongSerializationPolicy longSerializationPolicy) {
520-
if (longSerializationPolicy == LongSerializationPolicy.DEFAULT) {
521-
return TypeAdapters.LONG;
522-
}
523-
return new TypeAdapter<Number>() {
524-
@Override
525-
public Number read(JsonReader in) throws IOException {
526-
if (in.peek() == JsonToken.NULL) {
527-
in.nextNull();
528-
return null;
529-
}
530-
return in.nextLong();
531-
}
532-
533-
@Override
534-
public void write(JsonWriter out, Number value) throws IOException {
535-
if (value == null) {
536-
out.nullValue();
537-
return;
538-
}
539-
out.value(value.toString());
540-
}
541-
};
542-
}
543-
544-
private static TypeAdapter<AtomicLong> atomicLongAdapter(TypeAdapter<Number> longAdapter) {
545-
return new TypeAdapter<AtomicLong>() {
546-
@Override
547-
public void write(JsonWriter out, AtomicLong value) throws IOException {
548-
longAdapter.write(out, value.get());
549-
}
550-
551-
@Override
552-
public AtomicLong read(JsonReader in) throws IOException {
553-
Number value = longAdapter.read(in);
554-
return new AtomicLong(value.longValue());
555-
}
556-
}.nullSafe();
442+
private TypeAdapter<Number> floatAdapter() {
443+
return serializeSpecialFloatingPointValues ? TypeAdapters.FLOAT : TypeAdapters.FLOAT_STRICT;
557444
}
558445

559-
private static TypeAdapter<AtomicLongArray> atomicLongArrayAdapter(
560-
TypeAdapter<Number> longAdapter) {
561-
return new TypeAdapter<AtomicLongArray>() {
562-
@Override
563-
public void write(JsonWriter out, AtomicLongArray value) throws IOException {
564-
out.beginArray();
565-
for (int i = 0, length = value.length(); i < length; i++) {
566-
longAdapter.write(out, value.get(i));
567-
}
568-
out.endArray();
569-
}
570-
571-
@Override
572-
public AtomicLongArray read(JsonReader in) throws IOException {
573-
List<Long> list = new ArrayList<>();
574-
in.beginArray();
575-
while (in.hasNext()) {
576-
long value = longAdapter.read(in).longValue();
577-
list.add(value);
578-
}
579-
in.endArray();
580-
int length = list.size();
581-
AtomicLongArray array = new AtomicLongArray(length);
582-
for (int i = 0; i < length; ++i) {
583-
array.set(i, list.get(i));
584-
}
585-
return array;
586-
}
587-
}.nullSafe();
446+
private TypeAdapter<Number> doubleAdapter() {
447+
return serializeSpecialFloatingPointValues ? TypeAdapters.DOUBLE : TypeAdapters.DOUBLE_STRICT;
588448
}
589449

590450
/**

gson/src/main/java/com/google/gson/LongSerializationPolicy.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.gson;
1818

19+
import com.google.gson.internal.bind.TypeAdapters;
20+
1921
/**
2022
* Defines the expected format for a {@code long} or {@code Long} type when it is serialized.
2123
*
@@ -39,6 +41,11 @@ public JsonElement serialize(Long value) {
3941
}
4042
return new JsonPrimitive(value);
4143
}
44+
45+
@Override
46+
TypeAdapter<Number> typeAdapter() {
47+
return TypeAdapters.LONG;
48+
}
4249
},
4350

4451
/**
@@ -55,6 +62,11 @@ public JsonElement serialize(Long value) {
5562
}
5663
return new JsonPrimitive(value.toString());
5764
}
65+
66+
@Override
67+
TypeAdapter<Number> typeAdapter() {
68+
return TypeAdapters.LONG_AS_STRING;
69+
}
5870
};
5971

6072
/**
@@ -64,4 +76,8 @@ public JsonElement serialize(Long value) {
6476
* @return the serialized version of {@code value}
6577
*/
6678
public abstract JsonElement serialize(Long value);
79+
80+
/** Returns the corresponding {@link TypeAdapter} for this serialization policy. */
81+
// Internal method
82+
abstract TypeAdapter<Number> typeAdapter();
6783
}

0 commit comments

Comments
 (0)