oj 3.13.9 → 3.16.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +101 -0
  3. data/README.md +13 -2
  4. data/ext/oj/buf.h +11 -6
  5. data/ext/oj/cache.c +25 -24
  6. data/ext/oj/cache8.c +10 -9
  7. data/ext/oj/circarray.c +8 -6
  8. data/ext/oj/circarray.h +2 -2
  9. data/ext/oj/code.c +19 -33
  10. data/ext/oj/code.h +2 -2
  11. data/ext/oj/compat.c +20 -60
  12. data/ext/oj/custom.c +76 -155
  13. data/ext/oj/debug.c +3 -9
  14. data/ext/oj/dump.c +203 -213
  15. data/ext/oj/dump.h +26 -12
  16. data/ext/oj/dump_compat.c +565 -642
  17. data/ext/oj/dump_leaf.c +17 -63
  18. data/ext/oj/dump_object.c +59 -181
  19. data/ext/oj/dump_strict.c +24 -48
  20. data/ext/oj/encoder.c +43 -0
  21. data/ext/oj/err.c +2 -13
  22. data/ext/oj/err.h +9 -12
  23. data/ext/oj/extconf.rb +18 -7
  24. data/ext/oj/fast.c +83 -108
  25. data/ext/oj/intern.c +52 -50
  26. data/ext/oj/intern.h +4 -8
  27. data/ext/oj/mem.c +318 -0
  28. data/ext/oj/mem.h +53 -0
  29. data/ext/oj/mimic_json.c +104 -81
  30. data/ext/oj/object.c +50 -67
  31. data/ext/oj/odd.c +89 -67
  32. data/ext/oj/odd.h +15 -15
  33. data/ext/oj/oj.c +171 -106
  34. data/ext/oj/oj.h +96 -74
  35. data/ext/oj/parse.c +169 -189
  36. data/ext/oj/parse.h +23 -24
  37. data/ext/oj/parser.c +89 -34
  38. data/ext/oj/parser.h +20 -9
  39. data/ext/oj/rails.c +86 -151
  40. data/ext/oj/rails.h +1 -1
  41. data/ext/oj/reader.c +12 -15
  42. data/ext/oj/reader.h +4 -2
  43. data/ext/oj/resolve.c +3 -4
  44. data/ext/oj/rxclass.c +6 -5
  45. data/ext/oj/rxclass.h +1 -1
  46. data/ext/oj/saj.c +21 -32
  47. data/ext/oj/saj2.c +329 -93
  48. data/ext/oj/saj2.h +23 -0
  49. data/ext/oj/scp.c +3 -14
  50. data/ext/oj/sparse.c +26 -70
  51. data/ext/oj/stream_writer.c +12 -22
  52. data/ext/oj/strict.c +20 -52
  53. data/ext/oj/string_writer.c +21 -22
  54. data/ext/oj/trace.h +31 -4
  55. data/ext/oj/usual.c +105 -150
  56. data/ext/oj/usual.h +68 -0
  57. data/ext/oj/util.h +1 -1
  58. data/ext/oj/val_stack.c +1 -1
  59. data/ext/oj/val_stack.h +8 -7
  60. data/ext/oj/validate.c +21 -26
  61. data/ext/oj/wab.c +32 -69
  62. data/lib/oj/active_support_helper.rb +1 -3
  63. data/lib/oj/bag.rb +7 -1
  64. data/lib/oj/easy_hash.rb +4 -5
  65. data/lib/oj/error.rb +0 -1
  66. data/lib/oj/json.rb +162 -150
  67. data/lib/oj/mimic.rb +6 -2
  68. data/lib/oj/saj.rb +20 -6
  69. data/lib/oj/state.rb +9 -6
  70. data/lib/oj/version.rb +1 -2
  71. data/lib/oj.rb +2 -0
  72. data/pages/Compatibility.md +1 -1
  73. data/pages/InstallOptions.md +20 -0
  74. data/pages/JsonGem.md +15 -0
  75. data/pages/Modes.md +6 -3
  76. data/pages/Options.md +10 -0
  77. data/pages/Rails.md +12 -0
  78. data/test/_test_active.rb +8 -9
  79. data/test/_test_active_mimic.rb +7 -8
  80. data/test/_test_mimic_rails.rb +17 -20
  81. data/test/activerecord/result_test.rb +5 -6
  82. data/test/{activesupport5 → activesupport7}/abstract_unit.rb +16 -12
  83. data/test/{activesupport5 → activesupport7}/decoding_test.rb +2 -10
  84. data/test/{activesupport5 → activesupport7}/encoding_test.rb +20 -34
  85. data/test/{activesupport5 → activesupport7}/encoding_test_cases.rb +6 -0
  86. data/test/{activesupport5 → activesupport7}/time_zone_test_helpers.rb +8 -0
  87. data/test/files.rb +15 -15
  88. data/test/foo.rb +15 -15
  89. data/test/helper.rb +11 -8
  90. data/test/isolated/shared.rb +3 -2
  91. data/test/json_gem/json_addition_test.rb +2 -2
  92. data/test/json_gem/json_common_interface_test.rb +8 -6
  93. data/test/json_gem/json_encoding_test.rb +0 -0
  94. data/test/json_gem/json_ext_parser_test.rb +1 -0
  95. data/test/json_gem/json_fixtures_test.rb +3 -2
  96. data/test/json_gem/json_generator_test.rb +49 -37
  97. data/test/json_gem/json_generic_object_test.rb +11 -11
  98. data/test/json_gem/json_parser_test.rb +54 -47
  99. data/test/json_gem/json_string_matching_test.rb +9 -9
  100. data/test/json_gem/test_helper.rb +7 -3
  101. data/test/mem.rb +13 -12
  102. data/test/perf.rb +21 -26
  103. data/test/perf_compat.rb +31 -33
  104. data/test/perf_dump.rb +50 -0
  105. data/test/perf_fast.rb +80 -82
  106. data/test/perf_file.rb +27 -29
  107. data/test/perf_object.rb +65 -69
  108. data/test/perf_once.rb +12 -11
  109. data/test/perf_parser.rb +42 -48
  110. data/test/perf_saj.rb +46 -54
  111. data/test/perf_scp.rb +57 -69
  112. data/test/perf_simple.rb +41 -39
  113. data/test/perf_strict.rb +68 -70
  114. data/test/perf_wab.rb +67 -69
  115. data/test/prec.rb +3 -3
  116. data/test/sample/change.rb +0 -1
  117. data/test/sample/dir.rb +0 -1
  118. data/test/sample/doc.rb +0 -1
  119. data/test/sample/file.rb +0 -1
  120. data/test/sample/group.rb +0 -1
  121. data/test/sample/hasprops.rb +0 -1
  122. data/test/sample/layer.rb +0 -1
  123. data/test/sample/rect.rb +0 -1
  124. data/test/sample/shape.rb +0 -1
  125. data/test/sample/text.rb +0 -1
  126. data/test/sample.rb +16 -16
  127. data/test/sample_json.rb +8 -8
  128. data/test/test_compat.rb +95 -43
  129. data/test/test_custom.rb +72 -51
  130. data/test/test_debian.rb +7 -10
  131. data/test/test_fast.rb +102 -87
  132. data/test/test_file.rb +41 -30
  133. data/test/test_gc.rb +16 -5
  134. data/test/test_generate.rb +5 -5
  135. data/test/test_hash.rb +4 -4
  136. data/test/test_integer_range.rb +9 -9
  137. data/test/test_null.rb +20 -20
  138. data/test/test_object.rb +85 -96
  139. data/test/test_parser.rb +6 -22
  140. data/test/test_parser_debug.rb +27 -0
  141. data/test/test_parser_saj.rb +115 -23
  142. data/test/test_parser_usual.rb +6 -6
  143. data/test/test_rails.rb +2 -2
  144. data/test/test_saj.rb +10 -8
  145. data/test/test_scp.rb +37 -39
  146. data/test/test_strict.rb +40 -32
  147. data/test/test_various.rb +163 -84
  148. data/test/test_wab.rb +48 -44
  149. data/test/test_writer.rb +47 -47
  150. data/test/tests.rb +13 -5
  151. data/test/tests_mimic.rb +12 -3
  152. data/test/tests_mimic_addition.rb +12 -3
  153. metadata +34 -144
  154. data/test/activesupport4/decoding_test.rb +0 -108
  155. data/test/activesupport4/encoding_test.rb +0 -531
  156. data/test/activesupport4/test_helper.rb +0 -41
  157. data/test/activesupport5/test_helper.rb +0 -72
  158. data/test/bar.rb +0 -16
  159. data/test/baz.rb +0 -16
  160. data/test/bug.rb +0 -16
  161. data/test/zoo.rb +0 -13
data/test/_test_active.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
- # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
- $: << File.dirname(__FILE__)
4
+ $LOAD_PATH << __dir__
5
5
  %w(lib ext test).each do |dir|
6
6
  $LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
7
7
  end
@@ -13,14 +13,14 @@ require 'sqlite3'
13
13
  require 'active_record'
14
14
  require 'oj'
15
15
 
16
- #Oj.mimic_JSON()
16
+ # Oj.mimic_JSON()
17
17
  Oj.default_options = {mode: :compat, indent: 2}
18
18
 
19
- #ActiveRecord::Base.logger = Logger.new(STDERR)
19
+ # ActiveRecord::Base.logger = Logger.new(STDERR)
20
20
 
21
21
  ActiveRecord::Base.establish_connection(
22
- :adapter => "sqlite3",
23
- :database => ":memory:"
22
+ :adapter => 'sqlite3',
23
+ :database => ':memory:'
24
24
  )
25
25
 
26
26
  ActiveRecord::Schema.define do
@@ -37,8 +37,8 @@ end
37
37
  class ActiveTest < Minitest::Test
38
38
 
39
39
  def test_active
40
- User.find_or_create_by(first_name: "John", last_name: "Smith", email: "john@example.com")
41
- User.find_or_create_by(first_name: "Joan", last_name: "Smith", email: "joan@example.com")
40
+ User.find_or_create_by(first_name: 'John', last_name: 'Smith', email: 'john@example.com')
41
+ User.find_or_create_by(first_name: 'Joan', last_name: 'Smith', email: 'joan@example.com')
42
42
 
43
43
  # Single instance.
44
44
  assert_equal(%|{
@@ -71,6 +71,5 @@ class ActiveTest < Minitest::Test
71
71
 
72
72
  # Array of instances as json. (not Oj)
73
73
  assert_equal(%|[{"id":1,"first_name":"John","last_name":"Smith","email":"john@example.com"},{"id":2,"first_name":"Joan","last_name":"Smith","email":"joan@example.com"}]|, User.all.to_json)
74
-
75
74
  end
76
75
  end
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
- # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
- $: << File.dirname(__FILE__)
4
+ $LOAD_PATH << __dir__
5
5
  %w(lib ext test).each do |dir|
6
6
  $LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
7
7
  end
@@ -16,11 +16,11 @@ require 'oj'
16
16
  Oj.mimic_JSON()
17
17
  Oj.default_options = {mode: :compat, indent: 2}
18
18
 
19
- #ActiveRecord::Base.logger = Logger.new(STDERR)
19
+ # ActiveRecord::Base.logger = Logger.new(STDERR)
20
20
 
21
21
  ActiveRecord::Base.establish_connection(
22
- :adapter => "sqlite3",
23
- :database => ":memory:"
22
+ :adapter => 'sqlite3',
23
+ :database => ':memory:'
24
24
  )
25
25
 
26
26
  ActiveRecord::Schema.define do
@@ -37,8 +37,8 @@ end
37
37
  class ActiveTest < Minitest::Test
38
38
 
39
39
  def test_active
40
- User.find_or_create_by(first_name: "John", last_name: "Smith", email: "john@example.com")
41
- User.find_or_create_by(first_name: "Joan", last_name: "Smith", email: "joan@example.com")
40
+ User.find_or_create_by(first_name: 'John', last_name: 'Smith', email: 'john@example.com')
41
+ User.find_or_create_by(first_name: 'Joan', last_name: 'Smith', email: 'joan@example.com')
42
42
 
43
43
  # Single instance.
44
44
  assert_equal(%|{
@@ -91,6 +91,5 @@ class ActiveTest < Minitest::Test
91
91
  }
92
92
  ]
93
93
  |, User.all.to_json)
94
-
95
94
  end
96
95
  end
@@ -1,10 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
- # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
- $: << File.dirname(__FILE__)
4
+ $LOAD_PATH << __dir__
5
5
 
6
6
  require 'helper'
7
- #Oj.mimic_JSON
7
+ # Oj.mimic_JSON
8
8
  require 'rails/all'
9
9
 
10
10
  require 'active_model'
@@ -36,14 +36,12 @@ end
36
36
  class MimicRails < Minitest::Test
37
37
 
38
38
  def test_mimic_exception
39
- begin
40
- ActiveSupport::JSON.decode("{")
41
- puts "Failed"
42
- rescue ActiveSupport::JSON.parse_error
43
- assert(true)
44
- rescue Exception
45
- assert(false, 'Expected a JSON::ParserError')
46
- end
39
+ ActiveSupport::JSON.decode('{')
40
+ puts 'Failed'
41
+ rescue ActiveSupport::JSON.parse_error
42
+ assert(true)
43
+ rescue Exception
44
+ assert(false, 'Expected a JSON::ParserError')
47
45
  end
48
46
 
49
47
  def test_dump_string
@@ -84,18 +82,17 @@ class MimicRails < Minitest::Test
84
82
  category = Category.new(1, 'test')
85
83
  serializer = CategorySerializer.new(category)
86
84
 
87
- json = serializer.to_json()
85
+ serializer.to_json()
88
86
  puts "*** serializer.to_json() #{serializer.to_json()}"
89
- json = serializer.as_json()
87
+ serializer.as_json()
90
88
  puts "*** serializer.as_json() #{serializer.as_json()}"
91
- json = JSON.dump(serializer)
89
+ JSON.dump(serializer)
92
90
  puts "*** JSON.dump(serializer) #{JSON.dump(serializer)}"
93
91
 
94
92
  puts "*** category.to_json() #{category.to_json()}"
95
93
  puts "*** category.as_json() #{category.as_json()}"
96
94
  puts "*** JSON.dump(serializer) #{JSON.dump(category)}"
97
95
  puts "*** Oj.dump(serializer) #{Oj.dump(category)}"
98
-
99
96
  end
100
97
 
101
98
  def test_dump_object_array
@@ -104,7 +101,7 @@ class MimicRails < Minitest::Test
104
101
  cat2 = Category.new(2, 'test')
105
102
  a = Array.wrap([cat1, cat2])
106
103
 
107
- #serializer = CategorySerializer.new(a)
104
+ # serializer = CategorySerializer.new(a)
108
105
 
109
106
  puts "*** a.to_json() #{a.to_json()}"
110
107
  puts "*** a.as_json() #{a.as_json()}"
@@ -114,13 +111,13 @@ class MimicRails < Minitest::Test
114
111
 
115
112
  def test_dump_time
116
113
  Oj.default_options= {:indent => 2}
117
- now = ActiveSupport::TimeZone['America/Chicago'].parse("2014-11-01 13:20:47")
114
+ now = ActiveSupport::TimeZone['America/Chicago'].parse('2014-11-01 13:20:47')
118
115
  json = Oj.dump(now, mode: :object, time_format: :xmlschema)
119
- #puts "*** json: #{json}"
116
+ # puts "*** json: #{json}"
120
117
 
121
118
  oj_dump = Oj.load(json, mode: :object, time_format: :xmlschema)
122
- #puts "Now: #{now}\n Oj: #{oj_dump}"
123
- assert_equal("2014-11-01T13:20:47-05:00", oj_dump.xmlschema)
119
+ # puts "Now: #{now}\n Oj: #{oj_dump}"
120
+ assert_equal('2014-11-01T13:20:47-05:00', oj_dump.xmlschema)
124
121
  end
125
122
 
126
123
  end # MimicRails
@@ -13,13 +13,12 @@ Oj.default_options = { mode: :rails }
13
13
 
14
14
  class ActiveRecordResultTest < Minitest::Test
15
15
  def test_hash_rows
16
-
17
16
  result = ActiveRecord::Result.new(["one", "two"],
18
- [
19
- ["row 1 col 1", "row 1 col 2"],
20
- ["row 2 col 1", "row 2 col 2"],
21
- ["row 3 col 1", "row 3 col 2"],
22
- ])
17
+ [
18
+ ["row 1 col 1", "row 1 col 2"],
19
+ ["row 2 col 1", "row 2 col 2"],
20
+ ["row 3 col 1", "row 3 col 2"],
21
+ ])
23
22
  #puts "*** result: #{Oj.dump(result, indent: 2)}"
24
23
  json_result = if ActiveRecord.version >= Gem::Version.new("6")
25
24
  result.to_a
@@ -2,6 +2,7 @@
2
2
 
3
3
  ORIG_ARGV = ARGV.dup
4
4
 
5
+ require "bundler/setup"
5
6
  require "active_support/core_ext/kernel/reporting"
6
7
 
7
8
  silence_warnings do
@@ -27,19 +28,22 @@ ActiveSupport.to_time_preserves_timezone = ENV["PRESERVE_TIMEZONES"] == "1"
27
28
  I18n.enforce_available_locales = false
28
29
 
29
30
  class ActiveSupport::TestCase
30
- include ActiveSupport::Testing::MethodCallAssertions
31
-
32
- # Skips the current run on Rubinius using Minitest::Assertions#skip
33
- private def rubinius_skip(message = "")
34
- skip message if RUBY_ENGINE == "rbx"
31
+ if Process.respond_to?(:fork) && !Gem.win_platform?
32
+ parallelize
33
+ else
34
+ parallelize(with: :threads)
35
35
  end
36
36
 
37
- # Skips the current run on JRuby using Minitest::Assertions#skip
38
- private def jruby_skip(message = "")
39
- skip message if defined?(JRUBY_VERSION)
40
- end
37
+ include ActiveSupport::Testing::MethodCallAssertions
41
38
 
42
- def frozen_error_class
43
- Object.const_defined?(:FrozenError) ? FrozenError : RuntimeError
44
- end
39
+ private
40
+ # Skips the current run on Rubinius using Minitest::Assertions#skip
41
+ def rubinius_skip(message = "")
42
+ skip message if RUBY_ENGINE == "rbx"
43
+ end
44
+
45
+ # Skips the current run on JRuby using Minitest::Assertions#skip
46
+ def jruby_skip(message = "")
47
+ skip message if defined?(JRUBY_VERSION)
48
+ end
45
49
  end
@@ -5,18 +5,9 @@ require "active_support/json"
5
5
  require "active_support/time"
6
6
  require_relative "time_zone_test_helpers"
7
7
 
8
- require 'oj'
9
-
10
- Oj::Rails.set_decoder()
11
-
12
8
  class TestJSONDecoding < ActiveSupport::TestCase
13
9
  include TimeZoneTestHelpers
14
10
 
15
- # Added for testing if Oj is used.
16
- test "oj is used as an encoder" do
17
- assert_equal ActiveSupport.json_encoder, Oj::Rails::Encoder
18
- end
19
-
20
11
  class Foo
21
12
  def self.json_create(object)
22
13
  "Foo"
@@ -49,6 +40,8 @@ class TestJSONDecoding < ActiveSupport::TestCase
49
40
  # needs to be *exact*
50
41
  %({"a": " 2007-01-01 01:12:34 Z "}) => { "a" => " 2007-01-01 01:12:34 Z " },
51
42
  %({"a": "2007-01-01 : it's your birthday"}) => { "a" => "2007-01-01 : it's your birthday" },
43
+ %({"a": "Today is:\\n2020-05-21"}) => { "a" => "Today is:\n2020-05-21" },
44
+ %({"a": "2007-01-01 01:12:34 Z\\nwas my birthday"}) => { "a" => "2007-01-01 01:12:34 Z\nwas my birthday" },
52
45
  %([]) => [],
53
46
  %({}) => {},
54
47
  %({"a":1}) => { "a" => 1 },
@@ -122,7 +115,6 @@ class TestJSONDecoding < ActiveSupport::TestCase
122
115
  end
123
116
 
124
117
  private
125
-
126
118
  def with_parse_json_times(value)
127
119
  old_value = ActiveSupport.parse_json_times
128
120
  ActiveSupport.parse_json_times = value
@@ -3,28 +3,14 @@
3
3
  require "securerandom"
4
4
  require_relative "abstract_unit"
5
5
  require "active_support/core_ext/string/inflections"
6
- require "active_support/core_ext/regexp"
7
6
  require "active_support/json"
8
7
  require "active_support/time"
9
8
  require_relative "time_zone_test_helpers"
10
9
  require_relative "encoding_test_cases"
11
10
 
12
- require 'oj'
13
-
14
- # Sets the ActiveSupport encoder to be Oj and also wraps the setting of
15
- # globals.
16
- Oj::Rails.set_encoder()
17
- #Oj::Rails.optimize(Hash, Array, BigDecimal, Time, Range, Regexp, ActiveSupport::TimeWithZone)
18
- Oj::Rails.optimize()
19
-
20
11
  class TestJSONEncoding < ActiveSupport::TestCase
21
12
  include TimeZoneTestHelpers
22
13
 
23
- # Added for testing if Oj is used.
24
- test "oj is used as an encoder" do
25
- assert_equal ActiveSupport.json_encoder, Oj::Rails::Encoder
26
- end
27
-
28
14
  def sorted_json(json)
29
15
  if json.start_with?("{") && json.end_with?("}")
30
16
  "{" + json[1..-2].split(",").sort.join(",") + "}"
@@ -35,20 +21,18 @@ class TestJSONEncoding < ActiveSupport::TestCase
35
21
 
36
22
  JSONTest::EncodingTestCases.constants.each do |class_tests|
37
23
  define_method("test_#{class_tests[0..-6].underscore}") do
38
- begin
39
- prev = ActiveSupport.use_standard_json_time_format
40
-
41
- standard_class_tests = /Standard/.match?(class_tests)
42
-
43
- ActiveSupport.escape_html_entities_in_json = !standard_class_tests
44
- ActiveSupport.use_standard_json_time_format = standard_class_tests
45
- JSONTest::EncodingTestCases.const_get(class_tests).each do |pair|
46
- assert_equal pair.last, sorted_json(ActiveSupport::JSON.encode(pair.first))
47
- end
48
- ensure
49
- ActiveSupport.escape_html_entities_in_json = false
50
- ActiveSupport.use_standard_json_time_format = prev
24
+ prev = ActiveSupport.use_standard_json_time_format
25
+
26
+ standard_class_tests = /Standard/.match?(class_tests)
27
+
28
+ ActiveSupport.escape_html_entities_in_json = !standard_class_tests
29
+ ActiveSupport.use_standard_json_time_format = standard_class_tests
30
+ JSONTest::EncodingTestCases.const_get(class_tests).each do |pair|
31
+ assert_equal pair.last, sorted_json(ActiveSupport::JSON.encode(pair.first))
51
32
  end
33
+ ensure
34
+ ActiveSupport.escape_html_entities_in_json = false
35
+ ActiveSupport.use_standard_json_time_format = prev
52
36
  end
53
37
  end
54
38
 
@@ -78,12 +62,6 @@ class TestJSONEncoding < ActiveSupport::TestCase
78
62
  end
79
63
 
80
64
  def test_utf8_string_encoded_properly
81
- # The original test seems to expect that
82
- # ActiveSupport.escape_html_entities_in_json reverts to true even after
83
- # being set to false. I haven't been able to figure that out so the value is
84
- # set to true, the default, before running the test. This might be wrong but
85
- # for now it will have to do.
86
- ActiveSupport.escape_html_entities_in_json = true
87
65
  result = ActiveSupport::JSON.encode("€2.99")
88
66
  assert_equal '"€2.99"', result
89
67
  assert_equal(Encoding::UTF_8, result.encoding)
@@ -177,6 +155,15 @@ class TestJSONEncoding < ActiveSupport::TestCase
177
155
  assert_equal({ "foo" => "hello" }, JSON.parse(json))
178
156
  end
179
157
 
158
+ def test_struct_to_json_with_options_nested
159
+ klass = Struct.new(:foo, :bar)
160
+ struct = klass.new "hello", "world"
161
+ parent_struct = klass.new struct, "world"
162
+ json = parent_struct.to_json only: [:foo]
163
+
164
+ assert_equal({ "foo" => { "foo" => "hello" } }, JSON.parse(json))
165
+ end
166
+
180
167
  def test_hash_should_pass_encoding_options_to_children_in_as_json
181
168
  person = {
182
169
  name: "John",
@@ -478,7 +465,6 @@ EXPECTED
478
465
  end
479
466
 
480
467
  private
481
-
482
468
  def object_keys(json_object)
483
469
  json_object[1..-2].scan(/([^{}:,\s]+):/).flatten.sort
484
470
  end
@@ -68,6 +68,10 @@ module JSONTest
68
68
  [ :this, %("this") ],
69
69
  [ :"a b", %("a b") ]]
70
70
 
71
+ ModuleTests = [[ Module, %("Module") ],
72
+ [ Class, %("Class") ],
73
+ [ ActiveSupport, %("ActiveSupport") ],
74
+ [ ActiveSupport::MessageEncryptor, %("ActiveSupport::MessageEncryptor") ]]
71
75
  ObjectTests = [[ Foo.new(1, 2), %({\"a\":1,\"b\":2}) ]]
72
76
  HashlikeTests = [[ Hashlike.new, %({\"bar\":\"world\",\"foo\":\"hello\"}) ]]
73
77
  StructTests = [[ MyStruct.new(:foo, "bar"), %({\"name\":\"foo\",\"value\":\"bar\"}) ],
@@ -86,6 +90,8 @@ module JSONTest
86
90
 
87
91
  PathnameTests = [[ Pathname.new("lib/index.rb"), %("lib/index.rb") ]]
88
92
 
93
+ IPAddrTests = [[ IPAddr.new("127.0.0.1"), %("127.0.0.1") ]]
94
+
89
95
  DateTests = [[ Date.new(2005, 2, 1), %("2005/02/01") ]]
90
96
  TimeTests = [[ Time.utc(2005, 2, 1, 15, 15, 10), %("2005/02/01 15:15:10 +0000") ]]
91
97
  DateTimeTests = [[ DateTime.civil(2005, 2, 1, 15, 15, 10), %("2005/02/01 15:15:10 +0000") ]]
@@ -36,4 +36,12 @@ module TimeZoneTestHelpers
36
36
  ActiveSupport::TimeZone::MAPPING.clear
37
37
  ActiveSupport::TimeZone::MAPPING.merge!(old_mappings)
38
38
  end
39
+
40
+ def with_utc_to_local_returns_utc_offset_times(value)
41
+ old_tzinfo2_format = ActiveSupport.utc_to_local_returns_utc_offset_times
42
+ ActiveSupport.utc_to_local_returns_utc_offset_times = value
43
+ yield
44
+ ensure
45
+ ActiveSupport.utc_to_local_returns_utc_offset_times = old_tzinfo2_format
46
+ end
39
47
  end
data/test/files.rb CHANGED
@@ -1,29 +1,29 @@
1
1
  #!/usr/bin/env ruby -wW2
2
+ # frozen_string_literal: true
2
3
 
3
- if $0 == __FILE__
4
- $: << '.'
5
- $: << '..'
6
- $: << '../lib'
7
- $: << '../ext'
4
+ if $PROGRAM_NAME == __FILE__
5
+ $LOAD_PATH << '.'
6
+ $LOAD_PATH << '..'
7
+ $LOAD_PATH << '../lib'
8
+ $LOAD_PATH << '../ext'
8
9
  end
9
10
 
10
- require 'pp'
11
11
  require 'sample/file'
12
12
  require 'sample/dir'
13
13
 
14
14
  def files(dir)
15
- d = ::Sample::Dir.new(dir)
15
+ d = Sample::Dir.new(dir)
16
16
  Dir.new(dir).each do |fn|
17
17
  next if fn.start_with?('.')
18
+
18
19
  filename = File.join(dir, fn)
19
- #filename = '.' == dir ? fn : File.join(dir, fn)
20
- if File.directory?(filename)
21
- d << files(filename)
22
- else
23
- d << ::Sample::File.new(filename)
24
- end
20
+ # filename = '.' == dir ? fn : File.join(dir, fn)
21
+ d << if File.directory?(filename)
22
+ files(filename)
23
+ else
24
+ Sample::File.new(filename)
25
+ end
25
26
  end
26
- #pp d
27
+ # pp d
27
28
  d
28
29
  end
29
-
data/test/foo.rb CHANGED
@@ -1,20 +1,20 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- $: << '.'
4
- $: << File.join(File.dirname(__FILE__), "../lib")
5
- $: << File.join(File.dirname(__FILE__), "../ext")
4
+ $LOAD_PATH << '.'
5
+ $LOAD_PATH << File.join(__dir__, '../lib')
6
+ $LOAD_PATH << File.join(__dir__, '../ext')
6
7
 
7
- # require 'json'
8
+ require 'active_support'
9
+ require 'json'
8
10
  require 'oj'
9
- Oj.mimic_JSON
10
11
 
11
- source = %( {"a": 1, "b": 2} )
12
- puts "JSON.load, no symbolize => OK"
13
- pp JSON.load( source )
14
- puts "JSON.load, do symbolize => KO: keys are not symbols"
15
- #pp JSON.load( source, nil, symbolize_names: true, create_additions: false )
16
- pp JSON.load( source, nil, symbolize_names: true, create_additions: false )
17
- puts "JSON.parse, no symbolize => OK"
18
- pp JSON.parse( source )
19
- puts "JSON.parse, do symbolize => OK"
20
- pp JSON.parse( source, symbolize_names: true )
12
+ Oj.mimic_JSON()
13
+ # Oj::Rails.mimic_JSON()
14
+
15
+ begin
16
+ ::JSON.parse('{ "foo": 84e }')
17
+ #::JSON.parse('{ "foo": 84eb234 }')
18
+ rescue Exception => e
19
+ puts "#{e.class}: #{e.message}"
20
+ end
data/test/helper.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- #
2
+
3
3
  # Ubuntu does not accept arguments to ruby when called using env. To get warnings to show up the -w options is
4
4
  # required. That can be set in the RUBYOPT environment variable.
5
5
  # export RUBYOPT=-w
@@ -15,22 +15,25 @@ require 'minitest/autorun'
15
15
  require 'stringio'
16
16
  require 'date'
17
17
  require 'bigdecimal'
18
- require 'pp'
19
18
  require 'oj'
20
19
 
21
-
22
- if defined?(GC.verify_compaction_references) == 'method'
20
+ def verify_gc_compaction
23
21
  # This method was added in Ruby 3.0.0. Calling it this way asks the GC to
24
22
  # move objects around, helping to find object movement bugs.
25
- GC.verify_compaction_references(double_heap: true, toward: :empty)
23
+ if defined?(GC.verify_compaction_references) == 'method' && RbConfig::CONFIG['host_os'] !~ /(mingw|mswin)/
24
+ if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.2.0')
25
+ GC.verify_compaction_references(expand_heap: true, toward: :empty)
26
+ else
27
+ GC.verify_compaction_references(double_heap: true, toward: :empty)
28
+ end
29
+ end
26
30
  end
27
31
 
28
-
29
32
  $ruby = RUBY_DESCRIPTION.split(' ')[0]
30
33
  $ruby = 'ree' if 'ruby' == $ruby && RUBY_DESCRIPTION.include?('Ruby Enterprise Edition')
31
34
 
32
35
  class Range
33
- def to_hash()
34
- { 'begin' => self.begin, 'end' => self.end, 'exclude_end' => self.exclude_end? }
36
+ def to_hash
37
+ { 'begin' => self.begin, 'end' => self.end, 'exclude_end' => exclude_end? }
35
38
  end
36
39
  end
@@ -21,7 +21,7 @@ class SharedMimicTest < Minitest::Test
21
21
  alias == eql?
22
22
 
23
23
  def as_json()
24
- {"json_class" => self.class.to_s,"x" => @x,"y" => @y}
24
+ {"json_class" => self.class.to_s, "x" => @x, "y" => @y}
25
25
  end
26
26
 
27
27
  def self.json_create(h)
@@ -207,7 +207,6 @@ class SharedMimicTest < Minitest::Test
207
207
  --],#
208
208
  --"a"~:*1#
209
209
  }} == json)
210
-
211
210
  end
212
211
 
213
212
  # fast_generate
@@ -261,6 +260,8 @@ class SharedMimicTest < Minitest::Test
261
260
  obj = JSON.parse(json, :create_additions => true)
262
261
  JSON.create_id = 'json_class'
263
262
  assert_equal(jam, obj)
263
+
264
+ assert_nothing_raised { JSON.create_id = nil }
264
265
  end
265
266
  def test_parse_bang
266
267
  json = %{{"a":1,"b":[true,false]}}
@@ -191,9 +191,9 @@ class JSONAdditionTest < Test::Unit::TestCase
191
191
  assert_equal d, JSON.parse(d.to_json, :create_additions => true)
192
192
  d = DateTime.parse(now.utc.to_s) # of = 0
193
193
  assert_equal d, JSON.parse(d.to_json, :create_additions => true)
194
- d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(1,24))
194
+ d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(1, 24))
195
195
  assert_equal d, JSON.parse(d.to_json, :create_additions => true)
196
- d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(12,24))
196
+ d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(12, 24))
197
197
  assert_equal d, JSON.parse(d.to_json, :create_additions => true)
198
198
  end
199
199
 
@@ -29,7 +29,7 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase
29
29
  #@json = '{"a":2,"b":5.23683071,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},'\
30
30
  #'"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
31
31
  @json = '{"a":2,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},'\
32
- '"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
32
+ '"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
33
33
  end
34
34
 
35
35
  def test_index
@@ -43,15 +43,15 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase
43
43
  # functionality and perform better.
44
44
 
45
45
  def test_parser
46
- assert_match /::Parser\z/, JSON.parser.name
46
+ assert_match(/::Parser\z/, JSON.parser.name)
47
47
  end
48
48
 
49
49
  def test_generator
50
- assert_match /::Generator\z/, JSON.generator.name
50
+ assert_match(/::Generator\z/, JSON.generator.name)
51
51
  end
52
52
 
53
53
  def test_state
54
- assert_match /::Generator::State\z/, JSON.state.name
54
+ assert_match(/::Generator::State\z/, JSON.state.name)
55
55
  end
56
56
 
57
57
  # This doesn't have anything to do with JSON parsing or generation. It seems
@@ -128,8 +128,10 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase
128
128
  too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
129
129
  assert_equal too_deep, JSON.dump(eval(too_deep))
130
130
  assert_kind_of String, Marshal.dump(eval(too_deep))
131
- assert_raise(ArgumentError) { JSON.dump(eval(too_deep), 100) }
132
- assert_raise(ArgumentError) { Marshal.dump(eval(too_deep), 100) }
131
+ if RUBY_ENGINE != 'truffleruby'
132
+ assert_raise(ArgumentError) { JSON.dump(eval(too_deep), 100) }
133
+ assert_raise(ArgumentError) { Marshal.dump(eval(too_deep), 100) }
134
+ end
133
135
  assert_equal too_deep, JSON.dump(eval(too_deep), 101)
134
136
  assert_kind_of String, Marshal.dump(eval(too_deep), 101)
135
137
  output = StringIO.new
File without changes
@@ -2,6 +2,7 @@
2
2
  # encoding: UTF-8
3
3
 
4
4
  #frozen_string_literal: false
5
+
5
6
  require 'json_gem/test_helper'
6
7
 
7
8
  class JSONExtParserTest < Test::Unit::TestCase
@@ -2,6 +2,7 @@
2
2
  # encoding: UTF-8
3
3
 
4
4
  #frozen_string_literal: false
5
+
5
6
  require 'json_gem/test_helper'
6
7
 
7
8
  class JSONFixturesTest < Test::Unit::TestCase
@@ -16,7 +17,7 @@ class JSONFixturesTest < Test::Unit::TestCase
16
17
  for name, source in @passed
17
18
  begin
18
19
  assert JSON.parse(source),
19
- "Did not pass for fixture '#{name}': #{source.inspect}"
20
+ "Did not pass for fixture '#{name}': #{source.inspect}"
20
21
  rescue => e
21
22
  warn "\nCaught #{e.class}(#{e}) for fixture '#{name}': #{source.inspect}\n#{e.backtrace * "\n"}"
22
23
  raise e
@@ -27,7 +28,7 @@ class JSONFixturesTest < Test::Unit::TestCase
27
28
  def test_failing
28
29
  for name, source in @failed
29
30
  assert_raise(JSON::ParserError, JSON::NestingError,
30
- "Did not fail for fixture '#{name}': #{source.inspect}") do
31
+ "Did not fail for fixture '#{name}': #{source.inspect}") do
31
32
  JSON.parse(source)
32
33
  end
33
34
  end