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
@@ -1,383 +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
- end
76
-
77
- def test_generate_custom
78
- state = JSON::State.new(:space_before => " ", :space => " ", :indent => "<i>", :object_nl => "\n", :array_nl => "<a_nl>")
79
- json = JSON.generate({1=>{2=>3,4=>[5,6]}}, state)
80
- assert_equal(<<'EOT'.chomp, json)
81
- {
82
- <i>"1" : {
83
- <i><i>"2" : 3,
84
- <i><i>"4" : [<a_nl><i><i><i>5,<a_nl><i><i><i>6<a_nl><i><i>]
85
- <i>}
86
- }
87
- EOT
88
- end
89
-
90
- def test_fast_generate
91
- json = JSON.fast_generate(@hash)
92
- assert_equal(JSON.parse(@json2), JSON.parse(json))
93
- parsed_json = JSON.parse(json)
94
- assert_equal(@hash, parsed_json)
95
- json = JSON.fast_generate({1=>2})
96
- assert_equal('{"1":2}', json)
97
- parsed_json = JSON.parse(json)
98
- assert_equal({"1"=>2}, parsed_json)
99
- assert_equal '666', JSON.fast_generate(666)
100
- end
101
-
102
- def test_own_state
103
- state = JSON::State.new
104
- json = JSON.generate(@hash, state)
105
- assert_equal(JSON.parse(@json2), JSON.parse(json))
106
- parsed_json = JSON.parse(json)
107
- assert_equal(@hash, parsed_json)
108
- json = JSON.generate({1=>2}, state)
109
- assert_equal('{"1":2}', json)
110
- parsed_json = JSON.parse(json)
111
- assert_equal({"1"=>2}, parsed_json)
112
- assert_equal '666', JSON.generate(666, state)
113
- end
114
-
115
- # TBD Implement JSON.state to return state class.
116
- # set state attibutes from defaults
117
- # implement methods
118
- # circular should use circular in defaults or maybe always set to true, allow changes with [:check_circular]=
119
- def test_states
120
- json = JSON.generate({1=>2}, nil)
121
- assert_equal('{"1":2}', json)
122
- s = JSON.state.new
123
- assert s.check_circular?
124
- assert s[:check_circular?]
125
- h = { 1=>2 }
126
- h[3] = h
127
- assert_raise(JSON::NestingError) { JSON.generate(h) }
128
- assert_raise(JSON::NestingError) { JSON.generate(h, s) }
129
- s = JSON.state.new
130
- a = [ 1, 2 ]
131
- a << a
132
- assert_raise(JSON::NestingError) { JSON.generate(a, s) }
133
- assert s.check_circular?
134
- assert s[:check_circular?]
135
- end
136
-
137
- def test_pretty_state
138
- state = JSON::PRETTY_STATE_PROTOTYPE.dup
139
- assert_equal({
140
- :allow_nan => false,
141
- :array_nl => "\n",
142
- :ascii_only => false,
143
- :buffer_initial_length => 1024,
144
- :depth => 0,
145
- :indent => " ",
146
- :max_nesting => 100,
147
- :object_nl => "\n",
148
- :space => " ",
149
- :space_before => "",
150
- }.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
151
- end
152
-
153
- def test_safe_state
154
- state = JSON::SAFE_STATE_PROTOTYPE.dup
155
- assert_equal({
156
- :allow_nan => false,
157
- :array_nl => "",
158
- :ascii_only => false,
159
- :buffer_initial_length => 1024,
160
- :depth => 0,
161
- :indent => "",
162
- :max_nesting => 100,
163
- :object_nl => "",
164
- :space => "",
165
- :space_before => "",
166
- }.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
167
- end
168
-
169
- def test_fast_state
170
- state = JSON::FAST_STATE_PROTOTYPE.dup
171
- assert_equal({
172
- :allow_nan => false,
173
- :array_nl => "",
174
- :ascii_only => false,
175
- :buffer_initial_length => 1024,
176
- :depth => 0,
177
- :indent => "",
178
- :max_nesting => 0,
179
- :object_nl => "",
180
- :space => "",
181
- :space_before => "",
182
- }.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
183
- end
184
-
185
- def test_allow_nan
186
- assert_raise(JSON::GeneratorError) { JSON.generate([JSON::NaN]) }
187
- assert_equal '[NaN]', JSON.generate([JSON::NaN], :allow_nan => true)
188
- assert_raise(JSON::GeneratorError) { JSON.fast_generate([JSON::NaN]) }
189
- assert_raise(JSON::GeneratorError) { JSON.pretty_generate([JSON::NaN]) }
190
- assert_equal "[\n NaN\n]", JSON.pretty_generate([JSON::NaN], :allow_nan => true)
191
- assert_raise(JSON::GeneratorError) { JSON.generate([JSON::Infinity]) }
192
- assert_equal '[Infinity]', JSON.generate([JSON::Infinity], :allow_nan => true)
193
- assert_raise(JSON::GeneratorError) { JSON.fast_generate([JSON::Infinity]) }
194
- assert_raise(JSON::GeneratorError) { JSON.pretty_generate([JSON::Infinity]) }
195
- assert_equal "[\n Infinity\n]", JSON.pretty_generate([JSON::Infinity], :allow_nan => true)
196
- assert_raise(JSON::GeneratorError) { JSON.generate([JSON::MinusInfinity]) }
197
- assert_equal '[-Infinity]', JSON.generate([JSON::MinusInfinity], :allow_nan => true)
198
- assert_raise(JSON::GeneratorError) { JSON.fast_generate([JSON::MinusInfinity]) }
199
- assert_raise(JSON::GeneratorError) { JSON.pretty_generate([JSON::MinusInfinity]) }
200
- assert_equal "[\n -Infinity\n]", JSON.pretty_generate([JSON::MinusInfinity], :allow_nan => true)
201
- end
202
-
203
- def test_depth
204
- ary = []; ary << ary
205
- assert_equal 0, JSON::SAFE_STATE_PROTOTYPE.depth
206
- assert_raise(JSON::NestingError) { JSON.generate(ary) }
207
- assert_equal 0, JSON::SAFE_STATE_PROTOTYPE.depth
208
- assert_equal 0, JSON::PRETTY_STATE_PROTOTYPE.depth
209
- assert_raise(JSON::NestingError) { JSON.pretty_generate(ary) }
210
- assert_equal 0, JSON::PRETTY_STATE_PROTOTYPE.depth
211
- s = JSON.state.new
212
- assert_equal 0, s.depth
213
- assert_raise(JSON::NestingError) { ary.to_json(s) }
214
- assert_equal 100, s.depth
215
- end
216
-
217
- def test_buffer_initial_length
218
- s = JSON.state.new
219
- assert_equal 1024, s.buffer_initial_length
220
- s.buffer_initial_length = 0
221
- assert_equal 1024, s.buffer_initial_length
222
- s.buffer_initial_length = -1
223
- assert_equal 1024, s.buffer_initial_length
224
- s.buffer_initial_length = 128
225
- assert_equal 128, s.buffer_initial_length
226
- end
227
-
228
- def test_gc
229
- if respond_to?(:assert_in_out_err)
230
- assert_in_out_err(%w[-rjson --disable-gems], <<-EOS, [], [])
231
- bignum_too_long_to_embed_as_string = 1234567890123456789012345
232
- expect = bignum_too_long_to_embed_as_string.to_s
233
- GC.stress = true
234
-
235
- 10.times do |i|
236
- tmp = bignum_too_long_to_embed_as_string.to_json
237
- raise "'\#{expect}' is expected, but '\#{tmp}'" unless tmp == expect
238
- end
239
- EOS
240
- end
241
- end if GC.respond_to?(:stress=)
242
-
243
- def test_configure_using_configure_and_merge
244
- numbered_state = {
245
- :indent => "1",
246
- :space => '2',
247
- :space_before => '3',
248
- :object_nl => '4',
249
- :array_nl => '5'
250
- }
251
- state1 = JSON.state.new
252
- state1.merge(numbered_state)
253
- assert_equal '1', state1.indent
254
- assert_equal '2', state1.space
255
- assert_equal '3', state1.space_before
256
- assert_equal '4', state1.object_nl
257
- assert_equal '5', state1.array_nl
258
- state2 = JSON.state.new
259
- state2.configure(numbered_state)
260
- assert_equal '1', state2.indent
261
- assert_equal '2', state2.space
262
- assert_equal '3', state2.space_before
263
- assert_equal '4', state2.object_nl
264
- assert_equal '5', state2.array_nl
265
- end
266
-
267
- def test_configure_hash_conversion
268
- state = JSON.state.new
269
- state.configure(:indent => '1')
270
- assert_equal '1', state.indent
271
- state = JSON.state.new
272
- foo = 'foo'
273
- assert_raise(TypeError) do
274
- state.configure(foo)
275
- end
276
- def foo.to_h
277
- { :indent => '2' }
278
- end
279
- state.configure(foo)
280
- assert_equal '2', state.indent
281
- end
282
-
283
- if defined?(JSON::Ext::Generator)
284
- def test_broken_bignum # [ruby-core:38867]
285
- pid = fork do
286
- x = 1 << 64
287
- x.class.class_eval do
288
- def to_s
289
- end
290
- end
291
- begin
292
- j = JSON::Ext::Generator::State.new.generate(x)
293
- exit 1
294
- rescue TypeError
295
- exit 0
296
- end
297
- end
298
- _, status = Process.waitpid2(pid)
299
- assert status.success?
300
- rescue NotImplementedError
301
- # forking to avoid modifying core class of a parent process and
302
- # introducing race conditions of tests are run in parallel
303
- end
304
- end
305
-
306
- def test_hash_likeness_set_symbol
307
- state = JSON.state.new
308
- assert_equal nil, state[:foo]
309
- assert_equal nil.class, state[:foo].class
310
- assert_equal nil, state['foo']
311
- state[:foo] = :bar
312
- assert_equal :bar, state[:foo]
313
- assert_equal :bar, state['foo']
314
- state_hash = state.to_hash
315
- assert_kind_of Hash, state_hash
316
- assert_equal :bar, state_hash[:foo]
317
- end
318
-
319
- def test_hash_likeness_set_string
320
- state = JSON.state.new
321
- assert_equal nil, state[:foo]
322
- assert_equal nil, state['foo']
323
- state['foo'] = :bar
324
- assert_equal :bar, state[:foo]
325
- assert_equal :bar, state['foo']
326
- state_hash = state.to_hash
327
- assert_kind_of Hash, state_hash
328
- assert_equal :bar, state_hash[:foo]
329
- end
330
-
331
- def test_json_generate
332
- assert_raise JSON::GeneratorError do
333
- assert_equal true, JSON.generate(["\xea"])
334
- end
335
- end
336
-
337
- def test_nesting
338
- too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
339
- too_deep_ary = eval too_deep
340
- assert_raise(JSON::NestingError) { JSON.generate too_deep_ary }
341
- assert_raise(JSON::NestingError) { JSON.generate too_deep_ary, :max_nesting => 100 }
342
- ok = JSON.generate too_deep_ary, :max_nesting => 101
343
- assert_equal too_deep, ok
344
- ok = JSON.generate too_deep_ary, :max_nesting => nil
345
- assert_equal too_deep, ok
346
- ok = JSON.generate too_deep_ary, :max_nesting => false
347
- assert_equal too_deep, ok
348
- ok = JSON.generate too_deep_ary, :max_nesting => 0
349
- assert_equal too_deep, ok
350
- end
351
-
352
- def test_backslash
353
- data = [ '\\.(?i:gif|jpe?g|png)$' ]
354
- json = '["\\\\.(?i:gif|jpe?g|png)$"]'
355
- assert_equal json, JSON.generate(data)
356
- #
357
- data = [ '\\"' ]
358
- json = '["\\\\\""]'
359
- assert_equal json, JSON.generate(data)
360
- #
361
- data = [ '/' ]
362
- json = '["/"]'
363
- assert_equal json, JSON.generate(data)
364
- #
365
- data = ['"']
366
- json = '["\""]'
367
- assert_equal json, JSON.generate(data)
368
- #
369
- data = ["'"]
370
- json = '["\\\'"]'
371
- assert_equal '["\'"]', JSON.generate(data)
372
- end
373
-
374
- def test_string_subclass
375
- s = Class.new(String) do
376
- def to_s; self; end
377
- undef to_json
378
- end
379
- assert_nothing_raised(SystemStackError) do
380
- assert_equal '["foo"]', JSON.generate([s.new('foo')])
381
- end
382
- end
383
- 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