oj 2.7.2 → 2.7.3

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.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f30913f8e7b9a96cd9388dcd6959330f34960c04
4
- data.tar.gz: 8bb5882a6def1fbb9348f0ac0a48f59d1d7468f4
3
+ metadata.gz: 86fa5360a8dad0be6218e5b707229d1123242f5c
4
+ data.tar.gz: bd7112345eca19080d314ef865db6d377a81596a
5
5
  SHA512:
6
- metadata.gz: 70e9ec5a16483c18e964d2ab4f8801c111353018fef608fc035de8bbd19b855baf825659e55a1af11a0ed9795902316517a27ad58b82cc05b81773f5678a434a
7
- data.tar.gz: c9f605979c2b2051a8257fcb57a125aa3b5da8d833829a36701655415951deb6540b11ba7c7e2a69e622d6920725685c817cda08419e70c7d0cbc9183a6c0c2f
6
+ metadata.gz: ccc6d971d9a8f0be85cb7c67b523a981d756c73fb1a2d1bd499f61bb1120a6da5925d8723cca8c008e8e2b281be7e81c20574358fb20b8b53340fb88f74439bc
7
+ data.tar.gz: 172de575b67abc4012818a9e526be9661bd78e0817bb62db6c060692f3dfd19fd29f5d40240d3d45e9557085fa34ebf9de2a1d23fe43daacf781cb7665d0b4eb
data/README.md CHANGED
@@ -26,9 +26,9 @@ Follow [@peterohler on Twitter](http://twitter.com/#!/peterohler) for announceme
26
26
 
27
27
  [![Build Status](https://secure.travis-ci.org/ohler55/oj.png?branch=master)](http://travis-ci.org/ohler55/oj)
28
28
 
29
- ### Current Release 2.7.2
29
+ ### Current Release 2.7.3
30
30
 
31
- - Added option return nil if nil is provided as input to load.
31
+ - Fixed bug where load and dump of Structs in modules did not work correctly.
32
32
 
33
33
  [Older release notes](http://www.ohler.com/dev/oj_misc/release_notes.html).
34
34
 
@@ -191,8 +191,10 @@ hat_value(ParseInfo pi, Val parent, const char *key, size_t klen, volatile VALUE
191
191
  oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "Invalid struct data");
192
192
  return 1;
193
193
  }
194
+
194
195
  // If struct is not defined then we let this fail and raise an exception.
195
- sc = rb_const_get(oj_struct_class, rb_to_id(*RARRAY_PTR(value)));
196
+ sc = oj_name2struct(pi, *RARRAY_PTR(value));
197
+
196
198
  // Create a properly initialized struct instance without calling the initialize method.
197
199
  parent->val = rb_obj_alloc(sc);
198
200
  // If the JSON array has more entries than the struct class allows, we record an error.
@@ -119,3 +119,11 @@ oj_name2class(ParseInfo pi, const char *name, size_t len, int auto_define) {
119
119
  #endif
120
120
  return clas;
121
121
  }
122
+
123
+ VALUE
124
+ oj_name2struct(ParseInfo pi, VALUE nameVal) {
125
+ size_t len = RSTRING_LEN(nameVal);
126
+ const char *str = StringValuePtr(nameVal);
127
+
128
+ return resolve_classpath(pi, str, len, 0);
129
+ }
@@ -34,5 +34,6 @@
34
34
  #include "ruby.h"
35
35
 
36
36
  extern VALUE oj_name2class(ParseInfo pi, const char *name, size_t len, int auto_define);
37
+ extern VALUE oj_name2struct(ParseInfo pi, VALUE nameVal);
37
38
 
38
39
  #endif /* __OJ_RESOLVE_H__ */
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Oj
3
3
  # Current version of the module.
4
- VERSION = '2.7.2'
4
+ VERSION = '2.7.3'
5
5
  end
@@ -0,0 +1,64 @@
1
+ #!/usr/bin/env ruby -wW1
2
+ # encoding: UTF-8
3
+
4
+ $: << File.join(File.dirname(__FILE__), "../lib")
5
+ $: << File.join(File.dirname(__FILE__), "../ext")
6
+
7
+ #require 'test/unit'
8
+ require 'optparse'
9
+ require 'oj'
10
+ require 'ox'
11
+
12
+ $indent = 2
13
+
14
+ opts = OptionParser.new
15
+ opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
16
+ files = opts.parse(ARGV)
17
+
18
+ iter = 100000
19
+ s = %{
20
+ { "class": "Foo::Bar",
21
+ "attr1": [ true, [false, [12345, null], 3.967, ["something", false], null]],
22
+ "attr2": { "one": 1 }
23
+ }
24
+ }
25
+ #s = File.read('sample.json')
26
+
27
+ Oj.default_options = { :indent => 0 }
28
+
29
+ obj = Oj.load(s)
30
+ xml = Ox.dump(obj, :indent => 0)
31
+
32
+ puts xml
33
+
34
+ start = Time.now
35
+ iter.times do
36
+ Oj.load(s)
37
+ end
38
+ dt = Time.now - start
39
+ puts "%d Oj.load()s in %0.3f seconds or %0.1f loads/msec" % [iter, dt, iter/dt/1000.0]
40
+
41
+ start = Time.now
42
+ iter.times do
43
+ Ox.load(xml)
44
+ end
45
+ dt = Time.now - start
46
+ puts "%d Ox.load()s in %0.3f seconds or %0.1f loads/msec" % [iter, dt, iter/dt/1000.0]
47
+
48
+ puts
49
+
50
+ start = Time.now
51
+ iter.times do
52
+ Oj.dump(obj)
53
+ end
54
+ dt = Time.now - start
55
+ puts "%d Oj.dump()s in %0.3f seconds or %0.1f dumps/msec" % [iter, dt, iter/dt/1000.0]
56
+
57
+ start = Time.now
58
+ iter.times do
59
+ Ox.dump(obj)
60
+ end
61
+ dt = Time.now - start
62
+ puts "%d Ox.dump()s in %0.3f seconds or %0.1f dumps/msec" % [iter, dt, iter/dt/1000.0]
63
+
64
+ puts
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/env ruby -wW1
2
+ # encoding: UTF-8
3
+
4
+ $: << File.join(File.dirname(__FILE__), "../lib")
5
+ $: << File.join(File.dirname(__FILE__), "../ext")
6
+
7
+ #require 'test/unit'
8
+ require 'optparse'
9
+ require 'yajl'
10
+ require 'oj'
11
+
12
+ $indent = 2
13
+
14
+ opts = OptionParser.new
15
+ opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
16
+ files = opts.parse(ARGV)
17
+
18
+ class Foo
19
+ def initialize()
20
+ @x = true
21
+ @y = 58
22
+ end
23
+ def to_json()
24
+ %{{"x":#{@x},"y":#{@y}}}
25
+ end
26
+ def to_hash()
27
+ { 'x' => @x, 'y' => @y }
28
+ end
29
+ end
30
+
31
+ iter = 100000
32
+ s = %{
33
+ { "class": "Foo::Bar",
34
+ "attr1": [ true, [false, [12345, null], 3.967, ["something", false], null]],
35
+ "attr2": { "one": 1 }
36
+ }
37
+ }
38
+
39
+ obj = Oj.load(s)
40
+ obj["foo"] = Foo.new()
41
+
42
+ Oj.default_options = { :indent => 0, :effort => :internal }
43
+
44
+ puts
45
+
46
+ start = Time.now
47
+ iter.times do
48
+ Oj.load(s)
49
+ end
50
+ dt = Time.now - start
51
+ puts "%d Oj.load()s in %0.3f seconds or %0.1f loads/msec" % [iter, dt, iter/dt/1000.0]
52
+
53
+ start = Time.now
54
+ iter.times do
55
+ Yajl::Parser.parse(s)
56
+ end
57
+ dt = Time.now - start
58
+ puts "%d Yajl::Parser.parse()s in %0.3f seconds or %0.1f parses/msec" % [iter, dt, iter/dt/1000.0]
59
+
60
+ puts
61
+
62
+ start = Time.now
63
+ iter.times do
64
+ Oj.dump(obj)
65
+ end
66
+ dt = Time.now - start
67
+ puts "%d Oj.dump()s in %0.3f seconds or %0.1f dumps/msec" % [iter, dt, iter/dt/1000.0]
68
+
69
+ start = Time.now
70
+ iter.times do
71
+ Yajl::Encoder.encode(obj)
72
+ end
73
+ dt = Time.now - start
74
+ puts "%d Yajl::Encoder.encode()s in %0.3f seconds or %0.1f encodes/msec" % [iter, dt, iter/dt/1000.0]
75
+
76
+ puts
@@ -0,0 +1,213 @@
1
+ #!/usr/bin/env ruby -wW1
2
+
3
+ $: << '.'
4
+ $: << '..'
5
+ $: << '../lib'
6
+ $: << '../ext'
7
+
8
+ if __FILE__ == $0
9
+ if (i = ARGV.index('-I'))
10
+ x,path = ARGV.slice!(i, 2)
11
+ $: << path
12
+ end
13
+ end
14
+
15
+ require 'optparse'
16
+ require 'ox'
17
+ require 'oj'
18
+ require 'perf'
19
+ require 'sample'
20
+ require 'files'
21
+
22
+ $verbose = 0
23
+ $circular = false
24
+ $indent = 0
25
+
26
+ do_sample = false
27
+ do_files = false
28
+
29
+ do_load = false
30
+ do_dump = false
31
+ do_read = false
32
+ do_write = false
33
+ $iter = 1000
34
+
35
+ opts = OptionParser.new
36
+ opts.on("-v", "increase verbosity") { $verbose += 1 }
37
+
38
+ opts.on("-c", "circular options") { $circular = true }
39
+
40
+ opts.on("-s", "load and dump as sample Ruby object") { do_sample = true }
41
+ opts.on("-f", "load and dump as files Ruby object") { do_files = true }
42
+
43
+ opts.on("-l", "load") { do_load = true }
44
+ opts.on("-d", "dump") { do_dump = true }
45
+ opts.on("-r", "read") { do_read = true }
46
+ opts.on("-w", "write") { do_write = true }
47
+ opts.on("-a", "load, dump, read and write") { do_load = true; do_dump = true; do_read = true; do_write = true }
48
+
49
+ opts.on("-i", "--iterations [Int]", Integer, "iterations") { |i| $iter = i }
50
+
51
+ opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
52
+ files = opts.parse(ARGV)
53
+
54
+ if files.empty?
55
+ data = []
56
+ obj = do_sample ? sample_doc(2) : files('..')
57
+ mars = Marshal.dump(obj)
58
+ xml = Ox.dump(obj, :indent => $indent, circular: $circular)
59
+ json = Oj.dump(obj, :indent => $indent, circular: $circular)
60
+ File.open('sample.xml', 'w') { |f| f.write(xml) }
61
+ File.open('sample.json', 'w') { |f| f.write(json) }
62
+ File.open('sample.marshal', 'w') { |f| f.write(mars) }
63
+ data << { :file => 'sample.xml', :obj => obj, :xml => xml, :marshal => mars, :json => json }
64
+ else
65
+ puts "loading and parsing #{files}\n\n"
66
+ # TBD change to allow xml and json
67
+ data = files.map do |f|
68
+ xml = File.read(f)
69
+ obj = Ox.load(xml);
70
+ mars = Marshal.dump(obj)
71
+ json = Oj.dump(obj, :indent => $indent, circular: $circular)
72
+ { :file => f, :obj => obj, :xml => xml, :marshal => mars, :json => json }
73
+ end
74
+ end
75
+
76
+ $ox_load_time = 0
77
+ $mars_load_time = 0
78
+ $ox_dump_time = 0
79
+ $oj_dump_time = 0
80
+ $mars_dump_time = 0
81
+
82
+ def perf_load(d)
83
+ filename = d[:file]
84
+ marshal_filename = 'sample.marshal'
85
+ xml = d[:xml]
86
+ mars = d[:marshal]
87
+ json = d[:json]
88
+
89
+ if 0 < $verbose
90
+ obj = Ox.load(xml, :mode => :object, :trace => $verbose)
91
+ return
92
+ end
93
+ start = Time.now
94
+ (1..$iter).each do
95
+ obj = Ox.load(xml, :mode => :object)
96
+ end
97
+ $ox_load_time = Time.now - start
98
+ puts "Parsing #{$iter} times with Ox took #{$ox_load_time} seconds."
99
+
100
+ start = Time.now
101
+ (1..$iter).each do
102
+ obj = Oj.load(json, :mode => :object)
103
+ end
104
+ $oj_load_time = Time.now - start
105
+ puts "Parsing #{$iter} times with Oj took #{$oj_load_time} seconds."
106
+
107
+ start = Time.now
108
+ (1..$iter).each do
109
+ obj = Marshal.load(mars)
110
+ end
111
+ $mars_load_time = Time.now - start
112
+ puts "Marshalling #{$iter} times took #{$mars_load_time} seconds."
113
+ puts ">>> Ox is %0.1f faster than Marshal loading.\n\n" % [$mars_load_time/$ox_load_time]
114
+ end
115
+
116
+ def perf_dump(d)
117
+ obj = d[:obj]
118
+
119
+ start = Time.now
120
+ (1..$iter).each do
121
+ xml = Ox.dump(obj, :indent => $indent, :circular => $circular)
122
+ #puts "*** ox:\n#{xml}"
123
+ end
124
+ $ox_dump_time = Time.now - start
125
+ puts "Ox dumping #{$iter} times with ox took #{$ox_dump_time} seconds."
126
+
127
+ Oj.default_options = {:indent => $indent}
128
+ start = Time.now
129
+ (1..$iter).each do
130
+ json = Oj.dump(obj)
131
+ end
132
+ $oj_dump_time = Time.now - start
133
+ puts "Oj dumping #{$iter} times with oj took #{$oj_dump_time} seconds."
134
+
135
+ obj = d[:obj]
136
+ start = Time.now
137
+ (1..$iter).each do
138
+ m = Marshal.dump(obj)
139
+ end
140
+ $mars_dump_time = Time.now - start
141
+ puts "Marshal dumping #{$iter} times took #{$mars_dump_time} seconds."
142
+ puts ">>> Ox is %0.1f faster than Marshal dumping.\n\n" % [$mars_dump_time/$ox_dump_time]
143
+ end
144
+
145
+ def perf_read(d)
146
+ ox_read_time = 0
147
+ mars_read_time = 0
148
+
149
+ filename = d[:file]
150
+ marshal_filename = 'sample.marshal'
151
+ xml = d[:xml]
152
+ mars = d[:marshal]
153
+
154
+ # now load from the file
155
+ start = Time.now
156
+ (1..$iter).each do
157
+ obj = Ox.load_file(filename, :mode => :object)
158
+ end
159
+ ox_read_time = Time.now - start
160
+ puts "Loading and parsing #{$iter} times with ox took #{ox_read_time} seconds."
161
+
162
+ start = Time.now
163
+ (1..$iter).each do
164
+ m = File.read(marshal_filename)
165
+ obj = Marshal.load(m)
166
+ end
167
+ mars_read_time = Time.now - start
168
+ puts "Reading and marshalling #{$iter} times took #{mars_read_time} seconds."
169
+ puts ">>> Ox is %0.1f faster than Marshal loading and parsing.\n\n" % [mars_read_time/ox_read_time]
170
+
171
+ end
172
+
173
+ def perf_write(d)
174
+ ox_write_time = 0
175
+ mars_write_time = 0
176
+
177
+ ox_filename = 'out.xml'
178
+ marshal_filename = 'out.marshal'
179
+ obj = d[:obj]
180
+
181
+ start = Time.now
182
+ (1..$iter).each do
183
+ xml = Ox.to_file(ox_filename, obj, :indent => $indent)
184
+ end
185
+ ox_write_time = Time.now - start
186
+ puts "Ox dumping #{$iter} times with ox took #{ox_write_time} seconds."
187
+
188
+ start = Time.now
189
+ (1..$iter).each do
190
+ m = Marshal.dump(obj, circular: $circular)
191
+ File.open(marshal_filename, "w") { |f| f.write(m) }
192
+ end
193
+ mars_write_time = Time.now - start
194
+ puts "Marshal dumping and writing #{$iter} times took #{mars_write_time} seconds."
195
+ puts ">>> Ox is %0.1f faster than Marshal dumping.\n\n" % [mars_write_time/ox_write_time]
196
+
197
+ end
198
+
199
+ #if do_sample or do_files
200
+ data.each do |d|
201
+ puts "Using file #{d[:file]}."
202
+
203
+ perf_load(d) if do_load
204
+ perf_dump(d) if do_dump
205
+ if do_load and do_dump
206
+ puts ">>> Ox is %0.1f faster than Marshal dumping and loading.\n\n" % [($mars_load_time + $mars_dump_time)/($ox_load_time + $ox_dump_time)] unless 0 == $mars_load_time
207
+ end
208
+
209
+ perf_read(d) if do_read
210
+ perf_write(d) if do_write
211
+
212
+ end
213
+ #end
@@ -68,6 +68,10 @@ module One
68
68
  end # Deep
69
69
  end # Three
70
70
  end # Two
71
+
72
+ class Stuck2 < Struct.new(:a, :b)
73
+ end
74
+
71
75
  end # One
72
76
 
73
77
  class Stuck < Struct.new(:a, :b)
@@ -330,6 +334,13 @@ class ObjectJuice < ::Test::Unit::TestCase
330
334
  end
331
335
  end
332
336
 
337
+ def test_json_struct2
338
+ unless 'jruby' == RUBY_DESCRIPTION.split(' ')[0]
339
+ obj = One::Stuck2.new(false, 7)
340
+ dump_and_load(obj, false)
341
+ end
342
+ end
343
+
333
344
  def test_json_non_str_hash
334
345
  obj = { 59 => "young", false => true }
335
346
  dump_and_load(obj, false)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oj
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.2
4
+ version: 2.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Ohler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-06 00:00:00.000000000 Z
11
+ date: 2014-04-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'The fastest JSON parser and object serializer. '
14
14
  email: peter@ohler.com
@@ -56,17 +56,14 @@ files:
56
56
  - lib/oj/saj.rb
57
57
  - lib/oj/schandler.rb
58
58
  - lib/oj/version.rb
59
- - test/a.rb
60
- - test/bug.rb
61
59
  - test/debian_test.rb
62
- - test/e.rb
63
60
  - test/files.rb
64
- - test/foo.rb
65
- - test/lots.rb
66
- - test/mj.rb
67
61
  - test/perf.rb
62
+ - test/perf1.rb
63
+ - test/perf2.rb
68
64
  - test/perf_compat.rb
69
65
  - test/perf_fast.rb
66
+ - test/perf_obj_old.rb
70
67
  - test/perf_object.rb
71
68
  - test/perf_saj.rb
72
69
  - test/perf_scp.rb
@@ -86,7 +83,6 @@ files:
86
83
  - test/sample/shape.rb
87
84
  - test/sample/text.rb
88
85
  - test/sample_json.rb
89
- - test/struct.rb
90
86
  - test/test_compat.rb
91
87
  - test/test_fast.rb
92
88
  - test/test_gc.rb
@@ -98,7 +94,6 @@ files:
98
94
  - test/test_strict.rb
99
95
  - test/test_writer.rb
100
96
  - test/tests.rb
101
- - test/x.rb
102
97
  homepage: http://www.ohler.com/oj
103
98
  licenses:
104
99
  - MIT
@@ -128,4 +123,3 @@ signing_key:
128
123
  specification_version: 4
129
124
  summary: A fast JSON parser and serializer.
130
125
  test_files: []
131
- has_rdoc: true
data/test/a.rb DELETED
@@ -1,38 +0,0 @@
1
- #!/usr/bin/env ruby -wW1
2
- # encoding: UTF-8
3
-
4
- $: << File.dirname(__FILE__)
5
- $: << File.join(File.dirname(__FILE__), "../lib")
6
- $: << File.join(File.dirname(__FILE__), "../ext")
7
-
8
- require 'pp'
9
- require 'oj'
10
- require 'perf'
11
-
12
- obj = [[1],[2],[3],[4],[5],[6],[7],[8],[9]]
13
- obj = [[],[],[],[],[],[],[],[],[]]
14
- obj = {
15
- 'a' => 'Alpha', # string
16
- 'b' => true, # boolean
17
- 'c' => 12345, # number
18
- 'd' => [ true, [false, [12345, nil], 3.967, ['something', false], nil]], # mix it up array
19
- 'e' => { 'one' => 1, 'two' => 2 }, # hash
20
- 'f' => nil, # nil
21
- 'g' => 12345678901234567890123456789, # big number
22
- 'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
23
- 'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
24
- }
25
-
26
- json = Oj.dump(obj, mode: :compat)
27
-
28
- puts json
29
- #pp Oj.saj_parse(nil, json)
30
- pp Oj.t_parse(json)
31
-
32
- if true
33
- perf = Perf.new()
34
- perf.add('SAJ', 'oj') { Oj.saj_parse(nil, json) }
35
- perf.add('T', 'oj') { Oj.t_parse(json) }
36
- perf.add('load', 'oj') { Oj.load(json) }
37
- perf.run(10000)
38
- end
@@ -1,17 +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
- obj = Oj.load_file('bug.json', :mode => :object)
16
-
17
- puts Oj.dump(obj, :mode => :object, :indent => 0)
data/test/e.rb DELETED
@@ -1,12 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- $: << File.join(File.dirname(__FILE__), "../lib")
4
- $: << File.join(File.dirname(__FILE__), "../ext")
5
-
6
- require 'oj'
7
-
8
- json = %{"\xc2\xa9\xc3\x98"}
9
- puts "original:\n#{json}"
10
-
11
- str = Oj.load(json)
12
- puts "out: #{str}"
@@ -1,24 +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
- reltypes={}
16
- Oj::Doc.open_file('foo.json') do |doc|
17
- doc.each_child do |target|
18
- puts "#{target.local_key} is #{target.local_key.class}"
19
- target.each_leaf do |score|
20
- reltype=score.local_key
21
- reltypes[reltype] = (reltypes[reltype] || 0) + 1
22
- end
23
- end
24
- end
@@ -1,68 +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
- module One
16
- module Two
17
- module Three
18
- class Empty
19
-
20
- def initialize()
21
- end
22
-
23
- def eql?(o)
24
- self.class == o.class
25
- end
26
- alias == eql?
27
-
28
- def to_hash()
29
- {'json_class' => "#{self.class.name}"}
30
- end
31
-
32
- def to_json(*a)
33
- %{{"json_class":"#{self.class.name}"}}
34
- end
35
-
36
- def self.json_create(h)
37
- self.new()
38
- end
39
- end # Empty
40
- end # Three
41
- end # Two
42
- end # One
43
-
44
- $obj = {
45
- 'a' => 'Alpha', # string
46
- 'b' => true, # boolean
47
- 'c' => 12345, # number
48
- 'd' => [ true, [false, [-123456789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
49
- 'e' => { 'zero' => nil, 'one' => 1, 'two' => 2, 'three' => [3], 'four' => [0, 1, 2, 3, 4] }, # hash
50
- 'f' => nil, # nil
51
- 'g' => One::Two::Three::Empty.new(),
52
- 'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
53
- 'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
54
- }
55
-
56
- $obj = [$obj]*10000
57
-
58
- Oj.default_options = { :indent => 2, :mode => :compat }
59
-
60
- $json = Oj.dump($obj, :mode => :compat)
61
-
62
- $result = nil
63
- 100.times { |i|
64
- print(".") if (0 == i % 10)
65
- $result = Oj.compat_load($json)
66
- }
67
-
68
-
data/test/mj.rb DELETED
@@ -1,48 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- $: << File.join(File.dirname(__FILE__), "../lib")
5
- $: << File.join(File.dirname(__FILE__), "../ext")
6
- # $: << File.join(File.dirname(__FILE__), "../../multi_json/lib")
7
-
8
- require 'multi_json'
9
- require 'benchmark'
10
- require 'yajl'
11
- require 'json'
12
- require 'oj'
13
-
14
- iter = 1_000_000
15
- iter = 100_000
16
-
17
- json = %({"k1":"val1","k2":"val2","k3":"val3"})
18
- obj = { k1: "val1", k2: "val2", k3: "val3" }
19
-
20
- puts "Benchmarks for different JSON handlers with MultiJson."
21
- puts " Ruby #{RUBY_VERSION}"
22
- puts " #{iter} iterations"
23
-
24
- MultiJson.engine = :oj
25
- dt = Benchmark.realtime { iter.times { MultiJson.decode(json) }}
26
- et = Benchmark.realtime { iter.times { MultiJson.encode(obj) }}
27
- puts " Oj decode: #{dt} encode: #{et}"
28
-
29
- MultiJson.engine = :yajl
30
- dt = Benchmark.realtime { iter.times { MultiJson.decode(json) }}
31
- et = Benchmark.realtime { iter.times { MultiJson.encode(obj) }}
32
- puts " Yajl decode: #{dt} encode: #{et}"
33
-
34
- MultiJson.engine = :json_gem
35
- dt = Benchmark.realtime { iter.times { MultiJson.decode(json) }}
36
- et = Benchmark.realtime { iter.times { MultiJson.encode(obj) }}
37
- puts " Json decode: #{dt} encode: #{et}"
38
-
39
- Oj.default_options = { :mode => :compat, :time_format => :ruby }
40
- dt = Benchmark.realtime { iter.times { Oj.load(json) }}
41
- et = Benchmark.realtime { iter.times { Oj.dump(obj) }}
42
- puts "Raw Oj decode: #{dt} encode: #{et}"
43
-
44
- ye = Yajl::Encoder.new
45
- dt = Benchmark.realtime { iter.times { Yajl::Parser.parse(json) }}
46
- et = Benchmark.realtime { iter.times { Yajl::Encoder.encode(obj) }}
47
- e2 = Benchmark.realtime { iter.times { ye.encode(obj) }}
48
- puts "Raw Yajl decode: #{dt} encode: #{et}, encoder: #{e2}"
@@ -1,29 +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
- A = Struct.new(:a,:b,:c,:d)
16
- B = Struct.new(:e,:f)
17
-
18
- obj = [A.new(55, B.new(1, 'X'), B.new(2, 'Y'), 3)]
19
-
20
- s = Oj.dump(obj, :mode => :object)
21
-
22
- 100000.times do
23
- Oj.load(s, :mode => :object)
24
- # ds = Oj.dump(o, :mode => :object)
25
- # if ds != s
26
- # puts ds
27
- # raise "holy crap"
28
- # end
29
- end
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
-