Skip to content

Commit a638d0e

Browse files
committed
Merge branch '2.x' into 3.1
2 parents 670c5bb + d01965d commit a638d0e

4 files changed

Lines changed: 102 additions & 6 deletions

File tree

release-notes/CREDITS-2.x

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ Henning Langhorst (@HenningLanghorst)
288288
when preceding column is quoted
289289
(2.18.7)
290290
291-
Shanchao Li (@tonguaroot)
291+
Shanchao Li (@tonghuaroot)
292292
293293
* Fixed #679: (toml) Validate integer length for hex/octal/binary radix literals
294294
(2.18.8)
@@ -313,3 +313,15 @@ Dmitry Bufistov (@dmitry-workato)
313313
* Requested #601: (csv) Reader should allow separating plain `nullValue`
314314
and quoted value `"nullValue"`
315315
(2.22.0)
316+
317+
EverNife (@EverNife)
318+
319+
* Reported #696: (toml) Write non-finite floating-point values as TOML tokens
320+
(`nan`/`inf`/`-inf`) instead of Java tokens (`NaN`/`Infinity`)
321+
(2.23.0)
322+
323+
seonwoojung (@seonwooj0810)
324+
325+
* Fixed #696: (toml) Write non-finite floating-point values as TOML tokens
326+
(`nan`/`inf`/`-inf`) instead of Java tokens (`NaN`/`Infinity`)
327+
(2.23.0)

release-notes/VERSION-2.x

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,20 @@ Active Maintainers:
1414
=== Releases ===
1515
------------------------------------------------------------------------
1616

17-
2.22.0 (not yet released)
17+
2.23.0 (not yet released)
18+
19+
#696: (toml) Write non-finite floating-point values as TOML tokens
20+
(`nan`/`inf`/`-inf`) instead of Java tokens (`NaN`/`Infinity`)
21+
(reported by @EverNife)
22+
(fix by @seonwooj0810)
23+
24+
2.22.0 (31-May-2026)
1825

1926
#601: (csv) Reader should allow separating plain `nullValue` and quoted
2027
value `"nullValue"`
2128
(requested by Dmitry B)
2229

23-
2.21.4 (not yet released)
30+
2.21.4 (28-May-2026)
2431

2532
#679: (toml) Validate integer length for hex/octal/binary radix literals
2633
(fix by @tonghuaroot)
@@ -93,7 +100,7 @@ No changes since 2.19.0
93100
#554: (csv) Enforce, document thread-safety of `CsvSchema`
94101
(requested by Gili T)
95102

96-
2.18.8 (not yet released)
103+
2.18.8 (28-May-2026)
97104

98105
#679: (toml) Validate integer length for hex/octal/binary radix literals
99106
(fix by @tonghuaroot)

toml/src/main/java/tools/jackson/dataformat/toml/TomlGenerator.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,17 +603,37 @@ public JsonGenerator writeNumber(BigInteger v) throws JacksonException {
603603
@Override
604604
public JsonGenerator writeNumber(double d) throws JacksonException {
605605
_verifyValueWrite("write number");
606-
_writeRaw(String.valueOf(d));
606+
_writeRaw(_nonFiniteTomlToken(d, String.valueOf(d)));
607607
return writeValueEnd();
608608
}
609609

610610
@Override
611611
public JsonGenerator writeNumber(float f) throws JacksonException {
612612
_verifyValueWrite("write number");
613-
_writeRaw(String.valueOf(f));
613+
_writeRaw(_nonFiniteTomlToken(f, String.valueOf(f)));
614614
return writeValueEnd();
615615
}
616616

617+
/**
618+
* Maps a non-finite floating-point value to the TOML float token
619+
* ({@code nan}, {@code inf} or {@code -inf}); finite values are written
620+
* using the supplied Java text form. {@code String.valueOf(...)} would
621+
* otherwise emit {@code NaN} / {@code Infinity} / {@code -Infinity}, which
622+
* are not valid TOML and cannot be read back by the parser.
623+
*/
624+
private static String _nonFiniteTomlToken(double d, String finiteForm) {
625+
if (Double.isNaN(d)) {
626+
return "nan";
627+
}
628+
if (d == Double.POSITIVE_INFINITY) {
629+
return "inf";
630+
}
631+
if (d == Double.NEGATIVE_INFINITY) {
632+
return "-inf";
633+
}
634+
return finiteForm;
635+
}
636+
617637
@Override
618638
public JsonGenerator writeNumber(BigDecimal dec) throws JacksonException {
619639
if (dec == null) {

toml/src/test/java/tools/jackson/dataformat/toml/TomlGeneratorTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
import static org.junit.jupiter.api.Assertions.assertEquals;
99
import static org.junit.jupiter.api.Assertions.assertThrows;
10+
import static org.junit.jupiter.api.Assertions.assertTrue;
1011

1112
import tools.jackson.core.JsonGenerator;
13+
import tools.jackson.databind.JsonNode;
1214
import tools.jackson.databind.ObjectMapper;
1315

1416
public class TomlGeneratorTest extends TomlMapperTestBase {
@@ -48,6 +50,61 @@ public void floats() {
4850
assertEquals("abc = 1.23\n", w.toString());
4951
}
5052

53+
// [dataformats-text#696]: non-finite floats must be written as the TOML
54+
// tokens `nan` / `inf` / `-inf` (not Java's `NaN` / `Infinity`) so the
55+
// writer's own output can be parsed back.
56+
@Test
57+
public void nonFiniteDoubles() throws IOException {
58+
assertEquals("abc = nan\n", _writeDouble(Double.NaN));
59+
assertEquals("abc = inf\n", _writeDouble(Double.POSITIVE_INFINITY));
60+
assertEquals("abc = -inf\n", _writeDouble(Double.NEGATIVE_INFINITY));
61+
}
62+
63+
@Test
64+
public void nonFiniteFloats() throws IOException {
65+
assertEquals("abc = nan\n", _writeFloat(Float.NaN));
66+
assertEquals("abc = inf\n", _writeFloat(Float.POSITIVE_INFINITY));
67+
assertEquals("abc = -inf\n", _writeFloat(Float.NEGATIVE_INFINITY));
68+
}
69+
70+
// Written non-finite values must round-trip through the same mapper.
71+
@Test
72+
public void nonFiniteRoundTrip() throws IOException {
73+
TomlMapper mapper = newTomlMapper();
74+
for (double d : new double[] {
75+
Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY }) {
76+
String toml = mapper.writeValueAsString(java.util.Collections.singletonMap("x", d));
77+
JsonNode node = mapper.readTree(toml).get("x");
78+
if (Double.isNaN(d)) {
79+
assertTrue(Double.isNaN(node.doubleValue()), "expected NaN from: " + toml);
80+
} else {
81+
assertEquals(d, node.doubleValue(), 0.0, "round-trip failed for: " + toml);
82+
}
83+
}
84+
}
85+
86+
private String _writeDouble(double d) throws IOException {
87+
StringWriter w = new StringWriter();
88+
try (JsonGenerator generator = newTomlMapper().createGenerator(w)) {
89+
generator.writeStartObject();
90+
generator.writeName("abc");
91+
generator.writeNumber(d);
92+
generator.writeEndObject();
93+
}
94+
return w.toString();
95+
}
96+
97+
private String _writeFloat(float f) throws IOException {
98+
StringWriter w = new StringWriter();
99+
try (JsonGenerator generator = newTomlMapper().createGenerator(w)) {
100+
generator.writeStartObject();
101+
generator.writeName("abc");
102+
generator.writeNumber(f);
103+
generator.writeEndObject();
104+
}
105+
return w.toString();
106+
}
107+
51108
@Test
52109
public void stringNormal() {
53110
StringWriter w = new StringWriter();

0 commit comments

Comments
 (0)