oj 3.13.11 → 3.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +74 -0
  3. data/README.md +4 -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 +17 -24
  10. data/ext/oj/code.h +2 -2
  11. data/ext/oj/compat.c +17 -44
  12. data/ext/oj/custom.c +70 -141
  13. data/ext/oj/debug.c +3 -9
  14. data/ext/oj/dump.c +128 -118
  15. data/ext/oj/dump.h +12 -8
  16. data/ext/oj/dump_compat.c +564 -641
  17. data/ext/oj/dump_leaf.c +17 -63
  18. data/ext/oj/dump_object.c +70 -199
  19. data/ext/oj/dump_strict.c +22 -46
  20. data/ext/oj/encoder.c +1 -1
  21. data/ext/oj/err.c +2 -13
  22. data/ext/oj/err.h +9 -12
  23. data/ext/oj/extconf.rb +14 -5
  24. data/ext/oj/fast.c +75 -103
  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 +75 -47
  30. data/ext/oj/object.c +49 -66
  31. data/ext/oj/odd.c +89 -67
  32. data/ext/oj/odd.h +15 -15
  33. data/ext/oj/oj.c +140 -99
  34. data/ext/oj/oj.h +80 -51
  35. data/ext/oj/parse.c +162 -184
  36. data/ext/oj/parse.h +7 -10
  37. data/ext/oj/parser.c +89 -34
  38. data/ext/oj/parser.h +18 -7
  39. data/ext/oj/rails.c +82 -146
  40. data/ext/oj/rails.h +1 -1
  41. data/ext/oj/reader.c +11 -12
  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 +20 -31
  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 -21
  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 +31 -68
  62. data/lib/oj/active_support_helper.rb +0 -1
  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 +4 -2
  67. data/lib/oj/mimic.rb +4 -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/Options.md +10 -0
  75. data/test/_test_active.rb +8 -9
  76. data/test/_test_active_mimic.rb +7 -8
  77. data/test/_test_mimic_rails.rb +17 -20
  78. data/test/activerecord/result_test.rb +5 -6
  79. data/test/{activesupport5 → activesupport7}/abstract_unit.rb +16 -12
  80. data/test/{activesupport5 → activesupport7}/decoding_test.rb +2 -10
  81. data/test/{activesupport5 → activesupport7}/encoding_test.rb +20 -34
  82. data/test/{activesupport5 → activesupport7}/encoding_test_cases.rb +6 -0
  83. data/test/{activesupport5 → activesupport7}/time_zone_test_helpers.rb +8 -0
  84. data/test/files.rb +15 -15
  85. data/test/foo.rb +9 -71
  86. data/test/helper.rb +11 -8
  87. data/test/isolated/shared.rb +3 -2
  88. data/test/json_gem/json_addition_test.rb +2 -2
  89. data/test/json_gem/json_common_interface_test.rb +4 -4
  90. data/test/json_gem/json_encoding_test.rb +0 -0
  91. data/test/json_gem/json_ext_parser_test.rb +1 -0
  92. data/test/json_gem/json_fixtures_test.rb +3 -2
  93. data/test/json_gem/json_generator_test.rb +48 -36
  94. data/test/json_gem/json_generic_object_test.rb +11 -11
  95. data/test/json_gem/json_parser_test.rb +54 -47
  96. data/test/json_gem/json_string_matching_test.rb +9 -9
  97. data/test/json_gem/test_helper.rb +7 -3
  98. data/test/mem.rb +13 -12
  99. data/test/perf.rb +21 -26
  100. data/test/perf_compat.rb +31 -33
  101. data/test/perf_dump.rb +50 -0
  102. data/test/perf_fast.rb +80 -82
  103. data/test/perf_file.rb +27 -29
  104. data/test/perf_object.rb +65 -69
  105. data/test/perf_once.rb +12 -11
  106. data/test/perf_parser.rb +42 -48
  107. data/test/perf_saj.rb +46 -54
  108. data/test/perf_scp.rb +57 -69
  109. data/test/perf_simple.rb +41 -39
  110. data/test/perf_strict.rb +68 -70
  111. data/test/perf_wab.rb +67 -69
  112. data/test/prec.rb +3 -3
  113. data/test/sample/change.rb +0 -1
  114. data/test/sample/dir.rb +0 -1
  115. data/test/sample/doc.rb +0 -1
  116. data/test/sample/file.rb +0 -1
  117. data/test/sample/group.rb +0 -1
  118. data/test/sample/hasprops.rb +0 -1
  119. data/test/sample/layer.rb +0 -1
  120. data/test/sample/rect.rb +0 -1
  121. data/test/sample/shape.rb +0 -1
  122. data/test/sample/text.rb +0 -1
  123. data/test/sample.rb +16 -16
  124. data/test/sample_json.rb +8 -8
  125. data/test/test_compat.rb +76 -42
  126. data/test/test_custom.rb +72 -51
  127. data/test/test_debian.rb +7 -10
  128. data/test/test_fast.rb +86 -90
  129. data/test/test_file.rb +41 -30
  130. data/test/test_gc.rb +16 -5
  131. data/test/test_generate.rb +5 -5
  132. data/test/test_hash.rb +4 -4
  133. data/test/test_integer_range.rb +9 -9
  134. data/test/test_null.rb +20 -20
  135. data/test/test_object.rb +85 -96
  136. data/test/test_parser.rb +6 -22
  137. data/test/test_parser_debug.rb +27 -0
  138. data/test/test_parser_saj.rb +115 -23
  139. data/test/test_parser_usual.rb +6 -6
  140. data/test/test_rails.rb +2 -2
  141. data/test/test_saj.rb +10 -8
  142. data/test/test_scp.rb +37 -39
  143. data/test/test_strict.rb +30 -32
  144. data/test/test_various.rb +147 -99
  145. data/test/test_wab.rb +48 -44
  146. data/test/test_writer.rb +47 -47
  147. data/test/tests.rb +13 -4
  148. data/test/tests_mimic.rb +12 -3
  149. data/test/tests_mimic_addition.rb +12 -3
  150. metadata +33 -144
  151. data/test/activesupport4/decoding_test.rb +0 -108
  152. data/test/activesupport4/encoding_test.rb +0 -531
  153. data/test/activesupport4/test_helper.rb +0 -41
  154. data/test/activesupport5/test_helper.rb +0 -72
  155. data/test/bar.rb +0 -16
  156. data/test/baz.rb +0 -16
  157. data/test/bug.rb +0 -16
  158. data/test/zoo.rb +0 -13
data/lib/oj/saj.rb CHANGED
@@ -1,11 +1,17 @@
1
1
  module Oj
2
- # A SAX style parse handler for JSON hence the acronym SAJ for Simple API for
3
- # JSON. The Oj::Saj handler class should be subclassed and then used with the
4
- # Oj::Saj key_parse() method. The Saj methods will then be called as the file
5
- # is parsed.
2
+ # A SAX style parse handler for JSON hence the acronym SAJ for Simple API
3
+ # for JSON. The Oj::Saj handler class can be subclassed and then used with
4
+ # the Oj::Saj key_parse() method or with the more resent
5
+ # Oj::Parser.new(:saj). The Saj methods will then be called as the file is
6
+ # parsed.
7
+ #
8
+ # With Oj::Parser.new(:saj) each method can also include a line and column
9
+ # argument so hash_start(key) could also be hash_start(key, line,
10
+ # column). The error() method is no used with Oj::Parser.new(:saj) so it
11
+ # will never be called.
6
12
  #
7
13
  # @example
8
- #
14
+ #
9
15
  # require 'oj'
10
16
  #
11
17
  # class MySaj < ::Oj::Saj
@@ -23,6 +29,14 @@ module Oj
23
29
  # Oj.saj_parse(cnt, f)
24
30
  # end
25
31
  #
32
+ # or
33
+ #
34
+ # p = Oj::Parser.new(:saj)
35
+ # p.handler = MySaj.new()
36
+ # File.open('any.json', 'r') do |f|
37
+ # p.parse(f.read)
38
+ # end
39
+ #
26
40
  # To make the desired methods active while parsing the desired method should
27
41
  # be made public in the subclasses. If the methods remain private they will
28
42
  # not be called during parsing.
@@ -61,6 +75,6 @@ module Oj
61
75
 
62
76
  def error(message, line, column)
63
77
  end
64
-
78
+
65
79
  end # Saj
66
80
  end # Oj
data/lib/oj/state.rb CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  module JSON
3
2
  module Ext
4
3
  module Generator
@@ -59,6 +58,7 @@ module JSON
59
58
 
60
59
  def configure(opts)
61
60
  raise TypeError.new('expected a Hash') unless opts.respond_to?(:to_h)
61
+
62
62
  @attrs.merge!(opts.to_h)
63
63
  end
64
64
 
@@ -80,10 +80,11 @@ module JSON
80
80
  # @param [Symbol] m method symbol
81
81
  # @return [Boolean] true for any method that matches an instance
82
82
  # variable reader, otherwise false.
83
- def respond_to?(m)
83
+ def respond_to?(m, include_all = false)
84
84
  return true if super
85
85
  return true if has_key?(key)
86
86
  return true if has_key?(key.to_s)
87
+
87
88
  has_key?(key.to_sym)
88
89
  end
89
90
 
@@ -113,15 +114,17 @@ module JSON
113
114
  def method_missing(m, *args, &block)
114
115
  if m.to_s.end_with?('=')
115
116
  raise ArgumentError.new("wrong number of arguments (#{args.size} for 1 with #{m}) to method #{m}") if args.nil? or 1 != args.length
117
+
116
118
  m = m.to_s[0..-2]
117
119
  m = m.to_sym
118
120
  return @attrs.store(m, args[0])
119
- end
120
- if @attrs.has_key?(m.to_sym)
121
+ end
122
+ if @attrs.has_key?(m.to_sym)
121
123
  raise ArgumentError.new("wrong number of arguments (#{args.size} for 0 with #{m}) to method #{m}") unless args.nil? or args.empty?
124
+
122
125
  return @attrs[m.to_sym]
123
- end
124
- return @attrs.send(m, *args, &block)
126
+ end
127
+ return @attrs.send(m, *args, &block)
125
128
  end
126
129
 
127
130
  end # State
data/lib/oj/version.rb CHANGED
@@ -1,5 +1,4 @@
1
-
2
1
  module Oj
3
2
  # Current version of the module.
4
- VERSION = '3.13.11'
3
+ VERSION = '3.15.0'
5
4
  end
data/lib/oj.rb CHANGED
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
1
2
 
3
+ # Oj module is defined in oj.c.
2
4
  module Oj
3
5
  end
4
6
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  **Ruby**
4
4
 
5
- Oj is compatible with Ruby 2.0.0, 2.1, 2.2, 2.3, 2.4 and RBX.
5
+ Oj is compatible with Ruby 2.4+ and RBX.
6
6
  Support for JRuby has been removed as JRuby no longer supports C extensions and
7
7
  there are bugs in the older versions that are not being fixed.
8
8
 
@@ -0,0 +1,20 @@
1
+ # Oj Install Options
2
+
3
+ ### Enable trace log
4
+
5
+ ```
6
+ $ gem install oj -- --enable-trace-log
7
+ ```
8
+
9
+ To enable Oj trace feature, it uses `--enable-trace-log` option when installing the gem.
10
+ Then, the trace logs will be displayed when `:trace` option is set to `true`.
11
+
12
+
13
+ ### Enable SIMD instructions
14
+
15
+ ```
16
+ $ gem install oj -- --with-sse42
17
+ ```
18
+
19
+ To enable the use of SIMD instructions in Oj, it uses the `--with-sse42` option when installing the gem.
20
+ This will enable the use of the SSE4.2 instructions in the internal.
data/pages/Options.md CHANGED
@@ -66,6 +66,10 @@ Determines how to load decimals.
66
66
 
67
67
  - `:auto` the most precise for the number of digits is used.
68
68
 
69
+ - `:fast` faster conversion to Float.
70
+
71
+ - `:ruby` convert to Float using the Ruby `to_f` conversion.
72
+
69
73
  This can also be set with `:decimal_class` when used as a load or
70
74
  parse option to match the JSON gem. In that case either `Float`,
71
75
  `BigDecimal`, or `nil` can be provided.
@@ -154,6 +158,8 @@ Determines the characters to escape when dumping. Only the :ascii and
154
158
 
155
159
  - `:json` follows the JSON specification. This is the default mode.
156
160
 
161
+ - `:slash` escapes `/` characters.
162
+
157
163
  - `:xss_safe` escapes HTML and XML characters such as `&` and `<`.
158
164
 
159
165
  - `:ascii` escapes all non-ascii or characters with the hi-bit set.
@@ -259,6 +265,10 @@ to true.
259
265
 
260
266
  The number of digits after the decimal when dumping the seconds of time.
261
267
 
268
+ ### :skip_null_byte [Boolean]
269
+
270
+ If true, null bytes in strings will be omitted when dumping.
271
+
262
272
  ### :space
263
273
 
264
274
  String inserted after the ':' character when dumping a JSON object. The
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
-