oj 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +17 -23
  3. data/README.md +74 -425
  4. data/ext/oj/buf.h +103 -0
  5. data/ext/oj/cache8.c +4 -0
  6. data/ext/oj/circarray.c +68 -0
  7. data/ext/oj/circarray.h +23 -0
  8. data/ext/oj/code.c +227 -0
  9. data/ext/oj/code.h +40 -0
  10. data/ext/oj/compat.c +243 -0
  11. data/ext/oj/custom.c +1097 -0
  12. data/ext/oj/dump.c +766 -1534
  13. data/ext/oj/dump.h +92 -0
  14. data/ext/oj/dump_compat.c +937 -0
  15. data/ext/oj/dump_leaf.c +254 -0
  16. data/ext/oj/dump_object.c +810 -0
  17. data/ext/oj/dump_rails.c +329 -0
  18. data/ext/oj/dump_strict.c +416 -0
  19. data/ext/oj/encode.h +51 -0
  20. data/ext/oj/err.c +57 -0
  21. data/ext/oj/err.h +70 -0
  22. data/ext/oj/extconf.rb +17 -7
  23. data/ext/oj/fast.c +213 -180
  24. data/ext/oj/hash.c +163 -0
  25. data/ext/oj/hash.h +46 -0
  26. data/ext/oj/hash_test.c +512 -0
  27. data/ext/oj/mimic_json.c +817 -0
  28. data/ext/oj/mimic_rails.c +806 -0
  29. data/ext/oj/mimic_rails.h +17 -0
  30. data/ext/oj/object.c +752 -0
  31. data/ext/oj/odd.c +230 -0
  32. data/ext/oj/odd.h +44 -0
  33. data/ext/oj/oj.c +1288 -929
  34. data/ext/oj/oj.h +240 -69
  35. data/ext/oj/parse.c +1014 -0
  36. data/ext/oj/parse.h +92 -0
  37. data/ext/oj/reader.c +223 -0
  38. data/ext/oj/reader.h +151 -0
  39. data/ext/oj/resolve.c +127 -0
  40. data/ext/oj/{cache.h → resolve.h} +6 -13
  41. data/ext/oj/rxclass.c +133 -0
  42. data/ext/oj/rxclass.h +27 -0
  43. data/ext/oj/saj.c +77 -175
  44. data/ext/oj/scp.c +224 -0
  45. data/ext/oj/sparse.c +911 -0
  46. data/ext/oj/stream_writer.c +301 -0
  47. data/ext/oj/strict.c +162 -0
  48. data/ext/oj/string_writer.c +480 -0
  49. data/ext/oj/val_stack.c +98 -0
  50. data/ext/oj/val_stack.h +188 -0
  51. data/lib/oj/active_support_helper.rb +41 -0
  52. data/lib/oj/bag.rb +6 -10
  53. data/lib/oj/easy_hash.rb +52 -0
  54. data/lib/oj/json.rb +172 -0
  55. data/lib/oj/mimic.rb +260 -5
  56. data/lib/oj/saj.rb +13 -10
  57. data/lib/oj/schandler.rb +142 -0
  58. data/lib/oj/state.rb +131 -0
  59. data/lib/oj/version.rb +1 -1
  60. data/lib/oj.rb +11 -23
  61. data/pages/Advanced.md +22 -0
  62. data/pages/Compatibility.md +25 -0
  63. data/pages/Custom.md +23 -0
  64. data/pages/Encoding.md +65 -0
  65. data/pages/JsonGem.md +79 -0
  66. data/pages/Modes.md +140 -0
  67. data/pages/Options.md +250 -0
  68. data/pages/Rails.md +60 -0
  69. data/pages/Security.md +20 -0
  70. data/test/_test_active.rb +76 -0
  71. data/test/_test_active_mimic.rb +96 -0
  72. data/test/_test_mimic_rails.rb +126 -0
  73. data/test/activesupport4/decoding_test.rb +105 -0
  74. data/test/activesupport4/encoding_test.rb +531 -0
  75. data/test/activesupport4/test_helper.rb +41 -0
  76. data/test/activesupport5/decoding_test.rb +125 -0
  77. data/test/activesupport5/encoding_test.rb +483 -0
  78. data/test/activesupport5/encoding_test_cases.rb +90 -0
  79. data/test/activesupport5/test_helper.rb +50 -0
  80. data/test/activesupport5/time_zone_test_helpers.rb +24 -0
  81. data/test/helper.rb +27 -0
  82. data/test/isolated/shared.rb +310 -0
  83. data/test/isolated/test_mimic_after.rb +13 -0
  84. data/test/isolated/test_mimic_alone.rb +12 -0
  85. data/test/isolated/test_mimic_as_json.rb +45 -0
  86. data/test/isolated/test_mimic_before.rb +13 -0
  87. data/test/isolated/test_mimic_define.rb +28 -0
  88. data/test/isolated/test_mimic_rails_after.rb +22 -0
  89. data/test/isolated/test_mimic_rails_before.rb +21 -0
  90. data/test/isolated/test_mimic_redefine.rb +15 -0
  91. data/test/json_gem/json_addition_test.rb +216 -0
  92. data/test/json_gem/json_common_interface_test.rb +143 -0
  93. data/test/json_gem/json_encoding_test.rb +109 -0
  94. data/test/json_gem/json_ext_parser_test.rb +20 -0
  95. data/test/json_gem/json_fixtures_test.rb +35 -0
  96. data/test/json_gem/json_generator_test.rb +383 -0
  97. data/test/json_gem/json_generic_object_test.rb +90 -0
  98. data/test/json_gem/json_parser_test.rb +470 -0
  99. data/test/json_gem/json_string_matching_test.rb +42 -0
  100. data/test/json_gem/test_helper.rb +18 -0
  101. data/test/perf_compat.rb +130 -0
  102. data/test/perf_fast.rb +9 -9
  103. data/test/perf_file.rb +64 -0
  104. data/test/{perf_obj.rb → perf_object.rb} +24 -10
  105. data/test/perf_scp.rb +151 -0
  106. data/test/perf_strict.rb +32 -113
  107. data/test/sample.rb +2 -3
  108. data/test/test_compat.rb +474 -0
  109. data/test/test_custom.rb +355 -0
  110. data/test/test_debian.rb +53 -0
  111. data/test/test_fast.rb +66 -16
  112. data/test/test_file.rb +237 -0
  113. data/test/test_gc.rb +49 -0
  114. data/test/test_hash.rb +29 -0
  115. data/test/test_null.rb +376 -0
  116. data/test/test_object.rb +1010 -0
  117. data/test/test_saj.rb +16 -16
  118. data/test/test_scp.rb +417 -0
  119. data/test/test_strict.rb +410 -0
  120. data/test/test_various.rb +815 -0
  121. data/test/test_writer.rb +308 -0
  122. data/test/tests.rb +9 -902
  123. data/test/tests_mimic.rb +14 -0
  124. data/test/tests_mimic_addition.rb +7 -0
  125. metadata +253 -38
  126. data/ext/oj/cache.c +0 -148
  127. data/ext/oj/foo.rb +0 -6
  128. data/ext/oj/load.c +0 -1049
  129. data/test/a.rb +0 -38
  130. data/test/perf1.rb +0 -64
  131. data/test/perf2.rb +0 -76
  132. data/test/perf_obj_old.rb +0 -213
  133. data/test/test_mimic.rb +0 -208
data/test/tests.rb CHANGED
@@ -1,907 +1,14 @@
1
1
  #!/usr/bin/env ruby
2
2
  # encoding: UTF-8
3
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 'bigdecimal'
17
- require 'oj'
18
-
19
- $ruby = RUBY_DESCRIPTION.split(' ')[0]
20
- $ruby = 'ree' if 'ruby' == $ruby && RUBY_DESCRIPTION.include?('Ruby Enterprise Edition')
21
-
22
- def hash_eql(h1, h2)
23
- return false if h1.size != h2.size
24
- h1.keys.each do |k|
25
- return false unless h1[k] == h2[k]
26
- end
27
- true
4
+ $: << File.dirname(__FILE__)
5
+ $oj_dir = File.dirname(File.expand_path(File.dirname(__FILE__)))
6
+ %w(lib ext).each do |dir|
7
+ $: << File.join($oj_dir, dir)
28
8
  end
29
9
 
30
- class Jam
31
- attr_accessor :x, :y
32
-
33
- def initialize(x, y)
34
- @x = x
35
- @y = y
36
- end
37
-
38
- def eql?(o)
39
- self.class == o.class && @x == o.x && @y == o.y
40
- end
41
- alias == eql?
42
-
43
- end # Jam
44
-
45
- class Jeez < Jam
46
- def initialize(x, y)
47
- super
48
- end
49
-
50
- def to_json()
51
- %{{"json_class":"#{self.class}","x":#{@x},"y":#{@y}}}
52
- end
53
-
54
- def self.json_create(h)
55
- self.new(h['x'], h['y'])
56
- end
57
- end # Jeez
58
-
59
- # contributed by sauliusg to fix as_json
60
- class Orange < Jam
61
- def initialize(x, y)
62
- super
63
- end
64
-
65
- def as_json()
66
- { :json_class => self.class,
67
- :x => @x,
68
- :y => @y }
69
- end
70
-
71
- def self.json_create(h)
72
- self.new(h['x'], h['y'])
73
- end
74
- end
75
-
76
- class Melon < Jam
77
- def initialize(x, y)
78
- super
79
- end
80
-
81
- def as_json()
82
- "#{x} #{y}"
83
- end
84
-
85
- def self.json_create(h)
86
- self.new(h['x'], h['y'])
87
- end
88
- end
89
-
90
- class Jazz < Jam
91
- def initialize(x, y)
92
- super
93
- end
94
- def to_hash()
95
- { 'json_class' => self.class.to_s, 'x' => @x, 'y' => @y }
96
- end
97
- def self.json_create(h)
98
- self.new(h['x'], h['y'])
99
- end
100
- end # Jazz
101
-
102
- class Range
103
- def to_hash()
104
- { 'begin' => self.begin, 'end' => self.end, 'exclude_end' => self.exclude_end? }
105
- end
106
- end # Range
107
-
108
- class Juice < ::Test::Unit::TestCase
109
-
110
- def test0_get_options
111
- opts = Oj.default_options()
112
- assert_equal({ :indent=>0,
113
- :circular=>false,
114
- :auto_define=>true,
115
- :symbol_keys=>false,
116
- :ascii_only=>false,
117
- :mode=>:object,
118
- :time_format=>:unix,
119
- :max_stack=>65536,
120
- :create_id=>'json_class'}, opts)
121
- end
122
-
123
- def test0_set_options
124
- orig = {
125
- :indent=>0,
126
- :circular=>false,
127
- :auto_define=>true,
128
- :symbol_keys=>false,
129
- :ascii_only=>false,
130
- :mode=>:object,
131
- :time_format=>:unix,
132
- :max_stack=>65536,
133
- :create_id=>'json_class'}
134
- o2 = {
135
- :indent=>4,
136
- :circular=>true,
137
- :auto_define=>false,
138
- :symbol_keys=>true,
139
- :ascii_only=>true,
140
- :mode=>:compat,
141
- :time_format=>:ruby,
142
- :max_stack=>4000,
143
- :create_id=>nil}
144
- o3 = { :indent => 4 }
145
- Oj.default_options = o2
146
- opts = Oj.default_options()
147
- assert_equal(o2, opts);
148
- Oj.default_options = o3 # see if it throws an exception
149
- Oj.default_options = orig # return to original
150
- end
151
-
152
- def test_nil
153
- dump_and_load(nil, false)
154
- end
155
-
156
- def test_true
157
- dump_and_load(true, false)
158
- end
159
-
160
- def test_false
161
- dump_and_load(false, false)
162
- end
163
-
164
- def test_fixnum
165
- dump_and_load(0, false)
166
- dump_and_load(12345, false)
167
- dump_and_load(-54321, false)
168
- dump_and_load(1, false)
169
- end
170
-
171
- def test_float
172
- dump_and_load(0.0, false)
173
- dump_and_load(12345.6789, false)
174
- dump_and_load(-54321.012, false)
175
- dump_and_load(2.48e16, false)
176
- dump_and_load(2.48e100 * 1.0e10, false)
177
- dump_and_load(-2.48e100 * 1.0e10, false)
178
- end
179
-
180
- def test_string
181
- dump_and_load('', false)
182
- dump_and_load('abc', false)
183
- dump_and_load("abc\ndef", false)
184
- dump_and_load("a\u0041", false)
185
- end
186
-
187
- def test_string_object
188
- dump_and_load('abc', false)
189
- dump_and_load(':abc', false)
190
- end
191
-
192
- def test_encode
193
- opts = Oj.default_options
194
- Oj.default_options = { :ascii_only => false }
195
- unless 'jruby' == $ruby
196
- dump_and_load("ぴーたー", false)
197
- end
198
- Oj.default_options = { :ascii_only => true }
199
- json = Oj.dump("ぴーたー")
200
- assert_equal(%{"\\u3074\\u30fc\\u305f\\u30fc"}, json)
201
- unless 'jruby' == $ruby
202
- dump_and_load("ぴーたー", false)
203
- end
204
- Oj.default_options = opts
205
- end
206
-
207
- def test_unicode
208
- # hits the 3 normal ranges and one extended surrogate pair
209
- json = %{"\\u019f\\u05e9\\u3074\\ud834\\udd1e"}
210
- obj = Oj.load(json)
211
- json2 = Oj.dump(obj, :ascii_only => true)
212
- assert_equal(json, json2)
213
- end
214
-
215
- def test_array
216
- dump_and_load([], false)
217
- dump_and_load([true, false], false)
218
- dump_and_load(['a', 1, nil], false)
219
- dump_and_load([[nil]], false)
220
- dump_and_load([[nil], 58], false)
221
- end
222
-
223
- # Symbol
224
- def test_symbol_strict
225
- begin
226
- Oj.dump(:abc, :mode => :strict)
227
- assert(false)
228
- rescue Exception
229
- assert(true)
230
- end
231
- end
232
- def test_symbol_null
233
- json = Oj.dump(:abc, :mode => :null)
234
- assert_equal('null', json)
235
- end
236
- def test_symbol_compat
237
- json = Oj.dump(:abc, :mode => :compat)
238
- assert_equal('"abc"', json)
239
- end
240
- def test_symbol_object
241
- Oj.default_options = { :mode => :object }
242
- #dump_and_load(''.to_sym, false)
243
- dump_and_load(:abc, false)
244
- dump_and_load(':xyz'.to_sym, false)
245
- end
246
-
247
- # Time
248
- def test_time_strict
249
- t = Time.local(2012, 1, 5, 23, 58, 7)
250
- begin
251
- Oj.dump(t, :mode => :strict)
252
- assert(false)
253
- rescue Exception
254
- assert(true)
255
- end
256
- end
257
- def test_time_null
258
- t = Time.local(2012, 1, 5, 23, 58, 7)
259
- json = Oj.dump(t, :mode => :null)
260
- assert_equal('null', json)
261
- end
262
- def test_unix_time_compat
263
- t = Time.xmlschema("2012-01-05T23:58:07.123456000+09:00")
264
- #t = Time.local(2012, 1, 5, 23, 58, 7, 123456)
265
- json = Oj.dump(t, :mode => :compat)
266
- assert_equal(%{1325775487.123456000}, json)
267
- end
268
- def test_ruby_time_compat
269
- t = Time.xmlschema("2012-01-05T23:58:07.123456000+09:00")
270
- json = Oj.dump(t, :mode => :compat, :time_format => :ruby)
271
- #assert_equal(%{"2012-01-05 23:58:07 +0900"}, json)
272
- assert_equal(%{"#{t.to_s}"}, json)
273
- end
274
- def test_xml_time_compat
275
- begin
276
- t = Time.new(2012, 1, 5, 23, 58, 7.123456000, 34200)
277
- json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema)
278
- assert_equal(%{"2012-01-05T23:58:07.123456000+09:30"}, json)
279
- rescue Exception
280
- # some Rubies (1.8.7) do not allow the timezome to be set
281
- t = Time.local(2012, 1, 5, 23, 58, 7, 123456)
282
- json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema)
283
- tz = t.utc_offset
284
- # Ruby does not handle a %+02d properly so...
285
- sign = '+'
286
- if 0 > tz
287
- sign = '-'
288
- tz = -tz
289
- end
290
- assert_equal(%{"2012-01-05T23:58:07.123456000%s%02d:%02d"} % [sign, tz / 3600, tz / 60 % 60], json)
291
- end
292
- end
293
- def test_xml_time_compat_no_secs
294
- begin
295
- t = Time.new(2012, 1, 5, 23, 58, 7.0, 34200)
296
- json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema)
297
- assert_equal(%{"2012-01-05T23:58:07+09:30"}, json)
298
- rescue Exception
299
- # some Rubies (1.8.7) do not allow the timezome to be set
300
- t = Time.local(2012, 1, 5, 23, 58, 7, 0)
301
- json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema)
302
- tz = t.utc_offset
303
- # Ruby does not handle a %+02d properly so...
304
- sign = '+'
305
- if 0 > tz
306
- sign = '-'
307
- tz = -tz
308
- end
309
- assert_equal(%{"2012-01-05T23:58:07%s%02d:%02d"} % [sign, tz / 3600, tz / 60 % 60], json)
310
- end
311
- end
312
- def test_xml_time_compat_zulu
313
- begin
314
- t = Time.new(2012, 1, 5, 23, 58, 7.0, 0)
315
- json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema)
316
- assert_equal(%{"2012-01-05T23:58:07Z"}, json)
317
- rescue Exception
318
- # some Rubies (1.8.7) do not allow the timezome to be set
319
- t = Time.utc(2012, 1, 5, 23, 58, 7, 0)
320
- json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema)
321
- #tz = t.utc_offset
322
- assert_equal(%{"2012-01-05T23:58:07Z"}, json)
323
- end
324
- end
325
- def test_time_object
326
- t = Time.now()
327
- Oj.default_options = { :mode => :object }
328
- dump_and_load(t, false)
329
- end
330
-
331
- # Class
332
- def test_class_strict
333
- begin
334
- Oj.dump(Juice, :mode => :strict)
335
- assert(false)
336
- rescue Exception
337
- assert(true)
338
- end
339
- end
340
- def test_class_null
341
- json = Oj.dump(Juice, :mode => :null)
342
- assert_equal('null', json)
343
- end
344
- def test_class_compat
345
- json = Oj.dump(Juice, :mode => :compat)
346
- assert_equal(%{"Juice"}, json)
347
- end
348
- def test_class_object
349
- Oj.default_options = { :mode => :object }
350
- dump_and_load(Juice, false)
351
- end
352
-
353
- # Hash
354
- def test_hash
355
- Oj.default_options = { :mode => :strict }
356
- dump_and_load({}, false)
357
- dump_and_load({ 'true' => true, 'false' => false}, false)
358
- dump_and_load({ 'true' => true, 'array' => [], 'hash' => { }}, false)
359
- end
360
- def test_non_str_hash_strict
361
- begin
362
- Oj.dump({ 1 => true, 0 => false }, :mode => :strict)
363
- assert(false)
364
- rescue Exception
365
- assert(true)
366
- end
367
- end
368
- def test_non_str_hash_null
369
- begin
370
- Oj.dump({ 1 => true, 0 => false }, :mode => :null)
371
- assert(false)
372
- rescue Exception
373
- assert(true)
374
- end
375
- end
376
- def test_non_str_hash_compat
377
- json = Oj.dump({ 1 => true, 0 => false }, :mode => :compat)
378
- h = Oj.load(json, :mode => :strict)
379
- assert_equal({ "1" => true, "0" => false }, h)
380
- end
381
- def test_non_str_hash_object
382
- Oj.default_options = { :mode => :object }
383
- json = Oj.dump({ 1 => true, :sim => nil })
384
- h = Oj.load(json, :mode => :strict)
385
- assert_equal({"^#1" => [1, true], ":sim" => nil}, h)
386
- h = Oj.load(json)
387
- assert_equal({ 1 => true, :sim => nil }, h)
388
- end
389
- def test_mixed_hash_object
390
- Oj.default_options = { :mode => :object }
391
- json = Oj.dump({ 1 => true, 'nil' => nil, :sim => 4 })
392
- h = Oj.load(json, :mode => :strict)
393
- assert_equal({"^#1" => [1, true], "nil" => nil, ":sim" => 4}, h)
394
- h = Oj.load(json)
395
- assert_equal({ 1 => true, 'nil' => nil, :sim => 4 }, h)
396
- end
397
-
398
- # Object with to_json()
399
- def test_json_object_strict
400
- obj = Jeez.new(true, 58)
401
- begin
402
- Oj.dump(obj, :mode => :strict)
403
- assert(false)
404
- rescue Exception
405
- assert(true)
406
- end
407
- end
408
- def test_json_object_null
409
- obj = Jeez.new(true, 58)
410
- json = Oj.dump(obj, :mode => :null)
411
- assert_equal('null', json)
412
- end
413
- def test_json_object_compat
414
- Oj.default_options = { :mode => :compat }
415
- obj = Jeez.new(true, 58)
416
- json = Oj.dump(obj, :indent => 2)
417
- assert(%{{"json_class":"Jeez","x":true,"y":58}} == json ||
418
- %{{"json_class":"Jeez","y":58,"x":true}} == json)
419
- dump_and_load(obj, false)
420
- end
421
- def test_json_object_create_id
422
- Oj.default_options = { :mode => :compat, :create_id => 'kson_class' }
423
- expected = Jeez.new(true, 58)
424
- json = %{{"kson_class":"Jeez","x":true,"y":58}}
425
- obj = Oj.load(json)
426
- assert_equal(expected, obj)
427
- Oj.default_options = { :create_id => 'json_class' }
428
- end
429
- def test_json_object_object
430
- obj = Jeez.new(true, 58)
431
- json = Oj.dump(obj, :mode => :object, :indent => 2)
432
- assert(%{{
433
- "^o":"Jeez",
434
- "x":true,
435
- "y":58}} == json ||
436
- %{{
437
- "^o":"Jeez",
438
- "y":58,
439
- "x":true}} == json)
440
- obj2 = Oj.load(json, :mode => :object)
441
- assert_equal(obj, obj2)
442
- end
443
-
444
- # Object with to_hash()
445
- def test_to_hash_object_strict
446
- obj = Jazz.new(true, 58)
447
- begin
448
- Oj.dump(obj, :mode => :strict)
449
- assert(false)
450
- rescue Exception
451
- assert(true)
452
- end
453
- end
454
- def test_to_hash_object_null
455
- obj = Jazz.new(true, 58)
456
- json = Oj.dump(obj, :mode => :null)
457
- assert_equal('null', json)
458
- end
459
- def test_to_hash_object_compat
460
- obj = Jazz.new(true, 58)
461
- json = Oj.dump(obj, :mode => :compat, :indent => 2)
462
- h = Oj.load(json, :mode => :strict)
463
- assert_equal(obj.to_hash, h)
464
- end
465
- def test_to_hash_object_object
466
- obj = Jazz.new(true, 58)
467
- json = Oj.dump(obj, :mode => :object, :indent => 2)
468
- assert(%{{
469
- "^o":"Jazz",
470
- "x":true,
471
- "y":58}} == json ||
472
- %{{
473
- "^o":"Jazz",
474
- "y":58,
475
- "x":true}} == json)
476
- obj2 = Oj.load(json, :mode => :object)
477
- assert_equal(obj, obj2)
478
- end
479
-
480
- # Object with as_json() # contributed by sauliusg
481
- def test_as_json_object_strict
482
- obj = Orange.new(true, 58)
483
- begin
484
- Oj.dump(obj, :mode => :strict)
485
- assert(false)
486
- rescue Exception
487
- assert(true)
488
- end
489
- end
490
-
491
- def test_as_json_object_null
492
- obj = Orange.new(true, 58)
493
- json = Oj.dump(obj, :mode => :null)
494
- assert_equal('null', json)
495
- end
496
-
497
- def test_as_json_object_compat_hash
498
- Oj.default_options = { :mode => :compat }
499
- obj = Orange.new(true, 58)
500
- json = Oj.dump(obj, :indent => 2)
501
- assert(!json.nil?)
502
- =begin
503
- assert_equal(%{{
504
- "json_class":"Orange",
505
- "x":true,
506
- "y":58}}, json)
507
- =end
508
- dump_and_load(obj, false)
509
- end
510
-
511
- def test_as_json_object_compat_non_hash
512
- Oj.default_options = { :mode => :compat }
513
- obj = Melon.new(true, 58)
514
- json = Oj.dump(obj, :indent => 2)
515
- assert_equal(%{"true 58"}, json)
516
- end
517
-
518
- def test_as_json_object_object
519
- obj = Orange.new(true, 58)
520
- json = Oj.dump(obj, :mode => :object, :indent => 2)
521
- assert(%{{
522
- "^o":"Orange",
523
- "x":true,
524
- "y":58}} == json ||
525
- %{{
526
- "^o":"Orange",
527
- "y":58,
528
- "x":true}} == json)
529
- obj2 = Oj.load(json, :mode => :object)
530
- assert_equal(obj, obj2)
531
- end
532
-
533
- # Object without to_json() or to_hash()
534
- def test_object_strict
535
- obj = Jam.new(true, 58)
536
- begin
537
- Oj.dump(obj, :mode => :strict)
538
- assert(false)
539
- rescue Exception
540
- assert(true)
541
- end
542
- end
543
- def test_object_null
544
- obj = Jam.new(true, 58)
545
- json = Oj.dump(obj, :mode => :null)
546
- assert_equal('null', json)
547
- end
548
- def test_object_compat
549
- obj = Jam.new(true, 58)
550
- json = Oj.dump(obj, :mode => :compat, :indent => 2)
551
- assert(%{{
552
- "x":true,
553
- "y":58}} == json ||
554
- %{{
555
- "y":58,
556
- "x":true}} == json)
557
- end
558
- def test_object_object
559
- obj = Jam.new(true, 58)
560
- json = Oj.dump(obj, :mode => :object, :indent => 2)
561
- assert(%{{
562
- "^o":"Jam",
563
- "x":true,
564
- "y":58}} == json ||
565
- %{{
566
- "^o":"Jam",
567
- "y":58,
568
- "x":true}} == json)
569
- obj2 = Oj.load(json, :mode => :object)
570
- assert_equal(obj, obj2)
571
- end
572
-
573
- # Exception
574
- def test_exception
575
- err = nil
576
- begin
577
- raise StandardError.new('A Message')
578
- assert(false)
579
- rescue Exception => e
580
- err = e
581
- end
582
- json = Oj.dump(err, :mode => :object, :indent => 2)
583
- #puts "*** #{json}"
584
- e2 = Oj.load(json, :mode => :strict)
585
- assert_equal(err.class.to_s, e2['^o'])
586
- assert_equal(err.message, e2['~mesg'])
587
- assert_equal(err.backtrace, e2['~bt'])
588
- e2 = Oj.load(json, :mode => :object)
589
- if RUBY_VERSION.start_with?('1.8') || 'rubinius' == $ruby
590
- assert_equal(e.class, e2.class);
591
- assert_equal(e.message, e2.message);
592
- assert_equal(e.backtrace, e2.backtrace);
593
- else
594
- assert_equal(e, e2);
595
- end
596
- end
597
-
598
- # Range
599
- def test_range_strict
600
- begin
601
- Oj.dump(1..7, :mode => :strict)
602
- assert(false)
603
- rescue Exception
604
- assert(true)
605
- end
606
- end
607
- def test_range_null
608
- json = Oj.dump(1..7, :mode => :null)
609
- assert_equal('null', json)
610
- end
611
- def test_range_compat
612
- json = Oj.dump(1..7, :mode => :compat)
613
- h = Oj.load(json, :mode => :strict)
614
- assert_equal({'begin' => 1, 'end' => 7, 'exclude_end' => false}, h)
615
- json = Oj.dump(1...7, :mode => :compat)
616
- h = Oj.load(json, :mode => :strict)
617
- assert_equal({'begin' => 1, 'end' => 7, 'exclude_end' => true}, h)
618
- end
619
- def test_range_object
620
- unless RUBY_VERSION.start_with?('1.8')
621
- Oj.default_options = { :mode => :object }
622
- json = Oj.dump(1..7, :mode => :object, :indent => 0)
623
- if 'rubinius' == $ruby
624
- assert(%{{"^o":"Range","excl":false,"begin":1,"end":7}} == json ||
625
- %{{"^o":"Range","begin":1,"end":7,"excl":false}} == json)
626
- elsif 'jruby' == $ruby
627
- assert(%{{"^O":"Range","begin":1,"end":7,"exclude_end?":false}} == json)
628
- else
629
- assert_equal(%{{"^u":["Range",1,7,false]}}, json)
630
- end
631
- dump_and_load(1..7, false)
632
- dump_and_load(1..1, false)
633
- dump_and_load(1...7, false)
634
- end
635
- end
636
-
637
- # BigNum
638
- def test_bignum_strict
639
- json = Oj.dump(7 ** 55, :mode => :strict)
640
- assert_equal('30226801971775055948247051683954096612865741943', json)
641
- end
642
- def test_bignum_null
643
- json = Oj.dump(7 ** 55, :mode => :null)
644
- assert_equal('30226801971775055948247051683954096612865741943', json)
645
- end
646
- def test_bignum_compat
647
- json = Oj.dump(7 ** 55, :mode => :compat)
648
- b = Oj.load(json, :mode => :strict)
649
- assert_equal(30226801971775055948247051683954096612865741943, b)
650
- end
651
- def test_bignum_object
652
- dump_and_load(7 ** 55, false)
653
- end
654
-
655
- # BigDecimal
656
- def test_bigdecimal_strict
657
- mode = Oj.default_options[:mode]
658
- Oj.default_options = {:mode => :strict}
659
- dump_and_load(BigDecimal.new('3.14159265358979323846'), false)
660
- Oj.default_options = {:mode => mode}
661
- end
662
- def test_bigdecimal_null
663
- mode = Oj.default_options[:mode]
664
- Oj.default_options = {:mode => :null}
665
- dump_and_load(BigDecimal.new('3.14159265358979323846'), false)
666
- Oj.default_options = {:mode => mode}
667
- end
668
- def test_bigdecimal_compat
669
- orig = BigDecimal.new('3.14159265358979323846')
670
- json = Oj.dump(orig, :mode => :compat)
671
- bg = Oj.load(json, :mode => :compat)
672
- assert_equal(orig, bg)
673
- end
674
- def test_bigdecimal_object
675
- mode = Oj.default_options[:mode]
676
- Oj.default_options = {:mode => :object}
677
- dump_and_load(BigDecimal.new('3.14159265358979323846'), false)
678
- Oj.default_options = {:mode => mode}
679
- # Infinity is the same for Float and BigDecimal
680
- json = Oj.dump(BigDecimal.new('Infinity'), :mode => :object)
681
- assert_equal('Infinity', json)
682
- json = Oj.dump(BigDecimal.new('-Infinity'), :mode => :object)
683
- assert_equal('-Infinity', json)
684
- end
685
-
686
- # Date
687
- def test_date_strict
688
- begin
689
- Oj.dump(Date.new(2012, 6, 19), :mode => :strict)
690
- assert(false)
691
- rescue Exception
692
- assert(true)
693
- end
694
- end
695
- def test_date_null
696
- json = Oj.dump(Date.new(2012, 6, 19), :mode => :null)
697
- assert_equal('null', json)
698
- end
699
- def test_date_compat
700
- orig = Date.new(2012, 6, 19)
701
- json = Oj.dump(orig, :mode => :compat)
702
- x = Oj.load(json, :mode => :compat)
703
- # Some Rubies implement Date as data and some as a real Object. Either are
704
- # okay for the test.
705
- if x.is_a?(String)
706
- assert_equal(orig.to_s, x)
707
- else # better be a Hash
708
- assert_equal({"year" => orig.year, "month" => orig.month, "day" => orig.day, "start" => orig.start}, x)
709
- end
710
- end
711
- def test_date_object
712
- dump_and_load(Date.new(2012, 6, 19), false)
713
- end
714
-
715
- # DateTime
716
- def test_datetime_strict
717
- begin
718
- Oj.dump(DateTime.new(2012, 6, 19, 20, 19, 27), :mode => :strict)
719
- assert(false)
720
- rescue Exception
721
- assert(true)
722
- end
723
- end
724
- def test_datetime_null
725
- json = Oj.dump(DateTime.new(2012, 6, 19, 20, 19, 27), :mode => :null)
726
- assert_equal('null', json)
727
- end
728
- def test_datetime_compat
729
- orig = DateTime.new(2012, 6, 19, 20, 19, 27)
730
- json = Oj.dump(orig, :mode => :compat)
731
- x = Oj.load(json, :mode => :compat)
732
- # Some Rubies implement Date as data and some as a real Object. Either are
733
- # okay for the test.
734
- if x.is_a?(String)
735
- assert_equal(orig.to_s, x)
736
- else # better be a Hash
737
- assert_equal({ "year" => orig.year, "month" => orig.month, "day" => orig.day,
738
- "hour" => orig.hour, "min" => orig.min, "sec" => orig.sec,
739
- "offset" => orig.offset, "start" => orig.start}, x)
740
- end
741
- end
742
- def test_datetime_object
743
- dump_and_load(DateTime.new(2012, 6, 19), false)
744
- end
745
-
746
- # autodefine Oj::Bag
747
- def test_bag
748
- json = %{{
749
- "^o":"Jem",
750
- "x":true,
751
- "y":58 }}
752
- obj = Oj.load(json, :mode => :object)
753
- assert_equal('Jem', obj.class.name)
754
- assert_equal(true, obj.x)
755
- assert_equal(58, obj.y)
756
- end
757
-
758
- # Circular
759
- def test_circular_object
760
- obj = Jam.new(nil, 58)
761
- obj.x = obj
762
- json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true)
763
- assert(%{{
764
- "^o":"Jam",
765
- "^i":1,
766
- "x":"^r1",
767
- "y":58}} == json ||
768
- %{{
769
- "^o":"Jam",
770
- "^i":1,
771
- "y":58,
772
- "x":"^r1"}} == json)
773
- obj2 = Oj.load(json, :mode => :object, :circular => true)
774
- assert_equal(obj2.x.__id__, obj2.__id__)
775
- end
776
-
777
- def test_circular_hash
778
- h = { 'a' => 7 }
779
- h['b'] = h
780
- json = Oj.dump(h, :mode => :object, :indent => 2, :circular => true)
781
- ha = Oj.load(json, :mode => :strict)
782
- assert_equal({'^i' => 1, 'a' => 7, 'b' => '^r1'}, ha)
783
- Oj.load(json, :mode => :object, :circular => true)
784
- assert_equal(h['b'].__id__, h.__id__)
785
- end
786
-
787
- def test_circular_array
788
- a = [7]
789
- a << a
790
- json = Oj.dump(a, :mode => :object, :indent => 2, :circular => true)
791
- assert_equal(%{[
792
- "^i1",
793
- 7,
794
- "^r1"]}, json)
795
- a2 = Oj.load(json, :mode => :object, :circular => true)
796
- assert_equal(a2[1].__id__, a2.__id__)
797
- end
798
-
799
- def test_circular
800
- h = { 'a' => 7 }
801
- obj = Jam.new(h, 58)
802
- obj.x['b'] = obj
803
- json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true)
804
- ha = Oj.load(json, :mode => :strict)
805
- assert_equal({'^o' => 'Jam', '^i' => 1, 'x' => { '^i' => 2, 'a' => 7, 'b' => '^r1' }, 'y' => 58 }, ha)
806
- Oj.load(json, :mode => :object, :circular => true)
807
- assert_equal(obj.x.__id__, h.__id__)
808
- assert_equal(h['b'].__id__, obj.__id__)
809
- end
810
-
811
- # Stream Deeply Nested
812
- def test_deep_nest
813
- unless 'jruby' == RUBY_DESCRIPTION.split(' ')[0]
814
- begin
815
- n = 100000
816
- Oj.load('[' * n + ']' * n)
817
- assert(false)
818
- rescue Exception => e
819
- assert(e.class == SystemStackError)
820
- end
821
- end
822
- end
823
-
824
- # Stream IO
825
- def test_io_string
826
- json = %{{
827
- "x":true,
828
- "y":58,
829
- "z": [1,2,3]
830
- }
831
- }
832
- input = StringIO.new(json)
833
- obj = Oj.load(input, :mode => :strict)
834
- assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
835
- end
836
-
837
- def test_io_file
838
- filename = 'open_file_test.json'
839
- File.open(filename, 'w') { |f| f.write(%{{
840
- "x":true,
841
- "y":58,
842
- "z": [1,2,3]
843
- }
844
- }) }
845
- f = File.new(filename)
846
- obj = Oj.load(f, :mode => :strict)
847
- f.close()
848
- assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
849
- end
850
-
851
- # symbol_keys option
852
- def test_symbol_keys
853
- json = %{{
854
- "x":true,
855
- "y":58,
856
- "z": [1,2,3]
857
- }
858
- }
859
- obj = Oj.load(json, :mode => :strict, :symbol_keys => true)
860
- assert_equal({ :x => true, :y => 58, :z => [1, 2, 3]}, obj)
861
- end
862
-
863
- # comments
864
- def test_comment_slash
865
- json = %{{
866
- "x":true,//three
867
- "y":58,
868
- "z": [1,2,
869
- 3 // six
870
- ]}
871
- }
872
- obj = Oj.load(json, :mode => :strict)
873
- assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
874
- end
875
-
876
- def test_comment_c
877
- json = %{{
878
- "x"/*one*/:/*two*/true,
879
- "y":58,
880
- "z": [1,2,3]}
881
- }
882
- obj = Oj.load(json, :mode => :strict)
883
- assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
884
- end
885
-
886
- def test_comment
887
- json = %{{
888
- "x"/*one*/:/*two*/true,//three
889
- "y":58/*four*/,
890
- "z": [1,2/*five*/,
891
- 3 // six
892
- ]
893
- }
894
- }
895
- obj = Oj.load(json, :mode => :strict)
896
- assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
897
- end
898
-
899
- def dump_and_load(obj, trace=false)
900
- json = Oj.dump(obj, :indent => 2)
901
- puts json if trace
902
- loaded = Oj.load(json);
903
- assert_equal(obj, loaded)
904
- loaded
905
- end
906
-
907
- end
10
+ require 'test_strict'
11
+ require 'test_null'
12
+ require 'test_compat'
13
+ require 'test_object'
14
+ require 'test_custom'