oj 3.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +104 -0
- data/ext/oj/buf.h +103 -0
- data/ext/oj/cache8.c +107 -0
- data/ext/oj/cache8.h +48 -0
- data/ext/oj/circarray.c +68 -0
- data/ext/oj/circarray.h +23 -0
- data/ext/oj/code.c +235 -0
- data/ext/oj/code.h +42 -0
- data/ext/oj/compat.c +299 -0
- data/ext/oj/custom.c +1191 -0
- data/ext/oj/dump.c +1252 -0
- data/ext/oj/dump.h +96 -0
- data/ext/oj/dump_compat.c +977 -0
- data/ext/oj/dump_leaf.c +252 -0
- data/ext/oj/dump_object.c +837 -0
- data/ext/oj/dump_strict.c +433 -0
- data/ext/oj/encode.h +45 -0
- data/ext/oj/err.c +57 -0
- data/ext/oj/err.h +70 -0
- data/ext/oj/extconf.rb +47 -0
- data/ext/oj/fast.c +1771 -0
- data/ext/oj/hash.c +163 -0
- data/ext/oj/hash.h +46 -0
- data/ext/oj/hash_test.c +512 -0
- data/ext/oj/mimic_json.c +878 -0
- data/ext/oj/object.c +771 -0
- data/ext/oj/odd.c +231 -0
- data/ext/oj/odd.h +44 -0
- data/ext/oj/oj.c +1704 -0
- data/ext/oj/oj.h +385 -0
- data/ext/oj/parse.c +1086 -0
- data/ext/oj/parse.h +111 -0
- data/ext/oj/rails.c +1493 -0
- data/ext/oj/rails.h +21 -0
- data/ext/oj/reader.c +231 -0
- data/ext/oj/reader.h +151 -0
- data/ext/oj/resolve.c +102 -0
- data/ext/oj/resolve.h +14 -0
- data/ext/oj/rxclass.c +147 -0
- data/ext/oj/rxclass.h +27 -0
- data/ext/oj/saj.c +714 -0
- data/ext/oj/scp.c +224 -0
- data/ext/oj/sparse.c +910 -0
- data/ext/oj/stream_writer.c +363 -0
- data/ext/oj/strict.c +212 -0
- data/ext/oj/string_writer.c +534 -0
- data/ext/oj/trace.c +79 -0
- data/ext/oj/trace.h +28 -0
- data/ext/oj/util.c +136 -0
- data/ext/oj/util.h +19 -0
- data/ext/oj/val_stack.c +118 -0
- data/ext/oj/val_stack.h +185 -0
- data/ext/oj/wab.c +631 -0
- data/lib/oj.rb +21 -0
- data/lib/oj/active_support_helper.rb +41 -0
- data/lib/oj/bag.rb +88 -0
- data/lib/oj/easy_hash.rb +52 -0
- data/lib/oj/error.rb +22 -0
- data/lib/oj/json.rb +176 -0
- data/lib/oj/mimic.rb +267 -0
- data/lib/oj/saj.rb +66 -0
- data/lib/oj/schandler.rb +142 -0
- data/lib/oj/state.rb +131 -0
- data/lib/oj/version.rb +5 -0
- data/pages/Advanced.md +22 -0
- data/pages/Compatibility.md +25 -0
- data/pages/Custom.md +23 -0
- data/pages/Encoding.md +65 -0
- data/pages/JsonGem.md +79 -0
- data/pages/Modes.md +155 -0
- data/pages/Options.md +283 -0
- data/pages/Rails.md +116 -0
- data/pages/Security.md +20 -0
- data/pages/WAB.md +13 -0
- data/test/_test_active.rb +76 -0
- data/test/_test_active_mimic.rb +96 -0
- data/test/_test_mimic_rails.rb +126 -0
- data/test/activerecord/result_test.rb +27 -0
- data/test/activesupport4/decoding_test.rb +108 -0
- data/test/activesupport4/encoding_test.rb +531 -0
- data/test/activesupport4/test_helper.rb +41 -0
- data/test/activesupport5/decoding_test.rb +125 -0
- data/test/activesupport5/encoding_test.rb +485 -0
- data/test/activesupport5/encoding_test_cases.rb +90 -0
- data/test/activesupport5/test_helper.rb +50 -0
- data/test/activesupport5/time_zone_test_helpers.rb +24 -0
- data/test/bar.rb +25 -0
- data/test/files.rb +29 -0
- data/test/foo.rb +167 -0
- data/test/helper.rb +26 -0
- data/test/isolated/shared.rb +308 -0
- data/test/isolated/test_mimic_after.rb +13 -0
- data/test/isolated/test_mimic_alone.rb +12 -0
- data/test/isolated/test_mimic_as_json.rb +45 -0
- data/test/isolated/test_mimic_before.rb +13 -0
- data/test/isolated/test_mimic_define.rb +28 -0
- data/test/isolated/test_mimic_rails_after.rb +22 -0
- data/test/isolated/test_mimic_rails_before.rb +21 -0
- data/test/isolated/test_mimic_redefine.rb +15 -0
- data/test/json_gem/json_addition_test.rb +216 -0
- data/test/json_gem/json_common_interface_test.rb +148 -0
- data/test/json_gem/json_encoding_test.rb +107 -0
- data/test/json_gem/json_ext_parser_test.rb +20 -0
- data/test/json_gem/json_fixtures_test.rb +35 -0
- data/test/json_gem/json_generator_test.rb +383 -0
- data/test/json_gem/json_generic_object_test.rb +90 -0
- data/test/json_gem/json_parser_test.rb +470 -0
- data/test/json_gem/json_string_matching_test.rb +42 -0
- data/test/json_gem/test_helper.rb +18 -0
- data/test/perf.rb +107 -0
- data/test/perf_compat.rb +130 -0
- data/test/perf_fast.rb +164 -0
- data/test/perf_file.rb +64 -0
- data/test/perf_object.rb +138 -0
- data/test/perf_saj.rb +109 -0
- data/test/perf_scp.rb +151 -0
- data/test/perf_simple.rb +287 -0
- data/test/perf_strict.rb +145 -0
- data/test/perf_wab.rb +131 -0
- data/test/sample.rb +54 -0
- data/test/sample/change.rb +14 -0
- data/test/sample/dir.rb +19 -0
- data/test/sample/doc.rb +36 -0
- data/test/sample/file.rb +48 -0
- data/test/sample/group.rb +16 -0
- data/test/sample/hasprops.rb +16 -0
- data/test/sample/layer.rb +12 -0
- data/test/sample/line.rb +20 -0
- data/test/sample/oval.rb +10 -0
- data/test/sample/rect.rb +10 -0
- data/test/sample/shape.rb +35 -0
- data/test/sample/text.rb +20 -0
- data/test/sample_json.rb +37 -0
- data/test/test_compat.rb +509 -0
- data/test/test_custom.rb +503 -0
- data/test/test_debian.rb +53 -0
- data/test/test_fast.rb +470 -0
- data/test/test_file.rb +239 -0
- data/test/test_gc.rb +49 -0
- data/test/test_hash.rb +29 -0
- data/test/test_integer_range.rb +73 -0
- data/test/test_null.rb +376 -0
- data/test/test_object.rb +1018 -0
- data/test/test_saj.rb +186 -0
- data/test/test_scp.rb +433 -0
- data/test/test_strict.rb +410 -0
- data/test/test_various.rb +741 -0
- data/test/test_wab.rb +307 -0
- data/test/test_writer.rb +380 -0
- data/test/tests.rb +24 -0
- data/test/tests_mimic.rb +14 -0
- data/test/tests_mimic_addition.rb +7 -0
- data/test/zoo.rb +13 -0
- metadata +359 -0
data/test/perf_file.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
#!/usr/bin/env ruby -wW1
|
2
|
+
|
3
|
+
$: << '.'
|
4
|
+
$: << '../lib'
|
5
|
+
$: << '../ext'
|
6
|
+
|
7
|
+
if __FILE__ == $0
|
8
|
+
if (i = ARGV.index('-I'))
|
9
|
+
x,path = ARGV.slice!(i, 2)
|
10
|
+
$: << path
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
require 'optparse'
|
15
|
+
require 'oj'
|
16
|
+
require 'perf'
|
17
|
+
|
18
|
+
$indent = 0
|
19
|
+
$iter = 1
|
20
|
+
$size = 1
|
21
|
+
|
22
|
+
opts = OptionParser.new
|
23
|
+
|
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 }
|
28
|
+
|
29
|
+
opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
|
30
|
+
files = opts.parse(ARGV)
|
31
|
+
|
32
|
+
$obj = {
|
33
|
+
'a' => 'Alpha', # string
|
34
|
+
'b' => true, # boolean
|
35
|
+
'c' => 12345, # number
|
36
|
+
'd' => [ true, [false, [-123456789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
|
37
|
+
'e' => { 'zero' => nil, 'one' => 1, 'two' => 2, 'three' => [3], 'four' => [0, 1, 2, 3, 4] }, # hash
|
38
|
+
'f' => nil, # nil
|
39
|
+
'g' => 12345678901234567890123456789, #_bignum
|
40
|
+
'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
|
41
|
+
'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
|
42
|
+
}
|
43
|
+
|
44
|
+
json = Oj.dump($obj, :indent => $indent)
|
45
|
+
cnt = ($size * 1024 * 1024 + json.size) / json.size
|
46
|
+
cnt = 1 if 0 == $size
|
47
|
+
|
48
|
+
filename = 'tmp.json'
|
49
|
+
File.open(filename, "w") { |f|
|
50
|
+
cnt.times do
|
51
|
+
Oj.to_stream(f, $obj, :indent => $indent)
|
52
|
+
end
|
53
|
+
}
|
54
|
+
|
55
|
+
Oj.default_options = { :mode => :strict, :indent => $indent }
|
56
|
+
|
57
|
+
puts '-' * 80
|
58
|
+
puts "Read from #{cnt * json.size / (1024 * 1024)} Mb file Performance"
|
59
|
+
perf = Perf.new()
|
60
|
+
perf.add('Oj.load_file', '') { Oj.load_file(filename) }
|
61
|
+
perf.add('Oj.load(string)', '') { Oj.load(File.read(filename)) }
|
62
|
+
perf.add('Oj.load(file)', '') { File.open(filename, 'r') { |f| Oj.load(f) } }
|
63
|
+
perf.run($iter)
|
64
|
+
|
data/test/perf_object.rb
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$: << '.'
|
4
|
+
$: << '../lib'
|
5
|
+
$: << '../ext'
|
6
|
+
|
7
|
+
if __FILE__ == $0
|
8
|
+
if (i = ARGV.index('-I'))
|
9
|
+
x,path = ARGV.slice!(i, 2)
|
10
|
+
$: << path
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
require 'optparse'
|
15
|
+
require 'ox'
|
16
|
+
require 'oj'
|
17
|
+
require 'perf'
|
18
|
+
require 'sample'
|
19
|
+
require 'files'
|
20
|
+
|
21
|
+
$circular = false
|
22
|
+
$indent = 0
|
23
|
+
$allow_gc = true
|
24
|
+
|
25
|
+
do_sample = false
|
26
|
+
do_files = false
|
27
|
+
|
28
|
+
do_load = false
|
29
|
+
do_dump = false
|
30
|
+
do_read = false
|
31
|
+
do_write = false
|
32
|
+
$iter = 1000
|
33
|
+
$mult = 1
|
34
|
+
|
35
|
+
opts = OptionParser.new
|
36
|
+
opts.on("-c", "circular options") { $circular = true }
|
37
|
+
|
38
|
+
opts.on("-x", "use sample instead of files") { do_sample = true }
|
39
|
+
opts.on("-g", "no GC during parsing") { $allow_gc = false }
|
40
|
+
|
41
|
+
opts.on("-s", "load and dump as sample Ruby object") { do_sample = true }
|
42
|
+
opts.on("-f", "load and dump as files Ruby object") { do_files = true }
|
43
|
+
|
44
|
+
opts.on("-l", "load") { do_load = true }
|
45
|
+
opts.on("-d", "dump") { do_dump = true }
|
46
|
+
opts.on("-r", "read") { do_read = true }
|
47
|
+
opts.on("-w", "write") { do_write = true }
|
48
|
+
opts.on("-a", "load, dump, read and write") { do_load = true; do_dump = true; do_read = true; do_write = true }
|
49
|
+
|
50
|
+
opts.on("-i", "--iterations [Int]", Integer, "iterations") { |i| $iter = i }
|
51
|
+
opts.on("-m", "--multiply [Int]", Integer, "multiplier") { |i| $mult = i }
|
52
|
+
|
53
|
+
opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
|
54
|
+
files = opts.parse(ARGV)
|
55
|
+
|
56
|
+
$obj = nil
|
57
|
+
$xml = nil
|
58
|
+
$mars = nil
|
59
|
+
$json = nil
|
60
|
+
|
61
|
+
unless do_load || do_dump || do_read || do_write
|
62
|
+
do_load = true
|
63
|
+
do_dump = true
|
64
|
+
do_read = true
|
65
|
+
do_write = true
|
66
|
+
end
|
67
|
+
|
68
|
+
# prepare all the formats for input
|
69
|
+
if files.empty?
|
70
|
+
$obj = []
|
71
|
+
$mult.times do
|
72
|
+
$obj << (do_sample ? sample_doc(2) : files('..'))
|
73
|
+
end
|
74
|
+
|
75
|
+
$mars = Marshal.dump($obj)
|
76
|
+
$xml = Ox.dump($obj, :indent => $indent, :circular => $circular)
|
77
|
+
$json = Oj.dump($obj, :indent => $indent, :circular => $circular, :mode => :object)
|
78
|
+
File.open('sample.xml', 'w') { |f| f.write($xml) }
|
79
|
+
File.open('sample.json', 'w') { |f| f.write($json) }
|
80
|
+
File.open('sample.marshal', 'w') { |f| f.write($mars) }
|
81
|
+
else
|
82
|
+
puts "loading and parsing #{files}\n\n"
|
83
|
+
data = files.map do |f|
|
84
|
+
$xml = File.read(f)
|
85
|
+
$obj = Ox.load($xml);
|
86
|
+
$mars = Marshal.dump($obj)
|
87
|
+
$json = Oj.dump($obj, :indent => $indent, :circular => $circular)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
Oj.default_options = { :mode => :object, :indent => $indent, :circular => $circular, :allow_gc => $allow_gc }
|
92
|
+
#puts "json: #{$json.size}"
|
93
|
+
#puts "xml: #{$xml.size}"
|
94
|
+
#puts "marshal: #{$mars.size}"
|
95
|
+
|
96
|
+
|
97
|
+
if do_load
|
98
|
+
puts '-' * 80
|
99
|
+
puts "Load Performance"
|
100
|
+
perf = Perf.new()
|
101
|
+
perf.add('Oj.object', 'load') { Oj.object_load($json) }
|
102
|
+
perf.add('Ox', 'load') { Ox.load($xml, :mode => :object) }
|
103
|
+
perf.add('Marshal', 'load') { Marshal.load($mars) }
|
104
|
+
perf.run($iter)
|
105
|
+
end
|
106
|
+
|
107
|
+
if do_dump
|
108
|
+
puts '-' * 80
|
109
|
+
puts "Dump Performance"
|
110
|
+
perf = Perf.new()
|
111
|
+
perf.add('Oj', 'dump') { Oj.dump($obj) }
|
112
|
+
perf.add('Ox', 'dump') { Ox.dump($obj, :indent => $indent, :circular => $circular) }
|
113
|
+
perf.add('Marshal', 'dump') { Marshal.dump($obj) }
|
114
|
+
perf.run($iter)
|
115
|
+
end
|
116
|
+
|
117
|
+
if do_read
|
118
|
+
puts '-' * 80
|
119
|
+
puts "Read from file Performance"
|
120
|
+
perf = Perf.new()
|
121
|
+
perf.add('Oj', 'load') { Oj.load_file('sample.json') }
|
122
|
+
#perf.add('Oj', 'load') { Oj.load(File.read('sample.json')) }
|
123
|
+
perf.add('Ox', 'load_file') { Ox.load_file('sample.xml', :mode => :object) }
|
124
|
+
perf.add('Marshal', 'load') { Marshal.load(File.new('sample.marshal')) }
|
125
|
+
perf.run($iter)
|
126
|
+
end
|
127
|
+
|
128
|
+
if do_write
|
129
|
+
puts '-' * 80
|
130
|
+
puts "Write to file Performance"
|
131
|
+
perf = Perf.new()
|
132
|
+
perf.add('Oj', 'to_file') { Oj.to_file('sample.json', $obj) }
|
133
|
+
perf.add('Ox', 'to_file') { Ox.to_file('sample.xml', $obj, :indent => $indent, :circular => $circular) }
|
134
|
+
perf.add('Marshal', 'dump') { Marshal.dump($obj, File.new('sample.marshal', 'w')) }
|
135
|
+
perf.run($iter)
|
136
|
+
end
|
137
|
+
|
138
|
+
|
data/test/perf_saj.rb
ADDED
@@ -0,0 +1,109 @@
|
|
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
ADDED
@@ -0,0 +1,151 @@
|
|
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 = 50000
|
18
|
+
$with_bignum = false
|
19
|
+
$size = 0
|
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 => :compat }
|
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') { Oj.saj_parse(saj_handler, $json) }
|
141
|
+
perf.add('Oj::Saj', 'none') { Oj.saj_parse(no_saj, $json) }
|
142
|
+
perf.add('Oj::Scp', 'all') { Oj.sc_parse(sc_handler, $json) }
|
143
|
+
perf.add('Oj::Scp', 'none') { Oj.sc_parse(no_handler, $json) }
|
144
|
+
perf.add('Yajl', 'parse') { Yajl::Parser.parse($json) } unless $failed.has_key?('Yajl')
|
145
|
+
perf.add('JSON::Ext', 'parse') { JSON::Ext::Parser.new($json).parse } unless $failed.has_key?('JSON::Ext')
|
146
|
+
perf.run($iter)
|
147
|
+
|
148
|
+
unless $failed.empty?
|
149
|
+
puts "The following packages were not included for the reason listed"
|
150
|
+
$failed.each { |tag,msg| puts "***** #{tag}: #{msg}" }
|
151
|
+
end
|