oj 3.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +104 -0
  4. data/ext/oj/buf.h +103 -0
  5. data/ext/oj/cache8.c +107 -0
  6. data/ext/oj/cache8.h +48 -0
  7. data/ext/oj/circarray.c +68 -0
  8. data/ext/oj/circarray.h +23 -0
  9. data/ext/oj/code.c +235 -0
  10. data/ext/oj/code.h +42 -0
  11. data/ext/oj/compat.c +299 -0
  12. data/ext/oj/custom.c +1191 -0
  13. data/ext/oj/dump.c +1252 -0
  14. data/ext/oj/dump.h +96 -0
  15. data/ext/oj/dump_compat.c +977 -0
  16. data/ext/oj/dump_leaf.c +252 -0
  17. data/ext/oj/dump_object.c +837 -0
  18. data/ext/oj/dump_strict.c +433 -0
  19. data/ext/oj/encode.h +45 -0
  20. data/ext/oj/err.c +57 -0
  21. data/ext/oj/err.h +70 -0
  22. data/ext/oj/extconf.rb +47 -0
  23. data/ext/oj/fast.c +1771 -0
  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 +878 -0
  28. data/ext/oj/object.c +771 -0
  29. data/ext/oj/odd.c +231 -0
  30. data/ext/oj/odd.h +44 -0
  31. data/ext/oj/oj.c +1704 -0
  32. data/ext/oj/oj.h +385 -0
  33. data/ext/oj/parse.c +1086 -0
  34. data/ext/oj/parse.h +111 -0
  35. data/ext/oj/rails.c +1493 -0
  36. data/ext/oj/rails.h +21 -0
  37. data/ext/oj/reader.c +231 -0
  38. data/ext/oj/reader.h +151 -0
  39. data/ext/oj/resolve.c +102 -0
  40. data/ext/oj/resolve.h +14 -0
  41. data/ext/oj/rxclass.c +147 -0
  42. data/ext/oj/rxclass.h +27 -0
  43. data/ext/oj/saj.c +714 -0
  44. data/ext/oj/scp.c +224 -0
  45. data/ext/oj/sparse.c +910 -0
  46. data/ext/oj/stream_writer.c +363 -0
  47. data/ext/oj/strict.c +212 -0
  48. data/ext/oj/string_writer.c +534 -0
  49. data/ext/oj/trace.c +79 -0
  50. data/ext/oj/trace.h +28 -0
  51. data/ext/oj/util.c +136 -0
  52. data/ext/oj/util.h +19 -0
  53. data/ext/oj/val_stack.c +118 -0
  54. data/ext/oj/val_stack.h +185 -0
  55. data/ext/oj/wab.c +631 -0
  56. data/lib/oj.rb +21 -0
  57. data/lib/oj/active_support_helper.rb +41 -0
  58. data/lib/oj/bag.rb +88 -0
  59. data/lib/oj/easy_hash.rb +52 -0
  60. data/lib/oj/error.rb +22 -0
  61. data/lib/oj/json.rb +176 -0
  62. data/lib/oj/mimic.rb +267 -0
  63. data/lib/oj/saj.rb +66 -0
  64. data/lib/oj/schandler.rb +142 -0
  65. data/lib/oj/state.rb +131 -0
  66. data/lib/oj/version.rb +5 -0
  67. data/pages/Advanced.md +22 -0
  68. data/pages/Compatibility.md +25 -0
  69. data/pages/Custom.md +23 -0
  70. data/pages/Encoding.md +65 -0
  71. data/pages/JsonGem.md +79 -0
  72. data/pages/Modes.md +155 -0
  73. data/pages/Options.md +283 -0
  74. data/pages/Rails.md +116 -0
  75. data/pages/Security.md +20 -0
  76. data/pages/WAB.md +13 -0
  77. data/test/_test_active.rb +76 -0
  78. data/test/_test_active_mimic.rb +96 -0
  79. data/test/_test_mimic_rails.rb +126 -0
  80. data/test/activerecord/result_test.rb +27 -0
  81. data/test/activesupport4/decoding_test.rb +108 -0
  82. data/test/activesupport4/encoding_test.rb +531 -0
  83. data/test/activesupport4/test_helper.rb +41 -0
  84. data/test/activesupport5/decoding_test.rb +125 -0
  85. data/test/activesupport5/encoding_test.rb +485 -0
  86. data/test/activesupport5/encoding_test_cases.rb +90 -0
  87. data/test/activesupport5/test_helper.rb +50 -0
  88. data/test/activesupport5/time_zone_test_helpers.rb +24 -0
  89. data/test/bar.rb +25 -0
  90. data/test/files.rb +29 -0
  91. data/test/foo.rb +167 -0
  92. data/test/helper.rb +26 -0
  93. data/test/isolated/shared.rb +308 -0
  94. data/test/isolated/test_mimic_after.rb +13 -0
  95. data/test/isolated/test_mimic_alone.rb +12 -0
  96. data/test/isolated/test_mimic_as_json.rb +45 -0
  97. data/test/isolated/test_mimic_before.rb +13 -0
  98. data/test/isolated/test_mimic_define.rb +28 -0
  99. data/test/isolated/test_mimic_rails_after.rb +22 -0
  100. data/test/isolated/test_mimic_rails_before.rb +21 -0
  101. data/test/isolated/test_mimic_redefine.rb +15 -0
  102. data/test/json_gem/json_addition_test.rb +216 -0
  103. data/test/json_gem/json_common_interface_test.rb +148 -0
  104. data/test/json_gem/json_encoding_test.rb +107 -0
  105. data/test/json_gem/json_ext_parser_test.rb +20 -0
  106. data/test/json_gem/json_fixtures_test.rb +35 -0
  107. data/test/json_gem/json_generator_test.rb +383 -0
  108. data/test/json_gem/json_generic_object_test.rb +90 -0
  109. data/test/json_gem/json_parser_test.rb +470 -0
  110. data/test/json_gem/json_string_matching_test.rb +42 -0
  111. data/test/json_gem/test_helper.rb +18 -0
  112. data/test/perf.rb +107 -0
  113. data/test/perf_compat.rb +130 -0
  114. data/test/perf_fast.rb +164 -0
  115. data/test/perf_file.rb +64 -0
  116. data/test/perf_object.rb +138 -0
  117. data/test/perf_saj.rb +109 -0
  118. data/test/perf_scp.rb +151 -0
  119. data/test/perf_simple.rb +287 -0
  120. data/test/perf_strict.rb +145 -0
  121. data/test/perf_wab.rb +131 -0
  122. data/test/sample.rb +54 -0
  123. data/test/sample/change.rb +14 -0
  124. data/test/sample/dir.rb +19 -0
  125. data/test/sample/doc.rb +36 -0
  126. data/test/sample/file.rb +48 -0
  127. data/test/sample/group.rb +16 -0
  128. data/test/sample/hasprops.rb +16 -0
  129. data/test/sample/layer.rb +12 -0
  130. data/test/sample/line.rb +20 -0
  131. data/test/sample/oval.rb +10 -0
  132. data/test/sample/rect.rb +10 -0
  133. data/test/sample/shape.rb +35 -0
  134. data/test/sample/text.rb +20 -0
  135. data/test/sample_json.rb +37 -0
  136. data/test/test_compat.rb +509 -0
  137. data/test/test_custom.rb +503 -0
  138. data/test/test_debian.rb +53 -0
  139. data/test/test_fast.rb +470 -0
  140. data/test/test_file.rb +239 -0
  141. data/test/test_gc.rb +49 -0
  142. data/test/test_hash.rb +29 -0
  143. data/test/test_integer_range.rb +73 -0
  144. data/test/test_null.rb +376 -0
  145. data/test/test_object.rb +1018 -0
  146. data/test/test_saj.rb +186 -0
  147. data/test/test_scp.rb +433 -0
  148. data/test/test_strict.rb +410 -0
  149. data/test/test_various.rb +741 -0
  150. data/test/test_wab.rb +307 -0
  151. data/test/test_writer.rb +380 -0
  152. data/test/tests.rb +24 -0
  153. data/test/tests_mimic.rb +14 -0
  154. data/test/tests_mimic_addition.rb +7 -0
  155. data/test/zoo.rb +13 -0
  156. metadata +359 -0
@@ -0,0 +1,503 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
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)
8
+ end
9
+
10
+ require 'minitest'
11
+ require 'minitest/autorun'
12
+ require 'stringio'
13
+ require 'date'
14
+ require 'bigdecimal'
15
+ require 'oj'
16
+
17
+ class CustomJuice < Minitest::Test
18
+
19
+ module TestModule
20
+ end
21
+
22
+ class Jeez
23
+ attr_accessor :x, :y
24
+
25
+ def initialize(x, y)
26
+ @x = x
27
+ @y = y
28
+ end
29
+ def ==(o)
30
+ self.class == o.class && @x == o.x && @y = o.y
31
+ end
32
+ def to_json(*args)
33
+ %|{"xx":#{@x},"yy":#{y}}|
34
+ end
35
+ def raw_json(depth, indent)
36
+ %|{"xxx":#{@x},"yyy":#{y}}|
37
+ end
38
+ def as_json(*args)
39
+ {'a' => @x, :b => @y }
40
+ end
41
+ def to_hash()
42
+ {'b' => @x, 'n' => @y }
43
+ end
44
+ end
45
+
46
+ class AsJson
47
+ attr_accessor :x, :y
48
+
49
+ def initialize(x, y)
50
+ @x = x
51
+ @y = y
52
+ end
53
+ def ==(o)
54
+ self.class == o.class && @x == o.x && @y = o.y
55
+ end
56
+ def as_json(*args)
57
+ {'a' => @x, :b => @y }
58
+ end
59
+ end
60
+
61
+ class AsRails
62
+ attr_accessor :x, :y
63
+
64
+ def initialize(x, y)
65
+ @x = x
66
+ @y = y
67
+ end
68
+ def ==(o)
69
+ self.class == o.class && @x == o.x && @y = o.y
70
+ end
71
+ def as_json(*args)
72
+ a = @x
73
+ a = a.as_json if a.respond_to?('as_json')
74
+ b = @y
75
+ b = b.as_json if b.respond_to?('as_json')
76
+ {'a' => a, :b => b }
77
+ end
78
+ end
79
+
80
+ def setup
81
+ @default_options = Oj.default_options
82
+ Oj.default_options = { :mode => :custom }
83
+ end
84
+
85
+ def teardown
86
+ Oj.default_options = @default_options
87
+ end
88
+
89
+ def test_nil
90
+ dump_and_load(nil, false)
91
+ end
92
+
93
+ def test_true
94
+ dump_and_load(true, false)
95
+ end
96
+
97
+ def test_false
98
+ dump_and_load(false, false)
99
+ end
100
+
101
+ def test_fixnum
102
+ dump_and_load(0, false)
103
+ dump_and_load(12345, false)
104
+ dump_and_load(-54321, false)
105
+ dump_and_load(1, false)
106
+ end
107
+
108
+ def test_float
109
+ dump_and_load(0.0, false)
110
+ dump_and_load(12345.6789, false)
111
+ dump_and_load(70.35, false)
112
+ dump_and_load(-54321.012, false)
113
+ dump_and_load(1.7775, false)
114
+ dump_and_load(2.5024, false)
115
+ dump_and_load(2.48e16, false)
116
+ dump_and_load(2.48e100 * 1.0e10, false)
117
+ dump_and_load(-2.48e100 * 1.0e10, false)
118
+ end
119
+
120
+ def test_nan_dump
121
+ assert_equal('null', Oj.dump(0/0.0, :nan => :null))
122
+ assert_equal('3.3e14159265358979323846', Oj.dump(0/0.0, :nan => :huge))
123
+ assert_equal('NaN', Oj.dump(0/0.0, :nan => :word))
124
+ begin
125
+ Oj.dump(0/0.0, :nan => :raise)
126
+ rescue Exception
127
+ assert(true)
128
+ return
129
+ end
130
+ assert(false, "*** expected an exception")
131
+ end
132
+
133
+ def test_infinity_dump
134
+ assert_equal('null', Oj.dump(1/0.0, :nan => :null))
135
+ assert_equal('3.0e14159265358979323846', Oj.dump(1/0.0, :nan => :huge))
136
+ assert_equal('Infinity', Oj.dump(1/0.0, :nan => :word))
137
+ begin
138
+ Oj.dump(1/0.0, :nan => :raise)
139
+ rescue Exception
140
+ assert(true)
141
+ return
142
+ end
143
+ assert(false, "*** expected an exception")
144
+ end
145
+
146
+ def test_neg_infinity_dump
147
+ assert_equal('null', Oj.dump(-1/0.0, :nan => :null))
148
+ assert_equal('-3.0e14159265358979323846', Oj.dump(-1/0.0, :nan => :huge))
149
+ assert_equal('-Infinity', Oj.dump(-1/0.0, :nan => :word))
150
+ begin
151
+ Oj.dump(-1/0.0, :nan => :raise)
152
+ rescue Exception
153
+ assert(true)
154
+ return
155
+ end
156
+ assert(false, "*** expected an exception")
157
+ end
158
+
159
+ def test_string
160
+ dump_and_load('', false)
161
+ dump_and_load('abc', false)
162
+ dump_and_load("abc\ndef", false)
163
+ dump_and_load("a\u0041", false)
164
+ end
165
+
166
+ def test_string_ascii
167
+ json = Oj.dump("ぴーたー", :escape_mode => :ascii)
168
+ assert_equal(%{"\\u3074\\u30fc\\u305f\\u30fc"}, json)
169
+ dump_and_load("ぴーたー", false, :escape_mode => :ascii)
170
+ end
171
+
172
+ def test_string_json
173
+ json = Oj.dump("ぴーたー", :escape_mode => :json)
174
+ assert_equal(%{"ぴーたー"}, json)
175
+ dump_and_load("ぴーたー", false, :escape_mode => :json)
176
+ end
177
+
178
+ def test_array
179
+ dump_and_load([], false)
180
+ dump_and_load([true, false], false)
181
+ dump_and_load(['a', 1, nil], false)
182
+ dump_and_load([[nil]], false)
183
+ dump_and_load([[nil], 58], false)
184
+ end
185
+
186
+ def test_array_deep
187
+ dump_and_load([1,[2,[3,[4,[5,[6,[7,[8,[9,[10,[11,[12,[13,[14,[15,[16,[17,[18,[19,[20]]]]]]]]]]]]]]]]]]]], false)
188
+ end
189
+
190
+ def test_deep_nest
191
+ begin
192
+ n = 10000
193
+ Oj.strict_load('[' * n + ']' * n)
194
+ rescue Exception => e
195
+ assert(false, e.message)
196
+ end
197
+ end
198
+
199
+ def test_hash
200
+ dump_and_load({}, false)
201
+ dump_and_load({ 'true' => true, 'false' => false}, false)
202
+ dump_and_load({ 'true' => true, 'array' => [], 'hash' => { }}, false)
203
+ end
204
+
205
+ def test_hash_deep
206
+ dump_and_load({'1' => {
207
+ '2' => {
208
+ '3' => {
209
+ '4' => {
210
+ '5' => {
211
+ '6' => {
212
+ '7' => {
213
+ '8' => {
214
+ '9' => {
215
+ '10' => {
216
+ '11' => {
217
+ '12' => {
218
+ '13' => {
219
+ '14' => {
220
+ '15' => {
221
+ '16' => {
222
+ '17' => {
223
+ '18' => {
224
+ '19' => {
225
+ '20' => {}}}}}}}}}}}}}}}}}}}}}, false)
226
+ end
227
+
228
+ def test_hash_escaped_key
229
+ json = %{{"a\nb":true,"c\td":false}}
230
+ obj = Oj.load(json)
231
+ assert_equal({"a\nb" => true, "c\td" => false}, obj)
232
+ end
233
+
234
+ def test_hash_non_string_key
235
+ assert_equal(%|{"1":true}|, Oj.dump({ 1 => true }, :indent => 0))
236
+ end
237
+
238
+ def test_bignum_object
239
+ dump_and_load(7 ** 55, false)
240
+ end
241
+
242
+ def test_bigdecimal
243
+ assert_equal('0.314159265358979323846e1', Oj.dump(BigDecimal('3.14159265358979323846'), bigdecimal_as_decimal: true).downcase())
244
+ assert_equal('"0.314159265358979323846e1"', Oj.dump(BigDecimal('3.14159265358979323846'), bigdecimal_as_decimal: false).downcase())
245
+ dump_and_load(BigDecimal('3.14159265358979323846'), false, :bigdecimal_load => true)
246
+ end
247
+
248
+ def test_object
249
+ obj = Jeez.new(true, 58)
250
+ Oj.dump(obj, :create_id => "^o", :use_to_json => false, :use_as_json => false, :use_to_hash => false)
251
+ dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
252
+ end
253
+
254
+ def test_object_to_json
255
+ obj = Jeez.new(true, 58)
256
+ json = Oj.dump(obj, :use_to_json => true, :use_as_json => false, :use_to_hash => false)
257
+ assert_equal(%|{"xx":true,"yy":58}|, json)
258
+ end
259
+
260
+ def test_object_as_json
261
+ obj = Jeez.new(true, 58)
262
+ json = Oj.dump(obj, :use_to_json => false, :use_as_json => true, :use_to_hash => false)
263
+ assert_equal(%|{"a":true,"b":58}|, json)
264
+ end
265
+
266
+ def test_object_to_hash
267
+ obj = Jeez.new(true, 58)
268
+ json = Oj.dump(obj, :use_to_json => false, :use_as_json => false, :use_to_hash => true)
269
+ assert_equal(%|{"b":true,"n":58}|, json)
270
+ end
271
+
272
+ def test_object_raw_json
273
+ obj = Jeez.new(true, 58)
274
+ json = Oj.dump(obj, :use_to_json => true, :use_as_json => false, :use_raw_json => true, :use_to_hash => false)
275
+ assert_equal(%|{"xxx":true,"yyy":58}|, json)
276
+ end
277
+
278
+ def test_raw_json_stringwriter
279
+ obj = Oj::StringWriter.new(:indent => 0)
280
+ obj.push_array()
281
+ obj.pop()
282
+ json = Oj.dump(obj, :use_raw_json => true)
283
+ assert_equal(%|[]|, json)
284
+ end
285
+
286
+ def test_as_raw_json_stringwriter
287
+ obj = Oj::StringWriter.new(:indent => 0)
288
+ obj.push_array()
289
+ obj.push_value(3)
290
+ obj.pop()
291
+ j = AsJson.new(1, obj)
292
+
293
+ json = Oj.dump(j, use_raw_json: true, use_as_json: true, indent: 2)
294
+ assert_equal(%|{
295
+ "a":1,
296
+ "b":[3]
297
+ }
298
+ |, json)
299
+
300
+ json = Oj.dump(j, use_raw_json: false, use_as_json: true, indent: 2)
301
+ assert_equal(%|{
302
+ "a":1,
303
+ "b":{}
304
+ }
305
+ |, json)
306
+ end
307
+
308
+ def test_rails_as_raw_json_stringwriter
309
+ obj = Oj::StringWriter.new(:indent => 0)
310
+ obj.push_array()
311
+ obj.push_value(3)
312
+ obj.pop()
313
+ j = AsRails.new(1, obj)
314
+ json = Oj.dump(j, mode: :rails, use_raw_json: true, indent: 2)
315
+ assert_equal(%|{
316
+ "a":1,
317
+ "b":{}
318
+ }
319
+ |, json)
320
+
321
+ Oj::Rails.optimize
322
+ json = Oj.dump(j, mode: :rails, use_raw_json: true, indent: 2)
323
+ Oj::Rails.deoptimize
324
+ assert_equal(%|{
325
+ "a":1,
326
+ "b":[3]
327
+ }
328
+ |, json)
329
+
330
+ end
331
+
332
+ def test_symbol
333
+ json = Oj.dump(:abc)
334
+ assert_equal('"abc"', json)
335
+ end
336
+
337
+ def test_class
338
+ assert_equal(%|"CustomJuice"|, Oj.dump(CustomJuice))
339
+ end
340
+
341
+ def test_module
342
+ assert_equal(%|"CustomJuice::TestModule"|, Oj.dump(TestModule))
343
+ end
344
+
345
+ def test_symbol_keys
346
+ json = %|{
347
+ "x":true,
348
+ "y":58,
349
+ "z": [1,2,3]
350
+ }
351
+ |
352
+ obj = Oj.load(json, :symbol_keys => true)
353
+ assert_equal({ :x => true, :y => 58, :z => [1, 2, 3]}, obj)
354
+ end
355
+
356
+ def test_double
357
+ json = %{{ "x": 1}{ "y": 2}}
358
+ results = []
359
+ Oj.load(json, :mode => :strict) { |x| results << x }
360
+
361
+ assert_equal([{ 'x' => 1 }, { 'y' => 2 }], results)
362
+ end
363
+
364
+ def test_circular_hash
365
+ h = { 'a' => 7 }
366
+ h['b'] = h
367
+ json = Oj.dump(h, :indent => 2, :circular => true)
368
+ assert_equal(%|{
369
+ "a":7,
370
+ "b":null
371
+ }
372
+ |, json)
373
+ end
374
+
375
+ def test_omit_nil
376
+ json = Oj.dump({'x' => {'a' => 1, 'b' => nil }, 'y' => nil}, :omit_nil => true)
377
+ assert_equal(%|{"x":{"a":1}}|, json)
378
+ end
379
+
380
+ def test_complex
381
+ obj = Complex(2, 9)
382
+ dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
383
+ end
384
+
385
+ def test_rational
386
+ obj = Rational(2, 9)
387
+ dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
388
+ end
389
+
390
+ def test_range
391
+ obj = 3..8
392
+ dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
393
+ end
394
+
395
+ def test_date
396
+ obj = Date.new(2017, 1, 5)
397
+ dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
398
+ end
399
+
400
+ def test_date_unix
401
+ obj = Date.new(2017, 1, 5)
402
+ json = Oj.dump(obj, :indent => 2, time_format: :unix)
403
+ assert_equal('1483574400.000000000', json)
404
+ end
405
+
406
+ def test_date_unix_zone
407
+ obj = Date.new(2017, 1, 5)
408
+ json = Oj.dump(obj, :indent => 2, time_format: :unix_zone)
409
+ assert_equal('1483574400.000000000', json)
410
+ end
411
+
412
+ def test_date_ruby
413
+ obj = Date.new(2017, 1, 5)
414
+ json = Oj.dump(obj, :indent => 2, time_format: :ruby)
415
+ assert_equal('"2017-01-05"', json)
416
+ end
417
+
418
+ def test_date_xmlschema
419
+ obj = Date.new(2017, 1, 5)
420
+ json = Oj.dump(obj, :indent => 2, time_format: :xmlschema)
421
+ assert_equal('"2017-01-05"', json)
422
+ end
423
+
424
+ def test_datetime
425
+ obj = DateTime.new(2017, 1, 5, 10, 20, 30)
426
+ dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
427
+ end
428
+
429
+ def test_datetime_unix
430
+ obj = DateTime.new(2017, 1, 5, 10, 20, 30, '-0500')
431
+ json = Oj.dump(obj, :indent => 2, time_format: :unix)
432
+ assert_equal('1483629630.000000000', json)
433
+ end
434
+
435
+ def test_datetime_unix_zone
436
+ # older versions seems to have issues getting the utc offset.
437
+ if '2.4' <= RUBY_VERSION
438
+ obj = DateTime.new(2017, 1, 5, 10, 20, 30, '-0500')
439
+ json = Oj.dump(obj, :indent => 2, time_format: :unix_zone)
440
+ assert_equal('1483629630.000000000e-18000', json)
441
+ end
442
+ end
443
+
444
+ def test_datetime_ruby
445
+ obj = DateTime.new(2017, 1, 5, 10, 20, 30, '-0500')
446
+ json = Oj.dump(obj, :indent => 2, time_format: :ruby)
447
+ assert_equal('"2017-01-05T10:20:30-05:00"', json)
448
+ end
449
+
450
+ def test_datetime_xmlschema
451
+ obj = DateTime.new(2017, 1, 5, 10, 20, 30, '-0500')
452
+ json = Oj.dump(obj, :indent => 2, time_format: :xmlschema)
453
+ assert_equal('"2017-01-05T10:20:30-05:00"', json)
454
+ end
455
+
456
+ def test_regexp
457
+ # this notation must be used to get an == match later
458
+ obj = /(?ix-m:^yes$)/
459
+ dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
460
+ end
461
+
462
+ def test_openstruct
463
+ obj = OpenStruct.new(:a => 1, 'b' => 2)
464
+ dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
465
+ end
466
+
467
+ def test_time
468
+ obj = Time.now()
469
+ dump_and_load(obj, false, :time_format => :unix, :create_id => "^o", :create_additions => true)
470
+ dump_and_load_inspect(obj, false, :time_format => :unix_zone, :create_id => "^o", :create_additions => true)
471
+ dump_and_load_inspect(obj, false, :time_format => :xmlschema, :create_id => "^o", :create_additions => true)
472
+ dump_and_load_inspect(obj, false, :time_format => :ruby, :create_id => "^o", :create_additions => true)
473
+ end
474
+
475
+ def dump_and_load(obj, trace=false, options={})
476
+ options = options.merge(:indent => 2, :mode => :custom)
477
+ json = Oj.dump(obj, options)
478
+ puts json if trace
479
+
480
+ loaded = Oj.load(json, options);
481
+ if obj.nil?
482
+ assert_nil(loaded)
483
+ else
484
+ assert_equal(obj, loaded)
485
+ end
486
+ loaded
487
+ end
488
+
489
+ def dump_and_load_inspect(obj, trace=false, options={})
490
+ options = options.merge(:indent => 2, :mode => :custom)
491
+ json = Oj.dump(obj, options)
492
+ puts json if trace
493
+
494
+ loaded = Oj.load(json, options);
495
+ if obj.nil?
496
+ assert_nil(loaded)
497
+ else
498
+ assert_equal(obj.inspect, loaded.inspect)
499
+ end
500
+ loaded
501
+ end
502
+
503
+ end