oj 2.18.5 → 3.16.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1452 -0
  3. data/README.md +53 -221
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +54 -72
  6. data/ext/oj/cache.c +329 -0
  7. data/ext/oj/cache.h +22 -0
  8. data/ext/oj/cache8.c +61 -63
  9. data/ext/oj/cache8.h +12 -39
  10. data/ext/oj/circarray.c +38 -67
  11. data/ext/oj/circarray.h +16 -42
  12. data/ext/oj/code.c +214 -0
  13. data/ext/oj/code.h +40 -0
  14. data/ext/oj/compat.c +194 -110
  15. data/ext/oj/custom.c +1074 -0
  16. data/ext/oj/debug.c +126 -0
  17. data/ext/oj/dump.c +1276 -2494
  18. data/ext/oj/dump.h +110 -0
  19. data/ext/oj/dump_compat.c +897 -0
  20. data/ext/oj/dump_leaf.c +162 -0
  21. data/ext/oj/dump_object.c +710 -0
  22. data/ext/oj/dump_strict.c +399 -0
  23. data/ext/oj/encode.h +7 -42
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +28 -53
  26. data/ext/oj/err.h +49 -46
  27. data/ext/oj/extconf.rb +33 -32
  28. data/ext/oj/fast.c +1082 -1098
  29. data/ext/oj/intern.c +313 -0
  30. data/ext/oj/intern.h +22 -0
  31. data/ext/oj/mem.c +318 -0
  32. data/ext/oj/mem.h +53 -0
  33. data/ext/oj/mimic_json.c +919 -0
  34. data/ext/oj/object.c +545 -625
  35. data/ext/oj/odd.c +158 -168
  36. data/ext/oj/odd.h +32 -58
  37. data/ext/oj/oj.c +1727 -2080
  38. data/ext/oj/oj.h +334 -259
  39. data/ext/oj/parse.c +974 -753
  40. data/ext/oj/parse.h +97 -90
  41. data/ext/oj/parser.c +1600 -0
  42. data/ext/oj/parser.h +103 -0
  43. data/ext/oj/rails.c +1478 -0
  44. data/ext/oj/rails.h +18 -0
  45. data/ext/oj/reader.c +136 -163
  46. data/ext/oj/reader.h +76 -112
  47. data/ext/oj/resolve.c +45 -94
  48. data/ext/oj/resolve.h +7 -34
  49. data/ext/oj/rxclass.c +144 -0
  50. data/ext/oj/rxclass.h +26 -0
  51. data/ext/oj/saj.c +445 -511
  52. data/ext/oj/saj2.c +584 -0
  53. data/ext/oj/saj2.h +23 -0
  54. data/ext/oj/scp.c +82 -143
  55. data/ext/oj/simd.h +10 -0
  56. data/ext/oj/sparse.c +749 -644
  57. data/ext/oj/stream_writer.c +329 -0
  58. data/ext/oj/strict.c +114 -112
  59. data/ext/oj/string_writer.c +517 -0
  60. data/ext/oj/trace.c +72 -0
  61. data/ext/oj/trace.h +55 -0
  62. data/ext/oj/usual.c +1218 -0
  63. data/ext/oj/usual.h +69 -0
  64. data/ext/oj/util.c +136 -0
  65. data/ext/oj/util.h +20 -0
  66. data/ext/oj/val_stack.c +75 -72
  67. data/ext/oj/val_stack.h +94 -127
  68. data/ext/oj/validate.c +46 -0
  69. data/ext/oj/wab.c +586 -0
  70. data/lib/oj/active_support_helper.rb +1 -3
  71. data/lib/oj/bag.rb +8 -1
  72. data/lib/oj/easy_hash.rb +21 -13
  73. data/lib/oj/error.rb +10 -12
  74. data/lib/oj/json.rb +188 -0
  75. data/lib/oj/mimic.rb +165 -26
  76. data/lib/oj/saj.rb +20 -6
  77. data/lib/oj/schandler.rb +5 -4
  78. data/lib/oj/state.rb +135 -0
  79. data/lib/oj/version.rb +2 -3
  80. data/lib/oj.rb +3 -31
  81. data/pages/Advanced.md +22 -0
  82. data/pages/Compatibility.md +25 -0
  83. data/pages/Custom.md +23 -0
  84. data/pages/Encoding.md +65 -0
  85. data/pages/InstallOptions.md +20 -0
  86. data/pages/JsonGem.md +94 -0
  87. data/pages/Modes.md +161 -0
  88. data/pages/Options.md +337 -0
  89. data/pages/Parser.md +309 -0
  90. data/pages/Rails.md +167 -0
  91. data/pages/Security.md +20 -0
  92. data/pages/WAB.md +13 -0
  93. metadata +126 -163
  94. data/ext/oj/hash.c +0 -163
  95. data/ext/oj/hash.h +0 -46
  96. data/ext/oj/hash_test.c +0 -512
  97. data/test/_test_active.rb +0 -76
  98. data/test/_test_active_mimic.rb +0 -96
  99. data/test/_test_mimic_rails.rb +0 -126
  100. data/test/activesupport_datetime_test.rb +0 -23
  101. data/test/bug.rb +0 -51
  102. data/test/bug2.rb +0 -10
  103. data/test/bug3.rb +0 -46
  104. data/test/bug_fast.rb +0 -32
  105. data/test/bug_load.rb +0 -24
  106. data/test/crash.rb +0 -111
  107. data/test/curl/curl_oj.rb +0 -46
  108. data/test/curl/get_oj.rb +0 -24
  109. data/test/curl/just_curl.rb +0 -31
  110. data/test/curl/just_oj.rb +0 -51
  111. data/test/example.rb +0 -11
  112. data/test/files.rb +0 -29
  113. data/test/foo.rb +0 -24
  114. data/test/helper.rb +0 -27
  115. data/test/io.rb +0 -48
  116. data/test/isolated/shared.rb +0 -310
  117. data/test/isolated/test_mimic_after.rb +0 -13
  118. data/test/isolated/test_mimic_alone.rb +0 -12
  119. data/test/isolated/test_mimic_as_json.rb +0 -45
  120. data/test/isolated/test_mimic_before.rb +0 -13
  121. data/test/isolated/test_mimic_define.rb +0 -28
  122. data/test/isolated/test_mimic_rails_after.rb +0 -22
  123. data/test/isolated/test_mimic_rails_before.rb +0 -21
  124. data/test/isolated/test_mimic_rails_datetime.rb +0 -27
  125. data/test/isolated/test_mimic_redefine.rb +0 -15
  126. data/test/mod.rb +0 -16
  127. data/test/perf.rb +0 -107
  128. data/test/perf_compat.rb +0 -128
  129. data/test/perf_fast.rb +0 -164
  130. data/test/perf_file.rb +0 -64
  131. data/test/perf_object.rb +0 -138
  132. data/test/perf_saj.rb +0 -109
  133. data/test/perf_scp.rb +0 -151
  134. data/test/perf_simple.rb +0 -287
  135. data/test/perf_strict.rb +0 -128
  136. data/test/rails.rb +0 -50
  137. data/test/russian.rb +0 -18
  138. data/test/sample/change.rb +0 -14
  139. data/test/sample/dir.rb +0 -19
  140. data/test/sample/doc.rb +0 -36
  141. data/test/sample/file.rb +0 -48
  142. data/test/sample/group.rb +0 -16
  143. data/test/sample/hasprops.rb +0 -16
  144. data/test/sample/layer.rb +0 -12
  145. data/test/sample/line.rb +0 -20
  146. data/test/sample/oval.rb +0 -10
  147. data/test/sample/rect.rb +0 -10
  148. data/test/sample/shape.rb +0 -35
  149. data/test/sample/text.rb +0 -20
  150. data/test/sample.rb +0 -55
  151. data/test/sample_json.rb +0 -37
  152. data/test/struct.rb +0 -29
  153. data/test/test_compat.rb +0 -398
  154. data/test/test_debian.rb +0 -53
  155. data/test/test_fast.rb +0 -458
  156. data/test/test_file.rb +0 -245
  157. data/test/test_gc.rb +0 -49
  158. data/test/test_hash.rb +0 -29
  159. data/test/test_object.rb +0 -745
  160. data/test/test_saj.rb +0 -186
  161. data/test/test_scp.rb +0 -396
  162. data/test/test_serializer.rb +0 -59
  163. data/test/test_strict.rb +0 -254
  164. data/test/test_various.rb +0 -1383
  165. data/test/test_writer.rb +0 -308
  166. data/test/write_timebars.rb +0 -31
data/test/test_object.rb DELETED
@@ -1,745 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- $: << File.dirname(__FILE__)
5
-
6
- require 'helper'
7
-
8
- class ObjectJuice < Minitest::Test
9
- class Jeez
10
- attr_accessor :x, :y
11
-
12
- def initialize(x, y)
13
- @x = x
14
- @y = y
15
- end
16
-
17
- def eql?(o)
18
- self.class == o.class && @x == o.x && @y == o.y
19
- end
20
- alias == eql?
21
-
22
- def to_json(*a)
23
- %{{"json_class":"#{self.class}","x":#{@x},"y":#{@y}}}
24
- end
25
-
26
- def self.json_create(h)
27
- self.new(h['x'], h['y'])
28
- end
29
- end # Jeez
30
-
31
- module One
32
- module Two
33
- module Three
34
- class Deep
35
-
36
- def initialize()
37
- end
38
-
39
- def eql?(o)
40
- self.class == o.class
41
- end
42
- alias == eql?
43
-
44
- def to_hash()
45
- {'json_class' => "#{self.class.name}"}
46
- end
47
-
48
- def to_json(*a)
49
- %{{"json_class":"#{self.class.name}"}}
50
- end
51
-
52
- def self.json_create(h)
53
- self.new()
54
- end
55
- end # Deep
56
- end # Three
57
- end # Two
58
-
59
- class Stuck2 < Struct.new(:a, :b)
60
- end
61
-
62
- end # One
63
-
64
- class Stuck < Struct.new(:a, :b)
65
- end
66
-
67
- class Strung < String
68
-
69
- def initialize(str, safe)
70
- super(str)
71
- @safe = safe
72
- end
73
-
74
- def safe?()
75
- @safe
76
- end
77
-
78
- def self.create(str, safe)
79
- new(str, safe)
80
- end
81
-
82
- def eql?(o)
83
- super && self.class == o.class && @safe == o.safe?
84
- end
85
- alias == eql?
86
-
87
- def inspect()
88
- return super + '(' + @safe + ')'
89
- end
90
- end
91
-
92
- class AutoStrung < String
93
- attr_accessor :safe
94
-
95
- def initialize(str, safe)
96
- super(str)
97
- @safe = safe
98
- end
99
-
100
- def eql?(o)
101
- self.class == o.class && super(o) && @safe == o.safe
102
- end
103
- alias == eql?
104
- end
105
-
106
- class AutoArray < Array
107
- attr_accessor :safe
108
-
109
- def initialize(a, safe)
110
- super(a)
111
- @safe = safe
112
- end
113
-
114
- def eql?(o)
115
- self.class == o.class && super(o) && @safe == o.safe
116
- end
117
- alias == eql?
118
- end
119
-
120
- class AutoHash < Hash
121
- attr_accessor :safe
122
-
123
- def initialize(h, safe)
124
- super(h)
125
- @safe = safe
126
- end
127
-
128
- def eql?(o)
129
- self.class == o.class && super(o) && @safe == o.safe
130
- end
131
- alias == eql?
132
- end
133
-
134
- class Raw
135
- attr_accessor :json
136
-
137
- def initialize(j)
138
- @json = j
139
- end
140
-
141
- def to_json(*a)
142
- @json
143
- end
144
-
145
- def self.create(h)
146
- h
147
- end
148
- end # Raw
149
-
150
- module Ichi
151
- module Ni
152
- def self.direct(h)
153
- h
154
- end
155
-
156
- module San
157
- class Shi
158
-
159
- attr_accessor :hash
160
-
161
- def initialize(h)
162
- @hash = h
163
- end
164
-
165
- def dump()
166
- @hash
167
- end
168
-
169
- end # Shi
170
- end # San
171
- end # Ni
172
- end # Ichi
173
-
174
- def setup
175
- @default_options = Oj.default_options
176
- end
177
-
178
- def teardown
179
- Oj.default_options = @default_options
180
- end
181
-
182
- def test_nil
183
- dump_and_load(nil, false)
184
- end
185
-
186
- def test_true
187
- dump_and_load(true, false)
188
- end
189
-
190
- def test_false
191
- dump_and_load(false, false)
192
- end
193
-
194
- def test_fixnum
195
- dump_and_load(0, false)
196
- dump_and_load(12345, false)
197
- dump_and_load(-54321, false)
198
- dump_and_load(1, false)
199
- end
200
-
201
- def test_float
202
- dump_and_load(0.0, false)
203
- dump_and_load(12345.6789, false)
204
- dump_and_load(70.35, false)
205
- dump_and_load(-54321.012, false)
206
- dump_and_load(1.7775, false)
207
- dump_and_load(2.5024, false)
208
- dump_and_load(2.48e16, false)
209
- dump_and_load(2.48e100 * 1.0e10, false)
210
- dump_and_load(-2.48e100 * 1.0e10, false)
211
- end
212
-
213
- def test_string
214
- dump_and_load('', false)
215
- dump_and_load('abc', false)
216
- dump_and_load("abc\ndef", false)
217
- dump_and_load("a\u0041", false)
218
- end
219
-
220
- def test_symbol
221
- dump_and_load(:abc, false)
222
- dump_and_load(":abc", false)
223
- end
224
-
225
- def test_encode
226
- opts = Oj.default_options
227
- Oj.default_options = { :ascii_only => false }
228
- dump_and_load("ぴーたー", false)
229
- Oj.default_options = { :ascii_only => true }
230
- json = Oj.dump("ぴーたー")
231
- assert_equal(%{"\\u3074\\u30fc\\u305f\\u30fc"}, json)
232
- dump_and_load("ぴーたー", false)
233
- Oj.default_options = opts
234
- end
235
-
236
- def test_unicode
237
- # hits the 3 normal ranges and one extended surrogate pair
238
- json = %{"\\u019f\\u05e9\\u3074\\ud834\\udd1e"}
239
- obj = Oj.load(json)
240
- json2 = Oj.dump(obj, :ascii_only => true)
241
- assert_equal(json, json2)
242
- end
243
-
244
- def test_array
245
- dump_and_load([], false)
246
- dump_and_load([true, false], false)
247
- dump_and_load(['a', 1, nil], false)
248
- dump_and_load([[nil]], false)
249
- dump_and_load([[nil], 58], false)
250
- end
251
-
252
- def test_array_deep
253
- dump_and_load([1,[2,[3,[4,[5,[6,[7,[8,[9,[10,[11,[12,[13,[14,[15,[16,[17,[18,[19,[20]]]]]]]]]]]]]]]]]]]], false)
254
- end
255
-
256
- # Hash
257
- def test_hash
258
- dump_and_load({}, false)
259
- dump_and_load({ 'true' => true, 'false' => false}, false)
260
- dump_and_load({ 'true' => true, 'array' => [], 'hash' => { }}, false)
261
- end
262
-
263
- def test_hash_deep
264
- dump_and_load({'1' => {
265
- '2' => {
266
- '3' => {
267
- '4' => {
268
- '5' => {
269
- '6' => {
270
- '7' => {
271
- '8' => {
272
- '9' => {
273
- '10' => {
274
- '11' => {
275
- '12' => {
276
- '13' => {
277
- '14' => {
278
- '15' => {
279
- '16' => {
280
- '17' => {
281
- '18' => {
282
- '19' => {
283
- '20' => {}}}}}}}}}}}}}}}}}}}}}, false)
284
- end
285
-
286
- def test_hash_escaped_key
287
- json = %{{"a\nb":true,"c\td":false}}
288
- obj = Oj.object_load(json)
289
- assert_equal({"a\nb" => true, "c\td" => false}, obj)
290
- end
291
-
292
- def test_bignum_object
293
- dump_and_load(7 ** 55, false)
294
- end
295
-
296
- # BigDecimal
297
- def test_bigdecimal_object
298
- dump_and_load(BigDecimal.new('3.14159265358979323846'), false)
299
- end
300
-
301
- def test_bigdecimal_load
302
- orig = BigDecimal.new('80.51')
303
- json = Oj.dump(orig, :mode => :object, :bigdecimal_as_decimal => true)
304
- bg = Oj.load(json, :mode => :object, :bigdecimal_load => true)
305
- assert_equal(BigDecimal, bg.class)
306
- assert_equal(orig, bg)
307
- end
308
-
309
- # Stream IO
310
- def test_io_string
311
- json = %{{
312
- "x":true,
313
- "y":58,
314
- "z": [1,2,3]
315
- }
316
- }
317
- input = StringIO.new(json)
318
- obj = Oj.object_load(input)
319
- assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
320
- end
321
-
322
- def test_io_file
323
- filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
324
- File.open(filename, 'w') { |f| f.write(%{{
325
- "x":true,
326
- "y":58,
327
- "z": [1,2,3]
328
- }
329
- }) }
330
- f = File.new(filename)
331
- obj = Oj.object_load(f)
332
- f.close()
333
- assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
334
- end
335
-
336
- # symbol_keys option
337
- def test_symbol_keys
338
- json = %{{
339
- "x":true,
340
- "y":58,
341
- "z": [1,2,3]
342
- }
343
- }
344
- obj = Oj.object_load(json, :symbol_keys => true)
345
- assert_equal({ :x => true, :y => 58, :z => [1, 2, 3]}, obj)
346
- end
347
-
348
- # comments
349
- def test_comment_slash
350
- json = %{{
351
- "x":true,//three
352
- "y":58,
353
- "z": [1,2,
354
- 3 // six
355
- ]}
356
- }
357
- obj = Oj.object_load(json)
358
- assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
359
- end
360
-
361
- def test_comment_c
362
- json = %{{
363
- "x"/*one*/:/*two*/true,
364
- "y":58,
365
- "z": [1,2,3]}
366
- }
367
- obj = Oj.object_load(json)
368
- assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
369
- end
370
-
371
- def test_comment
372
- json = %{{
373
- "x"/*one*/:/*two*/true,//three
374
- "y":58/*four*/,
375
- "z": [1,2/*five*/,
376
- 3 // six
377
- ]
378
- }
379
- }
380
- obj = Oj.object_load(json)
381
- assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
382
- end
383
-
384
- def test_json_module_object
385
- obj = One::Two::Three::Deep.new()
386
- dump_and_load(obj, false)
387
- end
388
-
389
- def test_xml_time
390
- if RUBY_VERSION.start_with?('1.8')
391
- t = Time.parse('2015-01-05T21:37:07.123456789-08:00')
392
- else
393
- t = Time.new(2015, 1, 5, 21, 37, 7.123456789, -8 * 3600)
394
- end
395
- # The fractional seconds are not always recreated exactly which causes a
396
- # mismatch so instead the seconds, nsecs, and gmt_offset are checked
397
- # separately along with utc.
398
- json = Oj.dump(t, :mode => :object, :time_format => :xmlschema)
399
- #puts "*** json for test_xml_time '#{json}'"
400
- loaded = Oj.object_load(json);
401
- assert_equal(t.tv_sec, loaded.tv_sec)
402
- if t.respond_to?(:tv_nsec)
403
- assert_equal(t.tv_nsec, loaded.tv_nsec)
404
- else
405
- assert_equal(t.tv_usec, loaded.tv_usec)
406
- end
407
- assert_equal(t.utc?, loaded.utc?)
408
- assert_equal(t.utc_offset, loaded.utc_offset)
409
- end
410
-
411
- def test_xml_time_utc
412
- if RUBY_VERSION.start_with?('1.8')
413
- t = Time.parse('2015-01-05T21:37:07.123456789Z')
414
- else
415
- t = Time.utc(2015, 1, 5, 21, 37, 7.123456789)
416
- end
417
- # The fractional seconds are not always recreated exactly which causes a
418
- # mismatch so instead the seconds, nsecs, and gmt_offset are checked
419
- # separately along with utc.
420
- json = Oj.dump(t, :mode => :object, :time_format => :xmlschema)
421
- loaded = Oj.object_load(json);
422
- assert_equal(t.tv_sec, loaded.tv_sec)
423
- if t.respond_to?(:tv_nsec)
424
- assert_equal(t.tv_nsec, loaded.tv_nsec)
425
- else
426
- assert_equal(t.tv_usec, loaded.tv_usec)
427
- end
428
- assert_equal(t.utc?, loaded.utc?)
429
- assert_equal(t.utc_offset, loaded.utc_offset)
430
- end
431
-
432
- def test_ruby_time
433
- if RUBY_VERSION.start_with?('1.8')
434
- t = Time.parse('2015-01-05T21:37:07.123456789-08:00')
435
- else
436
- t = Time.new(2015, 1, 5, 21, 37, 7.123456789, -8 * 3600)
437
- end
438
- # The fractional seconds are not always recreated exactly which causes a
439
- # mismatch so instead the seconds, nsecs, and gmt_offset are checked
440
- # separately along with utc.
441
- json = Oj.dump(t, :mode => :object, :time_format => :ruby)
442
- #puts "*** json for test_ruby_time '#{json}'"
443
- loaded = Oj.object_load(json);
444
- assert_equal(t.tv_sec, loaded.tv_sec)
445
- if t.respond_to?(:tv_nsec)
446
- assert_equal(t.tv_nsec, loaded.tv_nsec)
447
- else
448
- assert_equal(t.tv_usec, loaded.tv_usec)
449
- end
450
- assert_equal(t.utc?, loaded.utc?)
451
- assert_equal(t.utc_offset, loaded.utc_offset)
452
- end
453
-
454
- def test_ruby_time_12345
455
- if RUBY_VERSION.start_with?('1.8')
456
- t = Time.parse('2015-01-05T21:37:07.123456789+03:25')
457
- else
458
- t = Time.new(2015, 1, 5, 21, 37, 7.123456789, 12345/60*60)
459
- end
460
- # The fractional seconds are not always recreated exactly which causes a
461
- # mismatch so instead the seconds, nsecs, and gmt_offset are checked
462
- # separately along with utc.
463
- json = Oj.dump(t, :mode => :object, :time_format => :ruby)
464
- #puts "*** json for test_ruby_time '#{json}'"
465
- loaded = Oj.object_load(json);
466
- #puts "*** loaded: #{loaded}"
467
- assert_equal(t.tv_sec, loaded.tv_sec)
468
- if t.respond_to?(:tv_nsec)
469
- assert_equal(t.tv_nsec, loaded.tv_nsec)
470
- else
471
- assert_equal(t.tv_usec, loaded.tv_usec)
472
- end
473
- assert_equal(t.utc?, loaded.utc?)
474
- assert_equal(t.utc_offset, loaded.utc_offset)
475
- end
476
-
477
- def test_ruby_time_utc
478
- if RUBY_VERSION.start_with?('1.8')
479
- t = Time.parse('2015-01-05T21:37:07.123456789Z')
480
- else
481
- t = Time.utc(2015, 1, 5, 21, 37, 7.123456789)
482
- end
483
- # The fractional seconds are not always recreated exactly which causes a
484
- # mismatch so instead the seconds, nsecs, and gmt_offset are checked
485
- # separately along with utc.
486
- json = Oj.dump(t, :mode => :object, :time_format => :ruby)
487
- #puts json
488
- loaded = Oj.object_load(json);
489
- assert_equal(t.tv_sec, loaded.tv_sec)
490
- if t.respond_to?(:tv_nsec)
491
- assert_equal(t.tv_nsec, loaded.tv_nsec)
492
- else
493
- assert_equal(t.tv_usec, loaded.tv_usec)
494
- end
495
- assert_equal(t.utc?, loaded.utc?)
496
- assert_equal(t.utc_offset, loaded.utc_offset)
497
- end
498
-
499
- def test_time_early
500
- if RUBY_VERSION.start_with?('1.8')
501
- t = Time.parse('1954-01-05T21:37:07.123456789-08:00')
502
- else
503
- t = Time.new(1954, 1, 5, 21, 37, 7.123456789, -8 * 3600)
504
- end
505
- # The fractional seconds are not always recreated exactly which causes a
506
- # mismatch so instead the seconds, nsecs, and gmt_offset are checked
507
- # separately along with utc.
508
- json = Oj.dump(t, :mode => :object, :time_format => :unix_zone)
509
- #puts json
510
- loaded = Oj.object_load(json);
511
- assert_equal(t.tv_sec, loaded.tv_sec)
512
- if t.respond_to?(:tv_nsec)
513
- assert_equal(t.tv_nsec, loaded.tv_nsec)
514
- else
515
- assert_equal(t.tv_usec, loaded.tv_usec)
516
- end
517
- assert_equal(t.utc?, loaded.utc?)
518
- assert_equal(t.utc_offset, loaded.utc_offset)
519
- end
520
-
521
- def test_time_unix_zone
522
- if RUBY_VERSION.start_with?('1.8')
523
- t = Time.parse('2015-01-05T21:37:07.123456789-08:00')
524
- else
525
- t = Time.new(2015, 1, 5, 21, 37, 7.123456789, -8 * 3600)
526
- end
527
- # The fractional seconds are not always recreated exactly which causes a
528
- # mismatch so instead the seconds, nsecs, and gmt_offset are checked
529
- # separately along with utc.
530
- json = Oj.dump(t, :mode => :object, :time_format => :unix_zone)
531
- #puts json
532
- loaded = Oj.object_load(json);
533
- assert_equal(t.tv_sec, loaded.tv_sec)
534
- if t.respond_to?(:tv_nsec)
535
- assert_equal(t.tv_nsec, loaded.tv_nsec)
536
- else
537
- assert_equal(t.tv_usec, loaded.tv_usec)
538
- end
539
- assert_equal(t.utc?, loaded.utc?)
540
- assert_equal(t.utc_offset, loaded.utc_offset)
541
- end
542
-
543
- unless RUBY_VERSION.start_with?('1.8')
544
- def test_time_unix_zone_12345
545
- t = Time.new(2015, 1, 5, 21, 37, 7.123456789, 12345)
546
- # The fractional seconds are not always recreated exactly which causes a
547
- # mismatch so instead the seconds, nsecs, and gmt_offset are checked
548
- # separately along with utc.
549
- json = Oj.dump(t, :mode => :object, :time_format => :unix_zone)
550
- #puts json
551
- loaded = Oj.object_load(json);
552
- assert_equal(t.tv_sec, loaded.tv_sec)
553
- if t.respond_to?(:tv_nsec)
554
- assert_equal(t.tv_nsec, loaded.tv_nsec)
555
- else
556
- assert_equal(t.tv_usec, loaded.tv_usec)
557
- end
558
- assert_equal(t.utc?, loaded.utc?)
559
- assert_equal(t.utc_offset, loaded.utc_offset)
560
- end
561
- end
562
-
563
- def test_time_unix_zone_utc
564
- if RUBY_VERSION.start_with?('1.8')
565
- t = Time.parse('2015-01-05T21:37:07.123456789Z')
566
- else
567
- t = Time.utc(2015, 1, 5, 21, 37, 7.123456789)
568
- end
569
- # The fractional seconds are not always recreated exactly which causes a
570
- # mismatch so instead the seconds, nsecs, and gmt_offset are checked
571
- # separately along with utc.
572
- json = Oj.dump(t, :mode => :object, :time_format => :unix_zone)
573
- #puts json
574
- loaded = Oj.object_load(json);
575
- assert_equal(t.tv_sec, loaded.tv_sec)
576
- if t.respond_to?(:tv_nsec)
577
- assert_equal(t.tv_nsec, loaded.tv_nsec)
578
- else
579
- assert_equal(t.tv_usec, loaded.tv_usec)
580
- end
581
- assert_equal(t.utc?, loaded.utc?)
582
- assert_equal(t.utc_offset, loaded.utc_offset)
583
- end
584
-
585
- def test_json_object
586
- obj = Jeez.new(true, 58)
587
- dump_and_load(obj, false)
588
- end
589
-
590
- def test_json_object_create_deep
591
- obj = One::Two::Three::Deep.new()
592
- dump_and_load(obj, false)
593
- end
594
-
595
- def test_json_object_bad
596
- json = %{{"^o":"Junk","x":true}}
597
- begin
598
- Oj.object_load(json)
599
- rescue Exception => e
600
- assert_equal("Oj::ParseError", e.class().name)
601
- return
602
- end
603
- assert(false, "*** expected an exception")
604
- end
605
-
606
- def test_json_object_not_hat_hash
607
- json = %{{"^#x":[1,2]}}
608
- h = Oj.object_load(json)
609
- assert_equal({1 => 2}, h);
610
-
611
- json = %{{"~#x":[1,2]}}
612
- h = Oj.object_load(json)
613
- assert_equal({'~#x' => [1,2]}, h);
614
- end
615
-
616
- def test_json_struct
617
- obj = Stuck.new(false, 7)
618
- dump_and_load(obj, false)
619
- end
620
-
621
- def test_json_struct2
622
- obj = One::Stuck2.new(false, 7)
623
- dump_and_load(obj, false)
624
- end
625
-
626
- def test_json_anonymous_struct
627
- s = Struct.new(:x, :y)
628
- obj = s.new(1, 2)
629
- json = Oj.dump(obj, :indent => 2, :mode => :object)
630
- #puts json
631
- loaded = Oj.object_load(json);
632
- assert_equal(obj.members, loaded.members)
633
- assert_equal(obj.values, loaded.values)
634
- end
635
-
636
- def test_json_non_str_hash
637
- obj = { 59 => "young", false => true }
638
- dump_and_load(obj, false)
639
- end
640
-
641
- def test_mixed_hash_object
642
- Oj.default_options = { :mode => :object }
643
- json = Oj.dump({ 1 => true, 'nil' => nil, :sim => 4 })
644
- h = Oj.object_load(json)
645
- assert_equal({ 1 => true, 'nil' => nil, :sim => 4 }, h)
646
- end
647
-
648
- def test_circular_hash
649
- h = { 'a' => 7 }
650
- h['b'] = h
651
- json = Oj.dump(h, :mode => :object, :indent => 2, :circular => true)
652
- h2 = Oj.object_load(json, :circular => true)
653
- assert_equal(h2['b'].__id__, h2.__id__)
654
- end
655
-
656
- def test_circular_array
657
- a = [7]
658
- a << a
659
- json = Oj.dump(a, :mode => :object, :indent => 2, :circular => true)
660
- a2 = Oj.object_load(json, :circular => true)
661
- assert_equal(a2[1].__id__, a2.__id__)
662
- end
663
-
664
- def test_circular_object
665
- obj = Jeez.new(nil, 58)
666
- obj.x = obj
667
- json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true)
668
- obj2 = Oj.object_load(json, :circular => true)
669
- assert_equal(obj2.x.__id__, obj2.__id__)
670
- end
671
-
672
- def test_circular
673
- h = { 'a' => 7 }
674
- obj = Jeez.new(h, 58)
675
- obj.x['b'] = obj
676
- json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true)
677
- Oj.object_load(json, :circular => true)
678
- assert_equal(obj.x.__id__, h.__id__)
679
- assert_equal(h['b'].__id__, obj.__id__)
680
- end
681
-
682
- def test_odd_date
683
- dump_and_load(Date.new(2012, 6, 19), false)
684
- end
685
-
686
- def test_odd_datetime
687
- dump_and_load(DateTime.new(2012, 6, 19, 13, 5, Rational(4, 3)), false)
688
- dump_and_load(DateTime.new(2012, 6, 19, 13, 5, Rational(7123456789, 1000000000)), false)
689
- end
690
-
691
- def test_odd_string
692
- Oj.register_odd(Strung, Strung, :create, :to_s, 'safe?')
693
- s = Strung.new("Pete", true)
694
- dump_and_load(s, false)
695
- end
696
-
697
- def test_odd_date_replaced
698
- Oj.register_odd(Date, Date, :jd, :jd)
699
- json = Oj.dump(Date.new(2015, 3, 7), :mode => :object)
700
- assert_equal(%|{"^O":"Date","jd":2457089}|, json)
701
- dump_and_load(Date.new(2012, 6, 19), false)
702
- end
703
-
704
- def test_odd_raw
705
- Oj.register_odd_raw(Raw, Raw, :create, :to_json)
706
- json = Oj.dump(Raw.new(%|{"a":1}|), :mode => :object)
707
- assert_equal(%|{"^O":"ObjectJuice::Raw","to_json":{"a":1}}|, json)
708
- h = Oj.load(json, :mode => :object)
709
- assert_equal({'a' => 1}, h)
710
- end
711
-
712
- def test_odd_mod
713
- Oj.register_odd(Ichi::Ni, Ichi::Ni, :direct, :dump)
714
- json = Oj.dump(Ichi::Ni::San::Shi.new({'a' => 1}), :mode => :object)
715
- assert_equal(%|{"^O":"ObjectJuice::Ichi::Ni::San::Shi","dump":{"a":1}}|, json)
716
- h = Oj.load(json, :mode => :object)
717
- assert_equal({'a' => 1}, h)
718
- end
719
-
720
- def test_auto_string
721
- s = AutoStrung.new("Pete", true)
722
- dump_and_load(s, false)
723
- end
724
-
725
- def test_auto_array
726
- a = AutoArray.new([1, 'abc', nil], true)
727
- dump_and_load(a, false)
728
- end
729
-
730
- def test_auto_hash
731
- h = AutoHash.new(nil, true)
732
- h['a'] = 1
733
- h['b'] = 2
734
- dump_and_load(h, false)
735
- end
736
-
737
- def dump_and_load(obj, trace=false)
738
- json = Oj.dump(obj, :indent => 2, :mode => :object)
739
- puts json if trace
740
- loaded = Oj.object_load(json);
741
- assert_equal(obj, loaded)
742
- loaded
743
- end
744
-
745
- end