From af799140028d9600c5c799356da9a4f1f31b2e7e Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Sat, 10 May 2025 11:31:33 +0200 Subject: [PATCH] [ruby/json] Favor decimal notation over scientific notation for floats e.g. ``` JSON.dump(1746861937.7842371) ``` master: ``` "1.https://github.com/ruby/json/commit/746861937784+9" ``` This branch and older json versions: ``` https://github.com/ruby/json/commit/1746861937.7842371 ``` In the end it's shorter, and according to `canada.json` benchmark performance is the same. https://github.com/ruby/json/commit/866f72a437 --- ext/json/vendor/fpconv.c | 2 +- test/json/json_generator_test.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/ext/json/vendor/fpconv.c b/ext/json/vendor/fpconv.c index 854cae288c..440759e5a0 100644 --- a/ext/json/vendor/fpconv.c +++ b/ext/json/vendor/fpconv.c @@ -340,7 +340,7 @@ static int emit_digits(char* digits, int ndigits, char* dest, int K, bool neg) } /* write decimal w/o scientific notation */ - if(K < 0 && (K > -7 || exp < 4)) { + if(K < 0 && (K > -7 || exp < 10)) { int offset = ndigits - absv(K); /* fp < 1.0 -> write leading zero */ if(offset <= 0) { diff --git a/test/json/json_generator_test.rb b/test/json/json_generator_test.rb index 3192b555a3..55a3065ae5 100755 --- a/test/json/json_generator_test.rb +++ b/test/json/json_generator_test.rb @@ -771,6 +771,14 @@ class JSONGeneratorTest < Test::Unit::TestCase values = [-1.0, 1.0, 0.0, 12.2, 7.5 / 3.2, 12.0, 100.0, 1000.0] expecteds = ["-1.0", "1.0", "0.0", "12.2", "2.34375", "12.0", "100.0", "1000.0"] + if RUBY_ENGINE == "jruby" + values << 1746861937.7842371 + expecteds << "1.7468619377842371E9" + else + values << 1746861937.7842371 + expecteds << "1746861937.7842371" + end + values.zip(expecteds).each do |value, expected| assert_equal expected, value.to_json end