Skip to content

Commit

Permalink
serialize NaN and +/-Infinity as strings in lenient mode (#72)
Browse files Browse the repository at this point in the history
Serialize NaN and +/-Infinity as strings in lenient mode
  • Loading branch information
tobias-hammerschmidt authored Nov 6, 2023
1 parent 237c997 commit fd2b990
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -433,12 +433,17 @@ public DefaultJsonWriter value(boolean value) {
/** {@inheritDoc} */
@Override
public DefaultJsonWriter value(double value) {
if (Double.isNaN(value) || Double.isInfinite(value)) {
final boolean isNaNorInfinity = Double.isNaN(value) || Double.isInfinite(value);
if (!lenient && isNaNorInfinity) {
throw new IllegalArgumentException("Numeric values must be finite, but was " + value);
}
writeDeferredName();
beforeValue(false);
out.append(value);
if (isNaNorInfinity) {
string(Double.toString(value));
} else {
out.append(value);
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,17 @@ public FastJsonWriter value(boolean value) {
/** {@inheritDoc} */
@Override
public FastJsonWriter value(double value) {
if (Double.isNaN(value) || Double.isInfinite(value)) {
final boolean isNaNorInfinity = Double.isNaN(value) || Double.isInfinite(value);
if (!lenient && isNaNorInfinity) {
throw new IllegalArgumentException("Numeric values must be finite, but was " + value);
}
writeDeferredName();
beforeValue(false);
out.append(value);
if (isNaNorInfinity) {
string(Double.toString(value));
} else {
out.append(value);
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ public void testDeserializeValue() {
assertDeserialization(-784.15454d, "\"-784.15454\"");
assertDeserialization(Double.MIN_VALUE, "4.9E-324");
assertDeserialization(Double.MAX_VALUE, "1.7976931348623157e+308");
assertTrue(Double.isNaN(deserialize("\"NaN\"")));
assertDeserialization(Double.NEGATIVE_INFINITY, "\"-Infinity\"");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,7 @@ public void testSerializeValue() {
? "1.7976931348623157e+308"
: "1.7976931348623157E308"),
Double.MAX_VALUE);
assertSerialization("\"NaN\"", Double.NaN);
assertSerialization("\"-Infinity\"", Double.NEGATIVE_INFINITY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.dominokit.jackson.server.deser.number;

import static org.junit.Assert.assertTrue;

import org.dominokit.jackson.deser.BaseNumberJsonDeserializer.DoubleJsonDeserializer;
import org.dominokit.jackson.server.deser.AbstractJsonDeserializerTest;
import org.junit.Test;
Expand All @@ -36,5 +38,7 @@ public void testDeserializeValue() {
assertDeserialization(-784.15454d, "\"-784.15454\"");
assertDeserialization(Double.MIN_VALUE, "4.9E-324");
assertDeserialization(Double.MAX_VALUE, "1.7976931348623157e+308");
assertTrue(Double.isNaN(deserialize("\"NaN\"")));
assertDeserialization(Double.NEGATIVE_INFINITY, "-Infinity");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ public void testSerializeValue() {
assertSerialization("-784.15454", -784.15454d);
assertSerialization("4.9E-324", Double.MIN_VALUE);
assertSerialization("1.7976931348623157E308", Double.MAX_VALUE);
assertSerialization("\"NaN\"", Double.NaN);
assertSerialization("\"-Infinity\"", Double.NEGATIVE_INFINITY);
}
}

0 comments on commit fd2b990

Please sign in to comment.