This commit is contained in:
Mamoru TASAKA 2023-12-05 15:57:45 +09:00
commit 4827cf8b4e
2 changed files with 2 additions and 66 deletions

View file

@ -1,64 +0,0 @@
From 8e0076a3f2f8a7277e659607ab195845ce505a01 Mon Sep 17 00:00:00 2001
From: Jean Boussier <jean.boussier@gmail.com>
Date: Fri, 1 Dec 2023 11:46:16 +0100
Subject: [PATCH] JSON.dump: handle unenclosed hashes regression
Fix: https://github.com/flori/json/issues/553
We can never add keyword arguments to `dump` otherwise
existing code using unenclosed hash will break.
---
lib/json/common.rb | 10 ++++++++--
tests/json_generator_test.rb | 4 ++++
2 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/lib/json/common.rb b/lib/json/common.rb
index 53b66a9b..f51f02d4 100644
--- a/lib/json/common.rb
+++ b/lib/json/common.rb
@@ -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)
@@ -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
@@ -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
diff --git a/tests/json_generator_test.rb b/tests/json_generator_test.rb
index 46cbf9c7..0ce51423 100755
--- a/tests/json_generator_test.rb
+++ b/tests/json_generator_test.rb
@@ -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)

View file

@ -1,2 +1,2 @@
SHA512 (json-2.7.0.gem) = 88b2aec93197764e47e27435112d4d3b6e7437c0b48fe2848a62fb637c4f356ad28d8ecbf9ba194bac5e567ec743cdebb3b15b91bcbd0788f3e417414c751218
SHA512 (rubygem-json-2.7.0-missing-files.tar.gz) = 4b5d137248b0bbce99eb7832bab6267b13a18826e0fd1672de26d29739f6d72c9c58e9379c9f951c6ea0e1b99cf2cb07c0c448b7fde84d52312daef2d538c775
SHA512 (json-2.7.1.gem) = c0f79f86b2550c8d4d985b2fec1d72eb16a9771ec3666ce1bc44557df3127d2ed3190d032e8644837e04fb54729be9235ec70bb498718f8cd381b937320fcde7
SHA512 (rubygem-json-2.7.1-missing-files.tar.gz) = 25454bf4e31a95f604047ebf0090b3573c3a6c7f795235dbce463588aecdcac10aa328311edbf668a158dc9c113ffb11c973ca7b425576eeba66363982f6df36