Restore support for long serialization policy.
This commit is contained in:
parent
5fc2db9e72
commit
aa067056c3
|
@ -16,18 +16,6 @@
|
||||||
|
|
||||||
package com.google.gson;
|
package com.google.gson;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.Reader;
|
|
||||||
import java.io.StringReader;
|
|
||||||
import java.io.StringWriter;
|
|
||||||
import java.io.Writer;
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import com.google.gson.internal.Streams;
|
import com.google.gson.internal.Streams;
|
||||||
import com.google.gson.internal.bind.ArrayTypeAdapter;
|
import com.google.gson.internal.bind.ArrayTypeAdapter;
|
||||||
import com.google.gson.internal.bind.CollectionTypeAdapter;
|
import com.google.gson.internal.bind.CollectionTypeAdapter;
|
||||||
|
@ -41,6 +29,17 @@ import com.google.gson.stream.JsonReader;
|
||||||
import com.google.gson.stream.JsonToken;
|
import com.google.gson.stream.JsonToken;
|
||||||
import com.google.gson.stream.JsonWriter;
|
import com.google.gson.stream.JsonWriter;
|
||||||
import com.google.gson.stream.MalformedJsonException;
|
import com.google.gson.stream.MalformedJsonException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.io.StringReader;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
import java.io.Writer;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the main class for using Gson. Gson is typically used by first constructing a
|
* This is the main class for using Gson. Gson is typically used by first constructing a
|
||||||
|
@ -161,7 +160,8 @@ public final class Gson {
|
||||||
this(DEFAULT_EXCLUSION_STRATEGY, DEFAULT_EXCLUSION_STRATEGY, DEFAULT_NAMING_POLICY,
|
this(DEFAULT_EXCLUSION_STRATEGY, DEFAULT_EXCLUSION_STRATEGY, DEFAULT_NAMING_POLICY,
|
||||||
new MappedObjectConstructor(DefaultTypeAdapters.getDefaultInstanceCreators()),
|
new MappedObjectConstructor(DefaultTypeAdapters.getDefaultInstanceCreators()),
|
||||||
false, DefaultTypeAdapters.getAllDefaultSerializers(),
|
false, DefaultTypeAdapters.getAllDefaultSerializers(),
|
||||||
DefaultTypeAdapters.getAllDefaultDeserializers(), DEFAULT_JSON_NON_EXECUTABLE, true, false);
|
DefaultTypeAdapters.getAllDefaultDeserializers(), DEFAULT_JSON_NON_EXECUTABLE, true, false,
|
||||||
|
false, LongSerializationPolicy.DEFAULT);
|
||||||
}
|
}
|
||||||
|
|
||||||
Gson(ExclusionStrategy deserializationExclusionStrategy,
|
Gson(ExclusionStrategy deserializationExclusionStrategy,
|
||||||
|
@ -170,7 +170,8 @@ public final class Gson {
|
||||||
final MappedObjectConstructor objectConstructor, boolean serializeNulls,
|
final MappedObjectConstructor objectConstructor, boolean serializeNulls,
|
||||||
final ParameterizedTypeHandlerMap<JsonSerializer<?>> serializers,
|
final ParameterizedTypeHandlerMap<JsonSerializer<?>> serializers,
|
||||||
final ParameterizedTypeHandlerMap<JsonDeserializer<?>> deserializers,
|
final ParameterizedTypeHandlerMap<JsonDeserializer<?>> deserializers,
|
||||||
boolean generateNonExecutableGson, boolean htmlSafe, boolean prettyPrinting) {
|
boolean generateNonExecutableGson, boolean htmlSafe, boolean prettyPrinting,
|
||||||
|
boolean serializeSpecialFloatingPointValues, LongSerializationPolicy longSerializationPolicy) {
|
||||||
this.deserializationExclusionStrategy = deserializationExclusionStrategy;
|
this.deserializationExclusionStrategy = deserializationExclusionStrategy;
|
||||||
this.serializationExclusionStrategy = serializationExclusionStrategy;
|
this.serializationExclusionStrategy = serializationExclusionStrategy;
|
||||||
this.fieldNamingPolicy = fieldNamingPolicy;
|
this.fieldNamingPolicy = fieldNamingPolicy;
|
||||||
|
@ -207,19 +208,35 @@ public final class Gson {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.miniGson = new MiniGson.Builder()
|
MiniGson.Builder builder = new MiniGson.Builder()
|
||||||
.withoutDefaultFactories()
|
.withoutDefaultFactories()
|
||||||
.factory(TypeAdapters.BOOLEAN_FACTORY)
|
.factory(TypeAdapters.BOOLEAN_FACTORY)
|
||||||
.factory(TypeAdapters.INTEGER_FACTORY)
|
.factory(TypeAdapters.INTEGER_FACTORY)
|
||||||
.factory(TypeAdapters.DOUBLE_FACTORY)
|
.factory(TypeAdapters.DOUBLE_FACTORY)
|
||||||
.factory(TypeAdapters.LONG_FACTORY)
|
.factory(TypeAdapters.newFactory(long.class, Long.class,
|
||||||
|
longAdapter(longSerializationPolicy)))
|
||||||
.factory(TypeAdapters.STRING_FACTORY)
|
.factory(TypeAdapters.STRING_FACTORY)
|
||||||
.factory(new GsonToMiniGsonTypeAdapter(serializers, deserializers, serializeNulls))
|
.factory(new GsonToMiniGsonTypeAdapter(serializers, deserializers, serializeNulls))
|
||||||
.factory(CollectionTypeAdapter.FACTORY)
|
.factory(CollectionTypeAdapter.FACTORY)
|
||||||
.factory(StringToValueMapTypeAdapter.FACTORY)
|
.factory(StringToValueMapTypeAdapter.FACTORY)
|
||||||
.factory(ArrayTypeAdapter.FACTORY)
|
.factory(ArrayTypeAdapter.FACTORY)
|
||||||
.factory(reflectiveTypeAdapterFactory)
|
.factory(reflectiveTypeAdapterFactory);
|
||||||
.build();
|
|
||||||
|
this.miniGson = builder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private TypeAdapter<Long> longAdapter(LongSerializationPolicy longSerializationPolicy) {
|
||||||
|
if (longSerializationPolicy == LongSerializationPolicy.DEFAULT) {
|
||||||
|
return TypeAdapters.LONG;
|
||||||
|
}
|
||||||
|
return new TypeAdapter<Long>() {
|
||||||
|
@Override public Long read(JsonReader reader) throws IOException {
|
||||||
|
return reader.nextLong();
|
||||||
|
}
|
||||||
|
@Override public void write(JsonWriter writer, Long value) throws IOException {
|
||||||
|
writer.value(value.toString());
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ExclusionStrategy createExclusionStrategy() {
|
private static ExclusionStrategy createExclusionStrategy() {
|
||||||
|
|
|
@ -675,9 +675,6 @@ public final class GsonBuilder {
|
||||||
addTypeAdaptersForDate(datePattern, dateStyle, timeStyle, customSerializers,
|
addTypeAdaptersForDate(datePattern, dateStyle, timeStyle, customSerializers,
|
||||||
customDeserializers);
|
customDeserializers);
|
||||||
|
|
||||||
customSerializers.registerIfAbsent(DefaultTypeAdapters.getDefaultSerializers(
|
|
||||||
serializeSpecialFloatingPointValues, longSerializationPolicy));
|
|
||||||
|
|
||||||
customDeserializers.registerIfAbsent(DefaultTypeAdapters.getDefaultDeserializers());
|
customDeserializers.registerIfAbsent(DefaultTypeAdapters.getDefaultDeserializers());
|
||||||
|
|
||||||
ParameterizedTypeHandlerMap<InstanceCreator<?>> customInstanceCreators =
|
ParameterizedTypeHandlerMap<InstanceCreator<?>> customInstanceCreators =
|
||||||
|
@ -690,12 +687,11 @@ public final class GsonBuilder {
|
||||||
|
|
||||||
MappedObjectConstructor objConstructor = new MappedObjectConstructor(customInstanceCreators);
|
MappedObjectConstructor objConstructor = new MappedObjectConstructor(customInstanceCreators);
|
||||||
|
|
||||||
Gson gson = new Gson(new DisjunctionExclusionStrategy(deserializationStrategies),
|
return new Gson(new DisjunctionExclusionStrategy(deserializationStrategies),
|
||||||
new DisjunctionExclusionStrategy(serializationStrategies),
|
new DisjunctionExclusionStrategy(serializationStrategies),
|
||||||
fieldNamingPolicy, objConstructor, serializeNulls,
|
fieldNamingPolicy, objConstructor, serializeNulls,
|
||||||
customSerializers, customDeserializers, generateNonExecutableJson, escapeHtmlChars,
|
customSerializers, customDeserializers, generateNonExecutableJson, escapeHtmlChars,
|
||||||
prettyPrinting);
|
prettyPrinting, serializeSpecialFloatingPointValues, longSerializationPolicy);
|
||||||
return gson;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void addTypeAdaptersForDate(String datePattern, int dateStyle, int timeStyle,
|
private static void addTypeAdaptersForDate(String datePattern, int dateStyle, int timeStyle,
|
||||||
|
|
|
@ -106,7 +106,7 @@ public final class TypeAdapters {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <T> TypeAdapter.Factory newFactory(
|
public static <T> TypeAdapter.Factory newFactory(
|
||||||
final Class<T> unboxed, final Class<T> boxed, final TypeAdapter<T> typeAdapter) {
|
final Class<T> unboxed, final Class<T> boxed, final TypeAdapter<T> typeAdapter) {
|
||||||
return new TypeAdapter.Factory() {
|
return new TypeAdapter.Factory() {
|
||||||
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
|
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
|
||||||
|
|
|
@ -42,7 +42,7 @@ public class FunctionWithInternalDependenciesTest extends TestCase {
|
||||||
new MappedObjectConstructor(DefaultTypeAdapters.getDefaultInstanceCreators()),
|
new MappedObjectConstructor(DefaultTypeAdapters.getDefaultInstanceCreators()),
|
||||||
false, DefaultTypeAdapters.getDefaultSerializers(),
|
false, DefaultTypeAdapters.getDefaultSerializers(),
|
||||||
DefaultTypeAdapters.getDefaultDeserializers(), Gson.DEFAULT_JSON_NON_EXECUTABLE, true,
|
DefaultTypeAdapters.getDefaultDeserializers(), Gson.DEFAULT_JSON_NON_EXECUTABLE, true,
|
||||||
false);
|
false, false, LongSerializationPolicy.DEFAULT);
|
||||||
assertEquals("{}", gson.toJson(new ClassWithNoFields() {
|
assertEquals("{}", gson.toJson(new ClassWithNoFields() {
|
||||||
// empty anonymous class
|
// empty anonymous class
|
||||||
}));
|
}));
|
||||||
|
|
Loading…
Reference in New Issue
Block a user