oj 2.11.5 → 2.12.0

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.

@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ $: << File.dirname(__FILE__)
5
+
6
+ %w(lib ext test).each do |dir|
7
+ $LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
8
+ end
9
+
10
+ require 'minitest'
11
+ require 'minitest/autorun'
12
+ require 'oj'
13
+
14
+ Oj.mimic_JSON
15
+
16
+ require 'rails/all'
17
+ require 'active_model'
18
+ require 'active_model_serializers'
19
+ require 'active_support/json'
20
+
21
+ #Oj.mimic_JSON
22
+
23
+ class Category
24
+ include ActiveModel::Model
25
+ include ActiveModel::SerializerSupport
26
+
27
+ attr_accessor :id, :name
28
+
29
+ def initialize(id, name)
30
+ @id = id
31
+ @name = name
32
+ end
33
+ end
34
+
35
+ class CategorySerializer < ActiveModel::Serializer
36
+ attributes :id, :name
37
+ end
38
+
39
+ class MimicRails < Minitest::Test
40
+
41
+ def test_dump_object
42
+ Oj.default_options= {:indent => 0}
43
+ category = Category.new(1, 'test')
44
+ serializer = CategorySerializer.new(category)
45
+
46
+ json = serializer.to_json()
47
+ puts "*** serializer.to_json() #{serializer.to_json()}"
48
+ assert_equal(%|{"category":{"id":1,"name":"test"}}|, json)
49
+
50
+ json = serializer.as_json()
51
+ puts "*** serializer.as_json() #{serializer.as_json()}"
52
+ assert_equal({"category" => {:id => 1, :name => "test"}}, json)
53
+
54
+ json = JSON.dump(serializer)
55
+ puts "*** JSON.dump(serializer) #{JSON.dump(serializer)}"
56
+ assert_equal(%|{"category":{"id":1,"name":"test"}}|, json)
57
+ end
58
+
59
+ end # MimicRails
@@ -386,26 +386,26 @@ class Juice < Minitest::Test
386
386
  def test_unix_time_compat
387
387
  t = Time.xmlschema("2012-01-05T23:58:07.123456000+09:00")
388
388
  #t = Time.local(2012, 1, 5, 23, 58, 7, 123456)
389
- json = Oj.dump(t, :mode => :compat)
389
+ json = Oj.dump(t, :mode => :compat, :time_format => :unix)
390
390
  assert_equal(%{1325775487.123456000}, json)
391
391
  end
392
392
  def test_unix_time_compat_precision
393
393
  t = Time.xmlschema("2012-01-05T23:58:07.123456789+09:00")
394
394
  #t = Time.local(2012, 1, 5, 23, 58, 7, 123456)
395
- json = Oj.dump(t, :mode => :compat, :second_precision => 5)
395
+ json = Oj.dump(t, :mode => :compat, :second_precision => 5, :time_format => :unix)
396
396
  assert_equal(%{1325775487.12346}, json)
397
397
  t = Time.xmlschema("2012-01-05T23:58:07.999600+09:00")
398
- json = Oj.dump(t, :mode => :compat, :second_precision => 3)
398
+ json = Oj.dump(t, :mode => :compat, :second_precision => 3, :time_format => :unix)
399
399
  assert_equal(%{1325775488.000}, json)
400
400
  end
401
401
  def test_unix_time_compat_early
402
402
  t = Time.xmlschema("1954-01-05T00:00:00.123456789+00:00")
403
- json = Oj.dump(t, :mode => :compat, :second_precision => 5)
403
+ json = Oj.dump(t, :mode => :compat, :second_precision => 5, :time_format => :unix)
404
404
  assert_equal(%{-504575999.87654}, json)
405
405
  end
406
406
  def test_unix_time_compat_1970
407
407
  t = Time.xmlschema("1970-01-01T00:00:00.123456789+00:00")
408
- json = Oj.dump(t, :mode => :compat, :second_precision => 5)
408
+ json = Oj.dump(t, :mode => :compat, :second_precision => 5, :time_format => :unix)
409
409
  assert_equal(%{0.12346}, json)
410
410
  end
411
411
  def test_ruby_time_compat
@@ -503,16 +503,6 @@ class Juice < Minitest::Test
503
503
  assert_equal(%{"2012-01-05T23:58:07Z"}, json)
504
504
  end
505
505
  end
506
- def test_time_object
507
- t = Time.now()
508
- Oj.default_options = { :mode => :object }
509
- dump_and_load(t, false)
510
- end
511
- def test_time_object_early
512
- t = Time.xmlschema("1954-01-05T00:00:00.123456")
513
- Oj.default_options = { :mode => :object }
514
- dump_and_load(t, false)
515
- end
516
506
 
517
507
  # Class
518
508
  def test_class_strict
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ %w(lib ext).each do |dir|
5
+ $LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
6
+ end
7
+
8
+ require 'stringio'
9
+ require 'oj'
10
+
11
+
12
+ filename = File.join(File.dirname(__FILE__), 'day.json')
13
+ File.open(filename, "w") do |f|
14
+ w = Oj::StreamWriter.new(f, :indent => -1)
15
+ 390.times do |i|
16
+ w.push_object()
17
+ w.push_value(12, 'msgType')
18
+ w.push_value(1, 'version')
19
+ w.push_value(1_400_074_200 + i * 60, 'bar')
20
+ w.push_value('TBC', 'source')
21
+ w.push_array('timebars')
22
+ w.push_object()
23
+ w.push_value('aapl_24', 'asset')
24
+ w.push_value(91.87, 'close')
25
+ w.pop()
26
+ w.pop()
27
+ w.pop()
28
+ end
29
+ f.write("\n")
30
+ end
31
+
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ $: << File.dirname(__FILE__)
5
+
6
+ require 'helper'
7
+
8
+ require 'zlib'
9
+
10
+ File.open('test.json.gz', 'r') do |file|
11
+ Zlib::GzipReader.wrap(file) do |f2|
12
+ puts "*** f2: #{f2}"
13
+ Oj.load(f2) do |val|
14
+ puts val
15
+ end
16
+ end
17
+ end
18
+
19
+ =begin
20
+ And a json file with the following contents (then gzipped):
21
+
22
+ {"a":2}
23
+ {"b":2}
24
+ The output is:
25
+
26
+ {"a"=>2}
27
+ {"b"=>2}
28
+ bin/test:8:in `load': undefined method `new' for #<EOFError: end of file reached> (NoMethodError)
29
+ from bin/test:8:in `block (2 levels) in <main>'
30
+ from bin/test:7:in `wrap'
31
+ from bin/test:7:in `block in <main>'
32
+ from bin/test:6:in `open'
33
+ from bin/test:6:in `<main>'
34
+ =end
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.11.5
4
+ version: 2.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Ohler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-25 00:00:00.000000000 Z
11
+ date: 2015-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -106,8 +106,10 @@ files:
106
106
  - test/_test_active_mimic.rb
107
107
  - test/_test_mimic_rails.rb
108
108
  - test/bug.rb
109
+ - test/bug2.rb
109
110
  - test/files.rb
110
111
  - test/helper.rb
112
+ - test/io.rb
111
113
  - test/isolated/shared.rb
112
114
  - test/isolated/test_mimic_after.rb
113
115
  - test/isolated/test_mimic_alone.rb
@@ -115,13 +117,11 @@ files:
115
117
  - test/isolated/test_mimic_define.rb
116
118
  - test/isolated/test_mimic_rails_after.rb
117
119
  - test/isolated/test_mimic_rails_before.rb
120
+ - test/mod.rb
118
121
  - test/perf.rb
119
- - test/perf1.rb
120
- - test/perf2.rb
121
122
  - test/perf_compat.rb
122
123
  - test/perf_fast.rb
123
124
  - test/perf_file.rb
124
- - test/perf_obj_old.rb
125
125
  - test/perf_object.rb
126
126
  - test/perf_saj.rb
127
127
  - test/perf_scp.rb
@@ -141,19 +141,21 @@ files:
141
141
  - test/sample/shape.rb
142
142
  - test/sample/text.rb
143
143
  - test/sample_json.rb
144
- - test/test_bigd.rb
144
+ - test/struct.rb
145
145
  - test/test_compat.rb
146
146
  - test/test_debian.rb
147
147
  - test/test_fast.rb
148
148
  - test/test_file.rb
149
149
  - test/test_gc.rb
150
150
  - test/test_object.rb
151
- - test/test_range.rb
152
151
  - test/test_saj.rb
153
152
  - test/test_scp.rb
153
+ - test/test_serializer.rb
154
154
  - test/test_strict.rb
155
155
  - test/test_various.rb
156
156
  - test/test_writer.rb
157
+ - test/write_timebars.rb
158
+ - test/zip.rb
157
159
  homepage: http://www.ohler.com/oj
158
160
  licenses:
159
161
  - MIT
@@ -182,3 +184,4 @@ signing_key:
182
184
  specification_version: 4
183
185
  summary: A fast JSON parser and serializer.
184
186
  test_files: []
187
+ has_rdoc: true
@@ -1,64 +0,0 @@
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
@@ -1,76 +0,0 @@
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
@@ -1,213 +0,0 @@
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