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

Fix: https://github.com/ruby/json/issues/748

`MultiJson` pass `State#to_h` as options, and the `as_json`
property defaults to `false` but `false` wasn't accepted by
the constructor.
This commit is contained in:
Jean Boussier 2025-02-10 21:09:35 +01:00 committed by Hiroshi SHIBATA
parent 502bd6ba37
commit 293ad8a4e9
No known key found for this signature in database
GPG Key ID: F9CF13417264FAC2
2 changed files with 6 additions and 1 deletions

View File

@ -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;
}

View File

@ -399,6 +399,11 @@ class JSONGeneratorTest < Test::Unit::TestCase
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"])