You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Upstream Bug: JsonNumber.of(double) hardcodes decimalOffset causing toLong to fail #118
The JsonNumber.of(double) factory method has a bug where it hardcodes decimalOffset=0 and exponentOffset=0 when creating the JsonNumberImpl. This causes toLong() to fail for integral doubles like 123.0.
decimalOffset should be 3 (position of .) but is passed as 0
This causes initNumLong() in JsonNumberImpl to misparse the number
Reproduction
varjn = JsonNumber.of(123.0);
jn.toString(); // "123.0" - OKjn.toDouble(); // 123.0 - OKjn.toLong(); // THROWS: "123.0 cannot be represented as a long"// EXPECTED: 123L (per API docs, "123.0" should be convertible)
Comparison with Json.parse()
varparsed = Json.parse("123.0");
parsed.toLong(); // 123L - WORKS correctly
The difference is that Json.parse() correctly computes the decimalOffset during parsing.
Suggested Fix
Option 1 - Reuse JsonNumber.of(String) which delegates to parser:
staticJsonNumberof(doublenum) {
if (!Double.isFinite(num)) {
thrownewIllegalArgumentException("Not a valid JSON number");
}
returnJsonNumber.of(Double.toString(num));
}
This bug exists in OpenJDK jdk-sandbox json branch at commit 91a479d. The upstream tests in TestJsonNumber.java do not cover this case - they test of(double).toDouble() but never of(double).toLong() for integral doubles.
Affected Versions
jdk-sandbox json branch (upstream)
java.util.json.Java21 backport 0.1.9 (this repo, synced from upstream)
This is an upstream bug in OpenJDK jdk-sandbox. Per OpenJDK process, bugs should be reported via the core-libs-dev mailing list, not via GitHub issues on jdk-sandbox.
Local Workaround Applied: We've applied a local fix in this backport (delegating of(double) to of(String)) until upstream addresses this. The fix is documented in README.md under "Upstream Bug Fixes".
This bug has been fixed in the backport. JsonNumber.of(double) now delegates to JsonNumber.of(String) which uses Json.parse() to correctly compute the decimalOffset and exponentOffset:
staticJsonNumberof(doublenum) {
if (!Double.isFinite(num)) {
thrownewIllegalArgumentException("Not a valid JSON number");
}
// Delegate to of(String) which correctly computes offsets via Json.parse()returnof(Double.toString(num));
}
Verified: JsonNumber.of(123.0).toLong() now correctly returns 123L. All 27 tests pass.
This fix was included in the sync to upstream commit c1a4f80 (release 2026.02.05). The upstream bug remains unfixed in jdk-sandbox.
Activity
simbo1905 commentedon Jan 26, 2026
Update: Upstream Bug Reporting
This is an upstream bug in OpenJDK jdk-sandbox. Per OpenJDK process, bugs should be reported via the core-libs-dev mailing list, not via GitHub issues on jdk-sandbox.
Local Workaround Applied: We've applied a local fix in this backport (delegating
of(double)toof(String)) until upstream addresses this. The fix is documented in README.md under "Upstream Bug Fixes".If anyone wishes to report this upstream:
JsonNumber.of(double)methodIssue #118 fix upstream bug in JsonNumber.of(double) hardcoding offsets
Issue #118 bump expected test count from 507 to 511
simbo1905 commentedon May 19, 2026
Fixed
This bug has been fixed in the backport.
JsonNumber.of(double)now delegates toJsonNumber.of(String)which usesJson.parse()to correctly compute thedecimalOffsetandexponentOffset:Verified:
JsonNumber.of(123.0).toLong()now correctly returns123L. All 27 tests pass.This fix was included in the sync to upstream commit
c1a4f80(release2026.02.05). The upstream bug remains unfixed in jdk-sandbox.Issue #145 port upstream tests, verify #118 closed, bump frontier docs