oj 0.6.0 → 0.7.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,14 @@
1
+
2
+ module Sample
3
+ class Change
4
+ attr_accessor :time
5
+ attr_accessor :user
6
+ attr_accessor :comment
7
+
8
+ def initialize(comment=nil, time=nil, user=nil)
9
+ @user = user || ENV['USER']
10
+ @time = time || Time.now
11
+ @comment = comment
12
+ end
13
+ end # Change
14
+ end # Sample
@@ -0,0 +1,19 @@
1
+
2
+ require 'etc'
3
+
4
+ module Sample
5
+
6
+ class Dir < File
7
+ attr_accessor :files
8
+
9
+ def initialize(filename)
10
+ super
11
+ @files = []
12
+ end
13
+
14
+ def <<(f)
15
+ @files << f
16
+ end
17
+
18
+ end # Dir
19
+ end # Sample
@@ -0,0 +1,36 @@
1
+
2
+ require 'sample/hasprops'
3
+ require 'sample/group'
4
+ require 'sample/layer'
5
+ require 'sample/line'
6
+ require 'sample/shape'
7
+ require 'sample/oval'
8
+ require 'sample/rect'
9
+ require 'sample/text'
10
+ require 'sample/change'
11
+
12
+ module Sample
13
+ class Doc
14
+ include HasProps
15
+
16
+ attr_accessor :title
17
+ attr_accessor :create_time
18
+ attr_accessor :user
19
+ # Hash of layers in the document indexed by layer name.
20
+ attr_reader :layers
21
+ attr_reader :change_history
22
+
23
+ def initialize(title)
24
+ @title = title
25
+ @user = ENV['USER']
26
+ @create_time = Time.now
27
+ @layers = { }
28
+ @change_history = []
29
+ end
30
+
31
+ def add_change(comment, time=nil, user=nil)
32
+ @change_history << Change.new(comment, time, user)
33
+ end
34
+
35
+ end # Doc
36
+ end # Sample
@@ -0,0 +1,48 @@
1
+
2
+ require 'etc'
3
+
4
+ module Sample
5
+
6
+ class File
7
+ attr_accessor :name, :ctime, :mtime, :size, :owner, :group, :permissions
8
+
9
+ def initialize(filename)
10
+ @name = ::File.basename(filename)
11
+ stat = ::File.stat(filename)
12
+ @ctime = stat.ctime
13
+ @mtime = stat.mtime
14
+ @size = stat.size
15
+ @owner = Etc.getpwuid(stat.uid).name
16
+ @group = Etc.getgrgid(stat.gid).name
17
+ if false
18
+ @permissions = {
19
+ 'user' => {
20
+ 'read' => (0 != (stat.mode & 0x0100)),
21
+ 'write' => (0 != (stat.mode & 0x0080)),
22
+ 'execute' => (0 != (stat.mode & 0x0040))},
23
+ 'group' => {
24
+ 'read' => (0 != (stat.mode & 0x0020)),
25
+ 'write' => (0 != (stat.mode & 0x0010)),
26
+ 'execute' => (0 != (stat.mode & 0x0008))},
27
+ 'other' => {
28
+ 'read' => (0 != (stat.mode & 0x0004)),
29
+ 'write' => (0 != (stat.mode & 0x0002)),
30
+ 'execute' => (0 != (stat.mode & 0x0001))}
31
+ }
32
+ else
33
+ @permissions = {
34
+ 'user' => [(0 != (stat.mode & 0x0100)) ? 'r' : '-',
35
+ (0 != (stat.mode & 0x0080)) ? 'w' : '-',
36
+ (0 != (stat.mode & 0x0040)) ? 'x' : '-'].join(''),
37
+ 'group' => [(0 != (stat.mode & 0x0020)) ? 'r' : '-',
38
+ (0 != (stat.mode & 0x0010)) ? 'w' : '-',
39
+ (0 != (stat.mode & 0x0008)) ? 'x' : '-'].join(''),
40
+ 'other' => [(0 != (stat.mode & 0x0004)) ? 'r' : '-',
41
+ (0 != (stat.mode & 0x0002)) ? 'w' : '-',
42
+ (0 != (stat.mode & 0x0001)) ? 'x' : '-'].join('')
43
+ }
44
+ end
45
+ end
46
+
47
+ end # File
48
+ end # Sample
@@ -0,0 +1,16 @@
1
+
2
+ module Sample
3
+ class Group
4
+ attr_reader :members
5
+
6
+ def initialize()
7
+ @members = []
8
+ end
9
+
10
+ def <<(member)
11
+ @members << member
12
+ end
13
+
14
+ end # Group
15
+ end # Sample
16
+
@@ -0,0 +1,16 @@
1
+
2
+ module Sample
3
+ module HasProps
4
+
5
+ def add_prop(key, value)
6
+ @props = { } unless self.instance_variable_defined?(:@props)
7
+ @props[key] = value
8
+ end
9
+
10
+ def props
11
+ @props = { } unless self.instance_variable_defined?(:@props)
12
+ @props
13
+ end
14
+
15
+ end # HasProps
16
+ end # Sample
@@ -0,0 +1,12 @@
1
+
2
+ module Sample
3
+ class Layer < Group
4
+ attr_accessor :name
5
+
6
+ def initialize(name)
7
+ super()
8
+ @name = name
9
+ end
10
+
11
+ end # Layer
12
+ end # Sample
@@ -0,0 +1,20 @@
1
+ module Sample
2
+
3
+ class Line
4
+ include HasProps
5
+
6
+ attr_accessor :x, :y, :dx, :dy
7
+ attr_accessor :color
8
+ attr_accessor :thick
9
+
10
+ def initialize(x, y, dx, dy, thick, color)
11
+ @x = x
12
+ @y = y
13
+ @dx = dx
14
+ @dy = dy
15
+ @thick = thick
16
+ @color = color
17
+ end
18
+
19
+ end # Line
20
+ end # Sample
@@ -0,0 +1,10 @@
1
+ module Sample
2
+
3
+ class Oval < Shape
4
+
5
+ def initialize(left, top, wide, high, color=nil)
6
+ super
7
+ end
8
+
9
+ end # Oval
10
+ end # Sample
@@ -0,0 +1,10 @@
1
+
2
+ module Sample
3
+ class Rect < Shape
4
+
5
+ def initialize(left, top, wide, high, color=nil)
6
+ super
7
+ end
8
+
9
+ end # Rect
10
+ end # Sample
@@ -0,0 +1,35 @@
1
+
2
+ module Sample
3
+ class Shape
4
+ include HasProps
5
+
6
+ attr_accessor :bounds
7
+ attr_accessor :color
8
+ attr_accessor :border, :border_color
9
+
10
+ def initialize(left, top, wide, high, color=nil)
11
+ @bounds = [[left, top], [left + wide, top + high]]
12
+ @color = color
13
+ @border = 1
14
+ @border_color = :black
15
+ end
16
+
17
+ def left
18
+ @bounds[0][0]
19
+ end
20
+
21
+ def top
22
+ @bounds[0][1]
23
+ end
24
+
25
+ def width
26
+ @bounds[1][0] - @bounds[0][0]
27
+ end
28
+
29
+ def height
30
+ @bounds[1][1] - @bounds[0][1]
31
+ end
32
+
33
+ end # Shape
34
+ end # Sample
35
+
@@ -0,0 +1,20 @@
1
+
2
+ module Sample
3
+ class Text < Shape
4
+ attr_accessor :text
5
+ attr_accessor :font
6
+ attr_accessor :font_size
7
+ attr_accessor :just
8
+ attr_accessor :text_color
9
+
10
+ def initialize(text, left, top, wide, high, color=nil)
11
+ super(left, top, wide, high, color)
12
+ @text = text
13
+ @font = 'helvetica'
14
+ @font_size = 14
15
+ @just = 'left'
16
+ @text_color = 'black'
17
+ end
18
+
19
+ end # Text
20
+ end # Sample
@@ -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
@@ -0,0 +1,359 @@
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 'oj'
9
+
10
+ class Jam
11
+ attr_reader :x, :y
12
+
13
+ def initialize(x, y)
14
+ @x = x
15
+ @y = y
16
+ end
17
+
18
+ def eql?(o)
19
+ self.class == o.class && @x == o.x && @y == o.y
20
+ end
21
+ alias == eql?
22
+
23
+ end # Jam
24
+
25
+ class Jeez < Jam
26
+ def initialize(x, y)
27
+ super
28
+ end
29
+
30
+ def to_json()
31
+ %{{"x":#{@x},"y":#{@y}}}
32
+ end
33
+
34
+ end # Jeez
35
+
36
+ class Jazz < Jam
37
+ def initialize(x, y)
38
+ super
39
+ end
40
+ def to_hash()
41
+ { 'x' => @x, 'y' => @y }
42
+ end
43
+ end # Jazz
44
+
45
+ class Juice < ::Test::Unit::TestCase
46
+
47
+ def test_get_options
48
+ opts = Oj.default_options()
49
+ assert_equal(opts, {
50
+ :encoding=>nil,
51
+ :indent=>0,
52
+ :circular=>false,
53
+ :mode=>:object})
54
+ end
55
+
56
+ def test_set_options
57
+ orig = {
58
+ :encoding=>nil,
59
+ :indent=>0,
60
+ :circular=>false,
61
+ :mode=>:object}
62
+ o2 = {
63
+ :encoding=>"UTF-8",
64
+ :indent=>4,
65
+ :circular=>true,
66
+ :mode=>:compat}
67
+ o3 = { :indent => 4 }
68
+ Oj.default_options = o2
69
+ opts = Oj.default_options()
70
+ assert_equal(opts, o2);
71
+ Oj.default_options = o3 # see if it throws an exception
72
+ Oj.default_options = orig # return to original
73
+ end
74
+
75
+ def test_nil
76
+ dump_and_load(nil, false)
77
+ end
78
+
79
+ def test_true
80
+ dump_and_load(true, false)
81
+ end
82
+
83
+ def test_false
84
+ dump_and_load(false, false)
85
+ end
86
+
87
+ def test_fixnum
88
+ dump_and_load(0, false)
89
+ dump_and_load(12345, false)
90
+ dump_and_load(-54321, false)
91
+ dump_and_load(1, false)
92
+ end
93
+
94
+ def test_bignum
95
+ dump_and_load(12345678901234567890123456789, false)
96
+ end
97
+
98
+ def test_float
99
+ dump_and_load(0.0, false)
100
+ dump_and_load(12345.6789, false)
101
+ dump_and_load(-54321.012, false)
102
+ dump_and_load(2.48e16, false)
103
+ end
104
+
105
+ def test_string
106
+ dump_and_load('', false)
107
+ dump_and_load('abc', false)
108
+ dump_and_load("abc\ndef", false)
109
+ dump_and_load("a\u0041", false)
110
+ end
111
+
112
+ def test_encode
113
+ Oj.default_options = { :encoding => 'UTF-8' }
114
+ dump_and_load("ぴーたー", false)
115
+ Oj.default_options = { :encoding => nil }
116
+ end
117
+
118
+ def test_array
119
+ dump_and_load([], false)
120
+ dump_and_load([true, false], false)
121
+ dump_and_load(['a', 1, nil], false)
122
+ dump_and_load([[nil]], false)
123
+ dump_and_load([[nil], 58], false)
124
+ end
125
+
126
+ # Symbol
127
+ def test_symbol_strict
128
+ begin
129
+ json = Oj.dump(:abc, :mode => :strict)
130
+ rescue Exception => e
131
+ assert(true)
132
+ end
133
+ end
134
+ def test_symbol_null
135
+ json = Oj.dump(:abc, :mode => :null)
136
+ assert_equal('null', json)
137
+ end
138
+ def test_symbol_compat
139
+ json = Oj.dump(:abc, :mode => :compat)
140
+ assert_equal('"abc"', json)
141
+ end
142
+ def test_symbol_object
143
+ Oj.default_options = { :mode => :object }
144
+ dump_and_load(''.to_sym, false)
145
+ dump_and_load(:abc, false)
146
+ dump_and_load(':xyz'.to_sym, false)
147
+ end
148
+
149
+ # Time
150
+ def test_time_strict
151
+ t = Time.new(2012, 1, 5, 23, 58, 7)
152
+ begin
153
+ json = Oj.dump(t, :mode => :strict)
154
+ rescue Exception => e
155
+ assert(true)
156
+ end
157
+ end
158
+ def test_time_null
159
+ t = Time.new(2012, 1, 5, 23, 58, 7)
160
+ json = Oj.dump(t, :mode => :null)
161
+ assert_equal('null', json)
162
+ end
163
+ def test_time_compat
164
+ t = Time.new(2012, 1, 5, 23, 58, 7)
165
+ json = Oj.dump(t, :mode => :compat)
166
+ assert_equal(%{1325775487.000000}, json)
167
+ end
168
+ def test_time_object
169
+ t = Time.now()
170
+ Oj.default_options = { :mode => :object }
171
+ dump_and_load(t, false)
172
+ end
173
+
174
+ # Class
175
+ def test_class_strict
176
+ begin
177
+ json = Oj.dump(Juice, :mode => :strict)
178
+ rescue Exception => e
179
+ assert(true)
180
+ end
181
+ end
182
+ def test_class_null
183
+ json = Oj.dump(Juice, :mode => :null)
184
+ assert_equal('null', json)
185
+ end
186
+ def test_class_compat
187
+ json = Oj.dump(Juice, :mode => :compat)
188
+ assert_equal(%{"Juice"}, json)
189
+ end
190
+ def test_class_object
191
+ Oj.default_options = { :mode => :object }
192
+ dump_and_load(Juice, false)
193
+ end
194
+
195
+ # Hash
196
+ def test_hash
197
+ Oj.default_options = { :mode => :strict }
198
+ dump_and_load({}, false)
199
+ dump_and_load({ 'true' => true, 'false' => false}, false)
200
+ dump_and_load({ 'true' => true, 'array' => [], 'hash' => { }}, false)
201
+ end
202
+ def test_non_str_hash_strict
203
+ begin
204
+ json = Oj.dump({ 1 => true, 0 => false }, :mode => :strict)
205
+ rescue Exception => e
206
+ assert(true)
207
+ end
208
+ end
209
+ def test_non_str_hash_null
210
+ begin
211
+ json = Oj.dump({ 1 => true, 0 => false }, :mode => :null)
212
+ rescue Exception => e
213
+ assert(true)
214
+ end
215
+ end
216
+ def test_non_str_hash_compat
217
+ begin
218
+ json = Oj.dump({ 1 => true, 0 => false }, :mode => :compat)
219
+ rescue Exception => e
220
+ assert(true)
221
+ end
222
+ end
223
+ def test_non_str_hash_object
224
+ Oj.default_options = { :mode => :object }
225
+ json = Oj.dump({ 1 => true, 0 => false, :sim => nil })
226
+ h = Oj.load(json, :mode => :strict)
227
+ assert_equal({"^#1" => [1, true], "^#2" => [0, false], ":sim" => nil}, h)
228
+ h = Oj.load(json)
229
+ assert_equal({ 1 => true, 0 => false, :sim => nil }, h)
230
+ end
231
+ def test_mixed_hash_object
232
+ Oj.default_options = { :mode => :object }
233
+ json = Oj.dump({ 1 => true, 0 => false, 'nil' => nil, :sim => 4 })
234
+ h = Oj.load(json, :mode => :strict)
235
+ assert_equal({"^#1" => [1, true], "^#2" => [0, false], "nil" => nil, ":sim" => 4}, h)
236
+ h = Oj.load(json)
237
+ assert_equal({ 1 => true, 0 => false, 'nil' => nil, :sim => 4 }, h)
238
+ end
239
+
240
+ # Object with to_json()
241
+ def test_json_object_strict
242
+ obj = Jeez.new(true, 58)
243
+ begin
244
+ json = Oj.dump(obj, :mode => :strict)
245
+ rescue Exception => e
246
+ assert(true)
247
+ end
248
+ end
249
+ def test_json_object_null
250
+ obj = Jeez.new(true, 58)
251
+ json = Oj.dump(obj, :mode => :null)
252
+ assert_equal('null', json)
253
+ end
254
+ def test_json_object_compat
255
+ obj = Jeez.new(true, 58)
256
+ json = Oj.dump(obj, :mode => :compat, :indent => 2)
257
+ assert_equal(%{{"x":true,"y":58}}, json)
258
+ end
259
+ def test_json_object_object
260
+ obj = Jeez.new(true, 58)
261
+ json = Oj.dump(obj, :mode => :object, :indent => 2)
262
+ assert_equal(%{{
263
+ "^o":"Jeez",
264
+ "x":true,
265
+ "y":58}}, json)
266
+ obj2 = Oj.load(json, :mode => :object)
267
+ assert_equal(obj, obj2)
268
+ end
269
+
270
+ # Object with to_hash()
271
+ def test_to_hash_object_strict
272
+ obj = Jazz.new(true, 58)
273
+ begin
274
+ json = Oj.dump(obj, :mode => :strict)
275
+ rescue Exception => e
276
+ assert(true)
277
+ end
278
+ end
279
+ def test_to_hash_object_null
280
+ obj = Jazz.new(true, 58)
281
+ json = Oj.dump(obj, :mode => :null)
282
+ assert_equal('null', json)
283
+ end
284
+ def test_to_hash_object_compat
285
+ obj = Jazz.new(true, 58)
286
+ json = Oj.dump(obj, :mode => :compat, :indent => 2)
287
+ assert_equal(%{{
288
+ "x":true,
289
+ "y":58}}, json)
290
+ end
291
+ def test_to_hash_object_object
292
+ obj = Jazz.new(true, 58)
293
+ json = Oj.dump(obj, :mode => :object, :indent => 2)
294
+ assert_equal(%{{
295
+ "^o":"Jazz",
296
+ "x":true,
297
+ "y":58}}, json)
298
+ obj2 = Oj.load(json, :mode => :object)
299
+ assert_equal(obj, obj2)
300
+ end
301
+
302
+ # Object without to_json() or to_hash()
303
+ def test_object_strict
304
+ obj = Jam.new(true, 58)
305
+ begin
306
+ json = Oj.dump(obj, :mode => :strict)
307
+ rescue Exception => e
308
+ assert(true)
309
+ end
310
+ end
311
+ def test_object_null
312
+ obj = Jam.new(true, 58)
313
+ json = Oj.dump(obj, :mode => :null)
314
+ assert_equal('null', json)
315
+ end
316
+ def test_object_compat
317
+ obj = Jam.new(true, 58)
318
+ json = Oj.dump(obj, :mode => :compat, :indent => 2)
319
+ assert_equal(%{{
320
+ "x":true,
321
+ "y":58}}, json)
322
+ end
323
+ def test_object_object
324
+ obj = Jam.new(true, 58)
325
+ json = Oj.dump(obj, :mode => :object, :indent => 2)
326
+ assert_equal(%{{
327
+ "^o":"Jam",
328
+ "x":true,
329
+ "y":58}}, json)
330
+ obj2 = Oj.load(json, :mode => :object)
331
+ assert_equal(obj, obj2)
332
+ end
333
+
334
+ def test_exception
335
+ err = nil
336
+ begin
337
+ raise StandardError.new('A Message')
338
+ rescue Exception => e
339
+ err = e
340
+ end
341
+ json = Oj.dump(err, :mode => :object, :indent => 2)
342
+ #puts "*** #{json}"
343
+ e2 = Oj.load(json, :mode => :strict)
344
+ assert_equal(err.class.to_s, e2['^o'])
345
+ assert_equal(err.message, e2['~mesg'])
346
+ assert_equal(err.backtrace, e2['~bt'])
347
+ e2 = Oj.load(json, :mode => :object)
348
+ assert_equal(e, e2);
349
+ end
350
+
351
+ def dump_and_load(obj, trace=false)
352
+ json = Oj.dump(obj, :indent => 2)
353
+ puts json if trace
354
+ loaded = Oj.load(json);
355
+ assert_equal(obj, loaded)
356
+ loaded
357
+ end
358
+
359
+ end