1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
# frozen_string_literal: true
require_relative 'test_helper'
require 'stringio'
require 'tempfile'
class JSONCommonInterfaceTest < Test::Unit::TestCase
include JSON
module MethodMissing
def method_missing(name, *args); end
def respond_to_missing?(name, include_private)
true
end
end
def setup
@hash = {
'a' => 2,
'b' => 3.141,
'c' => 'c',
'd' => [ 1, "b", 3.14 ],
'e' => { 'foo' => 'bar' },
'g' => "\"\0\037",
'h' => 1000.0,
'i' => 0.001
}
@hash_with_method_missing = {
'a' => 2,
'b' => 3.141,
'c' => 'c',
'd' => [ 1, "b", 3.14 ],
'e' => { 'foo' => 'bar' },
'g' => "\"\0\037",
'h' => 1000.0,
'i' => 0.001
}
@hash_with_method_missing.extend MethodMissing
@json = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},'\
'"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
end
def test_index
assert_equal @json, JSON[@hash]
assert_equal @json, JSON[@hash_with_method_missing]
assert_equal @hash, JSON[@json]
end
def test_parser
assert_match(/::Parser\z/, JSON.parser.name)
end
def test_generator
assert_match(/::(TruffleRuby)?Generator\z/, JSON.generator.name)
end
def test_state
assert_match(/::(TruffleRuby)?Generator::State\z/, JSON.state.name)
end
def test_create_id
assert_equal 'json_class', JSON.create_id
JSON.create_id = 'foo_bar'
assert_equal 'foo_bar', JSON.create_id
ensure
JSON.create_id = 'json_class'
end
def test_parse
assert_equal [ 1, 2, 3, ], JSON.parse('[ 1, 2, 3 ]')
end
def test_parse_bang
assert_equal [ 1, Infinity, 3, ], JSON.parse!('[ 1, Infinity, 3 ]')
end
def test_generate
assert_equal '[1,2,3]', JSON.generate([ 1, 2, 3 ])
end
def test_fast_generate
assert_equal '[1,2,3]', JSON.generate([ 1, 2, 3 ])
end
def test_pretty_generate
assert_equal "[\n 1,\n 2,\n 3\n]", JSON.pretty_generate([ 1, 2, 3 ])
assert_equal <<~JSON.strip, JSON.pretty_generate({ a: { b: "f"}, c: "d"})
{
"a": {
"b": "f"
},
"c": "d"
}
JSON
# Cause the state to be spilled on the heap.
o = Object.new
def o.to_s
"Object"
end
actual = JSON.pretty_generate({ a: { b: o}, c: "d", e: "f"})
assert_equal <<~JSON.strip, actual
{
"a": {
"b": "Object"
},
"c": "d",
"e": "f"
}
JSON
end
def test_load
assert_equal @hash, JSON.load(@json)
tempfile = Tempfile.open('@json')
tempfile.write @json
tempfile.rewind
assert_equal @hash, JSON.load(tempfile)
stringio = StringIO.new(@json)
stringio.rewind
assert_equal @hash, JSON.load(stringio)
assert_equal nil, JSON.load(nil)
assert_equal nil, JSON.load('')
ensure
tempfile.close!
end
def test_load_with_proc
visited = []
JSON.load('{"foo": [1, 2, 3], "bar": {"baz": "plop"}}', proc { |o| visited << JSON.dump(o); o })
expected = [
'"foo"',
'1',
'2',
'3',
'[1,2,3]',
'"bar"',
'"baz"',
'"plop"',
'{"baz":"plop"}',
'{"foo":[1,2,3],"bar":{"baz":"plop"}}',
]
assert_equal expected, visited
end
def test_load_with_options
json = '{ "foo": NaN }'
assert JSON.load(json, nil, :allow_nan => true)['foo'].nan?
assert JSON.load(json, :allow_nan => true)['foo'].nan?
end
def test_load_null
assert_equal nil, JSON.load(nil, nil, :allow_blank => true)
assert_raise(TypeError) { JSON.load(nil, nil, :allow_blank => false) }
assert_raise(JSON::ParserError) { JSON.load('', nil, :allow_blank => false) }
end
def test_unsafe_load
string_able_klass = Class.new do
def initialize(str)
@str = str
end
def to_str
@str
end
end
io_able_klass = Class.new do
def initialize(str)
@str = str
end
def to_io
StringIO.new(@str)
end
end
assert_equal @hash, JSON.unsafe_load(@json)
tempfile = Tempfile.open('@json')
tempfile.write @json
tempfile.rewind
assert_equal @hash, JSON.unsafe_load(tempfile)
stringio = StringIO.new(@json)
stringio.rewind
assert_equal @hash, JSON.unsafe_load(stringio)
string_able = string_able_klass.new(@json)
assert_equal @hash, JSON.unsafe_load(string_able)
io_able = io_able_klass.new(@json)
assert_equal @hash, JSON.unsafe_load(io_able)
assert_equal nil, JSON.unsafe_load(nil)
assert_equal nil, JSON.unsafe_load('')
ensure
tempfile.close!
end
def test_unsafe_load_with_proc
visited = []
JSON.unsafe_load('{"foo": [1, 2, 3], "bar": {"baz": "plop"}}', proc { |o| visited << JSON.dump(o); o })
expected = [
'"foo"',
'1',
'2',
'3',
'[1,2,3]',
'"bar"',
'"baz"',
'"plop"',
'{"baz":"plop"}',
'{"foo":[1,2,3],"bar":{"baz":"plop"}}',
]
assert_equal expected, visited
end
def test_unsafe_load_default_options
too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
assert JSON.unsafe_load(too_deep, nil).is_a?(Array)
nan_json = '{ "foo": NaN }'
assert JSON.unsafe_load(nan_json, nil)['foo'].nan?
assert_equal nil, JSON.unsafe_load(nil, nil)
t = Time.new(2025, 9, 3, 14, 50, 0)
assert_equal t.to_s, JSON.unsafe_load(JSON(t)).to_s
end
def test_unsafe_load_with_options
nan_json = '{ "foo": NaN }'
assert_raise(JSON::ParserError) { JSON.unsafe_load(nan_json, nil, :allow_nan => false)['foo'].nan? }
# make sure it still uses the defaults when something is provided
assert JSON.unsafe_load(nan_json, nil, :allow_blank => true)['foo'].nan?
assert JSON.unsafe_load(nan_json, :allow_nan => true)['foo'].nan?
end
def test_unsafe_load_null
assert_equal nil, JSON.unsafe_load(nil, nil, :allow_blank => true)
assert_raise(TypeError) { JSON.unsafe_load(nil, nil, :allow_blank => false) }
assert_raise(JSON::ParserError) { JSON.unsafe_load('', nil, :allow_blank => false) }
end
def test_dump
too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
obj = eval(too_deep)
assert_equal too_deep, dump(obj)
assert_kind_of String, Marshal.dump(obj)
assert_raise(ArgumentError) { dump(obj, 100) }
assert_raise(ArgumentError) { Marshal.dump(obj, 100) }
assert_equal too_deep, dump(obj, 101)
assert_kind_of String, Marshal.dump(obj, 101)
assert_equal too_deep, JSON.dump(obj, StringIO.new, 101, strict: false).string
assert_equal too_deep, dump(obj, StringIO.new, 101, strict: false).string
assert_raise(JSON::GeneratorError) { JSON.dump(Object.new, StringIO.new, 101, strict: true).string }
assert_raise(JSON::GeneratorError) { dump(Object.new, StringIO.new, 101, strict: true).string }
assert_equal too_deep, dump(obj, nil, nil, strict: false)
assert_equal too_deep, dump(obj, nil, 101, strict: false)
assert_equal too_deep, dump(obj, StringIO.new, nil, strict: false).string
assert_equal too_deep, dump(obj, nil, strict: false)
assert_equal too_deep, dump(obj, 101, strict: false)
assert_equal too_deep, dump(obj, StringIO.new, strict: false).string
assert_equal too_deep, dump(obj, strict: false)
end
def test_dump_in_io
io = StringIO.new
assert_same io, JSON.dump([1], io)
assert_equal "[1]", io.string
big_object = ["a" * 10, "b" * 40, { foo: 1.23 }] * 5000
io.rewind
assert_same io, JSON.dump(big_object, io)
assert_equal JSON.dump(big_object), io.string
end
def test_dump_should_modify_defaults
max_nesting = JSON._dump_default_options[:max_nesting]
dump([], StringIO.new, 10)
assert_equal max_nesting, JSON._dump_default_options[:max_nesting]
end
def test_JSON
assert_equal @json, JSON(@hash)
assert_equal @json, JSON(@hash_with_method_missing)
assert_equal @hash, JSON(@json)
end
def test_load_file
test_load_shared(:load_file)
end
def test_load_file!
test_load_shared(:load_file!)
end
def test_load_file_with_option
test_load_file_with_option_shared(:load_file)
end
def test_load_file_with_option!
test_load_file_with_option_shared(:load_file!)
end
def test_load_file_with_bad_default_external_encoding
data = { "key" => "€" }
temp_file_containing(JSON.dump(data)) do |path|
loaded_data = with_external_encoding(Encoding::US_ASCII) do
JSON.load_file(path)
end
assert_equal data, loaded_data
end
end
def test_deprecated_dump_default_options
assert_deprecated_warning(/dump_default_options/) do
JSON.dump_default_options
end
end
private
def with_external_encoding(encoding)
verbose = $VERBOSE
$VERBOSE = nil
previous_encoding = Encoding.default_external
Encoding.default_external = encoding
yield
ensure
Encoding.default_external = previous_encoding
$VERBOSE = verbose
end
def test_load_shared(method_name)
temp_file_containing(@json) do |filespec|
assert_equal JSON.public_send(method_name, filespec), @hash
end
end
def test_load_file_with_option_shared(method_name)
temp_file_containing(@json) do |filespec|
parsed_object = JSON.public_send(method_name, filespec, symbolize_names: true)
key_classes = parsed_object.keys.map(&:class)
assert_include(key_classes, Symbol)
assert_not_include(key_classes, String)
end
end
def temp_file_containing(text, file_prefix = '')
raise "This method must be called with a code block." unless block_given?
Tempfile.create(file_prefix) do |file|
file << text
file.close
yield file.path
end
end
end
|