Restore support for serializeSpecialFloatingPointValues.
This commit is contained in:
parent
6e3bf07300
commit
1885ba7dec
@ -212,7 +212,10 @@ public final class Gson {
|
||||
.withoutDefaultFactories()
|
||||
.factory(TypeAdapters.BOOLEAN_FACTORY)
|
||||
.factory(TypeAdapters.INTEGER_FACTORY)
|
||||
.factory(TypeAdapters.DOUBLE_FACTORY)
|
||||
.factory(TypeAdapters.newFactory(double.class, Double.class,
|
||||
doubleAdapter(serializeSpecialFloatingPointValues)))
|
||||
.factory(TypeAdapters.newFactory(float.class, Float.class,
|
||||
floatAdapter(serializeSpecialFloatingPointValues)))
|
||||
.factory(TypeAdapters.newFactory(long.class, Long.class,
|
||||
longAdapter(longSerializationPolicy)))
|
||||
.factory(TypeAdapters.STRING_FACTORY)
|
||||
@ -225,6 +228,44 @@ public final class Gson {
|
||||
this.miniGson = builder.build();
|
||||
}
|
||||
|
||||
private TypeAdapter<Double> doubleAdapter(boolean serializeSpecialFloatingPointValues) {
|
||||
if (serializeSpecialFloatingPointValues) {
|
||||
return TypeAdapters.DOUBLE;
|
||||
}
|
||||
return new TypeAdapter<Double>() {
|
||||
@Override public Double read(JsonReader reader) throws IOException {
|
||||
return reader.nextDouble();
|
||||
}
|
||||
@Override public void write(JsonWriter writer, Double value) throws IOException {
|
||||
checkValidFloatingPoint(value);
|
||||
writer.value(value);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private TypeAdapter<Float> floatAdapter(boolean serializeSpecialFloatingPointValues) {
|
||||
if (serializeSpecialFloatingPointValues) {
|
||||
return TypeAdapters.FLOAT;
|
||||
}
|
||||
return new TypeAdapter<Float>() {
|
||||
@Override public Float read(JsonReader reader) throws IOException {
|
||||
return (float) reader.nextDouble();
|
||||
}
|
||||
@Override public void write(JsonWriter writer, Float value) throws IOException {
|
||||
checkValidFloatingPoint(value);
|
||||
writer.value(value);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private void checkValidFloatingPoint(double value) {
|
||||
if (Double.isNaN(value) || Double.isInfinite(value)) {
|
||||
throw new IllegalArgumentException(value
|
||||
+ " is not a valid double value as per JSON specification. To override this"
|
||||
+ " behavior, use GsonBuilder.serializeSpecialDoubleValues() method.");
|
||||
}
|
||||
}
|
||||
|
||||
private TypeAdapter<Long> longAdapter(LongSerializationPolicy longSerializationPolicy) {
|
||||
if (longSerializationPolicy == LongSerializationPolicy.DEFAULT) {
|
||||
return TypeAdapters.LONG;
|
||||
|
@ -53,6 +53,7 @@ public final class MiniGson {
|
||||
factories.add(TypeAdapters.BOOLEAN_FACTORY);
|
||||
factories.add(TypeAdapters.INTEGER_FACTORY);
|
||||
factories.add(TypeAdapters.DOUBLE_FACTORY);
|
||||
factories.add(TypeAdapters.FLOAT_FACTORY);
|
||||
factories.add(TypeAdapters.LONG_FACTORY);
|
||||
factories.add(TypeAdapters.STRING_FACTORY);
|
||||
}
|
||||
|
@ -75,6 +75,18 @@ public final class TypeAdapters {
|
||||
public static final TypeAdapter.Factory DOUBLE_FACTORY
|
||||
= newFactory(double.class, Double.class, DOUBLE);
|
||||
|
||||
public static final TypeAdapter<Float> FLOAT = new TypeAdapter<Float>() {
|
||||
public Float read(JsonReader reader) throws IOException {
|
||||
return (float) reader.nextDouble();
|
||||
}
|
||||
public void write(JsonWriter writer, Float value) throws IOException {
|
||||
writer.value(value);
|
||||
}
|
||||
};
|
||||
|
||||
public static final TypeAdapter.Factory FLOAT_FACTORY
|
||||
= newFactory(float.class, Float.class, FLOAT);
|
||||
|
||||
public static final TypeAdapter<String> STRING = new TypeAdapter<String>() {
|
||||
public String read(JsonReader reader) throws IOException {
|
||||
return reader.nextString();
|
||||
|
Loading…
Reference in New Issue
Block a user