diff options
author | Jean Boussier <[email protected]> | 2025-04-27 11:10:00 +0200 |
---|---|---|
committer | Jean Boussier <[email protected]> | 2025-04-30 08:12:41 +0200 |
commit | 5566a7f7402197002a8d12420230f32d5fff727b (patch) | |
tree | 7f7910933769906d02c076bd0200af8cc8dd5897 /test | |
parent | 1103611600fd64569b450619bca68f7ef00dddfb (diff) |
[ruby/json] Handle non-string keys returning immediate values via `to_s`
We can't directly call `RBASIC_CLASS` as the return value of
`to_s` may be an immediate.
https://github.com/ruby/json/commit/12dc394d11
Diffstat (limited to 'test')
-rwxr-xr-x | test/json/json_generator_test.rb | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/test/json/json_generator_test.rb b/test/json/json_generator_test.rb index f87006ac7b..47c94bd393 100755 --- a/test/json/json_generator_test.rb +++ b/test/json/json_generator_test.rb @@ -605,6 +605,22 @@ class JSONGeneratorTest < Test::Unit::TestCase assert_equal '{"JSONGeneratorTest::StringWithToS#to_s":1}', JSON.generate(StringWithToS.new => 1) end + def test_string_subclass_with_broken_to_s + klass = Class.new(String) do + def to_s + false + end + end + s = klass.new("test") + assert_equal '["test"]', JSON.generate([s]) + + omit("Can't figure out how to match behavior in java code") if RUBY_PLATFORM == "java" + + assert_raise TypeError do + JSON.generate(s => 1) + end + end + if defined?(JSON::Ext::Generator) and RUBY_PLATFORM != "java" def test_valid_utf8_in_different_encoding utf8_string = "€™" |