oj 3.10.7

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