Skip to content

fix for pretty_generate throwing wrong number of arguments error #803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Unreleased

* Fix for JSON.pretty_generate to use passed state object's generate instead of state class as the required parameters aren't available.

### 2025-05-12 (2.12.0)

* Improve floating point generation to not use scientific notation as much.
Expand Down
2 changes: 1 addition & 1 deletion lib/json/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ def fast_generate(obj, opts = nil)
# }
#
def pretty_generate(obj, opts = nil)
return state.generate(obj) if State === opts
return opts.generate(obj) if State === opts

options = PRETTY_GENERATE_OPTIONS

Expand Down
16 changes: 16 additions & 0 deletions test/json/json_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,22 @@ def test_generate_pretty
assert_equal '666', pretty_generate(666)
end

def test_generate_pretty_custom
state = State.new(:space_before => "<psb>", :space => "<ps>", :indent => "<pi>", :object_nl => "\n<po_nl>\n", :array_nl => "<pa_nl>")
json = pretty_generate({1=>{}, 2=>['a','b'], 3=>4}, state)
assert_equal(<<~'JSON'.chomp, json)
{
<po_nl>
<pi>"1"<psb>:<ps>{},
<po_nl>
<pi>"2"<psb>:<ps>[<pa_nl><pi><pi>"a",<pa_nl><pi><pi>"b"<pa_nl><pi>],
<po_nl>
<pi>"3"<psb>:<ps>4
<po_nl>
}
JSON
end

def test_generate_custom
state = State.new(:space_before => " ", :space => " ", :indent => "<i>", :object_nl => "\n", :array_nl => "<a_nl>")
json = generate({1=>{2=>3,4=>[5,6]}}, state)
Expand Down