From 41f2ecc874fa6d572996425f0df2f169086f52fd Mon Sep 17 00:00:00 2001 From: Evan Brooks Date: Fri, 6 Feb 2026 23:54:01 -0400 Subject: [PATCH] Fix output formatting differences between JRuby and Ruby when converting Float to JSON --- java/src/json/ext/Generator.java | 6 ++++-- test/json/json_generator_test.rb | 7 +------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/java/src/json/ext/Generator.java b/java/src/json/ext/Generator.java index ea9e6d08..ddea2c1f 100644 --- a/java/src/json/ext/Generator.java +++ b/java/src/json/ext/Generator.java @@ -415,12 +415,14 @@ static void generateFloat(ThreadContext context, Session session, RubyFloat obje return; } } - + throw Utils.buildGeneratorError(context, object, object + " not allowed in JSON").toThrowable(); } } - buffer.write(Double.toString(value).getBytes(UTF_8)); + ByteList tmp = new ByteList(24); + RubyFloat.formatFloat(object, tmp); + buffer.write(tmp.getUnsafeBytes(), tmp.getBegin(), tmp.getRealSize()); } private static final byte[] EMPTY_ARRAY_BYTES = "[]".getBytes(); diff --git a/test/json/json_generator_test.rb b/test/json/json_generator_test.rb index d7c4173e..11ce53d7 100755 --- a/test/json/json_generator_test.rb +++ b/test/json/json_generator_test.rb @@ -998,12 +998,7 @@ def test_json_generate_float assert_float_roundtrip "12.0", 12.0 assert_float_roundtrip "100.0", 100.0 assert_float_roundtrip "1000.0", 1000.0 - - if RUBY_ENGINE == "jruby" - assert_float_roundtrip "1.7468619377842371E9", 1746861937.7842371 - else - assert_float_roundtrip "1746861937.7842371", 1746861937.7842371 - end + assert_float_roundtrip "1746861937.7842371", 1746861937.7842371 if RUBY_ENGINE == "ruby" assert_float_roundtrip "100000000000000.0", 100000000000000.0