Skip to content

Commit 8e0076a

Browse files
committed
JSON.dump: handle unenclosed hashes regression
Fix: #553 We can never add keyword arguments to `dump` otherwise existing code using unenclosed hash will break.
1 parent 6754be6 commit 8e0076a

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

lib/json/common.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ class << self
611611
# puts File.read(path)
612612
# Output:
613613
# {"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}
614-
def dump(obj, anIO = nil, limit = nil, strict: NOT_SET)
614+
def dump(obj, anIO = nil, limit = nil, kwargs = nil)
615615
if anIO and limit.nil?
616616
anIO = anIO.to_io if anIO.respond_to?(:to_io)
617617
unless anIO.respond_to?(:write)
@@ -621,7 +621,7 @@ def dump(obj, anIO = nil, limit = nil, strict: NOT_SET)
621621
end
622622
opts = JSON.dump_default_options
623623
opts = opts.merge(:max_nesting => limit) if limit
624-
opts[:strict] = strict if NOT_SET != strict
624+
merge_dump_options(opts, **kwargs) if kwargs
625625
result = generate(obj, opts)
626626
if anIO
627627
anIO.write result
@@ -637,6 +637,12 @@ def dump(obj, anIO = nil, limit = nil, strict: NOT_SET)
637637
def self.iconv(to, from, string)
638638
string.encode(to, from)
639639
end
640+
641+
private
642+
643+
def merge_dump_options(opts, strict: NOT_SET)
644+
opts[:strict] = strict if NOT_SET != strict
645+
end
640646
end
641647

642648
module ::Kernel

tests/json_generator_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ def test_generate
6262
assert_equal '666', generate(666)
6363
end
6464

65+
def test_dump_unenclosed_hash
66+
assert_equal '{"a":1,"b":2}', dump(a: 1, b: 2)
67+
end
68+
6569
def test_generate_pretty
6670
json = pretty_generate({})
6771
assert_equal(<<'EOT'.chomp, json)

0 commit comments

Comments
 (0)