Skip to content

Commit ddfe6f4

Browse files
authored
Merge pull request #803 from CodyBriscoe/bugfix/pretty_generate
fix for pretty_generate throwing wrong number of arguments error
2 parents 3226913 + 8433571 commit ddfe6f4

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 16 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy