json 2.10.2 → 2.13.2
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.
- checksums.yaml +4 -4
- data/CHANGES.md +70 -0
- data/README.md +13 -0
- data/ext/json/ext/fbuffer/fbuffer.h +80 -15
- data/ext/json/ext/generator/extconf.rb +6 -0
- data/ext/json/ext/generator/generator.c +458 -118
- data/ext/json/ext/parser/extconf.rb +5 -2
- data/ext/json/ext/parser/parser.c +333 -267
- data/ext/json/ext/simd/conf.rb +24 -0
- data/ext/json/ext/simd/simd.h +188 -0
- data/ext/json/ext/vendor/fpconv.c +479 -0
- data/ext/json/ext/vendor/jeaiii-ltoa.h +267 -0
- data/json.gemspec +2 -3
- data/lib/json/common.rb +282 -164
- data/lib/json/ext.rb +2 -2
- data/lib/json/truffle_ruby/generator.rb +1 -1
- data/lib/json/version.rb +1 -1
- data/lib/json.rb +33 -0
- metadata +6 -2
data/lib/json/common.rb
CHANGED
@@ -5,10 +5,114 @@ require 'json/version'
|
|
5
5
|
module JSON
|
6
6
|
autoload :GenericObject, 'json/generic_object'
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
module ParserOptions # :nodoc:
|
9
|
+
class << self
|
10
|
+
def prepare(opts)
|
11
|
+
if opts[:object_class] || opts[:array_class]
|
12
|
+
opts = opts.dup
|
13
|
+
on_load = opts[:on_load]
|
14
|
+
|
15
|
+
on_load = object_class_proc(opts[:object_class], on_load) if opts[:object_class]
|
16
|
+
on_load = array_class_proc(opts[:array_class], on_load) if opts[:array_class]
|
17
|
+
opts[:on_load] = on_load
|
18
|
+
end
|
19
|
+
|
20
|
+
if opts.fetch(:create_additions, false) != false
|
21
|
+
opts = create_additions_proc(opts)
|
22
|
+
end
|
23
|
+
|
24
|
+
opts
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def object_class_proc(object_class, on_load)
|
30
|
+
->(obj) do
|
31
|
+
if Hash === obj
|
32
|
+
object = object_class.new
|
33
|
+
obj.each { |k, v| object[k] = v }
|
34
|
+
obj = object
|
35
|
+
end
|
36
|
+
on_load.nil? ? obj : on_load.call(obj)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def array_class_proc(array_class, on_load)
|
41
|
+
->(obj) do
|
42
|
+
if Array === obj
|
43
|
+
array = array_class.new
|
44
|
+
obj.each { |v| array << v }
|
45
|
+
obj = array
|
46
|
+
end
|
47
|
+
on_load.nil? ? obj : on_load.call(obj)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# TODO: extract :create_additions support to another gem for version 3.0
|
52
|
+
def create_additions_proc(opts)
|
53
|
+
if opts[:symbolize_names]
|
54
|
+
raise ArgumentError, "options :symbolize_names and :create_additions cannot be used in conjunction"
|
55
|
+
end
|
56
|
+
|
57
|
+
opts = opts.dup
|
58
|
+
create_additions = opts.fetch(:create_additions, false)
|
59
|
+
on_load = opts[:on_load]
|
60
|
+
object_class = opts[:object_class] || Hash
|
61
|
+
|
62
|
+
opts[:on_load] = ->(object) do
|
63
|
+
case object
|
64
|
+
when String
|
65
|
+
opts[:match_string]&.each do |pattern, klass|
|
66
|
+
if match = pattern.match(object)
|
67
|
+
create_additions_warning if create_additions.nil?
|
68
|
+
object = klass.json_create(object)
|
69
|
+
break
|
70
|
+
end
|
71
|
+
end
|
72
|
+
when object_class
|
73
|
+
if opts[:create_additions] != false
|
74
|
+
if class_name = object[JSON.create_id]
|
75
|
+
klass = JSON.deep_const_get(class_name)
|
76
|
+
if (klass.respond_to?(:json_creatable?) && klass.json_creatable?) || klass.respond_to?(:json_create)
|
77
|
+
create_additions_warning if create_additions.nil?
|
78
|
+
object = klass.json_create(object)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
on_load.nil? ? object : on_load.call(object)
|
85
|
+
end
|
86
|
+
|
87
|
+
opts
|
88
|
+
end
|
89
|
+
|
90
|
+
def create_additions_warning
|
91
|
+
JSON.deprecation_warning "JSON.load implicit support for `create_additions: true` is deprecated " \
|
92
|
+
"and will be removed in 3.0, use JSON.unsafe_load or explicitly " \
|
93
|
+
"pass `create_additions: true`"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
10
97
|
|
11
98
|
class << self
|
99
|
+
def deprecation_warning(message, uplevel = 3) # :nodoc:
|
100
|
+
gem_root = File.expand_path("../../../", __FILE__) + "/"
|
101
|
+
caller_locations(uplevel, 10).each do |frame|
|
102
|
+
if frame.path.nil? || frame.path.start_with?(gem_root) || frame.path.end_with?("/truffle/cext_ruby.rb", ".c")
|
103
|
+
uplevel += 1
|
104
|
+
else
|
105
|
+
break
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
if RUBY_VERSION >= "3.0"
|
110
|
+
warn(message, uplevel: uplevel, category: :deprecated)
|
111
|
+
else
|
112
|
+
warn(message, uplevel: uplevel)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
12
116
|
# :call-seq:
|
13
117
|
# JSON[object] -> new_array or new_string
|
14
118
|
#
|
@@ -20,7 +124,7 @@ module JSON
|
|
20
124
|
# Otherwise, calls JSON.generate with +object+ and +opts+ (see method #generate):
|
21
125
|
# ruby = [0, 1, nil]
|
22
126
|
# JSON[ruby] # => '[0,1,null]'
|
23
|
-
def [](object, opts =
|
127
|
+
def [](object, opts = nil)
|
24
128
|
if object.is_a?(String)
|
25
129
|
return JSON.parse(object, opts)
|
26
130
|
elsif object.respond_to?(:to_str)
|
@@ -69,38 +173,39 @@ module JSON
|
|
69
173
|
end
|
70
174
|
end
|
71
175
|
self.state = generator::State
|
72
|
-
const_set :State,
|
73
|
-
const_set :SAFE_STATE_PROTOTYPE, State.new # for JRuby
|
74
|
-
const_set :FAST_STATE_PROTOTYPE, create_fast_state
|
75
|
-
const_set :PRETTY_STATE_PROTOTYPE, create_pretty_state
|
176
|
+
const_set :State, state
|
76
177
|
ensure
|
77
178
|
$VERBOSE = old
|
78
179
|
end
|
79
180
|
|
80
|
-
def create_fast_state
|
81
|
-
State.new(
|
82
|
-
:indent => '',
|
83
|
-
:space => '',
|
84
|
-
:object_nl => "",
|
85
|
-
:array_nl => "",
|
86
|
-
:max_nesting => false
|
87
|
-
)
|
88
|
-
end
|
89
|
-
|
90
|
-
def create_pretty_state
|
91
|
-
State.new(
|
92
|
-
:indent => ' ',
|
93
|
-
:space => ' ',
|
94
|
-
:object_nl => "\n",
|
95
|
-
:array_nl => "\n"
|
96
|
-
)
|
97
|
-
end
|
98
|
-
|
99
181
|
# Returns the JSON generator module that is used by JSON.
|
100
182
|
attr_reader :generator
|
101
183
|
|
102
184
|
# Sets or Returns the JSON generator state class that is used by JSON.
|
103
185
|
attr_accessor :state
|
186
|
+
|
187
|
+
private
|
188
|
+
|
189
|
+
def deprecated_singleton_attr_accessor(*attrs)
|
190
|
+
args = RUBY_VERSION >= "3.0" ? ", category: :deprecated" : ""
|
191
|
+
attrs.each do |attr|
|
192
|
+
singleton_class.class_eval <<~RUBY
|
193
|
+
def #{attr}
|
194
|
+
warn "JSON.#{attr} is deprecated and will be removed in json 3.0.0", uplevel: 1 #{args}
|
195
|
+
@#{attr}
|
196
|
+
end
|
197
|
+
|
198
|
+
def #{attr}=(val)
|
199
|
+
warn "JSON.#{attr}= is deprecated and will be removed in json 3.0.0", uplevel: 1 #{args}
|
200
|
+
@#{attr} = val
|
201
|
+
end
|
202
|
+
|
203
|
+
def _#{attr}
|
204
|
+
@#{attr}
|
205
|
+
end
|
206
|
+
RUBY
|
207
|
+
end
|
208
|
+
end
|
104
209
|
end
|
105
210
|
|
106
211
|
# Sets create identifier, which is used to decide if the _json_create_
|
@@ -116,32 +221,24 @@ module JSON
|
|
116
221
|
Thread.current[:"JSON.create_id"] || 'json_class'
|
117
222
|
end
|
118
223
|
|
119
|
-
NaN =
|
224
|
+
NaN = Float::NAN
|
120
225
|
|
121
|
-
Infinity =
|
226
|
+
Infinity = Float::INFINITY
|
122
227
|
|
123
228
|
MinusInfinity = -Infinity
|
124
229
|
|
125
230
|
# The base exception for JSON errors.
|
126
|
-
class JSONError < StandardError
|
127
|
-
def self.wrap(exception)
|
128
|
-
obj = new("Wrapped(#{exception.class}): #{exception.message.inspect}")
|
129
|
-
obj.set_backtrace exception.backtrace
|
130
|
-
obj
|
131
|
-
end
|
132
|
-
end
|
231
|
+
class JSONError < StandardError; end
|
133
232
|
|
134
233
|
# This exception is raised if a parser error occurs.
|
135
|
-
class ParserError < JSONError
|
234
|
+
class ParserError < JSONError
|
235
|
+
attr_reader :line, :column
|
236
|
+
end
|
136
237
|
|
137
238
|
# This exception is raised if the nesting of parsed data structures is too
|
138
239
|
# deep.
|
139
240
|
class NestingError < ParserError; end
|
140
241
|
|
141
|
-
# :stopdoc:
|
142
|
-
class CircularDatastructure < NestingError; end
|
143
|
-
# :startdoc:
|
144
|
-
|
145
242
|
# This exception is raised if a generator or unparser error occurs.
|
146
243
|
class GeneratorError < JSONError
|
147
244
|
attr_reader :invalid_object
|
@@ -163,13 +260,6 @@ module JSON
|
|
163
260
|
end
|
164
261
|
end
|
165
262
|
|
166
|
-
# For backwards compatibility
|
167
|
-
UnparserError = GeneratorError # :nodoc:
|
168
|
-
|
169
|
-
# This exception is raised if the required unicode support is missing on the
|
170
|
-
# system. Usually this means that the iconv library is not installed.
|
171
|
-
class MissingUnicodeSupport < JSONError; end
|
172
|
-
|
173
263
|
# Fragment of JSON document that is to be included as is:
|
174
264
|
# fragment = JSON::Fragment.new("[1, 2, 3]")
|
175
265
|
# JSON.generate({ count: 3, items: fragments })
|
@@ -179,7 +269,7 @@ module JSON
|
|
179
269
|
# to string interpolation.
|
180
270
|
#
|
181
271
|
# Note: no validation is performed on the provided string. It is the
|
182
|
-
#
|
272
|
+
# responsibility of the caller to ensure the string contains valid JSON.
|
183
273
|
Fragment = Struct.new(:json) do
|
184
274
|
def initialize(json)
|
185
275
|
unless string = String.try_convert(json)
|
@@ -245,9 +335,16 @@ module JSON
|
|
245
335
|
# JSON.parse('')
|
246
336
|
#
|
247
337
|
def parse(source, opts = nil)
|
338
|
+
opts = ParserOptions.prepare(opts) unless opts.nil?
|
248
339
|
Parser.parse(source, opts)
|
249
340
|
end
|
250
341
|
|
342
|
+
PARSE_L_OPTIONS = {
|
343
|
+
max_nesting: false,
|
344
|
+
allow_nan: true,
|
345
|
+
}.freeze
|
346
|
+
private_constant :PARSE_L_OPTIONS
|
347
|
+
|
251
348
|
# :call-seq:
|
252
349
|
# JSON.parse!(source, opts) -> object
|
253
350
|
#
|
@@ -260,12 +357,11 @@ module JSON
|
|
260
357
|
# which disables checking for nesting depth.
|
261
358
|
# - Option +allow_nan+, if not provided, defaults to +true+.
|
262
359
|
def parse!(source, opts = nil)
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
Parser.new(source, options).parse
|
360
|
+
if opts.nil?
|
361
|
+
parse(source, PARSE_L_OPTIONS)
|
362
|
+
else
|
363
|
+
parse(source, PARSE_L_OPTIONS.merge(opts))
|
364
|
+
end
|
269
365
|
end
|
270
366
|
|
271
367
|
# :call-seq:
|
@@ -334,13 +430,6 @@ module JSON
|
|
334
430
|
end
|
335
431
|
end
|
336
432
|
|
337
|
-
# :stopdoc:
|
338
|
-
# I want to deprecate these later, so I'll first be silent about them, and
|
339
|
-
# later delete them.
|
340
|
-
alias unparse generate
|
341
|
-
module_function :unparse
|
342
|
-
# :startdoc:
|
343
|
-
|
344
433
|
# :call-seq:
|
345
434
|
# JSON.fast_generate(obj, opts) -> new_string
|
346
435
|
#
|
@@ -355,19 +444,21 @@ module JSON
|
|
355
444
|
# # Raises SystemStackError (stack level too deep):
|
356
445
|
# JSON.fast_generate(a)
|
357
446
|
def fast_generate(obj, opts = nil)
|
358
|
-
if
|
359
|
-
|
447
|
+
if RUBY_VERSION >= "3.0"
|
448
|
+
warn "JSON.fast_generate is deprecated and will be removed in json 3.0.0, just use JSON.generate", uplevel: 1, category: :deprecated
|
360
449
|
else
|
361
|
-
|
450
|
+
warn "JSON.fast_generate is deprecated and will be removed in json 3.0.0, just use JSON.generate", uplevel: 1
|
362
451
|
end
|
363
|
-
|
452
|
+
generate(obj, opts)
|
364
453
|
end
|
365
454
|
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
455
|
+
PRETTY_GENERATE_OPTIONS = {
|
456
|
+
indent: ' ',
|
457
|
+
space: ' ',
|
458
|
+
object_nl: "\n",
|
459
|
+
array_nl: "\n",
|
460
|
+
}.freeze
|
461
|
+
private_constant :PRETTY_GENERATE_OPTIONS
|
371
462
|
|
372
463
|
# :call-seq:
|
373
464
|
# JSON.pretty_generate(obj, opts = nil) -> new_string
|
@@ -400,52 +491,46 @@ module JSON
|
|
400
491
|
# }
|
401
492
|
#
|
402
493
|
def pretty_generate(obj, opts = nil)
|
403
|
-
if State === opts
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
end
|
494
|
+
return opts.generate(obj) if State === opts
|
495
|
+
|
496
|
+
options = PRETTY_GENERATE_OPTIONS
|
497
|
+
|
408
498
|
if opts
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
499
|
+
unless opts.is_a?(Hash)
|
500
|
+
if opts.respond_to? :to_hash
|
501
|
+
opts = opts.to_hash
|
502
|
+
elsif opts.respond_to? :to_h
|
503
|
+
opts = opts.to_h
|
504
|
+
else
|
505
|
+
raise TypeError, "can't convert #{opts.class} into Hash"
|
506
|
+
end
|
415
507
|
end
|
416
|
-
|
508
|
+
options = options.merge(opts)
|
417
509
|
end
|
418
|
-
|
510
|
+
|
511
|
+
State.generate(obj, options, nil)
|
419
512
|
end
|
420
513
|
|
421
|
-
#
|
422
|
-
#
|
423
|
-
|
424
|
-
|
425
|
-
|
514
|
+
# Sets or returns default options for the JSON.unsafe_load method.
|
515
|
+
# Initially:
|
516
|
+
# opts = JSON.load_default_options
|
517
|
+
# opts # => {:max_nesting=>false, :allow_nan=>true, :allow_blank=>true, :create_additions=>true}
|
518
|
+
deprecated_singleton_attr_accessor :unsafe_load_default_options
|
426
519
|
|
427
|
-
|
428
|
-
# Sets or returns default options for the JSON.unsafe_load method.
|
429
|
-
# Initially:
|
430
|
-
# opts = JSON.load_default_options
|
431
|
-
# opts # => {:max_nesting=>false, :allow_nan=>true, :allow_blank=>true, :create_additions=>true}
|
432
|
-
attr_accessor :unsafe_load_default_options
|
433
|
-
end
|
434
|
-
self.unsafe_load_default_options = {
|
520
|
+
@unsafe_load_default_options = {
|
435
521
|
:max_nesting => false,
|
436
522
|
:allow_nan => true,
|
437
523
|
:allow_blank => true,
|
438
524
|
:create_additions => true,
|
439
525
|
}
|
440
526
|
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
self.load_default_options = {
|
527
|
+
# Sets or returns default options for the JSON.load method.
|
528
|
+
# Initially:
|
529
|
+
# opts = JSON.load_default_options
|
530
|
+
# opts # => {:max_nesting=>false, :allow_nan=>true, :allow_blank=>true, :create_additions=>true}
|
531
|
+
deprecated_singleton_attr_accessor :load_default_options
|
532
|
+
|
533
|
+
@load_default_options = {
|
449
534
|
:allow_nan => true,
|
450
535
|
:allow_blank => true,
|
451
536
|
:create_additions => nil,
|
@@ -581,9 +666,9 @@ module JSON
|
|
581
666
|
#
|
582
667
|
def unsafe_load(source, proc = nil, options = nil)
|
583
668
|
opts = if options.nil?
|
584
|
-
|
669
|
+
_unsafe_load_default_options
|
585
670
|
else
|
586
|
-
|
671
|
+
_unsafe_load_default_options.merge(options)
|
587
672
|
end
|
588
673
|
|
589
674
|
unless source.is_a?(String)
|
@@ -741,9 +826,9 @@ module JSON
|
|
741
826
|
#
|
742
827
|
def load(source, proc = nil, options = nil)
|
743
828
|
opts = if options.nil?
|
744
|
-
|
829
|
+
_load_default_options
|
745
830
|
else
|
746
|
-
|
831
|
+
_load_default_options.merge(options)
|
747
832
|
end
|
748
833
|
|
749
834
|
unless source.is_a?(String)
|
@@ -759,36 +844,21 @@ module JSON
|
|
759
844
|
if opts[:allow_blank] && (source.nil? || source.empty?)
|
760
845
|
source = 'null'
|
761
846
|
end
|
762
|
-
result = parse(source, opts)
|
763
|
-
recurse_proc(result, &proc) if proc
|
764
|
-
result
|
765
|
-
end
|
766
847
|
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
when Array
|
771
|
-
result.each { |x| recurse_proc x, &proc }
|
772
|
-
proc.call result
|
773
|
-
when Hash
|
774
|
-
result.each { |x, y| recurse_proc x, &proc; recurse_proc y, &proc }
|
775
|
-
proc.call result
|
776
|
-
else
|
777
|
-
proc.call result
|
848
|
+
if proc
|
849
|
+
opts = opts.dup
|
850
|
+
opts[:on_load] = proc.to_proc
|
778
851
|
end
|
779
|
-
end
|
780
|
-
|
781
|
-
alias restore load
|
782
|
-
module_function :restore
|
783
852
|
|
784
|
-
|
785
|
-
# Sets or returns the default options for the JSON.dump method.
|
786
|
-
# Initially:
|
787
|
-
# opts = JSON.dump_default_options
|
788
|
-
# opts # => {:max_nesting=>false, :allow_nan=>true}
|
789
|
-
attr_accessor :dump_default_options
|
853
|
+
parse(source, opts)
|
790
854
|
end
|
791
|
-
|
855
|
+
|
856
|
+
# Sets or returns the default options for the JSON.dump method.
|
857
|
+
# Initially:
|
858
|
+
# opts = JSON.dump_default_options
|
859
|
+
# opts # => {:max_nesting=>false, :allow_nan=>true}
|
860
|
+
deprecated_singleton_attr_accessor :dump_default_options
|
861
|
+
@dump_default_options = {
|
792
862
|
:max_nesting => false,
|
793
863
|
:allow_nan => true,
|
794
864
|
}
|
@@ -841,7 +911,7 @@ module JSON
|
|
841
911
|
end
|
842
912
|
end
|
843
913
|
|
844
|
-
opts = JSON.
|
914
|
+
opts = JSON._dump_default_options
|
845
915
|
opts = opts.merge(:max_nesting => limit) if limit
|
846
916
|
opts = opts.merge(kwargs) if kwargs
|
847
917
|
|
@@ -852,10 +922,67 @@ module JSON
|
|
852
922
|
end
|
853
923
|
end
|
854
924
|
|
855
|
-
#
|
856
|
-
|
857
|
-
|
925
|
+
# :stopdoc:
|
926
|
+
# All these were meant to be deprecated circa 2009, but were just set as undocumented
|
927
|
+
# so usage still exist in the wild.
|
928
|
+
def unparse(...)
|
929
|
+
if RUBY_VERSION >= "3.0"
|
930
|
+
warn "JSON.unparse is deprecated and will be removed in json 3.0.0, just use JSON.generate", uplevel: 1, category: :deprecated
|
931
|
+
else
|
932
|
+
warn "JSON.unparse is deprecated and will be removed in json 3.0.0, just use JSON.generate", uplevel: 1
|
933
|
+
end
|
934
|
+
generate(...)
|
935
|
+
end
|
936
|
+
module_function :unparse
|
937
|
+
|
938
|
+
def fast_unparse(...)
|
939
|
+
if RUBY_VERSION >= "3.0"
|
940
|
+
warn "JSON.fast_unparse is deprecated and will be removed in json 3.0.0, just use JSON.generate", uplevel: 1, category: :deprecated
|
941
|
+
else
|
942
|
+
warn "JSON.fast_unparse is deprecated and will be removed in json 3.0.0, just use JSON.generate", uplevel: 1
|
943
|
+
end
|
944
|
+
generate(...)
|
945
|
+
end
|
946
|
+
module_function :fast_unparse
|
947
|
+
|
948
|
+
def pretty_unparse(...)
|
949
|
+
if RUBY_VERSION >= "3.0"
|
950
|
+
warn "JSON.pretty_unparse is deprecated and will be removed in json 3.0.0, just use JSON.pretty_generate", uplevel: 1, category: :deprecated
|
951
|
+
else
|
952
|
+
warn "JSON.pretty_unparse is deprecated and will be removed in json 3.0.0, just use JSON.pretty_generate", uplevel: 1
|
953
|
+
end
|
954
|
+
pretty_generate(...)
|
955
|
+
end
|
956
|
+
module_function :fast_unparse
|
957
|
+
|
958
|
+
def restore(...)
|
959
|
+
if RUBY_VERSION >= "3.0"
|
960
|
+
warn "JSON.restore is deprecated and will be removed in json 3.0.0, just use JSON.load", uplevel: 1, category: :deprecated
|
961
|
+
else
|
962
|
+
warn "JSON.restore is deprecated and will be removed in json 3.0.0, just use JSON.load", uplevel: 1
|
963
|
+
end
|
964
|
+
load(...)
|
965
|
+
end
|
966
|
+
module_function :restore
|
967
|
+
|
968
|
+
class << self
|
969
|
+
private
|
970
|
+
|
971
|
+
def const_missing(const_name)
|
972
|
+
case const_name
|
973
|
+
when :PRETTY_STATE_PROTOTYPE
|
974
|
+
if RUBY_VERSION >= "3.0"
|
975
|
+
warn "JSON::PRETTY_STATE_PROTOTYPE is deprecated and will be removed in json 3.0.0, just use JSON.pretty_generate", uplevel: 1, category: :deprecated
|
976
|
+
else
|
977
|
+
warn "JSON::PRETTY_STATE_PROTOTYPE is deprecated and will be removed in json 3.0.0, just use JSON.pretty_generate", uplevel: 1
|
978
|
+
end
|
979
|
+
state.new(PRETTY_GENERATE_OPTIONS)
|
980
|
+
else
|
981
|
+
super
|
982
|
+
end
|
983
|
+
end
|
858
984
|
end
|
985
|
+
# :startdoc:
|
859
986
|
|
860
987
|
# JSON::Coder holds a parser and generator configuration.
|
861
988
|
#
|
@@ -899,10 +1026,9 @@ module JSON
|
|
899
1026
|
options[:strict] = true
|
900
1027
|
end
|
901
1028
|
options[:as_json] = as_json if as_json
|
902
|
-
options[:create_additions] = false unless options.key?(:create_additions)
|
903
1029
|
|
904
1030
|
@state = State.new(options).freeze
|
905
|
-
@parser_config = Ext::Parser::Config.new(options)
|
1031
|
+
@parser_config = Ext::Parser::Config.new(ParserOptions.prepare(options))
|
906
1032
|
end
|
907
1033
|
|
908
1034
|
# call-seq:
|
@@ -940,8 +1066,14 @@ module ::Kernel
|
|
940
1066
|
# Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in
|
941
1067
|
# one line.
|
942
1068
|
def j(*objs)
|
1069
|
+
if RUBY_VERSION >= "3.0"
|
1070
|
+
warn "Kernel#j is deprecated and will be removed in json 3.0.0", uplevel: 1, category: :deprecated
|
1071
|
+
else
|
1072
|
+
warn "Kernel#j is deprecated and will be removed in json 3.0.0", uplevel: 1
|
1073
|
+
end
|
1074
|
+
|
943
1075
|
objs.each do |obj|
|
944
|
-
puts JSON
|
1076
|
+
puts JSON.generate(obj, :allow_nan => true, :max_nesting => false)
|
945
1077
|
end
|
946
1078
|
nil
|
947
1079
|
end
|
@@ -949,8 +1081,14 @@ module ::Kernel
|
|
949
1081
|
# Outputs _objs_ to STDOUT as JSON strings in a pretty format, with
|
950
1082
|
# indentation and over many lines.
|
951
1083
|
def jj(*objs)
|
1084
|
+
if RUBY_VERSION >= "3.0"
|
1085
|
+
warn "Kernel#jj is deprecated and will be removed in json 3.0.0", uplevel: 1, category: :deprecated
|
1086
|
+
else
|
1087
|
+
warn "Kernel#jj is deprecated and will be removed in json 3.0.0", uplevel: 1
|
1088
|
+
end
|
1089
|
+
|
952
1090
|
objs.each do |obj|
|
953
|
-
puts JSON
|
1091
|
+
puts JSON.pretty_generate(obj, :allow_nan => true, :max_nesting => false)
|
954
1092
|
end
|
955
1093
|
nil
|
956
1094
|
end
|
@@ -961,27 +1099,7 @@ module ::Kernel
|
|
961
1099
|
#
|
962
1100
|
# The _opts_ argument is passed through to generate/parse respectively. See
|
963
1101
|
# generate and parse for their documentation.
|
964
|
-
def JSON(object,
|
965
|
-
|
966
|
-
return JSON.parse(object, args.first)
|
967
|
-
elsif object.respond_to?(:to_str)
|
968
|
-
str = object.to_str
|
969
|
-
if str.is_a?(String)
|
970
|
-
return JSON.parse(object.to_str, args.first)
|
971
|
-
end
|
972
|
-
end
|
973
|
-
|
974
|
-
JSON.generate(object, args.first)
|
975
|
-
end
|
976
|
-
end
|
977
|
-
|
978
|
-
# Extends any Class to include _json_creatable?_ method.
|
979
|
-
class ::Class
|
980
|
-
# Returns true if this class can be used to create an instance
|
981
|
-
# from a serialised JSON string. The class has to implement a class
|
982
|
-
# method _json_create_ that expects a hash as first parameter. The hash
|
983
|
-
# should include the required data.
|
984
|
-
def json_creatable?
|
985
|
-
respond_to?(:json_create)
|
1102
|
+
def JSON(object, opts = nil)
|
1103
|
+
JSON[object, opts]
|
986
1104
|
end
|
987
1105
|
end
|
data/lib/json/ext.rb
CHANGED
@@ -34,12 +34,12 @@ module JSON
|
|
34
34
|
|
35
35
|
if RUBY_ENGINE == 'truffleruby'
|
36
36
|
require 'json/truffle_ruby/generator'
|
37
|
-
JSON.generator =
|
37
|
+
JSON.generator = JSON::TruffleRuby::Generator
|
38
38
|
else
|
39
39
|
require 'json/ext/generator'
|
40
40
|
JSON.generator = Generator
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
JSON_LOADED = true unless defined?(
|
44
|
+
JSON_LOADED = true unless defined?(JSON::JSON_LOADED)
|
45
45
|
end
|
data/lib/json/version.rb
CHANGED