Skip to content
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
6 changes: 4 additions & 2 deletions lib/json/truffle_ruby/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def generate(obj, anIO = nil)
buf << obj.to_json(self)
end
when Integer
buf << obj.to_s
buf << String(obj)
when Symbol
if @strict
fast_serialize_string(obj.name, buf)
Expand Down Expand Up @@ -652,7 +652,9 @@ def json_transform(state)

module Integer
# Returns a JSON string representation for this Integer number.
def to_json(*) to_s end
def to_json(*)
String(self)
end
end

module Float
Expand Down
34 changes: 18 additions & 16 deletions test/json/json_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -493,25 +493,27 @@ def foo.to_h
assert_equal '2', state.indent
end

def test_broken_bignum # [ruby-core:38867]
pid = fork do
x = 1 << 64
x.class.class_eval do
def to_s
end
def test_broken_bignum # [Bug #5173]
bignum = 1 << 64
bignum_to_s = bignum.to_s

original_to_s = bignum.class.instance_method(:to_s)
bignum.class.class_eval do
def to_s
nil
end
begin
JSON::Ext::Generator::State.new.generate(x)
exit 1
rescue TypeError
exit 0
alias_method :to_s, :to_s
end
case RUBY_PLATFORM
when "java"
assert_equal bignum_to_s, JSON.generate(bignum)
else
assert_raise(TypeError) do
JSON.generate(bignum)
end
end
_, status = Process.waitpid2(pid)
assert status.success?
rescue NotImplementedError
# forking to avoid modifying core class of a parent process and
# introducing race conditions of tests are run in parallel
ensure
bignum.class.define_method(:to_s, original_to_s) if original_to_s
end

def test_hash_likeness_set_symbol
Expand Down