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

Fix a compatibility issue with MultiJson.dump(obj, pretty: true) #749

Merged
merged 1 commit into from
Feb 10, 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
@@ -1,5 +1,7 @@
# Changes

* Fix a compatibility issue with `MultiJson.dump(obj, pretty: true)`: `no implicit conversion of false into Proc (TypeError)`.

### 2025-02-10 (2.10.0)

* `strict: true` now accept symbols as values. Previously they'd only be accepted as hash keys.
Expand Down
2 changes: 1 addition & 1 deletion ext/json/ext/generator/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1626,7 +1626,7 @@ static int configure_state_i(VALUE key, VALUE val, VALUE _arg)
else if (key == sym_script_safe) { state->script_safe = RTEST(val); }
else if (key == sym_escape_slash) { state->script_safe = RTEST(val); }
else if (key == sym_strict) { state->strict = RTEST(val); }
else if (key == sym_as_json) { state->as_json = rb_convert_type(val, T_DATA, "Proc", "to_proc"); }
else if (key == sym_as_json) { state->as_json = RTEST(val) ? rb_convert_type(val, T_DATA, "Proc", "to_proc") : Qfalse; }
return ST_CONTINUE;
}

Expand Down
9 changes: 6 additions & 3 deletions java/src/json/ext/GeneratorState.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,12 @@ public IRubyObject as_json_get(ThreadContext context) {
}

@JRubyMethod(name="as_json=")
public IRubyObject as_json_set(ThreadContext context,
IRubyObject asJSON) {
this.asJSON = (RubyProc)TypeConverter.convertToType(asJSON, context.getRuntime().getProc(), "to_proc");
public IRubyObject as_json_set(ThreadContext context, IRubyObject asJSON) {
if (asJSON.isNil() || asJSON == context.getRuntime().getFalse()) {
this.asJSON = null;
} else {
this.asJSON = (RubyProc)TypeConverter.convertToType(asJSON, context.getRuntime().getProc(), "to_proc");
}
return asJSON;
}

Expand Down
2 changes: 1 addition & 1 deletion java/src/json/ext/OptionsReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public RubyHash getHash(String key) {

RubyProc getProc(String key) {
IRubyObject value = get(key);
if (value == null) return null;
if (value == null || value.isNil() || value == runtime.getFalse()) return null;
return (RubyProc)TypeConverter.convertToType(value, runtime.getProc(), "to_proc");
}
}
2 changes: 1 addition & 1 deletion lib/json/truffle_ruby/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def configure(opts)
@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)
@as_json = opts[:as_json].to_proc if opts.key?(:as_json)
@as_json = opts[:as_json].to_proc if opts[:as_json]
@ascii_only = opts[:ascii_only] if opts.key?(:ascii_only)
@depth = opts[:depth] || 0
@buffer_initial_length ||= opts[:buffer_initial_length]
Expand Down
5 changes: 5 additions & 0 deletions test/json/json_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,11 @@ def test_hash_likeness_set_string
assert_equal :bar, state_hash[:foo]
end

def test_json_state_to_h_roundtrip
state = JSON.state.new
assert_equal state.to_h, JSON.state.new(state.to_h).to_h
end

def test_json_generate
assert_raise JSON::GeneratorError do
generate(["\xea"])
Expand Down