Skip to content

Commit

Permalink
Merge pull request #654 from casperisfine/v2.7.x-handle-nil-configs
Browse files Browse the repository at this point in the history
Handle all formatting configs potentially being `nil`.
  • Loading branch information
byroot authored Oct 28, 2024
2 parents 04b43d2 + c318928 commit 9d71186
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Changes

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

### 2024-10-25 (2.7.4)
Expand Down
10 changes: 5 additions & 5 deletions lib/json/ext/generator/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ def configure(opts)
opts.each do |key, value|
case key
when :indent
self.indent = value
self.indent = value || ''
when :space
self.space = value
self.space = value || ''
when :space_before
self.space_before = value
self.space_before = value || ''
when :array_nl
self.array_nl = value
self.array_nl = value || ''
when :object_nl
self.object_nl = value
self.object_nl = value || ''
when :max_nesting
self.max_nesting = value || 0
when :depth
Expand Down
14 changes: 7 additions & 7 deletions lib/json/pure/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,13 @@ def configure(opts)
end

# NOTE: If adding new instance variables here, check whether #generate should check them for #generate_json
@indent = opts[:indent] if opts.key?(:indent)
@space = opts[:space] if opts.key?(:space)
@space_before = opts[:space_before] if opts.key?(:space_before)
@object_nl = opts[:object_nl] if opts.key?(:object_nl)
@array_nl = opts[:array_nl] if opts.key?(:array_nl)
@allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
@ascii_only = opts[:ascii_only] if opts.key?(:ascii_only)
@indent = opts[:indent] || '' if opts.key?(:indent)
@space = opts[:space] || '' if opts.key?(:space)
@space_before = opts[:space_before] || '' if opts.key?(:space_before)
@object_nl = opts[:object_nl] || '' if opts.key?(:object_nl)
@array_nl = opts[:array_nl] || '' if opts.key?(:array_nl)
@allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
@ascii_only = opts[:ascii_only] if opts.key?(:ascii_only)
@depth = opts[:depth] || 0
@buffer_initial_length ||= opts[:buffer_initial_length]

Expand Down
21 changes: 21 additions & 0 deletions test/json/json_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,27 @@ def test_states
assert s[:check_circular?]
end

def test_falsy_state
object = { foo: [1, 2], bar: { egg: :spam }}
expected_json = JSON.generate(
object,
array_nl: "",
indent: "",
object_nl: "",
space: "",
space_before: "",
)

assert_equal expected_json, JSON.generate(
object,
array_nl: nil,
indent: nil,
object_nl: nil,
space: nil,
space_before: nil,
)
end

def test_pretty_state
state = JSON.create_pretty_state
assert_equal({
Expand Down

0 comments on commit 9d71186

Please sign in to comment.