Skip to content

Commit ac0670b

Browse files
eregonbyroot
authored andcommitted
Keep Integer#to_json optimized and adapt the test
* Redefining Integer#to_s is bound to break many things, any non-segfault outcome should be acceptable. So adapt the test to just ensure it does not segfault. * Also redefining Kernel#String would also break with the previous code.
1 parent 35db859 commit ac0670b

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

lib/json/truffle_ruby/generator.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ def generate(obj, anIO = nil)
413413
buf << obj.to_json(self)
414414
end
415415
when Integer
416-
buf << String(obj)
416+
buf << obj.to_s
417417
when Symbol
418418
if @strict
419419
fast_serialize_string(obj.name, buf)
@@ -652,9 +652,7 @@ def json_transform(state)
652652

653653
module Integer
654654
# Returns a JSON string representation for this Integer number.
655-
def to_json(*)
656-
String(self)
657-
end
655+
def to_json(*) to_s end
658656
end
659657

660658
module Float

test/json/json_generator_test.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,14 @@ def to_s
504504
end
505505
alias_method :to_s, :to_s
506506
end
507-
case RUBY_PLATFORM
508-
when "java"
507+
case RUBY_ENGINE
508+
when "jruby"
509509
assert_equal bignum_to_s, JSON.generate(bignum)
510-
else
510+
when "truffleruby"
511+
assert_raise(NoMethodError) do
512+
JSON.generate(bignum)
513+
end
514+
when "ruby"
511515
assert_raise(TypeError) do
512516
JSON.generate(bignum)
513517
end

0 commit comments

Comments
 (0)