oj 2.9.4 → 2.9.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of oj might be problematic. Click here for more details.

@@ -1,38 +0,0 @@
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 'ox'
16
- require 'oj'
17
- require 'perf'
18
-
19
- $indent = 0
20
- $iter = 1000
21
-
22
- opts = OptionParser.new
23
-
24
- opts.on("-i", "--iterations [Int]", Integer, "iterations") { |i| $iter = i }
25
- opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
26
- files = opts.parse(ARGV)
27
-
28
- $obj = "x" * 50_000
29
-
30
- Oj.default_options = { :mode => :strict, :indent => $indent }
31
-
32
- puts '-' * 80
33
- puts "Dump Performance"
34
- perf = Perf.new()
35
- perf.add('Oj', 'dump') { Oj.dump($obj) }
36
- perf.add('Ox', 'dump') { Ox.dump($obj, :indent => $indent, :circular => $circular) }
37
- perf.add('Marshal', 'dump') { Marshal.dump($obj) }
38
- perf.run($iter)
@@ -1,97 +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 'stringio'
9
- require 'optparse'
10
- require 'perf'
11
- require 'oj'
12
-
13
- $verbose = false
14
- $indent = 0
15
- $iter = 20000
16
- $with_bignum = false
17
- $with_nums = true
18
- $size = 0
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("-i", "--indent [Int]", Integer, "indentation") { |i| $indent = i }
24
- opts.on("-s", "--size [Int]", Integer, "size (~Kbytes)") { |i| $size = i }
25
- opts.on("-b", "with bignum") { $with_bignum = true }
26
- opts.on("-n", "without numbers") { $with_nums = false }
27
- opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
28
- files = opts.parse(ARGV)
29
-
30
- if $with_nums
31
- $obj = {
32
- 'a' => 'Alpha', # string
33
- 'b' => true, # boolean
34
- 'c' => 12345, # number
35
- 'd' => [ true, [false, [-123456789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
36
- 'e' => { 'zero' => nil, 'one' => 1, 'two' => 2, 'three' => [3], 'four' => [0, 1, 2, 3, 4] }, # hash
37
- 'f' => nil, # nil
38
- 'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
39
- 'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
40
- }
41
- $obj['g'] = 12345678901234567890123456789 if $with_bignum
42
- else
43
- $obj = {
44
- 'a' => 'Alpha',
45
- 'b' => true,
46
- 'c' => '12345',
47
- 'd' => [ true, [false, ['12345', nil], '3.967', ['something', false], nil]],
48
- 'e' => { 'zero' => '0', 'one' => '1', 'two' => '2' },
49
- 'f' => nil,
50
- 'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
51
- 'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
52
- }
53
- end
54
-
55
- Oj.default_options = { :indent => $indent, :mode => :strict }
56
-
57
- if 0 < $size
58
- o = $obj
59
- $obj = []
60
- (4 * $size).times do
61
- $obj << o
62
- end
63
- end
64
-
65
- $json = Oj.dump($obj)
66
- $obj_json = Oj.dump($obj, :mode => :object)
67
- #puts "*** size: #{$obj_json.size}"
68
- $failed = {} # key is same as String used in tests later
69
-
70
- def capture_error(tag, orig, load_key, dump_key, &blk)
71
- begin
72
- obj = blk.call(orig)
73
- raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
74
- rescue Exception => e
75
- $failed[tag] = "#{e.class}: #{e.message}"
76
- end
77
- end
78
-
79
- # Verify that all packages dump and load correctly and return the same Object as the original.
80
- capture_error('Oj:strict-str', $obj, 'load', 'dump') { |o| Oj.strict_load(Oj.dump(o, :mode => :strict)) }
81
- capture_error('Oj:strict', $obj, 'load', 'dump') { |o| Oj.strict_load(StringIO.new(Oj.dump(o, :mode => :strict))) }
82
-
83
- puts '-' * 80
84
- puts "Strict Parse Performance"
85
- perf = Perf.new()
86
- perf.add('Oj:strict-str', 'strict_load-str') { Oj.strict_load($json) }
87
- perf.add('Oj:strict-io', 'strict_load') { Oj.strict_load(StringIO.new($json)) }
88
- perf.run($iter)
89
-
90
- puts
91
- puts '-' * 80
92
- puts
93
-
94
- unless $failed.empty?
95
- puts "The following packages were not included for the reason listed"
96
- $failed.each { |tag,msg| puts "***** #{tag}: #{msg}" }
97
- end
@@ -1,35 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- # Ubuntu does not accept arguments to ruby when called using env. To get warnings to show up the -w options is
5
- # required. That can be set in the RUBYOPT environment variable.
6
- # export RUBYOPT=-w
7
-
8
- $VERBOSE = true
9
-
10
- $: << File.join(File.dirname(__FILE__), "../lib")
11
- $: << File.join(File.dirname(__FILE__), "../ext")
12
-
13
- require 'test/unit'
14
- require 'stringio'
15
- require 'oj'
16
- require 'json'
17
-
18
- class MimicAfter < ::Test::Unit::TestCase
19
-
20
- def test0_mimic_json
21
- assert(!defined?(JSON).nil?)
22
- Oj.mimic_JSON
23
- end
24
-
25
- # dump
26
- def test_dump_string
27
- Oj.default_options= {:indent => 2} # JSON this will not change anything
28
- json = JSON.dump([1, true, nil])
29
- assert_equal(%{[
30
- 1,
31
- true,
32
- null]}, json)
33
- end
34
-
35
- end # MimicAfter
@@ -1,58 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- # Ubuntu does not accept arguments to ruby when called using env. To get warnings to show up the -w options is
5
- # required. That can be set in the RUBYOPT environment variable.
6
- # export RUBYOPT=-w
7
-
8
- $VERBOSE = true
9
-
10
- $: << File.join(File.dirname(__FILE__), "../lib")
11
- $: << File.join(File.dirname(__FILE__), "../ext")
12
-
13
- require 'test/unit'
14
- require 'stringio'
15
- require 'date'
16
- require 'oj'
17
-
18
- $ruby = RUBY_DESCRIPTION.split(' ')[0]
19
- $ruby = 'ree' if 'ruby' == $ruby && RUBY_DESCRIPTION.include?('Ruby Enterprise Edition')
20
-
21
- def hash_eql(h1, h2)
22
- return false if h1.size != h2.size
23
- h1.keys.each do |k|
24
- return false unless h1[k] == h2[k]
25
- end
26
- true
27
- end
28
-
29
- class StrictJuice < ::Test::Unit::TestCase
30
-
31
- # Stream IO
32
- def test_io_string
33
- json = %{{
34
- "x":true,
35
- "y":58,
36
- "z": [1,2,3]
37
- }
38
- }
39
- input = StringIO.new(json)
40
- obj = Oj.strict_loadx(input)
41
- assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
42
- end
43
-
44
- def xtest_io_file
45
- filename = 'open_file_test.json'
46
- File.open(filename, 'w') { |f| f.write(%{{
47
- "x":true,
48
- "y":58,
49
- "z": [1,2,3]
50
- }
51
- }) }
52
- f = File.new(filename)
53
- obj = Oj.strict_loadx(f)
54
- f.close()
55
- assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
56
- end
57
-
58
- end
@@ -1,63 +0,0 @@
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
-
21
- opts = OptionParser.new
22
-
23
- opts.on("-c", "--count [Int]", Integer, "iterations") { |i| $iter = i }
24
- opts.on("-i", "--indent [Int]", Integer, "indent") { |i| $indent = i }
25
-
26
- opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
27
- files = opts.parse(ARGV)
28
-
29
- $levels = [ "ERROR", "WARN", "INFO", "DEBUG", "TRACE" ]
30
- $entry = {
31
- "bar" => "0",
32
- "level" => "INFO",
33
- "message" => "A message",
34
- "msgType" => 1,
35
- "source" => "Test",
36
- "thread" => "main",
37
- "version" => 1,
38
- "when" => 1400000000000000000,
39
- }
40
-
41
- Oj.default_options = { :mode => :strict, :indent => $indent }
42
-
43
- filename = 'log.json'
44
- File.open(filename, "w") { |f|
45
- $iter.times do |i|
46
- $entry["bar"] = (1_400_000_000_000 + i * 200) / 1000
47
- $entry["when"] = 1_400_000_000_000_000_000 + i * 100_000_000
48
- $entry["message"] = "This is log message number #{i} out of #{$iter} entries."
49
- $entry["level"] = $levels[i % $levels.size]
50
- Oj.to_stream(f, $entry, :indent => $indent)
51
- if 0 == $indent
52
- f.flush()
53
- f.write("\n")
54
- end
55
- end
56
- }
57
-
58
- $cnt = 0
59
- perf = Perf.new()
60
- perf.add('Oj.load_file', '') { Oj.load_file(filename) { |o| $cnt += 1 } }
61
- perf.run(1)
62
-
63
- puts "*** read #{$cnt} entries"
data/test/x.rb DELETED
@@ -1,59 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- # Ubuntu does not accept arguments to ruby when called using env. To get warnings to show up the -w options is
5
- # required. That can be set in the RUBYOPT environment variable.
6
- # export RUBYOPT=-w
7
-
8
- $VERBOSE = true
9
-
10
- $: << File.join(File.dirname(__FILE__), "../lib")
11
- $: << File.join(File.dirname(__FILE__), "../ext")
12
-
13
- require 'oj'
14
-
15
- # Oj is not able to deserialize all classes that are a subclass of a Ruby
16
- # Exception. Only exception that take one required string argument in the
17
- # initialize() method are supported. This is an example of how to write an
18
- # Exception subclass that supports both a single string intializer and an
19
- # Exception as an argument. Additional optional arguments can be added as well.
20
- #
21
- # The reason for this restriction has to do with a design decision on the part
22
- # of the Ruby developers. Exceptions are special Objects. They do not follow the
23
- # rules of other Objects. Exceptions have 'mesg' and a 'bt' attribute. Note that
24
- # these are not '@mesg' and '@bt'. They can not be set using the normal C or
25
- # Ruby calls. The only way I have found to set the 'mesg' attribute is through
26
- # the initializer. Unfortunately that means any subclass that provides a
27
- # different initializer can not be automatically decoded. A way around this is
28
- # to use a create function but this example shows an alternative.
29
-
30
- class WrapException < StandardError
31
- attr_reader :original
32
-
33
- def initialize(msg_or_err)
34
- if msg_or_err.is_a?(Exception)
35
- super(msg_or_err.message)
36
- @original = msg_or_err
37
- set_backtrace(msg_or_err.backtrace)
38
- else
39
- super(message)
40
- @original = nil
41
- end
42
- end
43
- end
44
-
45
- e = WrapException.new(RuntimeError.new("Something broke."))
46
-
47
- json = Oj.dump(e, :mode => :object)
48
- puts "original:\n#{json}"
49
- # outputs:
50
- # original:
51
- # {"^o":"WrapException","original":{"^o":"RuntimeError","~mesg":"Something broke.","~bt":null},"~mesg":"Something broke.","~bt":null}
52
-
53
- e2 = Oj.load(json, :mode => :object)
54
- puts "dumped, loaded, and dumped again:\n#{Oj.dump(e2, :mode => :object)}"
55
- # outputs:
56
- # original: {"^o":"WrapException","original":{"^o":"RuntimeError","~mesg":"Something broke.","~bt":null},"~mesg":"Something broke.","~bt":null}
57
- # dumped, loaded, and dumped again:
58
- # {"^o":"WrapException","original":{"^o":"RuntimeError","~mesg":"Something broke.","~bt":null},"~mesg":"Something broke.","~bt":null}
59
-