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
@@ -1,396 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: utf-8
3
-
4
- # frozen_string_literal: false
5
-
6
- require 'json_gem/test_helper'
7
-
8
- class JSONGeneratorTest < Test::Unit::TestCase
9
- include Test::Unit::TestCaseOmissionSupport
10
- include Test::Unit::TestCasePendingSupport
11
-
12
- def setup
13
- @hash = {
14
- 'a' => 2,
15
- 'b' => 3.141,
16
- 'c' => 'c',
17
- 'd' => [ 1, "b", 3.14 ],
18
- 'e' => { 'foo' => 'bar' },
19
- 'g' => "\"\0\037",
20
- 'h' => 1000.0,
21
- 'i' => 0.001
22
- }
23
- @json2 = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' +
24
- '"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
25
- @json3 = <<'EOT'.chomp
26
- {
27
- "a": 2,
28
- "b": 3.141,
29
- "c": "c",
30
- "d": [
31
- 1,
32
- "b",
33
- 3.14
34
- ],
35
- "e": {
36
- "foo": "bar"
37
- },
38
- "g": "\"\u0000\u001f",
39
- "h": 1000.0,
40
- "i": 0.001
41
- }
42
- EOT
43
- end
44
-
45
- def test_generate
46
- json = JSON.generate(@hash)
47
- assert_equal(JSON.parse(@json2), JSON.parse(json))
48
- json = JSON[@hash]
49
- assert_equal(JSON.parse(@json2), JSON.parse(json))
50
- parsed_json = JSON.parse(json)
51
- assert_equal(@hash, parsed_json)
52
- json = JSON.generate({1=>2})
53
- assert_equal('{"1":2}', json)
54
- parsed_json = JSON.parse(json)
55
- assert_equal({"1"=>2}, parsed_json)
56
- assert_equal '666', JSON.generate(666)
57
- end
58
-
59
- def test_generate_pretty
60
- json = JSON.pretty_generate(@hash)
61
- # hashes aren't (insertion) ordered on every ruby implementation
62
- assert_equal(@json3, json)
63
- assert_equal(JSON.parse(@json3), JSON.parse(json))
64
- parsed_json = JSON.parse(json)
65
- assert_equal(@hash, parsed_json)
66
- json = JSON.pretty_generate({1=>2})
67
- assert_equal(<<'EOT'.chomp, json)
68
- {
69
- "1": 2
70
- }
71
- EOT
72
- parsed_json = JSON.parse(json)
73
- assert_equal({"1"=>2}, parsed_json)
74
- assert_equal '666', JSON.pretty_generate(666)
75
- json_nil_opts = JSON.pretty_generate({1=>2}, nil)
76
- assert_equal json, json_nil_opts
77
- end
78
-
79
- def test_generate_custom
80
- state = JSON::State.new(:space_before => " ", :space => " ", :indent => "<i>", :object_nl => "\n", :array_nl => "<a_nl>")
81
- json = JSON.generate({1=>{2=>3,4=>[5,6]}}, state)
82
- assert_equal(<<'EOT'.chomp, json)
83
- {
84
- <i>"1" : {
85
- <i><i>"2" : 3,
86
- <i><i>"4" : [<a_nl><i><i><i>5,<a_nl><i><i><i>6<a_nl><i><i>]
87
- <i>}
88
- }
89
- EOT
90
- end
91
-
92
- def test_fast_generate
93
- json = JSON.fast_generate(@hash)
94
- assert_equal(JSON.parse(@json2), JSON.parse(json))
95
- parsed_json = JSON.parse(json)
96
- assert_equal(@hash, parsed_json)
97
- json = JSON.fast_generate({1=>2})
98
- assert_equal('{"1":2}', json)
99
- parsed_json = JSON.parse(json)
100
- assert_equal({"1"=>2}, parsed_json)
101
- assert_equal '666', JSON.fast_generate(666)
102
- end
103
-
104
- def test_own_state
105
- state = JSON::State.new
106
- json = JSON.generate(@hash, state)
107
- assert_equal(JSON.parse(@json2), JSON.parse(json))
108
- parsed_json = JSON.parse(json)
109
- assert_equal(@hash, parsed_json)
110
- json = JSON.generate({1=>2}, state)
111
- assert_equal('{"1":2}', json)
112
- parsed_json = JSON.parse(json)
113
- assert_equal({"1"=>2}, parsed_json)
114
- assert_equal '666', JSON.generate(666, state)
115
- end
116
-
117
- # TBD Implement JSON.state to return state class.
118
- # set state attributes from defaults
119
- # implement methods
120
- # circular should use circular in defaults or maybe always set to true, allow changes with [:check_circular]=
121
- def test_states
122
- json = JSON.generate({1=>2}, nil)
123
- assert_equal('{"1":2}', json)
124
- s = JSON.state.new
125
- assert s.check_circular?
126
- assert s[:check_circular?]
127
- h = { 1=>2 }
128
- h[3] = h
129
- assert_raise(JSON::NestingError) { JSON.generate(h) }
130
- assert_raise(JSON::NestingError) { JSON.generate(h, s) }
131
- s = JSON.state.new
132
- a = [ 1, 2 ]
133
- a << a
134
- assert_raise(JSON::NestingError) { JSON.generate(a, s) }
135
- assert s.check_circular?
136
- assert s[:check_circular?]
137
- end
138
-
139
- def test_pretty_state
140
- state = JSON::PRETTY_STATE_PROTOTYPE.dup
141
- # In come cases in Ruby 3.0 an :escape_slash is included in the state. It
142
- # seems to occur on travis but not locally.
143
- actual = state.to_h
144
- actual.delete(:escape_slash)
145
- assert_equal({
146
- :allow_nan => false,
147
- :array_nl => "\n",
148
- :ascii_only => false,
149
- :buffer_initial_length => 1024,
150
- :depth => 0,
151
- :indent => " ",
152
- :max_nesting => 100,
153
- :object_nl => "\n",
154
- :space => " ",
155
- :space_before => "",
156
- }.sort_by { |n,| n.to_s }, actual.sort_by { |n,| n.to_s })
157
- end
158
-
159
- def test_safe_state
160
- state = JSON::SAFE_STATE_PROTOTYPE.dup
161
- # In come cases in Ruby 3.0 an :escape_slash is included in the state. It
162
- # seems to occur on travis but not locally.
163
- actual = state.to_h
164
- actual.delete(:escape_slash)
165
- assert_equal({
166
- :allow_nan => false,
167
- :array_nl => "",
168
- :ascii_only => false,
169
- :buffer_initial_length => 1024,
170
- :depth => 0,
171
- :indent => "",
172
- :max_nesting => 100,
173
- :object_nl => "",
174
- :space => "",
175
- :space_before => "",
176
- }.sort_by { |n,| n.to_s }, actual.sort_by { |n,| n.to_s })
177
- end
178
-
179
- def test_fast_state
180
- state = JSON::FAST_STATE_PROTOTYPE.dup
181
- # In come cases in Ruby 3.0 an :escape_slash is included in the state. It
182
- # seems to occur on travis but not locally.
183
- actual = state.to_h
184
- actual.delete(:escape_slash)
185
- assert_equal({
186
- :allow_nan => false,
187
- :array_nl => "",
188
- :ascii_only => false,
189
- :buffer_initial_length => 1024,
190
- :depth => 0,
191
- :indent => "",
192
- :max_nesting => 0,
193
- :object_nl => "",
194
- :space => "",
195
- :space_before => "",
196
- }.sort_by { |n,| n.to_s }, actual.sort_by { |n,| n.to_s })
197
- end
198
-
199
- def test_allow_nan
200
- assert_raise(JSON::GeneratorError) { JSON.generate([JSON::NaN]) }
201
- assert_equal '[NaN]', JSON.generate([JSON::NaN], :allow_nan => true)
202
- assert_raise(JSON::GeneratorError) { JSON.fast_generate([JSON::NaN]) }
203
- assert_raise(JSON::GeneratorError) { JSON.pretty_generate([JSON::NaN]) }
204
- assert_equal "[\n NaN\n]", JSON.pretty_generate([JSON::NaN], :allow_nan => true)
205
- assert_raise(JSON::GeneratorError) { JSON.generate([JSON::Infinity]) }
206
- assert_equal '[Infinity]', JSON.generate([JSON::Infinity], :allow_nan => true)
207
- assert_raise(JSON::GeneratorError) { JSON.fast_generate([JSON::Infinity]) }
208
- assert_raise(JSON::GeneratorError) { JSON.pretty_generate([JSON::Infinity]) }
209
- assert_equal "[\n Infinity\n]", JSON.pretty_generate([JSON::Infinity], :allow_nan => true)
210
- assert_raise(JSON::GeneratorError) { JSON.generate([JSON::MinusInfinity]) }
211
- assert_equal '[-Infinity]', JSON.generate([JSON::MinusInfinity], :allow_nan => true)
212
- assert_raise(JSON::GeneratorError) { JSON.fast_generate([JSON::MinusInfinity]) }
213
- assert_raise(JSON::GeneratorError) { JSON.pretty_generate([JSON::MinusInfinity]) }
214
- assert_equal "[\n -Infinity\n]", JSON.pretty_generate([JSON::MinusInfinity], :allow_nan => true)
215
- end
216
-
217
- def test_depth
218
- ary = []; ary << ary
219
- assert_equal 0, JSON::SAFE_STATE_PROTOTYPE.depth
220
- assert_raise(JSON::NestingError) { JSON.generate(ary) }
221
- assert_equal 0, JSON::SAFE_STATE_PROTOTYPE.depth
222
- assert_equal 0, JSON::PRETTY_STATE_PROTOTYPE.depth
223
- assert_raise(JSON::NestingError) { JSON.pretty_generate(ary) }
224
- assert_equal 0, JSON::PRETTY_STATE_PROTOTYPE.depth
225
- s = JSON.state.new
226
- assert_equal 0, s.depth
227
- assert_raise(JSON::NestingError) { ary.to_json(s) }
228
- assert_equal 100, s.depth
229
- end
230
-
231
- def test_buffer_initial_length
232
- s = JSON.state.new
233
- assert_equal 1024, s.buffer_initial_length
234
- s.buffer_initial_length = 0
235
- assert_equal 1024, s.buffer_initial_length
236
- s.buffer_initial_length = -1
237
- assert_equal 1024, s.buffer_initial_length
238
- s.buffer_initial_length = 128
239
- assert_equal 128, s.buffer_initial_length
240
- end
241
-
242
- def test_gc
243
- if respond_to?(:assert_in_out_err)
244
- assert_in_out_err(%w[-rjson --disable-gems], <<-EOS, [], [])
245
- bignum_too_long_to_embed_as_string = 1234567890123456789012345
246
- expect = bignum_too_long_to_embed_as_string.to_s
247
- GC.stress = true
248
-
249
- 10.times do |i|
250
- tmp = bignum_too_long_to_embed_as_string.to_json
251
- raise "'\#{expect}' is expected, but '\#{tmp}'" unless tmp == expect
252
- end
253
- EOS
254
- end
255
- end if GC.respond_to?(:stress=)
256
-
257
- def test_configure_using_configure_and_merge
258
- numbered_state = {
259
- :indent => "1",
260
- :space => '2',
261
- :space_before => '3',
262
- :object_nl => '4',
263
- :array_nl => '5'
264
- }
265
- state1 = JSON.state.new
266
- state1.merge(numbered_state)
267
- assert_equal '1', state1.indent
268
- assert_equal '2', state1.space
269
- assert_equal '3', state1.space_before
270
- assert_equal '4', state1.object_nl
271
- assert_equal '5', state1.array_nl
272
- state2 = JSON.state.new
273
- state2.configure(numbered_state)
274
- assert_equal '1', state2.indent
275
- assert_equal '2', state2.space
276
- assert_equal '3', state2.space_before
277
- assert_equal '4', state2.object_nl
278
- assert_equal '5', state2.array_nl
279
- end
280
-
281
- def test_configure_hash_conversion
282
- state = JSON.state.new
283
- state.configure(:indent => '1')
284
- assert_equal '1', state.indent
285
- state = JSON.state.new
286
- foo = 'foo'
287
- assert_raise(TypeError) do
288
- state.configure(foo)
289
- end
290
- def foo.to_h
291
- { :indent => '2' }
292
- end
293
- state.configure(foo)
294
- assert_equal '2', state.indent
295
- end
296
-
297
- if defined?(JSON::Ext::Generator) && Process.respond_to?(:fork)
298
- # forking to avoid modifying core class of a parent process and
299
- # introducing race conditions of tests are run in parallel
300
- def test_broken_bignum # [ruby-core:38867]
301
- pid = fork do
302
- x = 1 << 64
303
- x.class.class_eval do
304
- def to_s
305
- end
306
- end
307
- begin
308
- j = JSON::Ext::Generator::State.new.generate(x)
309
- exit 1
310
- rescue TypeError
311
- exit 0
312
- end
313
- end
314
- _, status = Process.waitpid2(pid)
315
- assert status.success?
316
- end
317
- end
318
-
319
- def test_hash_likeness_set_symbol
320
- state = JSON.state.new
321
- assert_equal nil, state[:foo]
322
- assert_equal nil.class, state[:foo].class
323
- assert_equal nil, state['foo']
324
- state[:foo] = :bar
325
- assert_equal :bar, state[:foo]
326
- assert_equal :bar, state['foo']
327
- state_hash = state.to_hash
328
- assert_kind_of Hash, state_hash
329
- assert_equal :bar, state_hash[:foo]
330
- end
331
-
332
- def test_hash_likeness_set_string
333
- state = JSON.state.new
334
- assert_equal nil, state[:foo]
335
- assert_equal nil, state['foo']
336
- state['foo'] = :bar
337
- assert_equal :bar, state[:foo]
338
- assert_equal :bar, state['foo']
339
- state_hash = state.to_hash
340
- assert_kind_of Hash, state_hash
341
- assert_equal :bar, state_hash[:foo]
342
- end
343
-
344
- def test_json_generate
345
- assert_raise JSON::GeneratorError do
346
- assert_equal true, JSON.generate(["\xea"])
347
- end
348
- end
349
-
350
- def test_nesting
351
- too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
352
- too_deep_ary = eval too_deep
353
- assert_raise(JSON::NestingError) { JSON.generate too_deep_ary }
354
- assert_raise(JSON::NestingError) { JSON.generate too_deep_ary, :max_nesting => 100 }
355
- ok = JSON.generate too_deep_ary, :max_nesting => 101
356
- assert_equal too_deep, ok
357
- ok = JSON.generate too_deep_ary, :max_nesting => nil
358
- assert_equal too_deep, ok
359
- ok = JSON.generate too_deep_ary, :max_nesting => false
360
- assert_equal too_deep, ok
361
- ok = JSON.generate too_deep_ary, :max_nesting => 0
362
- assert_equal too_deep, ok
363
- end
364
-
365
- def test_backslash
366
- data = [ '\\.(?i:gif|jpe?g|png)$' ]
367
- json = '["\\\\.(?i:gif|jpe?g|png)$"]'
368
- assert_equal json, JSON.generate(data)
369
- #
370
- data = [ '\\"' ]
371
- json = '["\\\\\""]'
372
- assert_equal json, JSON.generate(data)
373
- #
374
- data = [ '/' ]
375
- json = '["/"]'
376
- assert_equal json, JSON.generate(data)
377
- #
378
- data = ['"']
379
- json = '["\""]'
380
- assert_equal json, JSON.generate(data)
381
- #
382
- data = ["'"]
383
- json = '["\\\'"]'
384
- assert_equal '["\'"]', JSON.generate(data)
385
- end
386
-
387
- def test_string_subclass
388
- s = Class.new(String) do
389
- def to_s; self; end
390
- undef to_json
391
- end
392
- assert_nothing_raised(SystemStackError) do
393
- assert_equal '["foo"]', JSON.generate([s.new('foo')])
394
- end
395
- end
396
- end
@@ -1,90 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- #frozen_string_literal: false
5
-
6
- require 'json_gem/test_helper'
7
-
8
- class JSONGenericObjectTest < Test::Unit::TestCase
9
- include Test::Unit::TestCaseOmissionSupport
10
-
11
- def setup
12
- @go = JSON::GenericObject[ :a => 1, :b => 2 ]
13
- end
14
-
15
- def test_attributes
16
- assert_equal 1, @go.a
17
- assert_equal 1, @go[:a]
18
- assert_equal 2, @go.b
19
- assert_equal 2, @go[:b]
20
- assert_nil @go.c
21
- assert_nil @go[:c]
22
- end
23
-
24
- def test_generate_json
25
- switch_json_creatable do
26
- assert_equal @go, JSON(JSON(@go), :create_additions => true)
27
- end
28
- end
29
-
30
- def test_parse_json
31
- x = JSON(
32
- '{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }',
33
- :create_additions => true
34
- )
35
- assert_kind_of Hash,
36
- JSON(
37
- '{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }',
38
- :create_additions => true
39
- )
40
- switch_json_creatable do
41
- assert_equal @go, l =
42
- JSON(
43
- '{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }',
44
- :create_additions => true
45
- )
46
- assert_equal 1, l.a
47
- assert_equal @go,
48
- l = JSON('{ "a": 1, "b": 2 }', :object_class => JSON::GenericObject)
49
- assert_equal 1, l.a
50
- assert_equal JSON::GenericObject[:a => JSON::GenericObject[:b => 2]],
51
- l = JSON('{ "a": { "b": 2 } }', :object_class => JSON::GenericObject)
52
- assert_equal 2, l.a.b
53
- end
54
- end
55
-
56
- def test_from_hash
57
- result = JSON::GenericObject.from_hash(
58
- :foo => { :bar => { :baz => true }, :quux => [ { :foobar => true } ] })
59
- assert_kind_of JSON::GenericObject, result.foo
60
- assert_kind_of JSON::GenericObject, result.foo.bar
61
- assert_equal true, result.foo.bar.baz
62
- assert_kind_of JSON::GenericObject, result.foo.quux.first
63
- assert_equal true, result.foo.quux.first.foobar
64
- assert_equal true, JSON::GenericObject.from_hash(true)
65
- end
66
-
67
- def test_json_generic_object_load
68
- empty = JSON::GenericObject.load(nil)
69
- assert_kind_of JSON::GenericObject, empty
70
- simple_json = '{"json_class":"JSON::GenericObject","hello":"world"}'
71
- simple = JSON::GenericObject.load(simple_json)
72
- assert_kind_of JSON::GenericObject, simple
73
- assert_equal "world", simple.hello
74
- converting = JSON::GenericObject.load('{ "hello": "world" }')
75
- assert_kind_of JSON::GenericObject, converting
76
- assert_equal "world", converting.hello
77
-
78
- json = JSON::GenericObject.dump(JSON::GenericObject[:hello => 'world'])
79
- assert_equal JSON(json), JSON('{"json_class":"JSON::GenericObject","hello":"world"}')
80
- end
81
-
82
- private
83
-
84
- def switch_json_creatable
85
- JSON::GenericObject.json_creatable = true
86
- yield
87
- ensure
88
- JSON::GenericObject.json_creatable = false
89
- end
90
- end