Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
} else if (rawType == OptionalLong.class) {
adapter = OPTIONAL_LONG;
} else if (rawType == OptionalDouble.class) {
adapter = OPTIONAL_DOUBLE;
adapter = new OptionalDoubleAdapter(gson.getAdapter(Double.class)).nullSafe();
}
// Safe because adapter matches rawType
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -177,41 +177,48 @@ public void write(JsonWriter out, OptionalLong value) throws IOException {
}
}.nullSafe();

private static final TypeAdapter<OptionalDouble> OPTIONAL_DOUBLE =
new TypeAdapter<OptionalDouble>() {
@Override
public OptionalDouble read(JsonReader in) throws IOException {
boolean isPresent = false;
double value = 0;
in.beginObject();
while (in.hasNext()) {
String name = in.nextName();
switch (name) {
case "isPresent":
isPresent = in.nextBoolean();
break;
case "value":
value = in.nextDouble();
break;
default:
in.skipValue();
}
}
in.endObject();
return isPresent ? OptionalDouble.of(value) : OptionalDouble.empty();
}
private static final class OptionalDoubleAdapter extends TypeAdapter<OptionalDouble> {
private final TypeAdapter<Double> doubleAdapter;

@Override
public void write(JsonWriter out, OptionalDouble value) throws IOException {
out.beginObject();
out.name("isPresent");
out.value(value.isPresent());
out.name("value");
// Note: in OpenJDK, OptionalDouble.EMPTY has value = Double.NaN, which causes
// ReflectiveTypeAdapterFactory to fail unless serializeSpecialFloatingPointValues()
// is enabled. Writing 0.0 when empty avoids that failure.
out.value(value.orElse(0));
out.endObject();
OptionalDoubleAdapter(TypeAdapter<Double> doubleAdapter) {
this.doubleAdapter = doubleAdapter;
}

@Override
public OptionalDouble read(JsonReader in) throws IOException {
boolean isPresent = false;
double value = 0;
in.beginObject();
while (in.hasNext()) {
String name = in.nextName();
switch (name) {
case "isPresent":
isPresent = in.nextBoolean();
break;
case "value":
value = in.nextDouble();
break;
default:
in.skipValue();
}
}.nullSafe();
}
in.endObject();
return isPresent ? OptionalDouble.of(value) : OptionalDouble.empty();
}

@Override
public void write(JsonWriter out, OptionalDouble value) throws IOException {
out.beginObject();
out.name("isPresent");
out.value(value.isPresent());
out.name("value");
// Delegate to the Gson-configured Double adapter, so that NaN and Infinity are rejected
// by default as they are for plain double values, and are only written when
// serializeSpecialFloatingPointValues() is enabled.
// Note: in OpenJDK, OptionalDouble.EMPTY has value = Double.NaN; writing 0.0 when empty
// avoids that failure.
doubleAdapter.write(out, value.orElse(0));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delegating write() to gson.getAdapter(Double.class) while read() still calls in.nextDouble() makes round-tripping asymmetric whenever a custom TypeAdapter<Double> is registered on GsonBuilder (writes go through the custom adapter, reads bypass it). Also, ReflectiveTypeAdapterFactory resolves the underlying double field with double.class rather than Double.class. If custom adapters are meant to apply here, read() should delegate to the same double.class adapter (guarding against null); otherwise, checking serializeSpecialFloatingPointValues directly avoids routing OptionalDouble through unrelated custom Double adapters.

out.endObject();
}
}
}
28 changes: 28 additions & 0 deletions gson/src/test/java/com/google/gson/functional/JdkOptionalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import static org.junit.Assert.assertThrows;
import org.junit.Test;

public final class JdkOptionalTest {
Expand Down Expand Up @@ -174,6 +175,33 @@ 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}");
}

@Test
public void testCustomAdapterPrecedence() {
TypeAdapter<OptionalInt> customAdapter =
Expand Down
Loading