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/test/perf.rb CHANGED
@@ -1,7 +1,8 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  class Perf
3
4
 
4
- def initialize()
5
+ def initialize
5
6
  @items = []
6
7
  end
7
8
 
@@ -19,7 +20,7 @@ class Perf
19
20
  end
20
21
 
21
22
  def run(iter)
22
- base = Item.new(nil, nil) { }
23
+ base = Item.new(nil, nil) {}
23
24
  base.run(iter, 0.0)
24
25
  @items.each do |i|
25
26
  i.run(iter, base.duration)
@@ -32,35 +33,31 @@ class Perf
32
33
  summary()
33
34
  end
34
35
 
35
- def summary()
36
- fastest = nil
37
- slowest = nil
36
+ def summary
38
37
  width = 6
39
38
  @items.each do |i|
40
39
  next if i.duration.nil?
40
+
41
41
  width = i.title.size if width < i.title.size
42
42
  end
43
43
  iva = @items.clone
44
44
  iva.delete_if { |i| i.duration.nil? }
45
45
  iva = iva.sort_by { |i| i.duration }
46
46
  puts
47
- puts "Summary:"
48
- puts "%*s time (secs) rate (ops/sec)" % [width, 'System']
47
+ puts 'Summary:'
48
+ puts '%*s time (secs) rate (ops/sec)' % [width, 'System']
49
49
  puts "#{'-' * width} ----------- --------------"
50
50
  iva.each do |i|
51
- if i.duration.nil?
52
- else
53
- puts "%*s %11.3f %14.3f" % [width, i.title, i.duration, i.rate ]
54
- end
51
+ puts '%*s %11.3f %14.3f' % [width, i.title, i.duration, i.rate ] unless i.duration.nil?
55
52
  end
56
53
  puts
57
54
  puts "Comparison Matrix\n(performance factor, 2.0 means row is twice as fast as column)"
58
- puts ([' ' * width] + iva.map { |i| "%*s" % [width, i.title] }).join(' ')
59
- puts (['-' * width] + iva.map { |i| '-' * width }).join(' ')
55
+ puts ([' ' * width] + iva.map { |i| '%*s' % [width, i.title] }).join(' ')
56
+ puts (['-' * width] + iva.map { |_i| '-' * width }).join(' ')
60
57
  iva.each do |i|
61
- line = ["%*s" % [width, i.title]]
58
+ line = ['%*s' % [width, i.title]]
62
59
  iva.each do |o|
63
- line << "%*.2f" % [width, o.duration / i.duration]
60
+ line << ('%*.2f' % [width, o.duration / i.duration])
64
61
  end
65
62
  puts line.join(' ')
66
63
  end
@@ -90,17 +87,15 @@ class Perf
90
87
  end
91
88
 
92
89
  def run(iter, base)
93
- begin
94
- GC.start
95
- @before.call unless @before.nil?
96
- start = Time.now
97
- iter.times { @blk.call }
98
- @duration = Time.now - start - base
99
- @duration = 0.0 if @duration < 0.0
100
- @rate = iter / @duration
101
- rescue Exception => e
102
- @error = "#{e.class}: #{e.message}"
103
- end
90
+ GC.start
91
+ @before.call unless @before.nil?
92
+ start = Time.now
93
+ iter.times { @blk.call }
94
+ @duration = Time.now - start - base
95
+ @duration = 0.0 if @duration < 0.0
96
+ @rate = iter / @duration
97
+ rescue Exception => e
98
+ @error = "#{e.class}: #{e.message}"
104
99
  end
105
100
 
106
101
  end # Item
data/test/perf_compat.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
- # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
- $: << '.'
5
- $: << File.join(File.dirname(__FILE__), "../lib")
6
- $: << File.join(File.dirname(__FILE__), "../ext")
4
+ $LOAD_PATH << '.'
5
+ $LOAD_PATH << File.join(__dir__, '../lib')
6
+ $LOAD_PATH << File.join(__dir__, '../ext')
7
7
 
8
8
  require 'optparse'
9
9
  require 'perf'
@@ -11,25 +11,23 @@ require 'oj'
11
11
 
12
12
  $verbose = false
13
13
  $indent = 0
14
- $iter = 20000
14
+ $iter = 20_000
15
15
  $size = 0
16
16
 
17
17
  opts = OptionParser.new
18
- opts.on("-v", "verbose") { $verbose = true }
19
- opts.on("-c", "--count [Int]", Integer, "iterations") { |i| $iter = i }
20
- opts.on("-i", "--indent [Int]", Integer, "indentation") { |i| $indent = i }
21
- opts.on("-s", "--size [Int]", Integer, "size (~Kbytes)") { |i| $size = i }
22
- opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
23
- files = opts.parse(ARGV)
18
+ opts.on('-v', 'verbose') { $verbose = true }
19
+ opts.on('-c', '--count [Int]', Integer, 'iterations') { |i| $iter = i }
20
+ opts.on('-i', '--indent [Int]', Integer, 'indentation') { |i| $indent = i }
21
+ opts.on('-s', '--size [Int]', Integer, 'size (~Kbytes)') { |i| $size = i }
22
+ opts.on('-h', '--help', 'Show this display') { puts opts; Process.exit!(0) }
23
+ opts.parse(ARGV)
24
24
 
25
25
  def capture_error(tag, orig, load_key, dump_key, &blk)
26
- begin
27
- obj = blk.call(orig)
28
- puts obj unless orig == obj
29
- raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
30
- rescue Exception => e
31
- $failed[tag] = "#{e.class}: #{e.message}"
32
- end
26
+ obj = blk.call(orig)
27
+ puts obj unless orig == obj
28
+ raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
29
+ rescue Exception => e
30
+ $failed[tag] = "#{e.class}: #{e.message}"
33
31
  end
34
32
 
35
33
  # Verify that all packages dump and load correctly and return the same Object as the original.
@@ -39,7 +37,7 @@ capture_error('JSON::Ext', $obj, 'generate', 'parse') { |o|
39
37
  require 'json/ext'
40
38
  JSON.generator = JSON::Ext::Generator
41
39
  JSON.parser = JSON::Ext::Parser
42
- JSON.load(JSON.generate(o))
40
+ JSON.parse(JSON.generate(o))
43
41
  }
44
42
 
45
43
  module One
@@ -47,7 +45,7 @@ module One
47
45
  module Three
48
46
  class Empty
49
47
 
50
- def initialize()
48
+ def initialize
51
49
  @a = 1
52
50
  @b = 2
53
51
  @c = 3
@@ -58,16 +56,16 @@ module One
58
56
  end
59
57
  alias == eql?
60
58
 
61
- def as_json(*a)
59
+ def as_json(*_a)
62
60
  {JSON.create_id => self.class.name, 'a' => @a, 'b' => @b, 'c' => @c }
63
61
  end
64
-
65
- def to_json(*a)
62
+
63
+ def to_json(*_a)
66
64
  JSON.generate(as_json())
67
65
  end
68
66
 
69
- def self.json_create(h)
70
- self.new()
67
+ def self.json_create(_h)
68
+ new()
71
69
  end
72
70
  end # Empty
73
71
  end # Three
@@ -77,8 +75,8 @@ end # One
77
75
  $obj = {
78
76
  'a' => 'Alpha', # string
79
77
  'b' => true, # boolean
80
- 'c' => 12345, # number
81
- 'd' => [ true, [false, [-123456789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
78
+ 'c' => 12_345, # number
79
+ 'd' => [ true, [false, [-123_456_789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
82
80
  'e' => { 'zero' => nil, 'one' => 1, 'two' => 2, 'three' => [3], 'four' => [0, 1, 2, 3, 4] }, # hash
83
81
  'f' => nil, # nil
84
82
  'g' => One::Two::Three::Empty.new(),
@@ -109,13 +107,13 @@ if $verbose
109
107
  end
110
108
 
111
109
  puts '-' * 80
112
- puts "Compat Parse Performance"
110
+ puts 'Compat Parse Performance'
113
111
  perf = Perf.new()
114
- unless $failed.has_key?('JSON::Ext')
115
- perf.add('JSON::Ext', 'parse') { JSON.load($json) }
112
+ unless $failed.key?('JSON::Ext')
113
+ perf.add('JSON::Ext', 'parse') { JSON.parse($json) }
116
114
  perf.before('JSON::Ext') { JSON.parser = JSON::Ext::Parser }
117
115
  end
118
- unless $failed.has_key?('Oj:compat')
116
+ unless $failed.key?('Oj:compat')
119
117
  perf.add('Oj:compat', 'compat_load') { Oj.compat_load($json) }
120
118
  end
121
119
  perf.run($iter)
@@ -125,6 +123,6 @@ puts '-' * 80
125
123
  puts
126
124
 
127
125
  unless $failed.empty?
128
- puts "The following packages were not included for the reason listed"
129
- $failed.each { |tag,msg| puts "***** #{tag}: #{msg}" }
126
+ puts 'The following packages were not included for the reason listed'
127
+ $failed.each { |tag, msg| puts "***** #{tag}: #{msg}" }
130
128
  end
data/test/perf_dump.rb ADDED
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH << '.'
5
+ $LOAD_PATH << File.join(__dir__, '../lib')
6
+ $LOAD_PATH << File.join(__dir__, '../ext')
7
+
8
+ require 'optparse'
9
+ require 'oj'
10
+
11
+ @verbose = false
12
+ @indent = 2
13
+ @iter = 100_000
14
+ @size = 2
15
+
16
+ opts = OptionParser.new
17
+ opts.on('-v', 'verbose') { @verbose = true }
18
+ opts.on('-c', '--count [Int]', Integer, 'iterations') { |i| @iter = i }
19
+ opts.on('-i', '--indent [Int]', Integer, 'indentation') { |i| @indent = i }
20
+ opts.on('-s', '--size [Int]', Integer, 'size (~Kbytes)') { |i| @size = i }
21
+ opts.on('-h', '--help', 'Show this display') { puts opts; Process.exit!(0) }
22
+ opts.parse(ARGV)
23
+
24
+ @obj = {
25
+ 'a' => 'Alpha', # string
26
+ 'b' => true, # boolean
27
+ 'c' => 12345, # number
28
+ 'd' => [ true, [false, [-123456789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
29
+ 'e' => { 'zero' => nil, 'one' => 1, 'two' => 2, 'three' => [3], 'four' => [0, 1, 2, 3, 4] }, # hash
30
+ 'f' => nil, # nil
31
+ 'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
32
+ 'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
33
+ }
34
+
35
+ Oj.default_options = { :indent => @indent, :mode => :strict }
36
+
37
+ if 0 < @size
38
+ o = @obj
39
+ @obj = []
40
+ (4 * @size).times do
41
+ @obj << o
42
+ end
43
+ end
44
+
45
+ @json = Oj.dump(@obj)
46
+ GC.start
47
+ start = Time.now
48
+ @iter.times { Oj.dump(@obj) }
49
+ duration = Time.now - start
50
+ puts "Dumped #{@json.length} byte JSON #{@iter} times in %0.3f seconds or %0.3f iteration/sec." % [duration, @iter / duration]
data/test/perf_fast.rb CHANGED
@@ -1,35 +1,35 @@
1
1
  #!/usr/bin/env ruby -wW1
2
- # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
- $: << '.'
5
- $: << File.join(File.dirname(__FILE__), "../lib")
6
- $: << File.join(File.dirname(__FILE__), "../ext")
4
+ $LOAD_PATH << '.'
5
+ $LOAD_PATH << File.join(__dir__, '../lib')
6
+ $LOAD_PATH << File.join(__dir__, '../ext')
7
7
 
8
8
  require 'optparse'
9
- #require 'yajl'
9
+ # require 'yajl'
10
10
  require 'perf'
11
11
  require 'json'
12
12
  require 'json/ext'
13
13
  require 'oj'
14
14
 
15
- $verbose = false
16
- $indent = 0
17
- $iter = 100000
18
- $gets = 0
19
- $fetch = false
20
- $write = false
21
- $read = false
15
+ @verbose = false
16
+ @indent = 0
17
+ @iter = 100_000
18
+ @gets = 0
19
+ @fetch = false
20
+ @write = false
21
+ @read = false
22
22
 
23
23
  opts = OptionParser.new
24
- opts.on("-v", "verbose") { $verbose = true }
25
- opts.on("-c", "--count [Int]", Integer, "iterations") { |i| $iter = i }
26
- opts.on("-i", "--indent [Int]", Integer, "indentation") { |i| $indent = i }
27
- opts.on("-g", "--gets [Int]", Integer, "number of gets") { |i| $gets = i }
28
- opts.on("-f", "fetch") { $fetch = true }
29
- opts.on("-w", "write") { $write = true }
30
- opts.on("-r", "read") { $read = true }
31
- opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
32
- files = opts.parse(ARGV)
24
+ opts.on('-v', 'verbose') { @verbose = true }
25
+ opts.on('-c', '--count [Int]', Integer, 'iterations') { |i| @iter = i }
26
+ opts.on('-i', '--indent [Int]', Integer, 'indentation') { |i| @indent = i }
27
+ opts.on('-g', '--gets [Int]', Integer, 'number of gets') { |i| @gets = i }
28
+ opts.on('-f', 'fetch') { @fetch = true }
29
+ opts.on('-w', 'write') { @write = true }
30
+ opts.on('-r', 'read') { @read = true }
31
+ opts.on('-h', '--help', 'Show this display') { puts opts; Process.exit!(0) }
32
+ opts.parse(ARGV)
33
33
 
34
34
  # This just navigates to each leaf of the JSON structure.
35
35
  def dig(obj, &blk)
@@ -37,83 +37,81 @@ def dig(obj, &blk)
37
37
  when Array
38
38
  obj.each { |e| dig(e, &blk) }
39
39
  when Hash
40
- obj.values.each { |e| dig(e, &blk) }
40
+ obj.each_value { |e| dig(e, &blk) }
41
41
  else
42
42
  blk.yield(obj)
43
43
  end
44
44
  end
45
45
 
46
- $obj = {
46
+ @obj = {
47
47
  'a' => 'Alpha', # string
48
48
  'b' => true, # boolean
49
- 'c' => 12345, # number
50
- 'd' => [ true, [false, {'12345' => 12345, 'nil' => nil}, 3.967, { 'x' => 'something', 'y' => false, 'z' => true}, nil]], # mix it up array
49
+ 'c' => 12_345, # number
50
+ 'd' => [ true, [false, {'12345' => 12_345, 'nil' => nil}, 3.967, { 'x' => 'something', 'y' => false, 'z' => true}, nil]], # mix it up array
51
51
  'e' => { 'one' => 1, 'two' => 2 }, # hash
52
52
  'f' => nil, # nil
53
- 'g' => 12345678901234567890123456789, # big number
53
+ 'g' => 12_345_678_901_234_567_890_123_456_789, # big number
54
54
  'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
55
55
  'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
56
56
  }
57
57
 
58
- Oj.default_options = { :indent => $indent, :mode => :compat }
58
+ Oj.default_options = { :indent => @indent, :mode => :compat }
59
59
 
60
- $json = Oj.dump($obj)
61
- $failed = {} # key is same as String used in tests later
60
+ @json = Oj.dump(@obj)
61
+ @failed = {} # key is same as String used in tests later
62
62
 
63
63
  def capture_error(tag, orig, load_key, dump_key, &blk)
64
- begin
65
- obj = blk.call(orig)
66
- raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
67
- rescue Exception => e
68
- $failed[tag] = "#{e.class}: #{e.message}"
69
- end
64
+ obj = blk.call(orig)
65
+ raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
66
+ rescue Exception => e
67
+ @failed[tag] = "#{e.class}: #{e.message}"
70
68
  end
71
69
 
72
70
  # Verify that all packages dump and load correctly and return the same Object as the original.
73
- capture_error('Oj::Doc', $obj, 'load', 'dump') { |o| Oj::Doc.open(Oj.dump(o, :mode => :strict)) { |f| f.fetch() } }
74
- #capture_error('Yajl', $obj, 'encode', 'parse') { |o| Yajl::Parser.parse(Yajl::Encoder.encode(o)) }
75
- capture_error('JSON::Ext', $obj, 'generate', 'parse') { |o| JSON.generator = JSON::Ext::Generator; JSON::Ext::Parser.new(JSON.generate(o)).parse }
71
+ capture_error('Oj::Doc', @obj, 'load', 'dump') { |o| Oj::Doc.open(Oj.dump(o, :mode => :strict)) { |f| f.fetch() } }
72
+ # capture_error('Yajl', @obj, 'encode', 'parse') { |o| Yajl::Parser.parse(Yajl::Encoder.encode(o)) }
73
+ capture_error('JSON::Ext', @obj, 'generate', 'parse') { |o| JSON.generator = JSON::Ext::Generator; JSON::Ext::Parser.new(JSON.generate(o)).parse }
76
74
 
77
- if $verbose
78
- puts "json:\n#{$json}\n"
75
+ if @verbose
76
+ puts "json:\n#{@json}\n"
79
77
  end
80
78
 
81
79
  puts '-' * 80
82
- puts "Parse Performance"
80
+ puts 'Parse Performance'
83
81
  perf = Perf.new()
84
- perf.add('Oj::Doc', 'parse') { Oj::Doc.open($json) {|f| } } unless $failed.has_key?('Oj::Doc')
85
- #perf.add('Yajl', 'parse') { Yajl::Parser.parse($json) } unless $failed.has_key?('Yajl')
86
- perf.add('JSON::Ext', 'parse') { JSON::Ext::Parser.new($json).parse } unless $failed.has_key?('JSON::Ext')
87
- perf.run($iter)
82
+ perf.add('Oj::Doc', 'parse') { Oj::Doc.open(@json) { |f| } } unless @failed.key?('Oj::Doc')
83
+ # perf.add('Yajl', 'parse') { Yajl::Parser.parse(@json) } unless @failed.has_key?('Yajl')
84
+ perf.add('JSON::Ext', 'parse') { JSON::Ext::Parser.new(@json).parse } unless @failed.key?('JSON::Ext')
85
+ perf.run(@iter)
88
86
 
89
87
  puts '-' * 80
90
- puts "JSON generation Performance"
91
- Oj::Doc.open($json) do |doc|
88
+ puts 'JSON generation Performance'
89
+ Oj::Doc.open(@json) do |doc|
92
90
  perf = Perf.new()
93
91
  perf.add('Oj::Doc', 'dump') { doc.dump() }
94
- # perf.add('Yajl', 'encode') { Yajl::Encoder.encode($obj) }
95
- perf.add('JSON::Ext', 'fast_generate') { JSON.fast_generate($obj) }
92
+ # perf.add('Yajl', 'encode') { Yajl::Encoder.encode(@obj) }
93
+ perf.add('JSON::Ext', 'fast_generate') { JSON.fast_generate(@obj) }
96
94
  perf.before('JSON::Ext') { JSON.generator = JSON::Ext::Generator }
97
- perf.run($iter)
95
+ perf.run(@iter)
98
96
  end
99
97
 
100
- if 0 < $gets
98
+ if 0 < @gets
101
99
  puts '-' * 80
102
- puts "Parse and get all values Performance"
100
+ puts 'Parse and get all values Performance'
103
101
  perf = Perf.new()
104
- perf.add('Oj::Doc', 'parse') { Oj::Doc.open($json) {|f| $gets.times { f.each_value() {} } } } unless $failed.has_key?('Oj::Doc')
105
- # perf.add('Yajl', 'parse') { $gets.times { dig(Yajl::Parser.parse($json)) {} } } unless $failed.has_key?('Yajl')
106
- perf.add('JSON::Ext', 'parse') { $gets.times { dig(JSON::Ext::Parser.new($json).parse) {} } } unless $failed.has_key?('JSON::Ext')
107
- perf.run($iter)
102
+ perf.add('Oj::Doc', 'parse') { Oj::Doc.open(@json) { |f| @gets.times { f.each_value() {} } } } unless @failed.key?('Oj::Doc')
103
+ # perf.add('Yajl', 'parse') { @gets.times { dig(Yajl::Parser.parse(@json)) {} } } unless @failed.has_key?('Yajl')
104
+ perf.add('JSON::Ext', 'parse') { @gets.times { dig(JSON::Ext::Parser.new(@json).parse) {} } } unless @failed.key?('JSON::Ext')
105
+ perf.run(@iter)
108
106
  end
109
107
 
110
- if $fetch
108
+ if @fetch
111
109
  puts '-' * 80
112
- puts "fetch nested Performance"
113
- json_hash = Oj.load($json, :mode => :strict)
114
- Oj::Doc.open($json) do |fast|
115
- #puts "*** C fetch: #{fast.fetch('/d/2/4/y')}"
116
- #puts "*** Ruby fetch: #{json_hash.fetch('d', []).fetch(1, []).fetch(3, []).fetch('x', nil)}"
110
+ puts 'fetch nested Performance'
111
+ json_hash = Oj.load(@json, :mode => :strict)
112
+ Oj::Doc.open(@json) do |fast|
113
+ # puts "*** C fetch: #{fast.fetch('/d/2/4/y')}"
114
+ # puts "*** Ruby fetch: #{json_hash.fetch('d', []).fetch(1, []).fetch(3, []).fetch('x', nil)}"
117
115
  perf = Perf.new()
118
116
  perf.add('Oj::Doc', 'fetch') { fast.fetch('/d/2/4/x'); fast.fetch('/h/a/b/c/d/e/f/g'); fast.fetch('/i/1/1/1/1/1/1/1') }
119
117
  # version that fails gracefully
@@ -123,42 +121,42 @@ if $fetch
123
121
  json_hash.fetch('i', []).fetch(0, []).fetch(0, []).fetch(0, []).fetch(0, []).fetch(0, []).fetch(0, []).fetch(0, nil)
124
122
  end
125
123
  # version that raises if the path is incorrect
126
- # perf.add('Ruby', 'fetch') { $fetch.times { json_hash['d'][1][3][1] } }
127
- perf.run($iter)
124
+ # perf.add('Ruby', 'fetch') { @fetch.times { json_hash['d'][1][3][1] } }
125
+ perf.run(@iter)
128
126
  end
129
127
  end
130
128
 
131
- if $write
129
+ if @write
132
130
  puts '-' * 80
133
- puts "JSON write to file Performance"
134
- Oj::Doc.open($json) do |doc|
131
+ puts 'JSON write to file Performance'
132
+ Oj::Doc.open(@json) do |doc|
135
133
  perf = Perf.new()
136
134
  perf.add('Oj::Doc', 'dump') { doc.dump(nil, 'oj.json') }
137
- # perf.add('Yajl', 'encode') { File.open('yajl.json', 'w') { |f| Yajl::Encoder.encode($obj, f) } }
138
- perf.add('JSON::Ext', 'fast_generate') { File.open('json_ext.json', 'w') { |f| f.write(JSON.fast_generate($obj)) } }
135
+ # perf.add('Yajl', 'encode') { File.open('yajl.json', 'w') { |f| Yajl::Encoder.encode(@obj, f) } }
136
+ perf.add('JSON::Ext', 'fast_generate') { File.write('json_ext.json', JSON.fast_generate(@obj)) }
139
137
  perf.before('JSON::Ext') { JSON.generator = JSON::Ext::Generator }
140
- perf.run($iter)
138
+ perf.run(@iter)
141
139
  end
142
140
  end
143
141
 
144
- if $read
142
+ if @read
145
143
  puts '-' * 80
146
- puts "JSON read from file Performance"
147
- Oj::Doc.open($json) { |doc| doc.dump(nil, 'oj.json') }
148
- # File.open('yajl.json', 'w') { |f| Yajl::Encoder.encode($obj, f) }
144
+ puts 'JSON read from file Performance'
145
+ Oj::Doc.open(@json) { |doc| doc.dump(nil, 'oj.json') }
146
+ # File.open('yajl.json', 'w') { |f| Yajl::Encoder.encode(@obj, f) }
149
147
  JSON.generator = JSON::Ext::Generator
150
- File.open('json_ext.json', 'w') { |f| f.write(JSON.fast_generate($obj)) }
151
- Oj::Doc.open($json) do |doc|
148
+ File.write('json_ext.json', JSON.fast_generate(@obj))
149
+ Oj::Doc.open(@json) do |_doc|
152
150
  perf = Perf.new()
153
- perf.add('Oj::Doc', 'open_file') { ::Oj::Doc.open_file('oj.json') }
154
- # perf.add('Yajl', 'decode') { Yajl::decoder.decode(File.read('yajl.json')) }
151
+ perf.add('Oj::Doc', 'open_file') { Oj::Doc.open_file('oj.json') }
152
+ # perf.add('Yajl', 'decode') { Yajl::decoder.decode(File.read('yajl.json')) }
155
153
  perf.add('JSON::Ext', '') { JSON.parse(File.read('json_ext.json')) }
156
154
  perf.before('JSON::Ext') { JSON.parser = JSON::Ext::Parser }
157
- perf.run($iter)
155
+ perf.run(@iter)
158
156
  end
159
157
  end
160
158
 
161
- unless $failed.empty?
162
- puts "The following packages were not included for the reason listed"
163
- $failed.each { |tag,msg| puts "***** #{tag}: #{msg}" }
159
+ unless @failed.empty?
160
+ puts 'The following packages were not included for the reason listed'
161
+ @failed.each { |tag, msg| puts "***** #{tag}: #{msg}" }
164
162
  end
data/test/perf_file.rb CHANGED
@@ -1,58 +1,57 @@
1
1
  #!/usr/bin/env ruby -wW1
2
+ # frozen_string_literal: true
2
3
 
3
- $: << '.'
4
- $: << '../lib'
5
- $: << '../ext'
4
+ $LOAD_PATH << '.'
5
+ $LOAD_PATH << '../lib'
6
+ $LOAD_PATH << '../ext'
6
7
 
7
- if __FILE__ == $0
8
- if (i = ARGV.index('-I'))
9
- x,path = ARGV.slice!(i, 2)
10
- $: << path
11
- end
8
+ if __FILE__ == $PROGRAM_NAME && (i = ARGV.index('-I'))
9
+ _, path = ARGV.slice!(i, 2)
10
+ $LOAD_PATH << path
12
11
  end
13
12
 
14
13
  require 'optparse'
15
14
  require 'oj'
16
15
  require 'perf'
17
16
 
18
- $indent = 0
19
- $iter = 1
20
- $size = 1
17
+ @indent = 0
18
+ @iter = 1
19
+ @size = 1
21
20
 
22
21
  opts = OptionParser.new
23
22
 
24
- opts.on("-r", "read") { do_read = true }
25
- opts.on("-c", "--count [Int]", Integer, "iterations") { |i| $iter = i }
26
- opts.on("-i", "--indent [Int]", Integer, "indent") { |i| $indent = i }
27
- opts.on("-s", "--size [Int]", Integer, "size in Mbytes") { |s| $size = s }
23
+ opts.on('-r', 'read') { true }
24
+ opts.on('-c', '--count [Int]', Integer, 'iterations') { |v| @iter = v }
25
+ opts.on('-i', '--indent [Int]', Integer, 'indent') { |v| @indent = v }
26
+ opts.on('-s', '--size [Int]', Integer, 'size in Mbytes') { |s| @size = s }
28
27
 
29
- opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
30
- files = opts.parse(ARGV)
28
+ opts.on('-h', '--help', 'Show this display') { puts opts; Process.exit!(0) }
29
+ opts.parse(ARGV)
31
30
 
32
- $obj = {
31
+ @obj = {
33
32
  'a' => 'Alpha', # string
34
33
  'b' => true, # boolean
35
- 'c' => 12345, # number
36
- 'd' => [ true, [false, [-123456789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
34
+ 'c' => 12_345, # number
35
+ 'd' => [ true, [false, [-123_456_789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
37
36
  'e' => { 'zero' => nil, 'one' => 1, 'two' => 2, 'three' => [3], 'four' => [0, 1, 2, 3, 4] }, # hash
38
37
  'f' => nil, # nil
39
- 'g' => 12345678901234567890123456789, #_bignum
38
+ 'g' => 12_345_678_901_234_567_890_123_456_789, # _bignum
40
39
  'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
41
40
  'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
42
41
  }
43
42
 
44
- json = Oj.dump($obj, :indent => $indent)
45
- cnt = ($size * 1024 * 1024 + json.size) / json.size
46
- cnt = 1 if 0 == $size
43
+ json = Oj.dump(@obj, :indent => @indent)
44
+ cnt = ((@size * 1024 * 1024) + json.size) / json.size
45
+ cnt = 1 if 0 == @size
47
46
 
48
47
  filename = 'tmp.json'
49
- File.open(filename, "w") { |f|
48
+ File.open(filename, 'w') { |f|
50
49
  cnt.times do
51
- Oj.to_stream(f, $obj, :indent => $indent)
50
+ Oj.to_stream(f, @obj, :indent => @indent)
52
51
  end
53
52
  }
54
53
 
55
- Oj.default_options = { :mode => :strict, :indent => $indent }
54
+ Oj.default_options = { :mode => :strict, :indent => @indent }
56
55
 
57
56
  puts '-' * 80
58
57
  puts "Read from #{cnt * json.size / (1024 * 1024)} Mb file Performance"
@@ -60,5 +59,4 @@ perf = Perf.new()
60
59
  perf.add('Oj.load_file', '') { Oj.load_file(filename) }
61
60
  perf.add('Oj.load(string)', '') { Oj.load(File.read(filename)) }
62
61
  perf.add('Oj.load(file)', '') { File.open(filename, 'r') { |f| Oj.load(f) } }
63
- perf.run($iter)
64
-
62
+ perf.run(@iter)