Skip to content

Commit

Permalink
[flori/json] JSON.dump: handle unenclosed hashes regression
Browse files Browse the repository at this point in the history
Fix: flori/json#553

We can never add keyword arguments to `dump` otherwise
existing code using unenclosed hash will break.

flori/json@8e0076a3f2
  • Loading branch information
byroot authored and hsbt committed Dec 5, 2023
1 parent 5a2d70e commit a22ed89
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 8 additions & 2 deletions ext/json/lib/json/common.rb
Expand Up @@ -611,7 +611,7 @@ class << self
# puts File.read(path)
# Output:
# {"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}
def dump(obj, anIO = nil, limit = nil, strict: NOT_SET)
def dump(obj, anIO = nil, limit = nil, kwargs = nil)
if anIO and limit.nil?
anIO = anIO.to_io if anIO.respond_to?(:to_io)
unless anIO.respond_to?(:write)
Expand All @@ -621,7 +621,7 @@ def dump(obj, anIO = nil, limit = nil, strict: NOT_SET)
end
opts = JSON.dump_default_options
opts = opts.merge(:max_nesting => limit) if limit
opts[:strict] = strict if NOT_SET != strict
merge_dump_options(opts, **kwargs) if kwargs
result = generate(obj, opts)
if anIO
anIO.write result
Expand All @@ -637,6 +637,12 @@ def dump(obj, anIO = nil, limit = nil, strict: NOT_SET)
def self.iconv(to, from, string)
string.encode(to, from)
end

private

def merge_dump_options(opts, strict: NOT_SET)
opts[:strict] = strict if NOT_SET != strict
end
end

module ::Kernel
Expand Down
4 changes: 4 additions & 0 deletions test/json/json_generator_test.rb
Expand Up @@ -62,6 +62,10 @@ def test_generate
assert_equal '666', generate(666)
end

def test_dump_unenclosed_hash
assert_equal '{"a":1,"b":2}', dump(a: 1, b: 2)
end

def test_generate_pretty
json = pretty_generate({})
assert_equal(<<'EOT'.chomp, json)
Expand Down

0 comments on commit a22ed89

Please sign in to comment.