oj 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +17 -23
  3. data/README.md +74 -425
  4. data/ext/oj/buf.h +103 -0
  5. data/ext/oj/cache8.c +4 -0
  6. data/ext/oj/circarray.c +68 -0
  7. data/ext/oj/circarray.h +23 -0
  8. data/ext/oj/code.c +227 -0
  9. data/ext/oj/code.h +40 -0
  10. data/ext/oj/compat.c +243 -0
  11. data/ext/oj/custom.c +1097 -0
  12. data/ext/oj/dump.c +766 -1534
  13. data/ext/oj/dump.h +92 -0
  14. data/ext/oj/dump_compat.c +937 -0
  15. data/ext/oj/dump_leaf.c +254 -0
  16. data/ext/oj/dump_object.c +810 -0
  17. data/ext/oj/dump_rails.c +329 -0
  18. data/ext/oj/dump_strict.c +416 -0
  19. data/ext/oj/encode.h +51 -0
  20. data/ext/oj/err.c +57 -0
  21. data/ext/oj/err.h +70 -0
  22. data/ext/oj/extconf.rb +17 -7
  23. data/ext/oj/fast.c +213 -180
  24. data/ext/oj/hash.c +163 -0
  25. data/ext/oj/hash.h +46 -0
  26. data/ext/oj/hash_test.c +512 -0
  27. data/ext/oj/mimic_json.c +817 -0
  28. data/ext/oj/mimic_rails.c +806 -0
  29. data/ext/oj/mimic_rails.h +17 -0
  30. data/ext/oj/object.c +752 -0
  31. data/ext/oj/odd.c +230 -0
  32. data/ext/oj/odd.h +44 -0
  33. data/ext/oj/oj.c +1288 -929
  34. data/ext/oj/oj.h +240 -69
  35. data/ext/oj/parse.c +1014 -0
  36. data/ext/oj/parse.h +92 -0
  37. data/ext/oj/reader.c +223 -0
  38. data/ext/oj/reader.h +151 -0
  39. data/ext/oj/resolve.c +127 -0
  40. data/ext/oj/{cache.h → resolve.h} +6 -13
  41. data/ext/oj/rxclass.c +133 -0
  42. data/ext/oj/rxclass.h +27 -0
  43. data/ext/oj/saj.c +77 -175
  44. data/ext/oj/scp.c +224 -0
  45. data/ext/oj/sparse.c +911 -0
  46. data/ext/oj/stream_writer.c +301 -0
  47. data/ext/oj/strict.c +162 -0
  48. data/ext/oj/string_writer.c +480 -0
  49. data/ext/oj/val_stack.c +98 -0
  50. data/ext/oj/val_stack.h +188 -0
  51. data/lib/oj/active_support_helper.rb +41 -0
  52. data/lib/oj/bag.rb +6 -10
  53. data/lib/oj/easy_hash.rb +52 -0
  54. data/lib/oj/json.rb +172 -0
  55. data/lib/oj/mimic.rb +260 -5
  56. data/lib/oj/saj.rb +13 -10
  57. data/lib/oj/schandler.rb +142 -0
  58. data/lib/oj/state.rb +131 -0
  59. data/lib/oj/version.rb +1 -1
  60. data/lib/oj.rb +11 -23
  61. data/pages/Advanced.md +22 -0
  62. data/pages/Compatibility.md +25 -0
  63. data/pages/Custom.md +23 -0
  64. data/pages/Encoding.md +65 -0
  65. data/pages/JsonGem.md +79 -0
  66. data/pages/Modes.md +140 -0
  67. data/pages/Options.md +250 -0
  68. data/pages/Rails.md +60 -0
  69. data/pages/Security.md +20 -0
  70. data/test/_test_active.rb +76 -0
  71. data/test/_test_active_mimic.rb +96 -0
  72. data/test/_test_mimic_rails.rb +126 -0
  73. data/test/activesupport4/decoding_test.rb +105 -0
  74. data/test/activesupport4/encoding_test.rb +531 -0
  75. data/test/activesupport4/test_helper.rb +41 -0
  76. data/test/activesupport5/decoding_test.rb +125 -0
  77. data/test/activesupport5/encoding_test.rb +483 -0
  78. data/test/activesupport5/encoding_test_cases.rb +90 -0
  79. data/test/activesupport5/test_helper.rb +50 -0
  80. data/test/activesupport5/time_zone_test_helpers.rb +24 -0
  81. data/test/helper.rb +27 -0
  82. data/test/isolated/shared.rb +310 -0
  83. data/test/isolated/test_mimic_after.rb +13 -0
  84. data/test/isolated/test_mimic_alone.rb +12 -0
  85. data/test/isolated/test_mimic_as_json.rb +45 -0
  86. data/test/isolated/test_mimic_before.rb +13 -0
  87. data/test/isolated/test_mimic_define.rb +28 -0
  88. data/test/isolated/test_mimic_rails_after.rb +22 -0
  89. data/test/isolated/test_mimic_rails_before.rb +21 -0
  90. data/test/isolated/test_mimic_redefine.rb +15 -0
  91. data/test/json_gem/json_addition_test.rb +216 -0
  92. data/test/json_gem/json_common_interface_test.rb +143 -0
  93. data/test/json_gem/json_encoding_test.rb +109 -0
  94. data/test/json_gem/json_ext_parser_test.rb +20 -0
  95. data/test/json_gem/json_fixtures_test.rb +35 -0
  96. data/test/json_gem/json_generator_test.rb +383 -0
  97. data/test/json_gem/json_generic_object_test.rb +90 -0
  98. data/test/json_gem/json_parser_test.rb +470 -0
  99. data/test/json_gem/json_string_matching_test.rb +42 -0
  100. data/test/json_gem/test_helper.rb +18 -0
  101. data/test/perf_compat.rb +130 -0
  102. data/test/perf_fast.rb +9 -9
  103. data/test/perf_file.rb +64 -0
  104. data/test/{perf_obj.rb → perf_object.rb} +24 -10
  105. data/test/perf_scp.rb +151 -0
  106. data/test/perf_strict.rb +32 -113
  107. data/test/sample.rb +2 -3
  108. data/test/test_compat.rb +474 -0
  109. data/test/test_custom.rb +355 -0
  110. data/test/test_debian.rb +53 -0
  111. data/test/test_fast.rb +66 -16
  112. data/test/test_file.rb +237 -0
  113. data/test/test_gc.rb +49 -0
  114. data/test/test_hash.rb +29 -0
  115. data/test/test_null.rb +376 -0
  116. data/test/test_object.rb +1010 -0
  117. data/test/test_saj.rb +16 -16
  118. data/test/test_scp.rb +417 -0
  119. data/test/test_strict.rb +410 -0
  120. data/test/test_various.rb +815 -0
  121. data/test/test_writer.rb +308 -0
  122. data/test/tests.rb +9 -902
  123. data/test/tests_mimic.rb +14 -0
  124. data/test/tests_mimic_addition.rb +7 -0
  125. metadata +253 -38
  126. data/ext/oj/cache.c +0 -148
  127. data/ext/oj/foo.rb +0 -6
  128. data/ext/oj/load.c +0 -1049
  129. data/test/a.rb +0 -38
  130. data/test/perf1.rb +0 -64
  131. data/test/perf2.rb +0 -76
  132. data/test/perf_obj_old.rb +0 -213
  133. data/test/test_mimic.rb +0 -208
@@ -0,0 +1,470 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ # frozen_string_literal: false
5
+
6
+ require 'json_gem/test_helper'
7
+
8
+ require 'stringio'
9
+ require 'tempfile'
10
+ require 'ostruct'
11
+
12
+ class JSONParserTest < Test::Unit::TestCase
13
+ include Test::Unit::TestCaseOmissionSupport
14
+
15
+ def test_construction
16
+ parser = JSON::Parser.new('test')
17
+ assert_equal 'test', parser.source
18
+ end
19
+
20
+ def test_argument_encoding
21
+ source = "{}".encode("UTF-16")
22
+ JSON::Parser.new(source)
23
+ assert_equal Encoding::UTF_16, source.encoding
24
+ end if defined?(Encoding::UTF_16)
25
+
26
+ def test_error_message_encoding
27
+ bug10705 = '[ruby-core:67386] [Bug #10705]'
28
+ json = ".\"\xE2\x88\x9A\"".force_encoding(Encoding::UTF_8)
29
+ e = assert_raise(JSON::ParserError) {
30
+ JSON::Ext::Parser.new(json).parse
31
+ }
32
+ assert_equal(Encoding::UTF_8, e.message.encoding, bug10705)
33
+ assert_include(e.message, json, bug10705)
34
+ end if defined?(Encoding::UTF_8)
35
+
36
+ def test_parsing
37
+ parser = JSON::Parser.new('"test"')
38
+ assert_equal 'test', parser.parse
39
+ end
40
+
41
+ def test_parser_reset
42
+ parser = JSON::Parser.new('{"a":"b"}')
43
+ assert_equal({ 'a' => 'b' }, parser.parse)
44
+ assert_equal({ 'a' => 'b' }, parser.parse)
45
+ end
46
+
47
+ def test_parse_values
48
+ assert_equal(nil, JSON.parse('null'))
49
+ assert_equal(false, JSON.parse('false'))
50
+ assert_equal(true, JSON.parse('true'))
51
+ assert_equal(-23, JSON.parse('-23'))
52
+ assert_equal(23, JSON.parse('23'))
53
+ assert_in_delta(0.23, JSON.parse('0.23'), 1e-2)
54
+ assert_in_delta(0.0, JSON.parse('0e0'), 1e-2)
55
+ assert_equal("", JSON.parse('""'))
56
+ assert_equal("foobar", JSON.parse('"foobar"'))
57
+ end
58
+
59
+ def test_parse_simple_arrays
60
+ assert_equal([], JSON.parse('[]'))
61
+ assert_equal([], JSON.parse(' [ ] '))
62
+ assert_equal([ nil ], JSON.parse('[null]'))
63
+ assert_equal([ false ], JSON.parse('[false]'))
64
+ assert_equal([ true ], JSON.parse('[true]'))
65
+ assert_equal([ -23 ], JSON.parse('[-23]'))
66
+ assert_equal([ 23 ], JSON.parse('[23]'))
67
+ assert_equal_float([ 0.23 ], JSON.parse('[0.23]'))
68
+ assert_equal_float([ 0.0 ], JSON.parse('[0e0]'))
69
+ assert_equal([""], JSON.parse('[""]'))
70
+ assert_equal(["foobar"], JSON.parse('["foobar"]'))
71
+ assert_equal([{}], JSON.parse('[{}]'))
72
+ end
73
+
74
+ def test_parse_simple_objects
75
+ assert_equal({}, JSON.parse('{}'))
76
+ assert_equal({}, JSON.parse(' { } '))
77
+ assert_equal({ "a" => nil }, JSON.parse('{ "a" : null}'))
78
+ assert_equal({ "a" => nil }, JSON.parse('{"a":null}'))
79
+ assert_equal({ "a" => false }, JSON.parse('{ "a" : false } '))
80
+ assert_equal({ "a" => false }, JSON.parse('{"a":false}'))
81
+ assert_raise(JSON::ParserError) { JSON.parse('{false}') }
82
+ assert_equal({ "a" => true }, JSON.parse('{"a":true}'))
83
+ assert_equal({ "a" => true }, JSON.parse(' { "a" : true } '))
84
+ assert_equal({ "a" => -23 }, JSON.parse(' { "a" : -23 } '))
85
+ assert_equal({ "a" => -23 }, JSON.parse(' { "a" : -23 } '))
86
+ assert_equal({ "a" => 23 }, JSON.parse('{"a":23 } '))
87
+ assert_equal({ "a" => 23 }, JSON.parse(' { "a" : 23 } '))
88
+ assert_equal({ "a" => 0.23 }, JSON.parse(' { "a" : 0.23 } '))
89
+ assert_equal({ "a" => 0.23 }, JSON.parse(' { "a" : 0.23 } '))
90
+ end
91
+
92
+ def test_parse_numbers
93
+ assert_raise(JSON::ParserError) { JSON.parse('+23.2') }
94
+ assert_raise(JSON::ParserError) { JSON.parse('+23') }
95
+ assert_raise(JSON::ParserError) { JSON.parse('.23') }
96
+ assert_raise(JSON::ParserError) { JSON.parse('023') }
97
+ assert_equal 23, JSON.parse('23')
98
+ assert_equal -23, JSON.parse('-23')
99
+ assert_equal_float 3.141, JSON.parse('3.141')
100
+ assert_equal_float -3.141, JSON.parse('-3.141')
101
+ assert_equal_float 3.141, JSON.parse('3141e-3')
102
+ assert_equal_float 3.141, JSON.parse('3141.1e-3')
103
+ assert_equal_float 3.141, JSON.parse('3141E-3')
104
+ assert_equal_float 3.141, JSON.parse('3141.0E-3')
105
+ assert_equal_float -3.141, JSON.parse('-3141.0e-3')
106
+ assert_equal_float -3.141, JSON.parse('-3141e-3')
107
+ assert_raise(JSON::ParserError) { JSON.parse('NaN') }
108
+ assert JSON.parse('NaN', :allow_nan => true).nan?
109
+ assert_raise(JSON::ParserError) { JSON.parse('Infinity') }
110
+ assert_equal 1.0/0, JSON.parse('Infinity', :allow_nan => true)
111
+ assert_raise(JSON::ParserError) { JSON.parse('-Infinity') }
112
+ assert_equal -1.0/0, JSON.parse('-Infinity', :allow_nan => true)
113
+ end
114
+
115
+ if Array.method_defined?(:permutation)
116
+ def test_parse_more_complex_arrays
117
+ a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
118
+ a.permutation.each do |perm|
119
+ json = JSON.pretty_generate(perm)
120
+ assert_equal perm, JSON.parse(json)
121
+ end
122
+ end
123
+
124
+ def test_parse_complex_objects
125
+ a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
126
+ a.permutation.each do |perm|
127
+ s = "a"
128
+ orig_obj = perm.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h }
129
+ json = JSON.pretty_generate(orig_obj)
130
+ assert_equal orig_obj, JSON.parse(json)
131
+ end
132
+ end
133
+ end
134
+
135
+ def test_parse_arrays
136
+ assert_equal([1,2,3], JSON.parse('[1,2,3]'))
137
+ assert_equal([1.2,2,3], JSON.parse('[1.2,2,3]'))
138
+ assert_equal([[],[[],[]]], JSON.parse('[[],[[],[]]]'))
139
+ assert_equal([], JSON.parse('[]'))
140
+ assert_equal([], JSON.parse(' [ ] '))
141
+ assert_equal([1], JSON.parse('[1]'))
142
+ assert_equal([1], JSON.parse(' [ 1 ] '))
143
+ ary = [[1], ["foo"], [3.14], [4711.0], [2.718], [nil],
144
+ [[1, -2, 3]], [false], [true]]
145
+ assert_equal(ary,
146
+ JSON.parse('[[1],["foo"],[3.14],[47.11e+2],[2718.0E-3],[null],[[1,-2,3]],[false],[true]]'))
147
+ assert_equal(ary, JSON.parse(%Q{ [ [1] , ["foo"] , [3.14] \t , [47.11e+2]\s
148
+ , [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] }))
149
+ end
150
+
151
+ def test_parse_json_primitive_values
152
+ assert_raise(JSON::ParserError) { JSON.parse('') }
153
+ assert_raise(TypeError) { JSON.parse(nil) }
154
+ assert_raise(JSON::ParserError) { JSON.parse(' /* foo */ ') }
155
+ assert_equal nil, JSON.parse('null')
156
+ assert_equal false, JSON.parse('false')
157
+ assert_equal true, JSON.parse('true')
158
+ assert_equal 23, JSON.parse('23')
159
+ assert_equal 1, JSON.parse('1')
160
+ assert_equal_float 3.141, JSON.parse('3.141'), 1E-3
161
+ assert_equal 2 ** 64, JSON.parse('18446744073709551616')
162
+ assert_equal 'foo', JSON.parse('"foo"')
163
+ assert JSON.parse('NaN', :allow_nan => true).nan?
164
+ assert JSON.parse('Infinity', :allow_nan => true).infinite?
165
+ assert JSON.parse('-Infinity', :allow_nan => true).infinite?
166
+ assert_raise(JSON::ParserError) { JSON.parse('[ 1, ]') }
167
+ end
168
+
169
+ def test_parse_some_strings
170
+ assert_equal([""], JSON.parse('[""]'))
171
+ assert_equal(["\\"], JSON.parse('["\\\\"]'))
172
+ assert_equal(['"'], JSON.parse('["\""]'))
173
+ assert_equal(['\\"\\'], JSON.parse('["\\\\\\"\\\\"]'))
174
+ assert_equal(
175
+ ["\"\b\n\r\t\0\037"],
176
+ JSON.parse('["\"\b\n\r\t\u0000\u001f"]')
177
+ )
178
+ end
179
+
180
+ def test_parse_big_integers
181
+ json1 = JSON(orig = (1 << 31) - 1)
182
+ assert_equal orig, JSON.parse(json1)
183
+ json2 = JSON(orig = 1 << 31)
184
+ assert_equal orig, JSON.parse(json2)
185
+ json3 = JSON(orig = (1 << 62) - 1)
186
+ assert_equal orig, JSON.parse(json3)
187
+ json4 = JSON(orig = 1 << 62)
188
+ assert_equal orig, JSON.parse(json4)
189
+ json5 = JSON(orig = 1 << 64)
190
+ assert_equal orig, JSON.parse(json5)
191
+ end
192
+
193
+ def test_some_wrong_inputs
194
+ assert_raise(JSON::ParserError) { JSON.parse('[] bla') }
195
+ assert_raise(JSON::ParserError) { JSON.parse('[] 1') }
196
+ assert_raise(JSON::ParserError) { JSON.parse('[] []') }
197
+ assert_raise(JSON::ParserError) { JSON.parse('[] {}') }
198
+ assert_raise(JSON::ParserError) { JSON.parse('{} []') }
199
+ assert_raise(JSON::ParserError) { JSON.parse('{} {}') }
200
+ assert_raise(JSON::ParserError) { JSON.parse('[NULL]') }
201
+ assert_raise(JSON::ParserError) { JSON.parse('[FALSE]') }
202
+ assert_raise(JSON::ParserError) { JSON.parse('[TRUE]') }
203
+ assert_raise(JSON::ParserError) { JSON.parse('[07] ') }
204
+ assert_raise(JSON::ParserError) { JSON.parse('[0a]') }
205
+ assert_raise(JSON::ParserError) { JSON.parse('[1.]') }
206
+ assert_raise(JSON::ParserError) { JSON.parse(' ') }
207
+ end
208
+
209
+ def test_symbolize_names
210
+ assert_equal({ "foo" => "bar", "baz" => "quux" },
211
+ JSON.parse('{"foo":"bar", "baz":"quux"}'))
212
+ assert_equal({ :foo => "bar", :baz => "quux" },
213
+ JSON.parse('{"foo":"bar", "baz":"quux"}', :symbolize_names => true))
214
+ assert_raise(ArgumentError) do
215
+ JSON.parse('{}', :symbolize_names => true, :create_additions => true)
216
+ end
217
+ end
218
+
219
+ def test_parse_comments
220
+ json = <<EOT
221
+ {
222
+ "key1":"value1", // eol comment
223
+ "key2":"value2" /* multi line
224
+ * comment */,
225
+ "key3":"value3" /* multi line
226
+ // nested eol comment
227
+ * comment */
228
+ }
229
+ EOT
230
+ assert_equal(
231
+ { "key1" => "value1", "key2" => "value2", "key3" => "value3" },
232
+ JSON.parse(json))
233
+ json = <<EOT
234
+ {
235
+ "key1":"value1" /* multi line
236
+ // nested eol comment
237
+ /* illegal nested multi line comment */
238
+ * comment */
239
+ }
240
+ EOT
241
+ assert_raise(JSON::ParserError) { JSON.parse(json) }
242
+ json = <<EOT
243
+ {
244
+ "key1":"value1" /* multi line
245
+ // nested eol comment
246
+ closed multi comment */
247
+ and again, throw an Error */
248
+ }
249
+ EOT
250
+ assert_raise(JSON::ParserError) { JSON.parse(json) }
251
+ json = <<EOT
252
+ {
253
+ "key1":"value1" /*/*/
254
+ }
255
+ EOT
256
+ assert_equal({ "key1" => "value1" }, JSON.parse(json))
257
+ end
258
+
259
+ def test_nesting
260
+ too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
261
+ too_deep_ary = eval too_deep
262
+ assert_raise(JSON::NestingError) { JSON.parse too_deep }
263
+ assert_raise(JSON::NestingError) { JSON.parse too_deep, :max_nesting => 100 }
264
+ ok = JSON.parse too_deep, :max_nesting => 101
265
+ assert_equal too_deep_ary, ok
266
+ ok = JSON.parse too_deep, :max_nesting => nil
267
+ assert_equal too_deep_ary, ok
268
+ ok = JSON.parse too_deep, :max_nesting => false
269
+ assert_equal too_deep_ary, ok
270
+ ok = JSON.parse too_deep, :max_nesting => 0
271
+ assert_equal too_deep_ary, ok
272
+ end
273
+
274
+ def test_backslash
275
+ data = [ '\\.(?i:gif|jpe?g|png)$' ]
276
+ json = '["\\\\.(?i:gif|jpe?g|png)$"]'
277
+ assert_equal data, JSON.parse(json)
278
+ #
279
+ data = [ '\\"' ]
280
+ json = '["\\\\\""]'
281
+ assert_equal data, JSON.parse(json)
282
+ #
283
+ json = '["/"]'
284
+ data = [ '/' ]
285
+ assert_equal data, JSON.parse(json)
286
+ #
287
+ json = '["\""]'
288
+ data = ['"']
289
+ assert_equal data, JSON.parse(json)
290
+ #
291
+ json = '["\\\'"]'
292
+ data = ["'"]
293
+ assert_equal data, JSON.parse(json)
294
+ end
295
+
296
+ class SubArray < Array
297
+ def <<(v)
298
+ @shifted = true
299
+ super
300
+ end
301
+
302
+ def shifted?
303
+ @shifted
304
+ end
305
+ end
306
+
307
+ class SubArray2 < Array
308
+ def to_json(*a)
309
+ {
310
+ JSON.create_id => self.class.name,
311
+ 'ary' => to_a,
312
+ }.to_json(*a)
313
+ end
314
+
315
+ def self.json_create(o)
316
+ o.delete JSON.create_id
317
+ o['ary']
318
+ end
319
+ end
320
+
321
+ class SubArrayWrapper
322
+ def initialize
323
+ @data = []
324
+ end
325
+
326
+ attr_reader :data
327
+
328
+ def [](index)
329
+ @data[index]
330
+ end
331
+
332
+ def <<(value)
333
+ @data << value
334
+ @shifted = true
335
+ end
336
+
337
+ def shifted?
338
+ @shifted
339
+ end
340
+ end
341
+
342
+ def test_parse_array_custom_array_derived_class
343
+ res = JSON.parse('[1,2]', :array_class => SubArray)
344
+ assert_equal([1,2], res)
345
+ assert_equal(SubArray, res.class)
346
+ assert res.shifted?
347
+ end
348
+
349
+ def test_parse_array_custom_non_array_derived_class
350
+ res = JSON.parse('[1,2]', :array_class => SubArrayWrapper)
351
+ assert_equal(SubArrayWrapper, res.class)
352
+ assert_equal([1,2], res.data)
353
+ assert res.shifted?
354
+ end
355
+
356
+ def test_parse_object
357
+ assert_equal({}, JSON.parse('{}'))
358
+ assert_equal({}, JSON.parse(' { } '))
359
+ assert_equal({'foo'=>'bar'}, JSON.parse('{"foo":"bar"}'))
360
+ assert_equal({'foo'=>'bar'}, JSON.parse(' { "foo" : "bar" } '))
361
+ end
362
+
363
+ class SubHash < Hash
364
+ def []=(k, v)
365
+ @item_set = true
366
+ super
367
+ end
368
+
369
+ def item_set?
370
+ @item_set
371
+ end
372
+ end
373
+
374
+ class SubHash2 < Hash
375
+ def to_json(*a)
376
+ {
377
+ JSON.create_id => self.class.name,
378
+ }.merge(self).to_json(*a)
379
+ end
380
+
381
+ def self.json_create(o)
382
+ o.delete JSON.create_id
383
+ self[o]
384
+ end
385
+ end
386
+
387
+ class SubOpenStruct < OpenStruct
388
+ def [](k)
389
+ __send__(k)
390
+ end
391
+
392
+ def []=(k, v)
393
+ @item_set = true
394
+ __send__("#{k}=", v)
395
+ end
396
+
397
+ def item_set?
398
+ @item_set
399
+ end
400
+ end
401
+
402
+ def test_parse_object_custom_hash_derived_class
403
+ res = JSON.parse('{"foo":"bar"}', :object_class => SubHash)
404
+ assert_equal(SubHash, res.class)
405
+ assert_equal({"foo" => "bar"}, res)
406
+ assert res.item_set?
407
+ end
408
+
409
+ def test_parse_object_custom_non_hash_derived_class
410
+ res = JSON.parse('{"foo":"bar"}', :object_class => SubOpenStruct)
411
+ assert_equal(SubOpenStruct, res.class)
412
+ assert_equal "bar", res.foo
413
+ assert res.item_set?
414
+ end
415
+
416
+ def test_parse_generic_object
417
+ res = JSON.parse(
418
+ '{"foo":"bar", "baz":{}}',
419
+ :object_class => JSON::GenericObject
420
+ )
421
+ assert_equal(JSON::GenericObject, res.class)
422
+ assert_equal "bar", res.foo
423
+ assert_equal "bar", res["foo"]
424
+ assert_equal "bar", res[:foo]
425
+ assert_equal "bar", res.to_hash[:foo]
426
+ assert_equal(JSON::GenericObject, res.baz.class)
427
+ end if defined?(JSON::GenericObject)
428
+
429
+ def test_generate_core_subclasses_with_new_to_json
430
+ obj = SubHash2["foo" => SubHash2["bar" => true]]
431
+ obj_json = JSON(obj)
432
+ obj_again = JSON.parse(obj_json, :create_additions => true)
433
+ assert_kind_of SubHash2, obj_again
434
+ assert_kind_of SubHash2, obj_again['foo']
435
+ assert obj_again['foo']['bar']
436
+ assert_equal obj, obj_again
437
+ assert_equal ["foo"],
438
+ JSON(JSON(SubArray2["foo"]), :create_additions => true)
439
+ end
440
+
441
+ def test_generate_core_subclasses_with_default_to_json
442
+ assert_equal '{"foo":"bar"}', JSON(SubHash["foo" => "bar"])
443
+ assert_equal '["foo"]', JSON(SubArray["foo"])
444
+ end
445
+
446
+ def test_generate_of_core_subclasses
447
+ obj = SubHash["foo" => SubHash["bar" => true]]
448
+ obj_json = JSON(obj)
449
+ obj_again = JSON(obj_json)
450
+ assert_kind_of Hash, obj_again
451
+ assert_kind_of Hash, obj_again['foo']
452
+ assert obj_again['foo']['bar']
453
+ assert_equal obj, obj_again
454
+ end
455
+
456
+ def test_parsing_frozen_ascii8bit_string
457
+ assert_equal(
458
+ { 'foo' => 'bar' },
459
+ JSON('{ "foo": "bar" }'.force_encoding(Encoding::ASCII_8BIT).freeze)
460
+ )
461
+ end
462
+
463
+ private
464
+
465
+ def assert_equal_float(expected, actual, delta = 1e-2)
466
+ Array === expected and expected = expected.first
467
+ Array === actual and actual = actual.first
468
+ assert_in_delta(expected, actual, delta)
469
+ end
470
+ end
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+ #frozen_string_literal: false
4
+
5
+ require 'json_gem/test_helper'
6
+
7
+ require 'time'
8
+
9
+ class JSONStringMatchingTest < Test::Unit::TestCase
10
+ include Test::Unit::TestCaseOmissionSupport
11
+
12
+ class TestTime < ::Time
13
+ def self.json_create(string)
14
+ Time.parse(string)
15
+ end
16
+
17
+ def to_json(*)
18
+ %{"#{strftime('%FT%T%z')}"}
19
+ end
20
+
21
+ def ==(other)
22
+ to_i == other.to_i
23
+ end
24
+ end
25
+
26
+ def test_match_date
27
+ t = TestTime.new
28
+ t_json = [ t ].to_json
29
+ time_regexp = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\z/
30
+ assert_equal [ t ],
31
+ JSON.parse(
32
+ t_json,
33
+ :create_additions => true,
34
+ :match_string => { time_regexp => TestTime }
35
+ )
36
+ assert_equal [ t.strftime('%FT%T%z') ],
37
+ JSON.parse(
38
+ t_json,
39
+ :match_string => { time_regexp => TestTime }
40
+ )
41
+ end
42
+ end
@@ -0,0 +1,18 @@
1
+ $: << File.dirname(__FILE__)
2
+ $oj_dir = File.dirname(File.dirname(File.expand_path(File.dirname(__FILE__))))
3
+ %w(lib ext).each do |dir|
4
+ $: << File.join($oj_dir, dir)
5
+ end
6
+
7
+ require 'test/unit'
8
+ REAL_JSON_GEM = !!ENV['REAL_JSON_GEM']
9
+
10
+ if ENV['REAL_JSON_GEM']
11
+ require 'json'
12
+ else
13
+ require 'oj'
14
+ Oj.mimic_JSON
15
+ end
16
+
17
+ NaN = JSON::NaN if defined?(JSON::NaN)
18
+ NaN = 0.0/0 unless defined?(NaN)
@@ -0,0 +1,130 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ $: << '.'
5
+ $: << File.join(File.dirname(__FILE__), "../lib")
6
+ $: << File.join(File.dirname(__FILE__), "../ext")
7
+
8
+ require 'optparse'
9
+ require 'perf'
10
+ require 'oj'
11
+
12
+ $verbose = false
13
+ $indent = 0
14
+ $iter = 20000
15
+ $size = 0
16
+
17
+ opts = OptionParser.new
18
+ opts.on("-v", "verbose") { $verbose = true }
19
+ opts.on("-c", "--count [Int]", Integer, "iterations") { |i| $iter = i }
20
+ opts.on("-i", "--indent [Int]", Integer, "indentation") { |i| $indent = i }
21
+ opts.on("-s", "--size [Int]", Integer, "size (~Kbytes)") { |i| $size = i }
22
+ opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
23
+ files = opts.parse(ARGV)
24
+
25
+ def capture_error(tag, orig, load_key, dump_key, &blk)
26
+ begin
27
+ obj = blk.call(orig)
28
+ puts obj unless orig == obj
29
+ raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
30
+ rescue Exception => e
31
+ $failed[tag] = "#{e.class}: #{e.message}"
32
+ end
33
+ end
34
+
35
+ # Verify that all packages dump and load correctly and return the same Object as the original.
36
+ capture_error('Oj:compat', $obj, 'load', 'dump') { |o| Oj.compat_load(Oj.dump(o, :mode => :compat)) }
37
+ capture_error('JSON::Ext', $obj, 'generate', 'parse') { |o|
38
+ require 'json'
39
+ require 'json/ext'
40
+ JSON.generator = JSON::Ext::Generator
41
+ JSON.parser = JSON::Ext::Parser
42
+ JSON.load(JSON.generate(o))
43
+ }
44
+
45
+ module One
46
+ module Two
47
+ module Three
48
+ class Empty
49
+
50
+ def initialize()
51
+ @a = 1
52
+ @b = 2
53
+ @c = 3
54
+ end
55
+
56
+ def eql?(o)
57
+ self.class == o.class && @a == o.a && @b = o.b && @c = o.c
58
+ end
59
+ alias == eql?
60
+
61
+ def as_json(*a)
62
+ {JSON.create_id => self.class.name, 'a' => @a, 'b' => @b, 'c' => @c }
63
+ end
64
+
65
+ def to_json(*a)
66
+ JSON.generate(as_json())
67
+ end
68
+
69
+ def self.json_create(h)
70
+ self.new()
71
+ end
72
+ end # Empty
73
+ end # Three
74
+ end # Two
75
+ end # One
76
+
77
+ $obj = {
78
+ 'a' => 'Alpha', # string
79
+ 'b' => true, # boolean
80
+ 'c' => 12345, # number
81
+ 'd' => [ true, [false, [-123456789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
82
+ 'e' => { 'zero' => nil, 'one' => 1, 'two' => 2, 'three' => [3], 'four' => [0, 1, 2, 3, 4] }, # hash
83
+ 'f' => nil, # nil
84
+ 'g' => One::Two::Three::Empty.new(),
85
+ 'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
86
+ 'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
87
+ }
88
+
89
+ Oj.default_options = { :indent => $indent, :mode => :compat, :use_to_json => true, :create_additions => true, :create_id => '^o' }
90
+
91
+ if 0 < $size
92
+ s = Oj.dump($obj).size + 1
93
+ cnt = $size * 1024 / s
94
+ o = $obj
95
+ $obj = []
96
+ cnt.times do
97
+ $obj << o
98
+ end
99
+ end
100
+
101
+ $json = Oj.dump($obj)
102
+ $failed = {} # key is same as String used in tests later
103
+
104
+ if $verbose
105
+ puts "size: #{$json.size}"
106
+ puts "json:\n#{$json}\n"
107
+ puts "Oj:compat loaded object:\n#{Oj.compat_load($json)}\n"
108
+ puts "JSON loaded object:\n#{JSON::Ext::Parser.new($json).parse}\n"
109
+ end
110
+
111
+ puts '-' * 80
112
+ puts "Compat Parse Performance"
113
+ perf = Perf.new()
114
+ unless $failed.has_key?('JSON::Ext')
115
+ perf.add('JSON::Ext', 'parse') { JSON.load($json) }
116
+ perf.before('JSON::Ext') { JSON.parser = JSON::Ext::Parser }
117
+ end
118
+ unless $failed.has_key?('Oj:compat')
119
+ perf.add('Oj:compat', 'compat_load') { Oj.compat_load($json) }
120
+ end
121
+ perf.run($iter)
122
+
123
+ puts
124
+ puts '-' * 80
125
+ puts
126
+
127
+ unless $failed.empty?
128
+ puts "The following packages were not included for the reason listed"
129
+ $failed.each { |tag,msg| puts "***** #{tag}: #{msg}" }
130
+ end