diff options
author | Takashi Kokubun <takashikkbn@gmail.com> | 2023-12-01 09:53:44 -0800 |
---|---|---|
committer | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2023-12-05 12:04:08 +0900 |
commit | e6b35e8a6d70892037503d74cec2657b2b8bd116 (patch) | |
tree | aa9d4d51e0581776660d3ea772e5a91d55693bea | |
parent | a22ed8943859963c67533bb0edc13a27bfdac00c (diff) | |
download | ruby-e6b35e8a6d70892037503d74cec2657b2b8bd116.tar.gz |
[flori/json] Overload kwargs in JSON.dump
https://github.com/flori/json/commit/936f280f9f
-rw-r--r-- | ext/json/lib/json/common.rb | 11 | ||||
-rwxr-xr-x | test/json/json_generator_test.rb | 4 |
2 files changed, 12 insertions, 3 deletions
diff --git a/ext/json/lib/json/common.rb b/ext/json/lib/json/common.rb index f51f02d4df..5ba2aade0f 100644 --- a/ext/json/lib/json/common.rb +++ b/ext/json/lib/json/common.rb @@ -615,13 +615,17 @@ module JSON if anIO and limit.nil? anIO = anIO.to_io if anIO.respond_to?(:to_io) unless anIO.respond_to?(:write) - limit = anIO + if kwargs.nil? and anIO.is_a?(Hash) + kwargs = anIO + else + limit = anIO + end anIO = nil end end opts = JSON.dump_default_options opts = opts.merge(:max_nesting => limit) if limit - merge_dump_options(opts, **kwargs) if kwargs + opts = merge_dump_options(opts, **kwargs) if kwargs result = generate(obj, opts) if anIO anIO.write result @@ -641,7 +645,8 @@ module JSON private def merge_dump_options(opts, strict: NOT_SET) - opts[:strict] = strict if NOT_SET != strict + opts = opts.merge(strict: strict) if NOT_SET != strict + opts end end diff --git a/test/json/json_generator_test.rb b/test/json/json_generator_test.rb index 0ce5142327..09e56e53ec 100755 --- a/test/json/json_generator_test.rb +++ b/test/json/json_generator_test.rb @@ -66,6 +66,10 @@ EOT assert_equal '{"a":1,"b":2}', dump(a: 1, b: 2) end + def test_dump_strict + assert_equal '{}', dump({}, strict: true) + end + def test_generate_pretty json = pretty_generate({}) assert_equal(<<'EOT'.chomp, json) |