Skip to content
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

Handle all formatting configs potentially being nil. #654

Merged
merged 1 commit into from
Oct 28, 2024
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
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
Loading