Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 20 additions & 8 deletions src/main/java/net/openhft/chronicle/wire/JSONWire.java
Original file line number Diff line number Diff line change
Expand Up @@ -1193,29 +1193,41 @@ public void writeComment(@NotNull CharSequence s) {
}

/**
* Write a special double value (e.g. NaN) as a string to the given bytes.
* Write a double value to the given bytes. IEEE754 non-finite values (NaN, Infinity) are
* quoted as JSON strings since they have no numeric representation in JSON. Finite values,
* including large magnitudes, are written as unquoted JSON numbers.
*
* @param bytes The bytes to append the stringified double value to
* @param value The double value to convert to a string
*/
@Override
protected void writeSpecialDoubleValueToBytes(Bytes<?> bytes, double value) {
bytes.append('"');
bytes.append(Double.toString(value));
bytes.append('"');
if (Double.isNaN(value) || Double.isInfinite(value)) {
bytes.append('"');
bytes.append(Double.toString(value));
bytes.append('"');
} else {
bytes.append(Double.toString(value));
}
}

/**
* Write a special float value (e.g. NaN) as a string to the given bytes.
* Write a float value to the given bytes. IEEE754 non-finite values (NaN, Infinity) are
* quoted as JSON strings since they have no numeric representation in JSON. Finite values,
* including large magnitudes, are written as unquoted JSON numbers.
*
* @param bytes The bytes to append the stringified float value to
* @param value The float value to convert to a string
*/
@Override
protected void writeSpecialFloatValueToBytes(Bytes<?> bytes, float value) {
bytes.append('"');
bytes.append(Float.toString(value));
bytes.append('"');
if (Float.isNaN(value) || Float.isInfinite(value)) {
bytes.append('"');
bytes.append(Float.toString(value));
bytes.append('"');
} else {
bytes.append(Float.toString(value));
}
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ void floatRoundTrip(FloatTestInput floatTestInput) {
Assertions.assertTrue(floatTestInput.expectOutputFloatToMatchThisPredicate.test(object.value));
}

@ParameterizedTest
@MethodSource("largeFiniteFloatInputs")
void largeFiniteFloatSerialiseAsJsonNumber(float value) {
String json = toJson(value);
Assertions.assertFalse(json.startsWith("\""),
"Finite float " + value + " must serialise as a JSON number, not a quoted string literal");
}

@ParameterizedTest
@MethodSource("largeFiniteDoubleInputs")
void largeFiniteDoubleSerialiseAsJsonNumber(double value) {
String json = toJson(value);
Assertions.assertFalse(json.startsWith("\""),
"Finite double " + value + " must serialise as a JSON number, not a quoted string literal");
}

private static Stream<Float> largeFiniteFloatInputs() {
return Stream.of(5_000_000.0f, 1e7f, -5_000_000.0f, 1.23456789e10f);
}

private static Stream<Double> largeFiniteDoubleInputs() {
return Stream.of(1e15, 1e16, -5e15, 1.23456789e20);
}

private static Stream<DoubleTestInput> doubleTestInputs() {
return Stream.of(
new DoubleTestInput(Double.NaN, "NaN", Double::isNaN),
Expand Down