Skip to content

Commit 8433571

Browse files
Cody Hortonbyroot
Cody Horton
authored andcommitted
fix for pretty_generate throwing wrong number of arguments error
1 parent 3226913 commit 8433571

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Unreleased
44

5+
* Fix for JSON.pretty_generate to use passed state object's generate instead of state class as the required parameters aren't available.
6+
57
### 2025-05-12 (2.12.0)
68

79
* Improve floating point generation to not use scientific notation as much.

lib/json/common.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ def fast_generate(obj, opts = nil)
490490
# }
491491
#
492492
def pretty_generate(obj, opts = nil)
493-
return state.generate(obj) if State === opts
493+
return opts.generate(obj) if State === opts
494494

495495
options = PRETTY_GENERATE_OPTIONS
496496

test/json/json_generator_test.rb

+16
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,22 @@ def test_generate_pretty
122122
assert_equal '666', pretty_generate(666)
123123
end
124124

125+
def test_generate_pretty_custom
126+
state = State.new(:space_before => "<psb>", :space => "<ps>", :indent => "<pi>", :object_nl => "\n<po_nl>\n", :array_nl => "<pa_nl>")
127+
json = pretty_generate({1=>{}, 2=>['a','b'], 3=>4}, state)
128+
assert_equal(<<~'JSON'.chomp, json)
129+
{
130+
<po_nl>
131+
<pi>"1"<psb>:<ps>{},
132+
<po_nl>
133+
<pi>"2"<psb>:<ps>[<pa_nl><pi><pi>"a",<pa_nl><pi><pi>"b"<pa_nl><pi>],
134+
<po_nl>
135+
<pi>"3"<psb>:<ps>4
136+
<po_nl>
137+
}
138+
JSON
139+
end
140+
125141
def test_generate_custom
126142
state = State.new(:space_before => " ", :space => " ", :indent => "<i>", :object_nl => "\n", :array_nl => "<a_nl>")
127143
json = generate({1=>{2=>3,4=>[5,6]}}, state)

0 commit comments

Comments
 (0)