oj 3.13.23 → 3.16.10

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