oj 3.9.1 → 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 (171) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1452 -0
  3. data/README.md +21 -6
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +50 -68
  6. data/ext/oj/cache.c +329 -0
  7. data/ext/oj/cache.h +22 -0
  8. data/ext/oj/cache8.c +60 -62
  9. data/ext/oj/cache8.h +9 -36
  10. data/ext/oj/circarray.c +38 -42
  11. data/ext/oj/circarray.h +12 -13
  12. data/ext/oj/code.c +158 -179
  13. data/ext/oj/code.h +20 -22
  14. data/ext/oj/compat.c +145 -205
  15. data/ext/oj/custom.c +740 -880
  16. data/ext/oj/debug.c +126 -0
  17. data/ext/oj/dump.c +1145 -844
  18. data/ext/oj/dump.h +71 -57
  19. data/ext/oj/dump_compat.c +575 -655
  20. data/ext/oj/dump_leaf.c +96 -186
  21. data/ext/oj/dump_object.c +533 -660
  22. data/ext/oj/dump_strict.c +306 -340
  23. data/ext/oj/encode.h +4 -33
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +28 -28
  26. data/ext/oj/err.h +39 -42
  27. data/ext/oj/extconf.rb +28 -7
  28. data/ext/oj/fast.c +1052 -1113
  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 +471 -430
  34. data/ext/oj/object.c +532 -580
  35. data/ext/oj/odd.c +156 -142
  36. data/ext/oj/odd.h +25 -26
  37. data/ext/oj/oj.c +1346 -961
  38. data/ext/oj/oj.h +307 -290
  39. data/ext/oj/parse.c +954 -858
  40. data/ext/oj/parse.h +74 -72
  41. data/ext/oj/parser.c +1600 -0
  42. data/ext/oj/parser.h +103 -0
  43. data/ext/oj/rails.c +819 -836
  44. data/ext/oj/rails.h +8 -11
  45. data/ext/oj/reader.c +136 -147
  46. data/ext/oj/reader.h +69 -83
  47. data/ext/oj/resolve.c +41 -63
  48. data/ext/oj/resolve.h +4 -6
  49. data/ext/oj/rxclass.c +69 -72
  50. data/ext/oj/rxclass.h +12 -13
  51. data/ext/oj/saj.c +440 -485
  52. data/ext/oj/saj2.c +584 -0
  53. data/ext/oj/saj2.h +23 -0
  54. data/ext/oj/scp.c +79 -118
  55. data/ext/oj/simd.h +10 -0
  56. data/ext/oj/sparse.c +739 -709
  57. data/ext/oj/stream_writer.c +141 -175
  58. data/ext/oj/strict.c +103 -128
  59. data/ext/oj/string_writer.c +244 -261
  60. data/ext/oj/trace.c +34 -41
  61. data/ext/oj/trace.h +42 -15
  62. data/ext/oj/usual.c +1218 -0
  63. data/ext/oj/usual.h +69 -0
  64. data/ext/oj/util.c +107 -107
  65. data/ext/oj/util.h +4 -3
  66. data/ext/oj/val_stack.c +61 -78
  67. data/ext/oj/val_stack.h +80 -114
  68. data/ext/oj/validate.c +46 -0
  69. data/ext/oj/wab.c +316 -361
  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 +9 -9
  73. data/lib/oj/error.rb +1 -2
  74. data/lib/oj/json.rb +162 -150
  75. data/lib/oj/mimic.rb +54 -20
  76. data/lib/oj/saj.rb +20 -6
  77. data/lib/oj/schandler.rb +5 -4
  78. data/lib/oj/state.rb +12 -8
  79. data/lib/oj/version.rb +1 -2
  80. data/lib/oj.rb +2 -8
  81. data/pages/Compatibility.md +1 -1
  82. data/pages/Encoding.md +1 -1
  83. data/pages/InstallOptions.md +20 -0
  84. data/pages/JsonGem.md +15 -0
  85. data/pages/Modes.md +9 -3
  86. data/pages/Options.md +62 -12
  87. data/pages/Parser.md +309 -0
  88. data/pages/Rails.md +73 -22
  89. metadata +68 -192
  90. data/ext/oj/hash.c +0 -163
  91. data/ext/oj/hash.h +0 -46
  92. data/ext/oj/hash_test.c +0 -512
  93. data/test/_test_active.rb +0 -76
  94. data/test/_test_active_mimic.rb +0 -96
  95. data/test/_test_mimic_rails.rb +0 -126
  96. data/test/activerecord/result_test.rb +0 -27
  97. data/test/activesupport4/decoding_test.rb +0 -108
  98. data/test/activesupport4/encoding_test.rb +0 -531
  99. data/test/activesupport4/test_helper.rb +0 -41
  100. data/test/activesupport5/decoding_test.rb +0 -125
  101. data/test/activesupport5/encoding_test.rb +0 -485
  102. data/test/activesupport5/encoding_test_cases.rb +0 -90
  103. data/test/activesupport5/test_helper.rb +0 -50
  104. data/test/activesupport5/time_zone_test_helpers.rb +0 -24
  105. data/test/bar.rb +0 -25
  106. data/test/files.rb +0 -29
  107. data/test/foo.rb +0 -21
  108. data/test/helper.rb +0 -26
  109. data/test/isolated/shared.rb +0 -308
  110. data/test/isolated/test_mimic_after.rb +0 -13
  111. data/test/isolated/test_mimic_alone.rb +0 -12
  112. data/test/isolated/test_mimic_as_json.rb +0 -45
  113. data/test/isolated/test_mimic_before.rb +0 -13
  114. data/test/isolated/test_mimic_define.rb +0 -28
  115. data/test/isolated/test_mimic_rails_after.rb +0 -22
  116. data/test/isolated/test_mimic_rails_before.rb +0 -21
  117. data/test/isolated/test_mimic_redefine.rb +0 -15
  118. data/test/json_gem/json_addition_test.rb +0 -216
  119. data/test/json_gem/json_common_interface_test.rb +0 -148
  120. data/test/json_gem/json_encoding_test.rb +0 -107
  121. data/test/json_gem/json_ext_parser_test.rb +0 -20
  122. data/test/json_gem/json_fixtures_test.rb +0 -35
  123. data/test/json_gem/json_generator_test.rb +0 -383
  124. data/test/json_gem/json_generic_object_test.rb +0 -90
  125. data/test/json_gem/json_parser_test.rb +0 -470
  126. data/test/json_gem/json_string_matching_test.rb +0 -42
  127. data/test/json_gem/test_helper.rb +0 -18
  128. data/test/perf.rb +0 -107
  129. data/test/perf_compat.rb +0 -130
  130. data/test/perf_fast.rb +0 -164
  131. data/test/perf_file.rb +0 -64
  132. data/test/perf_object.rb +0 -138
  133. data/test/perf_saj.rb +0 -109
  134. data/test/perf_scp.rb +0 -151
  135. data/test/perf_simple.rb +0 -287
  136. data/test/perf_strict.rb +0 -145
  137. data/test/perf_wab.rb +0 -131
  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 -54
  151. data/test/sample_json.rb +0 -37
  152. data/test/test_compat.rb +0 -509
  153. data/test/test_custom.rb +0 -503
  154. data/test/test_debian.rb +0 -53
  155. data/test/test_fast.rb +0 -470
  156. data/test/test_file.rb +0 -239
  157. data/test/test_gc.rb +0 -49
  158. data/test/test_hash.rb +0 -29
  159. data/test/test_integer_range.rb +0 -73
  160. data/test/test_null.rb +0 -376
  161. data/test/test_object.rb +0 -1018
  162. data/test/test_saj.rb +0 -186
  163. data/test/test_scp.rb +0 -433
  164. data/test/test_strict.rb +0 -410
  165. data/test/test_various.rb +0 -741
  166. data/test/test_wab.rb +0 -307
  167. data/test/test_writer.rb +0 -380
  168. data/test/tests.rb +0 -24
  169. data/test/tests_mimic.rb +0 -14
  170. data/test/tests_mimic_addition.rb +0 -7
  171. data/test/zoo.rb +0 -13
data/test/test_fast.rb DELETED
@@ -1,470 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- $: << File.dirname(__FILE__)
5
-
6
- require 'helper'
7
-
8
- class DocTest < Minitest::Test
9
- def setup
10
- @default_options = Oj.default_options
11
- @json1 = %|{
12
- "array": [
13
- {
14
- "num" : 3,
15
- "string": "message",
16
- "hash" : {
17
- "h2" : {
18
- "a" : [ 1, 2, 3 ]
19
- }
20
- }
21
- }
22
- ],
23
- "boolean" : true
24
- }|
25
- end
26
-
27
- def teardown
28
- Oj.default_options = @default_options
29
- end
30
-
31
- def test_nil
32
- json = %{null}
33
- Oj::Doc.open(json) do |doc|
34
- assert_equal(NilClass, doc.type)
35
- assert_nil(doc.fetch())
36
- end
37
- end
38
-
39
- def test_true
40
- json = %{true}
41
- Oj::Doc.open(json) do |doc|
42
- assert_equal(TrueClass, doc.type)
43
- assert_equal(true, doc.fetch())
44
- end
45
- end
46
-
47
- def test_false
48
- json = %{false}
49
- Oj::Doc.open(json) do |doc|
50
- assert_equal(FalseClass, doc.type)
51
- assert_equal(false, doc.fetch())
52
- end
53
- end
54
-
55
- def test_string
56
- json = %{"a string"}
57
- Oj::Doc.open(json) do |doc|
58
- assert_equal(String, doc.type)
59
- assert_equal('a string', doc.fetch())
60
- end
61
- end
62
-
63
- def test_encoding
64
- json = %{"ぴーたー"}
65
- Oj::Doc.open(json) do |doc|
66
- assert_equal(String, doc.type)
67
- assert_equal("ぴーたー", doc.fetch())
68
- end
69
- end
70
-
71
- def test_encoding_escaped
72
- json = %{"\\u3074\\u30fc\\u305f\\u30fc"}
73
- Oj::Doc.open(json) do |doc|
74
- assert_equal(String, doc.type)
75
- assert_equal("ぴーたー", doc.fetch())
76
- end
77
- end
78
-
79
- def test_fixnum
80
- json = %{12345}
81
- Oj::Doc.open(json) do |doc|
82
- if '2.4.0' <= RUBY_VERSION
83
- assert_equal(Integer, doc.type)
84
- else
85
- assert_equal(Fixnum, doc.type)
86
- end
87
- assert_equal(12345, doc.fetch())
88
- end
89
- end
90
-
91
- def test_float
92
- json = %{12345.6789}
93
- Oj::Doc.open(json) do |doc|
94
- assert_equal(Float, doc.type)
95
- assert_equal(12345.6789, doc.fetch())
96
- end
97
- end
98
-
99
- def test_float_exp
100
- json = %{12345.6789e7}
101
- Oj::Doc.open(json) do |doc|
102
- assert_equal(Float, doc.type)
103
- #assert_equal(12345.6789e7, doc.fetch())
104
- assert_equal(12345.6789e7.to_i, doc.fetch().to_i)
105
- end
106
- end
107
-
108
- def test_array_empty
109
- json = %{[]}
110
- Oj::Doc.open(json) do |doc|
111
- assert_equal(Array, doc.type)
112
- assert_equal([], doc.fetch())
113
- end
114
- end
115
-
116
- def test_array
117
- json = %{[true,false]}
118
- Oj::Doc.open(json) do |doc|
119
- assert_equal(Array, doc.type)
120
- assert_equal([true, false], doc.fetch())
121
- end
122
- end
123
-
124
- def test_hash_empty
125
- json = %{{}}
126
- Oj::Doc.open(json) do |doc|
127
- assert_equal(Hash, doc.type)
128
- assert_equal({}, doc.fetch())
129
- end
130
- end
131
-
132
- def test_hash
133
- json = %{{"one":true,"two":false}}
134
- Oj::Doc.open(json) do |doc|
135
- assert_equal(Hash, doc.type)
136
- assert_equal({'one' => true, 'two' => false}, doc.fetch())
137
- end
138
- end
139
-
140
- # move() and where?()
141
- def test_move_hash
142
- json = %{{"one":{"two":false}}}
143
- Oj::Doc.open(json) do |doc|
144
- doc.move('/one')
145
- assert_equal('/one', doc.where?)
146
- doc.move('/one/two')
147
- assert_equal('/one/two', doc.where?)
148
- end
149
- end
150
-
151
- def test_move_array
152
- json = %{[1,[2,true]]}
153
- Oj::Doc.open(json) do |doc|
154
- doc.move('/1')
155
- assert_equal('/1', doc.where?)
156
- doc.move('/2/1')
157
- assert_equal('/2/1', doc.where?)
158
- end
159
- end
160
-
161
- def test_move
162
- Oj::Doc.open(@json1) do |doc|
163
- [ '/',
164
- '/array',
165
- '/boolean',
166
- '/array/1/hash/h2/a/3',
167
- ].each do |p|
168
- doc.move(p)
169
- assert_equal(p, doc.where?)
170
- end
171
- begin
172
- doc.move('/array/x')
173
- rescue Exception
174
- assert_equal('/', doc.where?)
175
- assert(true)
176
- end
177
- end
178
- end
179
-
180
- def test_move_slash
181
- Oj::Doc.open(%|{"top":{"a/b":3}}|) do |doc|
182
- doc.move('top/a\/b')
183
- assert_equal('/top/a\/b', doc.where?)
184
- end
185
- end
186
-
187
- def test_fetch_slash
188
- Oj::Doc.open(%|{"a/b":3}|) do |doc|
189
- x = doc.fetch('a\/b')
190
- assert_equal(3, x)
191
- end
192
- end
193
-
194
- def test_move_relative
195
- Oj::Doc.open(@json1) do |doc|
196
- [['/', 'array', '/array'],
197
- ['/array', '1/num', '/array/1/num'],
198
- ['/array/1/hash', 'h2/a', '/array/1/hash/h2/a'],
199
- ['/array/1', 'hash/h2/a/2', '/array/1/hash/h2/a/2'],
200
- ['/array/1/hash', '../string', '/array/1/string'],
201
- ['/array/1/hash', '..', '/array/1'],
202
- ].each do |start,path,where|
203
- doc.move(start)
204
- doc.move(path)
205
- assert_equal(where, doc.where?)
206
- end
207
- end
208
- end
209
-
210
- def test_type
211
- if '2.4.0' <= RUBY_VERSION
212
- num_class = Integer
213
- else
214
- num_class = Fixnum
215
- end
216
- Oj::Doc.open(@json1) do |doc|
217
- [['/', Hash],
218
- ['/array', Array],
219
- ['/array/1', Hash],
220
- ['/array/1/num', num_class],
221
- ['/array/1/string', String],
222
- ['/array/1/hash/h2/a', Array],
223
- ['/array/1/hash/../num', num_class],
224
- ['/array/1/hash/../..', Array],
225
- ].each do |path,type|
226
- assert_equal(type, doc.type(path))
227
- end
228
- end
229
- end
230
-
231
- def test_local_key
232
- Oj::Doc.open(@json1) do |doc|
233
- [['/', nil],
234
- ['/array', 'array'],
235
- ['/array/1', 1],
236
- ['/array/1/num', 'num'],
237
- ['/array/1/string', 'string'],
238
- ['/array/1/hash/h2/a', 'a'],
239
- ['/array/1/hash/../num', 'num'],
240
- ['/array/1/hash/..', 1],
241
- ['/array/1/hash/../..', 'array'],
242
- ].each do |path,key|
243
- doc.move(path)
244
- if key.nil?
245
- assert_nil(doc.local_key())
246
- else
247
- assert_equal(key, doc.local_key())
248
- end
249
- end
250
- end
251
- end
252
-
253
- def test_fetch_move
254
- Oj::Doc.open(@json1) do |doc|
255
- [['/array/1/num', 3],
256
- ['/array/1/string', 'message'],
257
- ['/array/1/hash/h2/a', [1, 2, 3]],
258
- ['/array/1/hash/../num', 3],
259
- ['/array/1/hash/..', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
260
- ['/array/1/hash/../..', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
261
- ['/array/1', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
262
- ['/array', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
263
- ['/', {'array' => [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}], 'boolean' => true}],
264
- ].each do |path,val|
265
- doc.move(path)
266
- assert_equal(val, doc.fetch())
267
- end
268
- end
269
- end
270
-
271
- def test_fetch_path
272
- Oj::Doc.open(@json1) do |doc|
273
- [['/array/1/num', 3],
274
- ['/array/1/string', 'message'],
275
- ['/array/1/hash/h2/a', [1, 2, 3]],
276
- ['/array/1/hash/../num', 3],
277
- ['/array/1/hash/..', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
278
- ['/array/1/hash/../..', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
279
- ['/array/1', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
280
- ['/array', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
281
- ['/', {'array' => [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}], 'boolean' => true}],
282
- ].each do |path,val|
283
- assert_equal(val, doc.fetch(path))
284
- end
285
- end
286
- end
287
-
288
- def test_move_fetch_path
289
- Oj::Doc.open(@json1) do |doc|
290
- [['/array/1', 'num', 3],
291
- ['/array/1', 'string', 'message'],
292
- ['/array/1/hash', 'h2/a', [1, 2, 3]],
293
- ].each do |path,fetch_path,val|
294
- doc.move(path)
295
- assert_equal(val, doc.fetch(fetch_path))
296
- end
297
- end
298
- end
299
-
300
- def test_home
301
- Oj::Doc.open(@json1) do |doc|
302
- doc.move('/array/1/num')
303
- doc.home()
304
- assert_equal('/', doc.where?)
305
- end
306
- end
307
-
308
- def test_each_value_root
309
- Oj::Doc.open(@json1) do |doc|
310
- values = []
311
- doc.each_value() { |v| values << v.to_s }
312
- assert_equal(['1', '2', '3', '3', 'message', 'true'], values.sort)
313
- end
314
- end
315
-
316
- def test_each_value_move
317
- Oj::Doc.open(@json1) do |doc|
318
- doc.move('/array/1/hash')
319
- values = []
320
- doc.each_value() { |v| values << v.to_s }
321
- assert_equal(['1', '2', '3'], values.sort)
322
- end
323
- end
324
-
325
- def test_each_value_path
326
- Oj::Doc.open(@json1) do |doc|
327
- values = []
328
- doc.each_value('/array/1/hash') { |v| values << v.to_s }
329
- assert_equal(['1', '2', '3'], values.sort)
330
- end
331
- end
332
-
333
- def test_each_child_move
334
- Oj::Doc.open(@json1) do |doc|
335
- locations = []
336
- doc.move('/array/1/hash/h2/a')
337
- doc.each_child() { |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.move('/array/1')
341
- doc.each_child() { |d| locations << d.where? }
342
- assert_equal(['/array/1/num', '/array/1/string', '/array/1/hash'], locations)
343
- end
344
- end
345
-
346
- def test_each_child_path
347
- Oj::Doc.open(@json1) do |doc|
348
- locations = []
349
- doc.each_child('/array/1/hash/h2/a') { |d| locations << d.where? }
350
- assert_equal(['/array/1/hash/h2/a/1', '/array/1/hash/h2/a/2', '/array/1/hash/h2/a/3'], locations)
351
- locations = []
352
- doc.each_child('/array/1') { |d| locations << d.where? }
353
- assert_equal(['/array/1/num', '/array/1/string', '/array/1/hash'], locations)
354
- end
355
- end
356
-
357
- def test_size
358
- Oj::Doc.open('[1,2,3]') do |doc|
359
- assert_equal(4, doc.size)
360
- end
361
- Oj::Doc.open('{"a":[1,2,3]}') do |doc|
362
- assert_equal(5, doc.size)
363
- end
364
- end
365
-
366
- def test_open_file
367
- filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
368
- File.open(filename, 'w') { |f| f.write('{"a":[1,2,3]}') }
369
- Oj::Doc.open_file(filename) do |doc|
370
- assert_equal(5, doc.size)
371
- end
372
- end
373
-
374
- def test_open_close
375
- json = %{{"a":[1,2,3]}}
376
- doc = Oj::Doc.open(json)
377
- assert_equal(Oj::Doc, doc.class)
378
- assert_equal(5, doc.size)
379
- assert_equal('/', doc.where?)
380
- doc.move('a/1')
381
- doc.home()
382
- assert_equal(2, doc.fetch('/a/2'))
383
- assert_equal(2, doc.fetch('a/2'))
384
- doc.close()
385
- begin
386
- doc.home()
387
- rescue Exception
388
- assert(true)
389
- end
390
- end
391
-
392
- def test_file_open_close
393
- filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
394
- File.open(filename, 'w') { |f| f.write('{"a":[1,2,3]}') }
395
- doc = Oj::Doc.open_file(filename)
396
- assert_equal(Oj::Doc, doc.class)
397
- assert_equal(5, doc.size)
398
- assert_equal('/', doc.where?)
399
- doc.move('a/1')
400
- doc.home()
401
- assert_equal(2, doc.fetch('/a/2'))
402
- assert_equal(2, doc.fetch('a/2'))
403
- doc.close()
404
- begin
405
- doc.home()
406
- rescue Exception
407
- assert(true)
408
- end
409
- end
410
-
411
- def test_open_no_close
412
- json = %{{"a":[1,2,3]}}
413
- doc = Oj::Doc.open(json)
414
- assert_equal(Oj::Doc, doc.class)
415
- assert_equal(5, doc.size)
416
- assert_equal('/', doc.where?)
417
- doc.move('a/1')
418
- doc.home()
419
- assert_equal(2, doc.fetch('/a/2'))
420
- assert_equal(2, doc.fetch('a/2'))
421
- doc = nil
422
- GC.start
423
- # a print statement confirms close is called
424
- end
425
-
426
- def test_dump
427
- Oj::Doc.open('[1,[2,3]]') do |doc|
428
- assert_equal('[1,[2,3]]', doc.dump())
429
- end
430
- Oj::Doc.open('[1,[2,3]]') do |doc|
431
- assert_equal('[2,3]', doc.dump('/2'))
432
- end
433
- end
434
-
435
- def test_each_leaf
436
- results = Oj::Doc.open('[1,[2,3]]') do |doc|
437
- h = {}
438
- doc.each_leaf() { |d| h[d.where?] = d.fetch() }
439
- h
440
- end
441
- assert_equal({'/1' => 1, '/2/1' => 2, '/2/2' => 3}, results)
442
- end
443
-
444
- def test_each_leaf_hash
445
- results = Oj::Doc.open('{"a":{"x":2},"b":{"y":4}}') do |doc|
446
- h = {}
447
- doc.each_leaf() { |d| h[d.where?] = d.fetch() }
448
- h
449
- end
450
- assert_equal({'/a/x' => 2, '/b/y' => 4}, results)
451
- end
452
-
453
- def test_comment
454
- json = %{{
455
- "x"/*one*/:/*two*/true,//three
456
- "y":58/*four*/,
457
- "z": [1,2/*five*/,
458
- 3 // six
459
- ]
460
- }
461
- }
462
- results = Oj::Doc.open(json) do |doc|
463
- h = {}
464
- doc.each_leaf() { |d| h[d.where?] = d.fetch() }
465
- h
466
- end
467
- assert_equal({'/x' => true, '/y' => 58, '/z/1' => 1, '/z/2' => 2, '/z/3' => 3}, results)
468
- end
469
-
470
- end # DocTest
data/test/test_file.rb DELETED
@@ -1,239 +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
- # Windows does not support dates before 1970.
134
- return if RbConfig::CONFIG['host_os'] =~ /(mingw|mswin)/
135
- t = Time.xmlschema("1954-01-05T00:00:00.123456")
136
- Oj.default_options = { :mode => :object, :time_format => :unix_zone }
137
- dump_and_load(t, false)
138
- end
139
-
140
- # Hash
141
- def test_hash
142
- Oj.default_options = { :mode => :strict }
143
- dump_and_load({}, false)
144
- dump_and_load({ 'true' => true, 'false' => false}, false)
145
- dump_and_load({ 'true' => true, 'array' => [], 'hash' => { }}, false)
146
- end
147
-
148
- # Object with to_json()
149
- def test_json_object_compat
150
- Oj.default_options = { :mode => :compat, :use_to_json => true, :create_additions => true }
151
- obj = Jeez.new(true, 58)
152
- json = Oj.dump(obj, :indent => 2)
153
- assert(%{{"json_class":"FileJuice::Jeez","x":true,"y":58}
154
- } == json ||
155
- %{{"json_class":"FileJuice::Jeez","y":58,"x":true}
156
- } == json)
157
- dump_and_load(obj, false)
158
- Oj.default_options = { :mode => :compat, :use_to_json => false }
159
- end
160
-
161
- # Range
162
- def test_range_object
163
- Oj.default_options = { :mode => :object }
164
- json = Oj.dump(1..7, :mode => :object, :indent => 0)
165
- if 'rubinius' == $ruby
166
- assert(%{{"^O":"Range","begin":1,"end":7,"exclude_end?":false}} == json)
167
- elsif 'jruby' == $ruby
168
- assert(%{{"^O":"Range","begin":1,"end":7,"exclude_end?":false}} == json)
169
- else
170
- assert_equal(%{{"^u":["Range",1,7,false]}}, json)
171
- end
172
- dump_and_load(1..7, false)
173
- dump_and_load(1..1, false)
174
- dump_and_load(1...7, false)
175
- end
176
-
177
- # BigNum
178
- def test_bignum_object
179
- Oj.default_options = { :mode => :compat }
180
- dump_and_load(7 ** 55, false)
181
- end
182
-
183
- # BigDecimal
184
- def test_bigdecimal_strict
185
- mode = Oj.default_options[:mode]
186
- Oj.default_options = {:mode => :strict}
187
- dump_and_load(BigDecimal('3.14159265358979323846'), false)
188
- Oj.default_options = {:mode => mode}
189
- end
190
-
191
- def test_bigdecimal_null
192
- mode = Oj.default_options[:mode]
193
- Oj.default_options = {:mode => :null}
194
- dump_and_load(BigDecimal('3.14159265358979323846'), false)
195
- Oj.default_options = {:mode => mode}
196
- end
197
-
198
- def test_bigdecimal_object
199
- Oj.default_options = {:mode => :object}
200
- dump_and_load(BigDecimal('3.14159265358979323846'), false)
201
- end
202
-
203
- # Date
204
- def test_date_object
205
- Oj.default_options = { :mode => :object }
206
- dump_and_load(Date.new(2012, 6, 19), false)
207
- end
208
-
209
- # DateTime
210
- def test_datetime_object
211
- Oj.default_options = { :mode => :object }
212
- dump_and_load(DateTime.new(2012, 6, 19), false)
213
- end
214
-
215
- def dump_and_load(obj, trace=false)
216
- filename = File.join(File.dirname(__FILE__), 'file_test.json')
217
- File.open(filename, "w") { |f|
218
- Oj.to_stream(f, obj, :indent => 2)
219
- }
220
- puts "\n*** file: '#{File.read(filename)}'" if trace
221
- loaded = Oj.load_file(filename)
222
- if obj.is_a?(Time) && loaded.is_a?(Time)
223
- assert_equal(obj.tv_sec, loaded.tv_sec)
224
- if obj.respond_to?(:tv_nsec)
225
- assert_equal(obj.tv_nsec, loaded.tv_nsec)
226
- else
227
- assert_equal(obj.tv_usec, loaded.tv_usec)
228
- end
229
- assert_equal(obj.utc?, loaded.utc?)
230
- assert_equal(obj.utc_offset, loaded.utc_offset)
231
- elsif obj.nil?
232
- assert_nil(loaded)
233
- else
234
- assert_equal(obj, loaded)
235
- end
236
- loaded
237
- end
238
-
239
- end