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_fast.rb DELETED
@@ -1,458 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- $: << File.dirname(__FILE__)
5
-
6
- require 'helper'
7
-
8
- $json1 = %|{
9
- "array": [
10
- {
11
- "num" : 3,
12
- "string": "message",
13
- "hash" : {
14
- "h2" : {
15
- "a" : [ 1, 2, 3 ]
16
- }
17
- }
18
- }
19
- ],
20
- "boolean" : true
21
- }|
22
-
23
- class DocTest < Minitest::Test
24
- def setup
25
- @default_options = Oj.default_options
26
- end
27
-
28
- def teardown
29
- Oj.default_options = @default_options
30
- end
31
-
32
- def test_nil
33
- json = %{null}
34
- Oj::Doc.open(json) do |doc|
35
- assert_equal(NilClass, doc.type)
36
- assert_equal(nil, doc.fetch())
37
- end
38
- end
39
-
40
- def test_true
41
- json = %{true}
42
- Oj::Doc.open(json) do |doc|
43
- assert_equal(TrueClass, doc.type)
44
- assert_equal(true, doc.fetch())
45
- end
46
- end
47
-
48
- def test_false
49
- json = %{false}
50
- Oj::Doc.open(json) do |doc|
51
- assert_equal(FalseClass, doc.type)
52
- assert_equal(false, doc.fetch())
53
- end
54
- end
55
-
56
- def test_string
57
- json = %{"a string"}
58
- Oj::Doc.open(json) do |doc|
59
- assert_equal(String, doc.type)
60
- assert_equal('a string', doc.fetch())
61
- end
62
- end
63
-
64
- def test_encoding
65
- json = %{"ぴーたー"}
66
- Oj::Doc.open(json) do |doc|
67
- assert_equal(String, doc.type)
68
- assert_equal("ぴーたー", doc.fetch())
69
- end
70
- end
71
-
72
- def test_encoding_escaped
73
- json = %{"\\u3074\\u30fc\\u305f\\u30fc"}
74
- Oj::Doc.open(json) do |doc|
75
- assert_equal(String, doc.type)
76
- assert_equal("ぴーたー", doc.fetch())
77
- end
78
- end
79
-
80
- def test_fixnum
81
- json = %{12345}
82
- Oj::Doc.open(json) do |doc|
83
- assert_equal(Fixnum, doc.type)
84
- assert_equal(12345, doc.fetch())
85
- end
86
- end
87
-
88
- def test_float
89
- json = %{12345.6789}
90
- Oj::Doc.open(json) do |doc|
91
- assert_equal(Float, doc.type)
92
- assert_equal(12345.6789, doc.fetch())
93
- end
94
- end
95
-
96
- def test_float_exp
97
- json = %{12345.6789e7}
98
- Oj::Doc.open(json) do |doc|
99
- assert_equal(Float, doc.type)
100
- #assert_equal(12345.6789e7, doc.fetch())
101
- assert_equal(12345.6789e7.to_i, doc.fetch().to_i)
102
- end
103
- end
104
-
105
- def test_array_empty
106
- json = %{[]}
107
- Oj::Doc.open(json) do |doc|
108
- assert_equal(Array, doc.type)
109
- assert_equal([], doc.fetch())
110
- end
111
- end
112
-
113
- def test_array
114
- json = %{[true,false]}
115
- Oj::Doc.open(json) do |doc|
116
- assert_equal(Array, doc.type)
117
- assert_equal([true, false], doc.fetch())
118
- end
119
- end
120
-
121
- def test_hash_empty
122
- json = %{{}}
123
- Oj::Doc.open(json) do |doc|
124
- assert_equal(Hash, doc.type)
125
- assert_equal({}, doc.fetch())
126
- end
127
- end
128
-
129
- def test_hash
130
- json = %{{"one":true,"two":false}}
131
- Oj::Doc.open(json) do |doc|
132
- assert_equal(Hash, doc.type)
133
- assert_equal({'one' => true, 'two' => false}, doc.fetch())
134
- end
135
- end
136
-
137
- # move() and where?()
138
- def test_move_hash
139
- json = %{{"one":{"two":false}}}
140
- Oj::Doc.open(json) do |doc|
141
- doc.move('/one')
142
- assert_equal('/one', doc.where?)
143
- doc.move('/one/two')
144
- assert_equal('/one/two', doc.where?)
145
- end
146
- end
147
-
148
- def test_move_array
149
- json = %{[1,[2,true]]}
150
- Oj::Doc.open(json) do |doc|
151
- doc.move('/1')
152
- assert_equal('/1', doc.where?)
153
- doc.move('/2/1')
154
- assert_equal('/2/1', doc.where?)
155
- end
156
- end
157
-
158
- def test_move
159
- Oj::Doc.open($json1) do |doc|
160
- [ '/',
161
- '/array',
162
- '/boolean',
163
- '/array/1/hash/h2/a/3',
164
- ].each do |p|
165
- doc.move(p)
166
- assert_equal(p, doc.where?)
167
- end
168
- begin
169
- doc.move('/array/x')
170
- rescue Exception
171
- assert_equal('/', doc.where?)
172
- assert(true)
173
- end
174
- end
175
- end
176
-
177
- def test_move_slash
178
- Oj::Doc.open(%|{"top":{"a/b":3}}|) do |doc|
179
- doc.move('top/a\/b')
180
- assert_equal('/top/a\/b', doc.where?)
181
- end
182
- end
183
-
184
- def test_fetch_slash
185
- Oj::Doc.open(%|{"a/b":3}|) do |doc|
186
- x = doc.fetch('a\/b')
187
- assert_equal(3, x)
188
- end
189
- end
190
-
191
- def test_move_relative
192
- Oj::Doc.open($json1) do |doc|
193
- [['/', 'array', '/array'],
194
- ['/array', '1/num', '/array/1/num'],
195
- ['/array/1/hash', 'h2/a', '/array/1/hash/h2/a'],
196
- ['/array/1', 'hash/h2/a/2', '/array/1/hash/h2/a/2'],
197
- ['/array/1/hash', '../string', '/array/1/string'],
198
- ['/array/1/hash', '..', '/array/1'],
199
- ].each do |start,path,where|
200
- doc.move(start)
201
- doc.move(path)
202
- assert_equal(where, doc.where?)
203
- end
204
- end
205
- end
206
-
207
- def test_type
208
- Oj::Doc.open($json1) do |doc|
209
- [['/', Hash],
210
- ['/array', Array],
211
- ['/array/1', Hash],
212
- ['/array/1/num', Fixnum],
213
- ['/array/1/string', String],
214
- ['/array/1/hash/h2/a', Array],
215
- ['/array/1/hash/../num', Fixnum],
216
- ['/array/1/hash/../..', Array],
217
- ].each do |path,type|
218
- assert_equal(type, doc.type(path))
219
- end
220
- end
221
- end
222
-
223
- def test_local_key
224
- Oj::Doc.open($json1) do |doc|
225
- [['/', nil],
226
- ['/array', 'array'],
227
- ['/array/1', 1],
228
- ['/array/1/num', 'num'],
229
- ['/array/1/string', 'string'],
230
- ['/array/1/hash/h2/a', 'a'],
231
- ['/array/1/hash/../num', 'num'],
232
- ['/array/1/hash/..', 1],
233
- ['/array/1/hash/../..', 'array'],
234
- ].each do |path,key|
235
- doc.move(path)
236
- assert_equal(key, doc.local_key())
237
- end
238
- end
239
- end
240
-
241
- def test_fetch_move
242
- Oj::Doc.open($json1) do |doc|
243
- [['/array/1/num', 3],
244
- ['/array/1/string', 'message'],
245
- ['/array/1/hash/h2/a', [1, 2, 3]],
246
- ['/array/1/hash/../num', 3],
247
- ['/array/1/hash/..', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
248
- ['/array/1/hash/../..', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
249
- ['/array/1', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
250
- ['/array', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
251
- ['/', {'array' => [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}], 'boolean' => true}],
252
- ].each do |path,val|
253
- doc.move(path)
254
- assert_equal(val, doc.fetch())
255
- end
256
- end
257
- end
258
-
259
- def test_fetch_path
260
- Oj::Doc.open($json1) do |doc|
261
- [['/array/1/num', 3],
262
- ['/array/1/string', 'message'],
263
- ['/array/1/hash/h2/a', [1, 2, 3]],
264
- ['/array/1/hash/../num', 3],
265
- ['/array/1/hash/..', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
266
- ['/array/1/hash/../..', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
267
- ['/array/1', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
268
- ['/array', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
269
- ['/', {'array' => [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}], 'boolean' => true}],
270
- ].each do |path,val|
271
- assert_equal(val, doc.fetch(path))
272
- end
273
- end
274
- end
275
-
276
- def test_move_fetch_path
277
- Oj::Doc.open($json1) do |doc|
278
- [['/array/1', 'num', 3],
279
- ['/array/1', 'string', 'message'],
280
- ['/array/1/hash', 'h2/a', [1, 2, 3]],
281
- ].each do |path,fetch_path,val|
282
- doc.move(path)
283
- assert_equal(val, doc.fetch(fetch_path))
284
- end
285
- end
286
- end
287
-
288
- def test_home
289
- Oj::Doc.open($json1) do |doc|
290
- doc.move('/array/1/num')
291
- doc.home()
292
- assert_equal('/', doc.where?)
293
- end
294
- end
295
-
296
- def test_each_value_root
297
- Oj::Doc.open($json1) do |doc|
298
- values = []
299
- doc.each_value() { |v| values << v.to_s }
300
- assert_equal(['1', '2', '3', '3', 'message', 'true'], values.sort)
301
- end
302
- end
303
-
304
- def test_each_value_move
305
- Oj::Doc.open($json1) do |doc|
306
- doc.move('/array/1/hash')
307
- values = []
308
- doc.each_value() { |v| values << v.to_s }
309
- assert_equal(['1', '2', '3'], values.sort)
310
- end
311
- end
312
-
313
- def test_each_value_path
314
- Oj::Doc.open($json1) do |doc|
315
- values = []
316
- doc.each_value('/array/1/hash') { |v| values << v.to_s }
317
- assert_equal(['1', '2', '3'], values.sort)
318
- end
319
- end
320
-
321
- def test_each_child_move
322
- Oj::Doc.open($json1) do |doc|
323
- locations = []
324
- doc.move('/array/1/hash/h2/a')
325
- doc.each_child() { |d| locations << d.where? }
326
- assert_equal(['/array/1/hash/h2/a/1', '/array/1/hash/h2/a/2', '/array/1/hash/h2/a/3'], locations)
327
- locations = []
328
- doc.move('/array/1')
329
- doc.each_child() { |d| locations << d.where? }
330
- assert_equal(['/array/1/num', '/array/1/string', '/array/1/hash'], locations)
331
- end
332
- end
333
-
334
- def test_each_child_path
335
- Oj::Doc.open($json1) do |doc|
336
- locations = []
337
- doc.each_child('/array/1/hash/h2/a') { |d| locations << d.where? }
338
- assert_equal(['/array/1/hash/h2/a/1', '/array/1/hash/h2/a/2', '/array/1/hash/h2/a/3'], locations)
339
- locations = []
340
- doc.each_child('/array/1') { |d| locations << d.where? }
341
- assert_equal(['/array/1/num', '/array/1/string', '/array/1/hash'], locations)
342
- end
343
- end
344
-
345
- def test_size
346
- Oj::Doc.open('[1,2,3]') do |doc|
347
- assert_equal(4, doc.size)
348
- end
349
- Oj::Doc.open('{"a":[1,2,3]}') do |doc|
350
- assert_equal(5, doc.size)
351
- end
352
- end
353
-
354
- def test_open_file
355
- filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
356
- File.open(filename, 'w') { |f| f.write('{"a":[1,2,3]}') }
357
- Oj::Doc.open_file(filename) do |doc|
358
- assert_equal(5, doc.size)
359
- end
360
- end
361
-
362
- def test_open_close
363
- json = %{{"a":[1,2,3]}}
364
- doc = Oj::Doc.open(json)
365
- assert_equal(Oj::Doc, doc.class)
366
- assert_equal(5, doc.size)
367
- assert_equal('/', doc.where?)
368
- doc.move('a/1')
369
- doc.home()
370
- assert_equal(2, doc.fetch('/a/2'))
371
- assert_equal(2, doc.fetch('a/2'))
372
- doc.close()
373
- begin
374
- doc.home()
375
- rescue Exception
376
- assert(true)
377
- end
378
- end
379
-
380
- def test_file_open_close
381
- filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
382
- File.open(filename, 'w') { |f| f.write('{"a":[1,2,3]}') }
383
- doc = Oj::Doc.open_file(filename)
384
- assert_equal(Oj::Doc, doc.class)
385
- assert_equal(5, doc.size)
386
- assert_equal('/', doc.where?)
387
- doc.move('a/1')
388
- doc.home()
389
- assert_equal(2, doc.fetch('/a/2'))
390
- assert_equal(2, doc.fetch('a/2'))
391
- doc.close()
392
- begin
393
- doc.home()
394
- rescue Exception
395
- assert(true)
396
- end
397
- end
398
-
399
- def test_open_no_close
400
- json = %{{"a":[1,2,3]}}
401
- doc = Oj::Doc.open(json)
402
- assert_equal(Oj::Doc, doc.class)
403
- assert_equal(5, doc.size)
404
- assert_equal('/', doc.where?)
405
- doc.move('a/1')
406
- doc.home()
407
- assert_equal(2, doc.fetch('/a/2'))
408
- assert_equal(2, doc.fetch('a/2'))
409
- doc = nil
410
- GC.start
411
- # a print statement confirms close is called
412
- end
413
-
414
- def test_dump
415
- Oj::Doc.open('[1,[2,3]]') do |doc|
416
- assert_equal('[1,[2,3]]', doc.dump())
417
- end
418
- Oj::Doc.open('[1,[2,3]]') do |doc|
419
- assert_equal('[2,3]', doc.dump('/2'))
420
- end
421
- end
422
-
423
- def test_each_leaf
424
- results = Oj::Doc.open('[1,[2,3]]') do |doc|
425
- h = {}
426
- doc.each_leaf() { |d| h[d.where?] = d.fetch() }
427
- h
428
- end
429
- assert_equal({'/1' => 1, '/2/1' => 2, '/2/2' => 3}, results)
430
- end
431
-
432
- def test_each_leaf_hash
433
- results = Oj::Doc.open('{"a":{"x":2},"b":{"y":4}}') do |doc|
434
- h = {}
435
- doc.each_leaf() { |d| h[d.where?] = d.fetch() }
436
- h
437
- end
438
- assert_equal({'/a/x' => 2, '/b/y' => 4}, results)
439
- end
440
-
441
- def test_comment
442
- json = %{{
443
- "x"/*one*/:/*two*/true,//three
444
- "y":58/*four*/,
445
- "z": [1,2/*five*/,
446
- 3 // six
447
- ]
448
- }
449
- }
450
- results = Oj::Doc.open(json) do |doc|
451
- h = {}
452
- doc.each_leaf() { |d| h[d.where?] = d.fetch() }
453
- h
454
- end
455
- assert_equal({'/x' => true, '/y' => 58, '/z/1' => 1, '/z/2' => 2, '/z/3' => 3}, results)
456
- end
457
-
458
- end # DocTest
data/test/test_file.rb DELETED
@@ -1,245 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- $: << File.dirname(__FILE__)
5
-
6
- require 'helper'
7
-
8
- class FileJuice < Minitest::Test
9
- class Jam
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
- end# Jam
23
-
24
- class Jeez < Jam
25
- def initialize(x, y)
26
- super
27
- end
28
-
29
- def to_json()
30
- %{{"json_class":"#{self.class}","x":#{@x},"y":#{@y}}}
31
- end
32
-
33
- def self.json_create(h)
34
- self.new(h['x'], h['y'])
35
- end
36
- end# Jeez
37
-
38
- class Orange < Jam
39
- def initialize(x, y)
40
- super
41
- end
42
-
43
- def as_json()
44
- { :json_class => self.class,
45
- :x => @x,
46
- :y => @y }
47
- end
48
-
49
- def self.json_create(h)
50
- self.new(h['x'], h['y'])
51
- end
52
- end
53
-
54
- def setup
55
- @default_options = Oj.default_options
56
- end
57
-
58
- def teardown
59
- Oj.default_options = @default_options
60
- end
61
-
62
- def test_nil
63
- dump_and_load(nil, false)
64
- end
65
-
66
- def test_true
67
- dump_and_load(true, false)
68
- end
69
-
70
- def test_false
71
- dump_and_load(false, false)
72
- end
73
-
74
- def test_fixnum
75
- dump_and_load(0, false)
76
- dump_and_load(12345, false)
77
- dump_and_load(-54321, false)
78
- dump_and_load(1, false)
79
- end
80
-
81
- def test_float
82
- mode = Oj.default_options()[:mode]
83
- Oj.default_options = {:mode => :object}
84
- dump_and_load(0.0, false)
85
- dump_and_load(12345.6789, false)
86
- dump_and_load(70.35, false)
87
- dump_and_load(-54321.012, false)
88
- dump_and_load(1.7775, false)
89
- dump_and_load(2.5024, false)
90
- dump_and_load(2.48e16, false)
91
- dump_and_load(2.48e100 * 1.0e10, false)
92
- dump_and_load(-2.48e100 * 1.0e10, false)
93
- dump_and_load(1/0.0, false)
94
- Oj.default_options = {:mode => mode}
95
- end
96
-
97
- def test_string
98
- dump_and_load('', false)
99
- dump_and_load('abc', false)
100
- dump_and_load("abc\ndef", false)
101
- dump_and_load("a\u0041", false)
102
- assert_equal("a\u0000a", dump_and_load("a\u0000a", false))
103
- end
104
-
105
- def test_string_object
106
- dump_and_load('abc', false)
107
- dump_and_load(':abc', false)
108
- end
109
-
110
- def test_array
111
- dump_and_load([], false)
112
- dump_and_load([true, false], false)
113
- dump_and_load(['a', 1, nil], false)
114
- dump_and_load([[nil]], false)
115
- dump_and_load([[nil], 58], false)
116
- end
117
-
118
- # Symbol
119
- def test_symbol_object
120
- Oj.default_options = { :mode => :object }
121
- #dump_and_load(''.to_sym, false)
122
- dump_and_load(:abc, false)
123
- dump_and_load(':xyz'.to_sym, false)
124
- end
125
-
126
- # Time
127
- def test_time_object
128
- t = Time.now()
129
- Oj.default_options = { :mode => :object, :time_format => :unix_zone }
130
- dump_and_load(t, false)
131
- end
132
- def test_time_object_early
133
- t = Time.xmlschema("1954-01-05T00:00:00.123456")
134
- Oj.default_options = { :mode => :object, :time_format => :unix_zone }
135
- dump_and_load(t, false)
136
- end
137
-
138
- # Hash
139
- def test_hash
140
- Oj.default_options = { :mode => :strict }
141
- dump_and_load({}, false)
142
- dump_and_load({ 'true' => true, 'false' => false}, false)
143
- dump_and_load({ 'true' => true, 'array' => [], 'hash' => { }}, false)
144
- end
145
-
146
- # Object with to_json()
147
- def test_json_object_compat
148
- Oj.default_options = { :mode => :compat, :use_to_json => true }
149
- obj = Jeez.new(true, 58)
150
- json = Oj.dump(obj, :indent => 2)
151
- assert(%{{"json_class":"FileJuice::Jeez","x":true,"y":58}
152
- } == json ||
153
- %{{"json_class":"FileJuice::Jeez","y":58,"x":true}
154
- } == json)
155
- dump_and_load(obj, false)
156
- Oj.default_options = { :mode => :compat, :use_to_json => false }
157
- end
158
-
159
- def test_as_json_object_compat_hash
160
- Oj.default_options = { :mode => :compat, :use_as_json => true }
161
- obj = Orange.new(true, 58)
162
- json = Oj.dump(obj, :indent => 2)
163
- assert(!json.nil?)
164
- dump_and_load(obj, false)
165
- end
166
-
167
- # Range
168
- def test_range_object
169
- unless RUBY_VERSION.start_with?('1.8')
170
- Oj.default_options = { :mode => :object }
171
- json = Oj.dump(1..7, :mode => :object, :indent => 0)
172
- if 'rubinius' == $ruby
173
- assert(%{{"^O":"Range","begin":1,"end":7,"exclude_end?":false}} == json)
174
- elsif 'jruby' == $ruby
175
- assert(%{{"^O":"Range","begin":1,"end":7,"exclude_end?":false}} == json)
176
- else
177
- assert_equal(%{{"^u":["Range",1,7,false]}}, json)
178
- end
179
- dump_and_load(1..7, false)
180
- dump_and_load(1..1, false)
181
- dump_and_load(1...7, false)
182
- end
183
- end
184
-
185
- # BigNum
186
- def test_bignum_object
187
- Oj.default_options = { :mode => :compat }
188
- dump_and_load(7 ** 55, false)
189
- end
190
-
191
- # BigDecimal
192
- def test_bigdecimal_strict
193
- mode = Oj.default_options[:mode]
194
- Oj.default_options = {:mode => :strict}
195
- dump_and_load(BigDecimal.new('3.14159265358979323846'), false)
196
- Oj.default_options = {:mode => mode}
197
- end
198
-
199
- def test_bigdecimal_null
200
- mode = Oj.default_options[:mode]
201
- Oj.default_options = {:mode => :null}
202
- dump_and_load(BigDecimal.new('3.14159265358979323846'), false)
203
- Oj.default_options = {:mode => mode}
204
- end
205
-
206
- def test_bigdecimal_object
207
- Oj.default_options = {:mode => :object}
208
- dump_and_load(BigDecimal.new('3.14159265358979323846'), false)
209
- end
210
-
211
- # Date
212
- def test_date_object
213
- Oj.default_options = { :mode => :object }
214
- dump_and_load(Date.new(2012, 6, 19), false)
215
- end
216
-
217
- # DateTime
218
- def test_datetime_object
219
- Oj.default_options = { :mode => :object }
220
- dump_and_load(DateTime.new(2012, 6, 19), false)
221
- end
222
-
223
- def dump_and_load(obj, trace=false)
224
- filename = File.join(File.dirname(__FILE__), 'file_test.json')
225
- File.open(filename, "w") { |f|
226
- Oj.to_stream(f, obj, :indent => 2)
227
- }
228
- puts "\n*** file: '#{File.read(filename)}'" if trace
229
- loaded = Oj.load_file(filename)
230
- if obj.is_a?(Time) && loaded.is_a?(Time)
231
- assert_equal(obj.tv_sec, loaded.tv_sec)
232
- if obj.respond_to?(:tv_nsec)
233
- assert_equal(obj.tv_nsec, loaded.tv_nsec)
234
- else
235
- assert_equal(obj.tv_usec, loaded.tv_usec)
236
- end
237
- assert_equal(obj.utc?, loaded.utc?)
238
- assert_equal(obj.utc_offset, loaded.utc_offset)
239
- else
240
- assert_equal(obj, loaded)
241
- end
242
- loaded
243
- end
244
-
245
- end