@@ -536,6 +536,72 @@ def test_to_json_called_with_state_object
536
536
assert_instance_of JSON ::State , argument
537
537
end
538
538
539
+ module CustomToJSON
540
+ def to_json ( *)
541
+ %{"#{ self . class . name } #to_json"}
542
+ end
543
+ end
544
+
545
+ module CustomToS
546
+ def to_s
547
+ "#{ self . class . name } #to_s"
548
+ end
549
+ end
550
+
551
+ class ArrayWithToJSON < Array
552
+ include CustomToJSON
553
+ end
554
+
555
+ def test_array_subclass_with_to_json
556
+ assert_equal '["JSONGeneratorTest::ArrayWithToJSON#to_json"]' , JSON . generate ( [ ArrayWithToJSON . new ] )
557
+ assert_equal '{"[]":1}' , JSON . generate ( ArrayWithToJSON . new => 1 )
558
+ end
559
+
560
+ class ArrayWithToS < Array
561
+ include CustomToS
562
+ end
563
+
564
+ def test_array_subclass_with_to_s
565
+ assert_equal '[[]]' , JSON . generate ( [ ArrayWithToS . new ] )
566
+ assert_equal '{"JSONGeneratorTest::ArrayWithToS#to_s":1}' , JSON . generate ( ArrayWithToS . new => 1 )
567
+ end
568
+
569
+ class HashWithToJSON < Hash
570
+ include CustomToJSON
571
+ end
572
+
573
+ def test_hash_subclass_with_to_json
574
+ assert_equal '["JSONGeneratorTest::HashWithToJSON#to_json"]' , JSON . generate ( [ HashWithToJSON . new ] )
575
+ assert_equal '{"{}":1}' , JSON . generate ( HashWithToJSON . new => 1 )
576
+ end
577
+
578
+ class HashWithToS < Hash
579
+ include CustomToS
580
+ end
581
+
582
+ def test_hash_subclass_with_to_s
583
+ assert_equal '[{}]' , JSON . generate ( [ HashWithToS . new ] )
584
+ assert_equal '{"JSONGeneratorTest::HashWithToS#to_s":1}' , JSON . generate ( HashWithToS . new => 1 )
585
+ end
586
+
587
+ class StringWithToJSON < String
588
+ include CustomToJSON
589
+ end
590
+
591
+ def test_string_subclass_with_to_json
592
+ assert_equal '["JSONGeneratorTest::StringWithToJSON#to_json"]' , JSON . generate ( [ StringWithToJSON . new ] )
593
+ assert_equal '{"":1}' , JSON . generate ( StringWithToJSON . new => 1 )
594
+ end
595
+
596
+ class StringWithToS < String
597
+ include CustomToS
598
+ end
599
+
600
+ def test_string_subclass_with_to_s
601
+ assert_equal '[""]' , JSON . generate ( [ StringWithToS . new ] )
602
+ assert_equal '{"JSONGeneratorTest::StringWithToS#to_s":1}' , JSON . generate ( StringWithToS . new => 1 )
603
+ end
604
+
539
605
if defined? ( JSON ::Ext ::Generator ) and RUBY_PLATFORM != "java"
540
606
def test_valid_utf8_in_different_encoding
541
607
utf8_string = "€™"
0 commit comments