Repository navigation
Reject NaN and Infinity when writing OptionalDouble by default #3193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
784a24b
06ce313
133a028
03b19ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| package com.google.gson.functional; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
| import static org.junit.Assert.assertThrows; | ||
|
|
||
| import com.google.gson.Gson; | ||
| import com.google.gson.GsonBuilder; | ||
|
|
@@ -174,6 +175,104 @@ public void testOptionalDouble() { | |
| .isEqualTo(OptionalDouble.empty()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOptionalDoubleSpecialValuesRejectedByDefault() { | ||
| IllegalArgumentException e = | ||
| assertThrows( | ||
| IllegalArgumentException.class, () -> gson.toJson(OptionalDouble.of(Double.NaN))); | ||
| assertThat(e) | ||
| .hasMessageThat() | ||
| .contains("NaN is not a valid double value as per JSON specification"); | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> gson.toJson(OptionalDouble.of(Double.POSITIVE_INFINITY))); | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> gson.toJson(OptionalDouble.of(Double.NEGATIVE_INFINITY))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOptionalDoubleSpecialValuesWhenEnabled() { | ||
| Gson specialValuesGson = new GsonBuilder().serializeSpecialFloatingPointValues().create(); | ||
| assertThat(specialValuesGson.toJson(OptionalDouble.of(Double.NaN))) | ||
| .isEqualTo("{\"isPresent\":true,\"value\":NaN}"); | ||
| assertThat(specialValuesGson.toJson(OptionalDouble.of(Double.POSITIVE_INFINITY))) | ||
| .isEqualTo("{\"isPresent\":true,\"value\":Infinity}"); | ||
| assertThat(specialValuesGson.toJson(OptionalDouble.of(Double.NEGATIVE_INFINITY))) | ||
| .isEqualTo("{\"isPresent\":true,\"value\":-Infinity}"); | ||
| Gson lenientSpecialValuesGson = | ||
| new GsonBuilder() | ||
| .serializeSpecialFloatingPointValues() | ||
| .setStrictness(Strictness.LENIENT) | ||
| .create(); | ||
| assertThat( | ||
| lenientSpecialValuesGson.fromJson( | ||
| specialValuesGson.toJson(OptionalDouble.of(Double.NaN)), OptionalDouble.class)) | ||
| .isEqualTo(OptionalDouble.of(Double.NaN)); | ||
| assertThat( | ||
| lenientSpecialValuesGson.fromJson( | ||
| specialValuesGson.toJson(OptionalDouble.of(Double.POSITIVE_INFINITY)), | ||
| OptionalDouble.class)) | ||
| .isEqualTo(OptionalDouble.of(Double.POSITIVE_INFINITY)); | ||
| assertThat( | ||
| lenientSpecialValuesGson.fromJson( | ||
| specialValuesGson.toJson(OptionalDouble.of(Double.NEGATIVE_INFINITY)), | ||
| OptionalDouble.class)) | ||
| .isEqualTo(OptionalDouble.of(Double.NEGATIVE_INFINITY)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOptionalDoubleWithCustomBoxedDoubleAdapter() { | ||
| // OptionalDouble reads and writes through the Gson-configured double.class adapter, | ||
| // mirroring what ReflectiveTypeAdapterFactory does for a primitive double field. A custom | ||
| // adapter registered only for Double.class therefore does not apply, just as it does not | ||
| // apply to double fields or double[] elements. | ||
| TypeAdapter<Double> customAdapter = | ||
| new TypeAdapter<Double>() { | ||
| @Override | ||
| public Double read(JsonReader in) throws IOException { | ||
| return in.nextDouble() - 1; | ||
| } | ||
|
|
||
| @Override | ||
| public void write(JsonWriter out, Double value) throws IOException { | ||
| out.value(value + 1); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a suggestion: Could alternatively for That might make the intention even clearer.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applied in d444349 - the |
||
| } | ||
| }; | ||
| Gson boxedCustomGson = | ||
| new GsonBuilder().registerTypeAdapter(Double.class, customAdapter).create(); | ||
| assertThat(boxedCustomGson.toJson(OptionalDouble.of(41.0))) | ||
| .isEqualTo("{\"isPresent\":true,\"value\":41.0}"); | ||
| assertThat( | ||
| boxedCustomGson.fromJson("{\"isPresent\":true,\"value\":41.0}", OptionalDouble.class)) | ||
| .isEqualTo(OptionalDouble.of(41.0)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOptionalDoubleWithCustomPrimitiveDoubleAdapter() { | ||
| // A custom adapter registered for double.class applies to reading and writing symmetrically. | ||
| TypeAdapter<Double> customAdapter = | ||
| new TypeAdapter<Double>() { | ||
| @Override | ||
| public Double read(JsonReader in) throws IOException { | ||
| return in.nextDouble() - 1; | ||
| } | ||
|
|
||
| @Override | ||
| public void write(JsonWriter out, Double value) throws IOException { | ||
| out.value(value + 1); | ||
| } | ||
| }; | ||
| Gson primitiveCustomGson = | ||
| new GsonBuilder().registerTypeAdapter(double.class, customAdapter).create(); | ||
| assertThat(primitiveCustomGson.toJson(OptionalDouble.of(41.0))) | ||
| .isEqualTo("{\"isPresent\":true,\"value\":42.0}"); | ||
| assertThat( | ||
| primitiveCustomGson.fromJson( | ||
| "{\"isPresent\":true,\"value\":42.0}", OptionalDouble.class)) | ||
| .isEqualTo(OptionalDouble.of(41.0)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCustomAdapterPrecedence() { | ||
| TypeAdapter<OptionalInt> customAdapter = | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delegating
write()togson.getAdapter(Double.class)whileread()still callsin.nextDouble()makes round-tripping asymmetric whenever a customTypeAdapter<Double>is registered onGsonBuilder(writes go through the custom adapter, reads bypass it). Also,ReflectiveTypeAdapterFactoryresolves the underlyingdoublefield withdouble.classrather thanDouble.class. If custom adapters are meant to apply here,read()should delegate to the samedouble.classadapter (guarding againstnull); otherwise, checkingserializeSpecialFloatingPointValuesdirectly avoids routingOptionalDoublethrough unrelated customDoubleadapters.