Skip to content

Commit 844a663

Browse files
committed
Handle all formatting configs potentially being nil.
Fix: #653 I don't think this was really fully supported in the past, but it kinda worked with some of the implementations.
1 parent 4d9dc98 commit 844a663

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### UNRELEASED (2.7.5)
66

7+
* Gracefully handle formatting configs being set to `nil` instead of `""`.
78
* Workaround another issue caused by conflicting versions of both `json_pure` and `json` being loaded.
89

910
### 2024-10-25 (2.7.4)

lib/json/ext/generator/state.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ def configure(opts)
4646
opts.each do |key, value|
4747
case key
4848
when :indent
49-
self.indent = value
49+
self.indent = value || ''
5050
when :space
51-
self.space = value
51+
self.space = value || ''
5252
when :space_before
53-
self.space_before = value
53+
self.space_before = value || ''
5454
when :array_nl
55-
self.array_nl = value
55+
self.array_nl = value || ''
5656
when :object_nl
57-
self.object_nl = value
57+
self.object_nl = value || ''
5858
when :max_nesting
5959
self.max_nesting = value || 0
6060
when :depth

lib/json/pure/generator.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,13 @@ def configure(opts)
239239
end
240240

241241
# NOTE: If adding new instance variables here, check whether #generate should check them for #generate_json
242-
@indent = opts[:indent] if opts.key?(:indent)
243-
@space = opts[:space] if opts.key?(:space)
244-
@space_before = opts[:space_before] if opts.key?(:space_before)
245-
@object_nl = opts[:object_nl] if opts.key?(:object_nl)
246-
@array_nl = opts[:array_nl] if opts.key?(:array_nl)
247-
@allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
248-
@ascii_only = opts[:ascii_only] if opts.key?(:ascii_only)
242+
@indent = opts[:indent] || '' if opts.key?(:indent)
243+
@space = opts[:space] || '' if opts.key?(:space)
244+
@space_before = opts[:space_before] || '' if opts.key?(:space_before)
245+
@object_nl = opts[:object_nl] || '' if opts.key?(:object_nl)
246+
@array_nl = opts[:array_nl] || '' if opts.key?(:array_nl)
247+
@allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
248+
@ascii_only = opts[:ascii_only] if opts.key?(:ascii_only)
249249
@depth = opts[:depth] || 0
250250
@buffer_initial_length ||= opts[:buffer_initial_length]
251251

test/json/json_generator_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,27 @@ def test_states
174174
assert s[:check_circular?]
175175
end
176176

177+
def test_falsy_state
178+
object = { foo: [1, 2], bar: { egg: :spam }}
179+
expected_json = JSON.generate(
180+
object,
181+
array_nl: "",
182+
indent: "",
183+
object_nl: "",
184+
space: "",
185+
space_before: "",
186+
)
187+
188+
assert_equal expected_json, JSON.generate(
189+
object,
190+
array_nl: nil,
191+
indent: nil,
192+
object_nl: nil,
193+
space: nil,
194+
space_before: nil,
195+
)
196+
end
197+
177198
def test_pretty_state
178199
state = JSON.create_pretty_state
179200
assert_equal({

0 commit comments

Comments
 (0)