oj 3.13.23 → 3.16.10

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 (178) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +86 -0
  3. data/README.md +2 -2
  4. data/ext/oj/buf.h +7 -6
  5. data/ext/oj/cache.c +29 -26
  6. data/ext/oj/cache.h +3 -2
  7. data/ext/oj/cache8.c +10 -9
  8. data/ext/oj/circarray.c +7 -5
  9. data/ext/oj/circarray.h +2 -2
  10. data/ext/oj/code.c +5 -12
  11. data/ext/oj/code.h +2 -2
  12. data/ext/oj/compat.c +20 -60
  13. data/ext/oj/custom.c +26 -59
  14. data/ext/oj/debug.c +3 -9
  15. data/ext/oj/dump.c +103 -53
  16. data/ext/oj/dump.h +1 -4
  17. data/ext/oj/dump_compat.c +557 -592
  18. data/ext/oj/dump_leaf.c +3 -5
  19. data/ext/oj/dump_object.c +42 -48
  20. data/ext/oj/dump_strict.c +10 -22
  21. data/ext/oj/encoder.c +1 -1
  22. data/ext/oj/err.c +2 -13
  23. data/ext/oj/err.h +9 -12
  24. data/ext/oj/extconf.rb +16 -7
  25. data/ext/oj/fast.c +63 -98
  26. data/ext/oj/intern.c +62 -47
  27. data/ext/oj/intern.h +3 -7
  28. data/ext/oj/mem.c +318 -0
  29. data/ext/oj/mem.h +53 -0
  30. data/ext/oj/mimic_json.c +54 -38
  31. data/ext/oj/object.c +33 -43
  32. data/ext/oj/odd.c +8 -6
  33. data/ext/oj/odd.h +4 -4
  34. data/ext/oj/oj.c +245 -216
  35. data/ext/oj/oj.h +83 -81
  36. data/ext/oj/parse.c +109 -153
  37. data/ext/oj/parse.h +21 -24
  38. data/ext/oj/parser.c +80 -67
  39. data/ext/oj/parser.h +9 -8
  40. data/ext/oj/rails.c +71 -94
  41. data/ext/oj/reader.c +9 -14
  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 +13 -15
  47. data/ext/oj/saj2.c +37 -49
  48. data/ext/oj/saj2.h +1 -1
  49. data/ext/oj/scp.c +6 -20
  50. data/ext/oj/sparse.c +22 -70
  51. data/ext/oj/stream_writer.c +46 -48
  52. data/ext/oj/strict.c +22 -56
  53. data/ext/oj/string_writer.c +64 -40
  54. data/ext/oj/trace.h +31 -4
  55. data/ext/oj/usual.c +125 -114
  56. data/ext/oj/usual.h +7 -6
  57. data/ext/oj/util.h +1 -1
  58. data/ext/oj/val_stack.c +13 -2
  59. data/ext/oj/val_stack.h +8 -7
  60. data/ext/oj/wab.c +25 -57
  61. data/lib/oj/active_support_helper.rb +1 -3
  62. data/lib/oj/bag.rb +7 -1
  63. data/lib/oj/easy_hash.rb +4 -5
  64. data/lib/oj/error.rb +0 -1
  65. data/lib/oj/json.rb +162 -150
  66. data/lib/oj/mimic.rb +7 -7
  67. data/lib/oj/schandler.rb +5 -4
  68. data/lib/oj/state.rb +8 -5
  69. data/lib/oj/version.rb +1 -2
  70. data/lib/oj.rb +2 -0
  71. data/pages/InstallOptions.md +20 -0
  72. data/pages/Options.md +4 -0
  73. metadata +46 -121
  74. data/test/_test_active.rb +0 -76
  75. data/test/_test_active_mimic.rb +0 -96
  76. data/test/_test_mimic_rails.rb +0 -126
  77. data/test/activerecord/result_test.rb +0 -32
  78. data/test/activesupport4/decoding_test.rb +0 -108
  79. data/test/activesupport4/encoding_test.rb +0 -531
  80. data/test/activesupport4/test_helper.rb +0 -41
  81. data/test/activesupport5/abstract_unit.rb +0 -45
  82. data/test/activesupport5/decoding_test.rb +0 -133
  83. data/test/activesupport5/encoding_test.rb +0 -500
  84. data/test/activesupport5/encoding_test_cases.rb +0 -98
  85. data/test/activesupport5/test_helper.rb +0 -72
  86. data/test/activesupport5/time_zone_test_helpers.rb +0 -39
  87. data/test/activesupport6/abstract_unit.rb +0 -44
  88. data/test/activesupport6/decoding_test.rb +0 -133
  89. data/test/activesupport6/encoding_test.rb +0 -507
  90. data/test/activesupport6/encoding_test_cases.rb +0 -98
  91. data/test/activesupport6/test_common.rb +0 -17
  92. data/test/activesupport6/test_helper.rb +0 -163
  93. data/test/activesupport6/time_zone_test_helpers.rb +0 -39
  94. data/test/activesupport7/abstract_unit.rb +0 -49
  95. data/test/activesupport7/decoding_test.rb +0 -125
  96. data/test/activesupport7/encoding_test.rb +0 -486
  97. data/test/activesupport7/encoding_test_cases.rb +0 -104
  98. data/test/activesupport7/time_zone_test_helpers.rb +0 -47
  99. data/test/bar.rb +0 -11
  100. data/test/baz.rb +0 -16
  101. data/test/bug.rb +0 -16
  102. data/test/files.rb +0 -29
  103. data/test/foo.rb +0 -77
  104. data/test/helper.rb +0 -42
  105. data/test/isolated/shared.rb +0 -308
  106. data/test/isolated/test_mimic_after.rb +0 -13
  107. data/test/isolated/test_mimic_alone.rb +0 -12
  108. data/test/isolated/test_mimic_as_json.rb +0 -45
  109. data/test/isolated/test_mimic_before.rb +0 -13
  110. data/test/isolated/test_mimic_define.rb +0 -28
  111. data/test/isolated/test_mimic_rails_after.rb +0 -22
  112. data/test/isolated/test_mimic_rails_before.rb +0 -21
  113. data/test/isolated/test_mimic_redefine.rb +0 -15
  114. data/test/json_gem/json_addition_test.rb +0 -216
  115. data/test/json_gem/json_common_interface_test.rb +0 -153
  116. data/test/json_gem/json_encoding_test.rb +0 -107
  117. data/test/json_gem/json_ext_parser_test.rb +0 -20
  118. data/test/json_gem/json_fixtures_test.rb +0 -35
  119. data/test/json_gem/json_generator_test.rb +0 -396
  120. data/test/json_gem/json_generic_object_test.rb +0 -90
  121. data/test/json_gem/json_parser_test.rb +0 -477
  122. data/test/json_gem/json_string_matching_test.rb +0 -42
  123. data/test/json_gem/test_helper.rb +0 -30
  124. data/test/mem.rb +0 -33
  125. data/test/perf.rb +0 -107
  126. data/test/perf_compat.rb +0 -130
  127. data/test/perf_dump.rb +0 -50
  128. data/test/perf_fast.rb +0 -164
  129. data/test/perf_file.rb +0 -64
  130. data/test/perf_object.rb +0 -138
  131. data/test/perf_once.rb +0 -58
  132. data/test/perf_parser.rb +0 -189
  133. data/test/perf_saj.rb +0 -109
  134. data/test/perf_scp.rb +0 -152
  135. data/test/perf_simple.rb +0 -287
  136. data/test/perf_strict.rb +0 -139
  137. data/test/perf_wab.rb +0 -131
  138. data/test/prec.rb +0 -23
  139. data/test/sample/change.rb +0 -14
  140. data/test/sample/dir.rb +0 -19
  141. data/test/sample/doc.rb +0 -36
  142. data/test/sample/file.rb +0 -48
  143. data/test/sample/group.rb +0 -16
  144. data/test/sample/hasprops.rb +0 -16
  145. data/test/sample/layer.rb +0 -12
  146. data/test/sample/line.rb +0 -20
  147. data/test/sample/oval.rb +0 -10
  148. data/test/sample/rect.rb +0 -10
  149. data/test/sample/shape.rb +0 -35
  150. data/test/sample/text.rb +0 -20
  151. data/test/sample.rb +0 -54
  152. data/test/sample_json.rb +0 -37
  153. data/test/test_compat.rb +0 -540
  154. data/test/test_custom.rb +0 -544
  155. data/test/test_debian.rb +0 -53
  156. data/test/test_fast.rb +0 -530
  157. data/test/test_file.rb +0 -255
  158. data/test/test_gc.rb +0 -60
  159. data/test/test_generate.rb +0 -21
  160. data/test/test_hash.rb +0 -39
  161. data/test/test_integer_range.rb +0 -72
  162. data/test/test_null.rb +0 -376
  163. data/test/test_object.rb +0 -1025
  164. data/test/test_parser.rb +0 -11
  165. data/test/test_parser_debug.rb +0 -27
  166. data/test/test_parser_saj.rb +0 -335
  167. data/test/test_parser_usual.rb +0 -217
  168. data/test/test_rails.rb +0 -35
  169. data/test/test_saj.rb +0 -186
  170. data/test/test_scp.rb +0 -431
  171. data/test/test_strict.rb +0 -435
  172. data/test/test_various.rb +0 -752
  173. data/test/test_wab.rb +0 -309
  174. data/test/test_writer.rb +0 -380
  175. data/test/tests.rb +0 -33
  176. data/test/tests_mimic.rb +0 -23
  177. data/test/tests_mimic_addition.rb +0 -16
  178. data/test/zoo.rb +0 -13
data/test/perf_parser.rb DELETED
@@ -1,189 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- $: << '.'
5
- $: << File.join(File.dirname(__FILE__), "../lib")
6
- $: << File.join(File.dirname(__FILE__), "../ext")
7
-
8
- require 'optparse'
9
- require 'perf'
10
- require 'oj'
11
- require 'json'
12
-
13
- $verbose = false
14
- $iter = 50_000
15
- $with_bignum = false
16
- $size = 1
17
- $cache_keys = true
18
- $symbol_keys = false
19
-
20
- opts = OptionParser.new
21
- opts.on("-v", "verbose") { $verbose = true }
22
- opts.on("-c", "--count [Int]", Integer, "iterations") { |i| $iter = i }
23
- opts.on("-s", "--size [Int]", Integer, "size (~Kbytes)") { |i| $size = i }
24
- opts.on("-b", "with bignum") { $with_bignum = true }
25
- opts.on("-k", "no cache") { $cache_keys = false }
26
- opts.on("-sym", "symbol keys") { $symbol_keys = true }
27
- opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
28
- files = opts.parse(ARGV)
29
-
30
- $obj = {
31
- 'a' => 'Alpha', # string
32
- 'b' => true, # boolean
33
- 'c' => 12345, # number
34
- 'd' => [ true, [false, [-123456789, nil], 3.9676, ['Something else.', false, 1, nil], nil]], # mix it up array
35
- 'e' => { 'zero' => nil, 'one' => 1, 'two' => 2, 'three' => [3], 'four' => [0, 1, 2, 3, 4] }, # hash
36
- 'f' => nil, # nil
37
- 'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
38
- 'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
39
- }
40
- $obj['g'] = 12345678901234567890123456789 if $with_bignum
41
-
42
- if 0 < $size
43
- o = $obj
44
- $obj = []
45
- (4 * $size).times do
46
- $obj << o
47
- end
48
- end
49
-
50
- $json = Oj.dump($obj)
51
- $failed = {} # key is same as String used in tests later
52
- Oj.default_options = {create_id: '^', create_additions: true, class_cache: true}
53
- if $cache_keys
54
- Oj.default_options = {cache_keys: true, cache_str: 6, symbol_keys: $symbol_keys}
55
- else
56
- Oj.default_options = {cache_keys: false, cache_str: -1, symbol_keys: $symbol_keys}
57
- end
58
- JSON.parser = JSON::Ext::Parser
59
-
60
- class AllSaj
61
- def initialize()
62
- end
63
-
64
- def hash_start(key)
65
- end
66
-
67
- def hash_end(key)
68
- end
69
-
70
- def array_start(key)
71
- end
72
-
73
- def array_end(key)
74
- end
75
-
76
- def add_value(value, key)
77
- end
78
- end # AllSaj
79
-
80
- class NoSaj
81
- def initialize()
82
- end
83
- end # NoSaj
84
-
85
- no_handler = NoSaj.new()
86
- all_handler = AllSaj.new()
87
-
88
- if $verbose
89
- puts "json:\n#{$json}\n"
90
- end
91
-
92
- ### Validate ######################
93
- p_val = Oj::Parser.new(:validate)
94
-
95
- puts '-' * 80
96
- puts "Validate Performance"
97
- perf = Perf.new()
98
- perf.add('Oj::Parser.validate', 'none') { p_val.parse($json) }
99
- perf.add('Oj::Saj.none', 'none') { Oj.saj_parse(no_handler, $json) }
100
- perf.run($iter)
101
-
102
- ### SAJ ######################
103
- p_all = Oj::Parser.new(:saj)
104
- p_all.handler = all_handler
105
- p_all.cache_keys = $cache_keys
106
- p_all.cache_strings = 6
107
-
108
- puts '-' * 80
109
- puts "Parse Callback Performance"
110
- perf = Perf.new()
111
- perf.add('Oj::Parser.saj', 'all') { p_all.parse($json) }
112
- perf.add('Oj::Saj.all', 'all') { Oj.saj_parse(all_handler, $json) }
113
- perf.run($iter)
114
-
115
- ### Usual ######################
116
- p_usual = Oj::Parser.new(:usual)
117
- p_usual.cache_keys = $cache_keys
118
- p_usual.cache_strings = ($cache_keys ? 6 : 0)
119
- p_usual.symbol_keys = $symbol_keys
120
-
121
- puts '-' * 80
122
- puts "Parse Usual Performance"
123
- perf = Perf.new()
124
- perf.add('Oj::Parser.usual', '') { p_usual.parse($json) }
125
- perf.add('Oj::strict_load', '') { Oj.strict_load($json) }
126
- perf.add('JSON::Ext', 'parse') { JSON.load($json) }
127
- perf.run($iter)
128
-
129
- ### Usual Objects ######################
130
-
131
- # Original Oj follows the JSON gem for creating objects which uses the class
132
- # json_create(arg) method. Oj::Parser in usual mode supprts the same but also
133
- # handles populating the object variables directly which is faster.
134
-
135
- class Stuff
136
- attr_accessor :alpha, :bravo, :charlie, :delta, :echo, :foxtrot, :golf, :hotel, :india, :juliet
137
- def self.json_create(arg)
138
- obj = self.new
139
- obj.alpha = arg["alpha"]
140
- obj.bravo = arg["bravo"]
141
- obj.charlie = arg["charlie"]
142
- obj.delta = arg["delta"]
143
- obj.echo = arg["echo"]
144
- obj.foxtrot = arg["foxtrot"]
145
- obj.golf = arg["golf"]
146
- obj.hotel = arg["hotel"]
147
- obj.india = arg["india"]
148
- obj.juliet = arg["juliet"]
149
- obj
150
- end
151
- end
152
-
153
- $obj_json = %|{
154
- "alpha": [0, 1,2,3,4,5,6,7,8,9],
155
- "bravo": true,
156
- "charlie": 123,
157
- "delta": "some string",
158
- "echo": null,
159
- "^": "Stuff",
160
- "foxtrot": false,
161
- "golf": "gulp",
162
- "hotel": {"x": true, "y": false},
163
- "india": [null, true, 123],
164
- "juliet": "junk"
165
- }|
166
-
167
-
168
- p_usual = Oj::Parser.new(:usual)
169
- p_usual.cache_keys = $cache_keys
170
- p_usual.cache_strings = ($cache_keys ? 6 : 0)
171
- p_usual.symbol_keys = $symbol_keys
172
- p_usual.create_id = '^'
173
- p_usual.class_cache = true
174
- p_usual.ignore_json_create = true
175
-
176
- JSON.create_id = '^'
177
-
178
- puts '-' * 80
179
- puts "Parse Usual Object Performance"
180
- perf = Perf.new()
181
- perf.add('Oj::Parser.usual', '') { p_usual.parse($obj_json) }
182
- perf.add('Oj::compat_load', '') { Oj.compat_load($obj_json) }
183
- perf.add('JSON::Ext', 'parse') { JSON.load($obj_json) }
184
- perf.run($iter)
185
-
186
- unless $failed.empty?
187
- puts "The following packages were not included for the reason listed"
188
- $failed.each { |tag,msg| puts "***** #{tag}: #{msg}" }
189
- end
data/test/perf_saj.rb DELETED
@@ -1,109 +0,0 @@
1
- #!/usr/bin/env ruby -wW1
2
- # encoding: UTF-8
3
-
4
- $: << '.'
5
- $: << File.join(File.dirname(__FILE__), "../lib")
6
- $: << File.join(File.dirname(__FILE__), "../ext")
7
-
8
- require 'optparse'
9
- require 'yajl'
10
- require 'perf'
11
- require 'json'
12
- require 'json/ext'
13
- require 'oj'
14
-
15
- $verbose = false
16
- $indent = 0
17
- $iter = 10000
18
- $gets = 0
19
- $fetch = false
20
- $write = false
21
- $read = false
22
-
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)
33
-
34
- class AllSaj < Oj::Saj
35
- def initialize()
36
- end
37
-
38
- def hash_start(key)
39
- end
40
-
41
- def hash_end(key)
42
- end
43
-
44
- def array_start(key)
45
- end
46
-
47
- def array_end(key)
48
- end
49
-
50
- def add_value(value, key)
51
- end
52
- end # AllSaj
53
-
54
- class NoSaj < Oj::Saj
55
- def initialize()
56
- end
57
- end # NoSaj
58
-
59
- saj_handler = AllSaj.new()
60
- no_saj = NoSaj.new()
61
-
62
- $obj = {
63
- 'a' => 'Alpha', # string
64
- 'b' => true, # boolean
65
- 'c' => 12345, # number
66
- 'd' => [ true, [false, {'12345' => 12345, 'nil' => nil}, 3.967, { 'x' => 'something', 'y' => false, 'z' => true}, nil]], # mix it up array
67
- 'e' => { 'one' => 1, 'two' => 2 }, # hash
68
- 'f' => nil, # nil
69
- 'g' => 12345678901234567890123456789, # big number
70
- 'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
71
- 'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
72
- }
73
-
74
- Oj.default_options = { :indent => $indent, :mode => :compat }
75
-
76
- $json = Oj.dump($obj)
77
- $failed = {} # key is same as String used in tests later
78
-
79
- def capture_error(tag, orig, load_key, dump_key, &blk)
80
- begin
81
- obj = blk.call(orig)
82
- raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
83
- rescue Exception => e
84
- $failed[tag] = "#{e.class}: #{e.message}"
85
- end
86
- end
87
-
88
- # Verify that all packages dump and load correctly and return the same Object as the original.
89
- capture_error('Yajl', $obj, 'encode', 'parse') { |o| Yajl::Parser.parse(Yajl::Encoder.encode(o)) }
90
- capture_error('JSON::Ext', $obj, 'generate', 'parse') { |o| JSON.generator = JSON::Ext::Generator; JSON::Ext::Parser.new(JSON.generate(o)).parse }
91
-
92
- if $verbose
93
- puts "json:\n#{$json}\n"
94
- end
95
-
96
-
97
- puts '-' * 80
98
- puts "Parse Performance"
99
- perf = Perf.new()
100
- perf.add('Oj::Saj', 'all') { Oj.saj_parse(saj_handler, $json) }
101
- perf.add('Oj::Saj', 'none') { Oj.saj_parse(no_saj, $json) }
102
- perf.add('Yajl', 'parse') { Yajl::Parser.parse($json) } unless $failed.has_key?('Yajl')
103
- perf.add('JSON::Ext', 'parse') { JSON::Ext::Parser.new($json).parse } unless $failed.has_key?('JSON::Ext')
104
- perf.run($iter)
105
-
106
- unless $failed.empty?
107
- puts "The following packages were not included for the reason listed"
108
- $failed.each { |tag,msg| puts "***** #{tag}: #{msg}" }
109
- end
data/test/perf_scp.rb DELETED
@@ -1,152 +0,0 @@
1
- #!/usr/bin/env ruby -wW1
2
- # encoding: UTF-8
3
-
4
- $: << '.'
5
- $: << File.join(File.dirname(__FILE__), "../lib")
6
- $: << File.join(File.dirname(__FILE__), "../ext")
7
-
8
- require 'optparse'
9
- require 'yajl'
10
- require 'perf'
11
- require 'json'
12
- require 'json/ext'
13
- require 'oj'
14
-
15
- $verbose = false
16
- $indent = 0
17
- $iter = 50_000
18
- $with_bignum = false
19
- $size = 1
20
-
21
- opts = OptionParser.new
22
- opts.on("-v", "verbose") { $verbose = true }
23
- opts.on("-c", "--count [Int]", Integer, "iterations") { |i| $iter = i }
24
- opts.on("-i", "--indent [Int]", Integer, "indentation") { |i| $indent = i }
25
- opts.on("-s", "--size [Int]", Integer, "size (~Kbytes)") { |i| $size = i }
26
- opts.on("-b", "with bignum") { $with_bignum = true }
27
- opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
28
- files = opts.parse(ARGV)
29
-
30
- $obj = {
31
- 'a' => 'Alpha', # string
32
- 'b' => true, # boolean
33
- 'c' => 12345, # number
34
- 'd' => [ true, [false, [-123456789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
35
- 'e' => { 'zero' => nil, 'one' => 1, 'two' => 2, 'three' => [3], 'four' => [0, 1, 2, 3, 4] }, # hash
36
- 'f' => nil, # nil
37
- 'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
38
- 'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
39
- }
40
- $obj['g'] = 12345678901234567890123456789 if $with_bignum
41
-
42
- if 0 < $size
43
- o = $obj
44
- $obj = []
45
- (4 * $size).times do
46
- $obj << o
47
- end
48
- end
49
-
50
- Oj.default_options = { :indent => $indent, :mode => :strict, cache_keys: true, cache_str: 5 }
51
-
52
- $json = Oj.dump($obj)
53
- $failed = {} # key is same as String used in tests later
54
-
55
- class AllSaj < Oj::Saj
56
- def initialize()
57
- end
58
-
59
- def hash_start(key)
60
- end
61
-
62
- def hash_end(key)
63
- end
64
-
65
- def array_start(key)
66
- end
67
-
68
- def array_end(key)
69
- end
70
-
71
- def add_value(value, key)
72
- end
73
- end # AllSaj
74
-
75
- class NoSaj < Oj::Saj
76
- def initialize()
77
- end
78
- end # NoSaj
79
-
80
- class NoHandler < Oj::ScHandler
81
- def initialize()
82
- end
83
- end # NoHandler
84
-
85
- class AllHandler < Oj::ScHandler
86
- def initialize()
87
- end
88
-
89
- def hash_start()
90
- return nil
91
- end
92
-
93
- def hash_end()
94
- end
95
-
96
- def array_start()
97
- return nil
98
- end
99
-
100
- def array_end()
101
- end
102
-
103
- def add_value(value)
104
- end
105
-
106
- def hash_set(h, key, value)
107
- end
108
-
109
- def array_append(a, value)
110
- end
111
-
112
- end # AllHandler
113
-
114
- saj_handler = AllSaj.new()
115
- no_saj = NoSaj.new()
116
-
117
- sc_handler = AllHandler.new()
118
- no_handler = NoHandler.new()
119
-
120
- def capture_error(tag, orig, load_key, dump_key, &blk)
121
- begin
122
- obj = blk.call(orig)
123
- raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
124
- rescue Exception => e
125
- $failed[tag] = "#{e.class}: #{e.message}"
126
- end
127
- end
128
-
129
- # Verify that all packages dump and load correctly and return the same Object as the original.
130
- capture_error('Yajl', $obj, 'encode', 'parse') { |o| Yajl::Parser.parse(Yajl::Encoder.encode(o)) }
131
- capture_error('JSON::Ext', $obj, 'generate', 'parse') { |o| JSON.generator = JSON::Ext::Generator; JSON::Ext::Parser.new(JSON.generate(o)).parse }
132
-
133
- if $verbose
134
- puts "json:\n#{$json}\n"
135
- end
136
-
137
- puts '-' * 80
138
- puts "Parse Performance"
139
- perf = Perf.new()
140
- perf.add('Oj::Saj.all', 'all') { Oj.saj_parse(saj_handler, $json) }
141
- perf.add('Oj::Saj.none', 'none') { Oj.saj_parse(no_saj, $json) }
142
- perf.add('Oj::Scp.all', 'all') { Oj.sc_parse(sc_handler, $json) }
143
- perf.add('Oj::Scp.none', 'none') { Oj.sc_parse(no_handler, $json) }
144
- perf.add('Oj::load', 'none') { Oj.wab_load($json) }
145
- perf.add('Yajl', 'parse') { Yajl::Parser.parse($json) } unless $failed.has_key?('Yajl')
146
- perf.add('JSON::Ext', 'parse') { JSON::Ext::Parser.new($json).parse } unless $failed.has_key?('JSON::Ext')
147
- perf.run($iter)
148
-
149
- unless $failed.empty?
150
- puts "The following packages were not included for the reason listed"
151
- $failed.each { |tag,msg| puts "***** #{tag}: #{msg}" }
152
- end