Skip to content

Commit

Permalink
Merge pull request #749 from byroot/fix-state-roundtrip
Browse files Browse the repository at this point in the history
Fix a compatibility issue with `MultiJson.dump(obj, pretty: true)`
  • Loading branch information
byroot authored Feb 10, 2025
2 parents 3c71c08 + 9beed85 commit 83b19af
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 6 deletions.
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

0 comments on commit 83b19af

Please sign in to comment.