oj 0.5.2 → 0.6.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,37 @@
1
+ #!/usr/bin/env ruby -wW2
2
+
3
+ if $0 == __FILE__
4
+ $: << '.'
5
+ $: << '..'
6
+ $: << '../lib'
7
+ $: << '../ext'
8
+ end
9
+
10
+ require 'pp'
11
+ require 'oj'
12
+
13
+ def sample_json(size=3)
14
+ colors = [ :black, :gray, :white, :red, :blue, :yellow, :green, :purple, :orange ]
15
+ container = []
16
+ size.times do |i|
17
+ box = {
18
+ 'color' => colors[i % colors.size],
19
+ 'fragile' => (0 == (i % 2)),
20
+ 'width' => i,
21
+ 'height' => i,
22
+ 'depth' => i,
23
+ 'weight' => i * 1.3,
24
+ 'address' => {
25
+ 'street' => "#{i} Main Street",
26
+ 'city' => 'Sity',
27
+ 'state' => nil
28
+ }
29
+ }
30
+ container << box
31
+ end
32
+ container
33
+ end
34
+
35
+ if $0 == __FILE__
36
+ File.open('sample.json', "w") { |f| f.write(Oj.dump(sample_json(3), :indent => 2)) }
37
+ end
@@ -7,6 +7,29 @@ $: << File.join(File.dirname(__FILE__), "../ext")
7
7
  require 'test/unit'
8
8
  require 'oj'
9
9
 
10
+ class Jeez
11
+ def initialize(x, y)
12
+ @x = x
13
+ @y = y
14
+ end
15
+
16
+ def to_json()
17
+ %{{"x":#{@x},"y":#{@y}}}
18
+ end
19
+
20
+ end # Jeez
21
+
22
+ class Jazz
23
+ def initialize(x, y)
24
+ @x = x
25
+ @y = y
26
+ end
27
+ def to_hash()
28
+ { 'x' => @x, 'y' => @y }
29
+ end
30
+
31
+ end # Jazz
32
+
10
33
  class Juice < ::Test::Unit::TestCase
11
34
 
12
35
  def test_get_options
@@ -15,8 +38,7 @@ class Juice < ::Test::Unit::TestCase
15
38
  :encoding=>nil,
16
39
  :indent=>0,
17
40
  :circular=>false,
18
- :mode=>nil,
19
- :effort=>:tolerant})
41
+ :mode=>:object})
20
42
  end
21
43
 
22
44
  def test_set_options
@@ -24,14 +46,12 @@ class Juice < ::Test::Unit::TestCase
24
46
  :encoding=>nil,
25
47
  :indent=>0,
26
48
  :circular=>false,
27
- :mode=>nil,
28
- :effort=>:tolerant}
49
+ :mode=>:object}
29
50
  o2 = {
30
51
  :encoding=>"UTF-8",
31
52
  :indent=>4,
32
53
  :circular=>true,
33
- :mode=>:object,
34
- :effort=>:strict }
54
+ :mode=>:compat}
35
55
  o3 = { :indent => 4 }
36
56
  Oj.default_options = o2
37
57
  opts = Oj.default_options()
@@ -59,10 +79,14 @@ class Juice < ::Test::Unit::TestCase
59
79
  dump_and_load(1, false)
60
80
  end
61
81
 
82
+ def test_bignum
83
+ dump_and_load(12345678901234567890123456789, true)
84
+ end
85
+
62
86
  def test_float
63
87
  dump_and_load(0.0, false)
64
88
  dump_and_load(12345.6789, false)
65
- dump_and_load(-54321.012, true)
89
+ dump_and_load(-54321.012, false)
66
90
  dump_and_load(2.48e16, false)
67
91
  end
68
92
 
@@ -73,6 +97,18 @@ class Juice < ::Test::Unit::TestCase
73
97
  dump_and_load("a\u0041", false)
74
98
  end
75
99
 
100
+ def test_class
101
+ begin
102
+ json = Oj.dump(self.class, :mode => :strict)
103
+ rescue Exception => e
104
+ assert(true)
105
+ end
106
+ json = Oj.dump(self.class, :mode => :object)
107
+ assert_equal('{"*":"Class","-":"Juice"}', json)
108
+ json = Oj.dump(self.class, :mode => :null)
109
+ assert_equal('null', json)
110
+ end
111
+
76
112
  def test_array
77
113
  dump_and_load([], false)
78
114
  dump_and_load([true, false], false)
@@ -93,10 +129,78 @@ class Juice < ::Test::Unit::TestCase
93
129
  Oj.default_options = { :encoding => nil }
94
130
  end
95
131
 
132
+ def test_time
133
+ t = Time.new(2012, 1, 5, 23, 58, 7)
134
+ begin
135
+ json = Oj.dump(t, :mode => :strict)
136
+ rescue Exception => e
137
+ assert(true)
138
+ end
139
+ json = Oj.dump(t, :mode => :compat)
140
+ assert_equal(%{{"*":"Time","-":1325775487.000000}}, json)
141
+ json = Oj.dump(t, :mode => :null)
142
+ assert_equal('null', json)
143
+ end
144
+
145
+ def test_json_object
146
+ begin
147
+ json = Oj.dump(Jeez.new(true, 58), :mode => :strict)
148
+ rescue Exception => e
149
+ assert(true)
150
+ end
151
+ json = Oj.dump(Jeez.new(true, 58), :mode => :compat, :indent => 2)
152
+ assert_equal(%{{"x":true,"y":58}}, json)
153
+ json = Oj.dump(Jeez.new(true, 58), :mode => :null)
154
+ assert_equal('null', json)
155
+ end
156
+
157
+ def test_hash_object
158
+ begin
159
+ json = Oj.dump(Jazz.new(true, 58), :mode => :strict)
160
+ rescue Exception => e
161
+ assert(true)
162
+ end
163
+ json = Oj.dump(Jazz.new(true, 58), :mode => :compat, :indent => 2)
164
+ assert_equal(%{{
165
+ "x":true,
166
+ "y":58}}, json)
167
+ json = Oj.dump(Jazz.new(true, 58), :mode => :null)
168
+ assert_equal('null', json)
169
+ end
170
+
171
+ def test_object
172
+ begin
173
+ json = Oj.dump(Jazz.new(true, 58), :mode => :strict)
174
+ rescue Exception => e
175
+ assert(true)
176
+ end
177
+ json = Oj.dump(Jazz.new(true, 58), :mode => :object, :indent => 2)
178
+ assert_equal(%{{
179
+ "*":"Jazz",
180
+ "x":true,
181
+ "y":58}}, json)
182
+ json = Oj.dump(Jazz.new(true, 58), :mode => :null)
183
+ assert_equal('null', json)
184
+ end
185
+
186
+ def test_exception
187
+ err = nil
188
+ begin
189
+ raise StandardError.new('A Message')
190
+ rescue Exception => e
191
+ err = e
192
+ end
193
+ json = Oj.dump(err, :mode => :object, :indent => 2)
194
+ e2 = Oj.load(json, :mode => :strict)
195
+ assert_equal(err.class.to_s, e2['*'])
196
+ assert_equal(err.message, e2['mesg'])
197
+ assert_equal(err.backtrace, e2['bt'])
198
+ end
199
+
96
200
  def dump_and_load(obj, trace=false)
97
201
  json = Oj.dump(obj, :indent => 2)
98
202
  puts json if trace
99
- loaded = Oj.load(json, :mode => :simple);
203
+ loaded = Oj.load(json);
100
204
  assert_equal(obj, loaded)
101
205
  loaded
102
206
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oj
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-19 00:00:00.000000000 Z
12
+ date: 2012-02-22 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! 'The fastest JSON parser and object serializer. '
15
15
  email: peter@ohler.com
@@ -29,6 +29,7 @@ files:
29
29
  - test/boo.rb
30
30
  - test/foo.rb
31
31
  - test/perf_simple.rb
32
+ - test/sample.rb
32
33
  - test/simple.rb
33
34
  - LICENSE
34
35
  - README.md