Skip to content

Commit 83b19af

Browse files
authored
Merge pull request #749 from byroot/fix-state-roundtrip
Fix a compatibility issue with `MultiJson.dump(obj, pretty: true)`
2 parents 3c71c08 + 9beed85 commit 83b19af

File tree

6 files changed

+16
-6
lines changed

6 files changed

+16
-6
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Changes
22

3+
* Fix a compatibility issue with `MultiJson.dump(obj, pretty: true)`: `no implicit conversion of false into Proc (TypeError)`.
4+
35
### 2025-02-10 (2.10.0)
46

57
* `strict: true` now accept symbols as values. Previously they'd only be accepted as hash keys.

ext/json/ext/generator/generator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,7 @@ static int configure_state_i(VALUE key, VALUE val, VALUE _arg)
16261626
else if (key == sym_script_safe) { state->script_safe = RTEST(val); }
16271627
else if (key == sym_escape_slash) { state->script_safe = RTEST(val); }
16281628
else if (key == sym_strict) { state->strict = RTEST(val); }
1629-
else if (key == sym_as_json) { state->as_json = rb_convert_type(val, T_DATA, "Proc", "to_proc"); }
1629+
else if (key == sym_as_json) { state->as_json = RTEST(val) ? rb_convert_type(val, T_DATA, "Proc", "to_proc") : Qfalse; }
16301630
return ST_CONTINUE;
16311631
}
16321632

java/src/json/ext/GeneratorState.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,12 @@ public IRubyObject as_json_get(ThreadContext context) {
368368
}
369369

370370
@JRubyMethod(name="as_json=")
371-
public IRubyObject as_json_set(ThreadContext context,
372-
IRubyObject asJSON) {
373-
this.asJSON = (RubyProc)TypeConverter.convertToType(asJSON, context.getRuntime().getProc(), "to_proc");
371+
public IRubyObject as_json_set(ThreadContext context, IRubyObject asJSON) {
372+
if (asJSON.isNil() || asJSON == context.getRuntime().getFalse()) {
373+
this.asJSON = null;
374+
} else {
375+
this.asJSON = (RubyProc)TypeConverter.convertToType(asJSON, context.getRuntime().getProc(), "to_proc");
376+
}
374377
return asJSON;
375378
}
376379

java/src/json/ext/OptionsReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public RubyHash getHash(String key) {
115115

116116
RubyProc getProc(String key) {
117117
IRubyObject value = get(key);
118-
if (value == null) return null;
118+
if (value == null || value.isNil() || value == runtime.getFalse()) return null;
119119
return (RubyProc)TypeConverter.convertToType(value, runtime.getProc(), "to_proc");
120120
}
121121
}

lib/json/truffle_ruby/generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def configure(opts)
258258
@object_nl = opts[:object_nl] || '' if opts.key?(:object_nl)
259259
@array_nl = opts[:array_nl] || '' if opts.key?(:array_nl)
260260
@allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
261-
@as_json = opts[:as_json].to_proc if opts.key?(:as_json)
261+
@as_json = opts[:as_json].to_proc if opts[:as_json]
262262
@ascii_only = opts[:ascii_only] if opts.key?(:ascii_only)
263263
@depth = opts[:depth] || 0
264264
@buffer_initial_length ||= opts[:buffer_initial_length]

test/json/json_generator_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,11 @@ def test_hash_likeness_set_string
399399
assert_equal :bar, state_hash[:foo]
400400
end
401401

402+
def test_json_state_to_h_roundtrip
403+
state = JSON.state.new
404+
assert_equal state.to_h, JSON.state.new(state.to_h).to_h
405+
end
406+
402407
def test_json_generate
403408
assert_raise JSON::GeneratorError do
404409
generate(["\xea"])

0 commit comments

Comments
 (0)