json 2.11.3 → 2.21.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 +194 -8
- data/LEGAL +12 -0
- data/README.md +44 -4
- data/ext/json/ext/fbuffer/fbuffer.h +99 -76
- data/ext/json/ext/generator/extconf.rb +9 -0
- data/ext/json/ext/generator/generator.c +788 -563
- data/ext/json/ext/json.h +183 -0
- data/ext/json/ext/parser/extconf.rb +34 -2
- data/ext/json/ext/parser/parser.c +2254 -695
- data/ext/json/ext/simd/conf.rb +24 -0
- data/ext/json/ext/simd/simd.h +208 -0
- data/ext/json/ext/vendor/fast_float_parser.h +814 -0
- data/ext/json/ext/vendor/fpconv.c +20 -19
- data/json.gemspec +2 -3
- data/lib/json/add/core.rb +1 -0
- data/lib/json/add/string.rb +35 -0
- data/lib/json/common.rb +135 -55
- data/lib/json/ext/generator/state.rb +12 -14
- data/lib/json/ext.rb +28 -2
- data/lib/json/generic_object.rb +0 -8
- data/lib/json/truffle_ruby/generator.rb +172 -72
- data/lib/json/version.rb +1 -1
- data/lib/json.rb +122 -5
- metadata +8 -3
|
@@ -29,6 +29,10 @@
|
|
|
29
29
|
#include <string.h>
|
|
30
30
|
#include <stdint.h>
|
|
31
31
|
|
|
32
|
+
#if JSON_DEBUG
|
|
33
|
+
#include <assert.h>
|
|
34
|
+
#endif
|
|
35
|
+
|
|
32
36
|
#define npowers 87
|
|
33
37
|
#define steppowers 8
|
|
34
38
|
#define firstpower -348 /* 10 ^ -348 */
|
|
@@ -41,7 +45,7 @@ typedef struct Fp {
|
|
|
41
45
|
int exp;
|
|
42
46
|
} Fp;
|
|
43
47
|
|
|
44
|
-
static Fp powers_ten[] = {
|
|
48
|
+
static const Fp powers_ten[] = {
|
|
45
49
|
{ 18054884314459144840U, -1220 }, { 13451937075301367670U, -1193 },
|
|
46
50
|
{ 10022474136428063862U, -1166 }, { 14934650266808366570U, -1140 },
|
|
47
51
|
{ 11127181549972568877U, -1113 }, { 16580792590934885855U, -1087 },
|
|
@@ -92,7 +96,7 @@ static Fp find_cachedpow10(int exp, int* k)
|
|
|
92
96
|
{
|
|
93
97
|
const double one_log_ten = 0.30102999566398114;
|
|
94
98
|
|
|
95
|
-
int approx = -(exp + npowers) * one_log_ten;
|
|
99
|
+
int approx = (int)(-(exp + npowers) * one_log_ten);
|
|
96
100
|
int idx = (approx - firstpower) / steppowers;
|
|
97
101
|
|
|
98
102
|
while(1) {
|
|
@@ -123,7 +127,7 @@ static Fp find_cachedpow10(int exp, int* k)
|
|
|
123
127
|
#define absv(n) ((n) < 0 ? -(n) : (n))
|
|
124
128
|
#define minv(a, b) ((a) < (b) ? (a) : (b))
|
|
125
129
|
|
|
126
|
-
static uint64_t tens[] = {
|
|
130
|
+
static const uint64_t tens[] = {
|
|
127
131
|
10000000000000000000U, 1000000000000000000U, 100000000000000000U,
|
|
128
132
|
10000000000000000U, 1000000000000000U, 100000000000000U,
|
|
129
133
|
10000000000000U, 1000000000000U, 100000000000U,
|
|
@@ -244,7 +248,7 @@ static int generate_digits(Fp* fp, Fp* upper, Fp* lower, char* digits, int* K)
|
|
|
244
248
|
uint64_t part2 = upper->frac & (one.frac - 1);
|
|
245
249
|
|
|
246
250
|
int idx = 0, kappa = 10;
|
|
247
|
-
uint64_t* divp;
|
|
251
|
+
const uint64_t* divp;
|
|
248
252
|
/* 1000000000 */
|
|
249
253
|
for(divp = tens + 10; kappa > 0; divp++) {
|
|
250
254
|
|
|
@@ -268,7 +272,7 @@ static int generate_digits(Fp* fp, Fp* upper, Fp* lower, char* digits, int* K)
|
|
|
268
272
|
}
|
|
269
273
|
|
|
270
274
|
/* 10 */
|
|
271
|
-
uint64_t* unit = tens + 18;
|
|
275
|
+
const uint64_t* unit = tens + 18;
|
|
272
276
|
|
|
273
277
|
while(true) {
|
|
274
278
|
part2 *= 10;
|
|
@@ -320,15 +324,7 @@ static int emit_digits(char* digits, int ndigits, char* dest, int K, bool neg)
|
|
|
320
324
|
{
|
|
321
325
|
int exp = absv(K + ndigits - 1);
|
|
322
326
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
if(neg) {
|
|
326
|
-
max_trailing_zeros -= 1;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
/* write plain integer */
|
|
330
|
-
if(K >= 0 && (exp < (ndigits + max_trailing_zeros))) {
|
|
331
|
-
|
|
327
|
+
if(K >= 0 && exp < 15) {
|
|
332
328
|
memcpy(dest, digits, ndigits);
|
|
333
329
|
memset(dest + ndigits, '0', K);
|
|
334
330
|
|
|
@@ -340,7 +336,7 @@ static int emit_digits(char* digits, int ndigits, char* dest, int K, bool neg)
|
|
|
340
336
|
}
|
|
341
337
|
|
|
342
338
|
/* write decimal w/o scientific notation */
|
|
343
|
-
if(K < 0 && (K > -7 || exp <
|
|
339
|
+
if(K < 0 && (K > -7 || exp < 10)) {
|
|
344
340
|
int offset = ndigits - absv(K);
|
|
345
341
|
/* fp < 1.0 -> write leading zero */
|
|
346
342
|
if(offset <= 0) {
|
|
@@ -432,10 +428,12 @@ static int filter_special(double fp, char* dest)
|
|
|
432
428
|
*
|
|
433
429
|
* Input:
|
|
434
430
|
* fp -> the double to convert, dest -> destination buffer.
|
|
435
|
-
* The generated string will never be longer than
|
|
436
|
-
* Make sure to pass a pointer to at least
|
|
431
|
+
* The generated string will never be longer than 32 characters.
|
|
432
|
+
* Make sure to pass a pointer to at least 32 bytes of memory.
|
|
437
433
|
* The emitted string will not be null terminated.
|
|
438
434
|
*
|
|
435
|
+
*
|
|
436
|
+
*
|
|
439
437
|
* Output:
|
|
440
438
|
* The number of written characters.
|
|
441
439
|
*
|
|
@@ -443,7 +441,7 @@ static int filter_special(double fp, char* dest)
|
|
|
443
441
|
*
|
|
444
442
|
* void print(double d)
|
|
445
443
|
* {
|
|
446
|
-
* char buf[
|
|
444
|
+
* char buf[28 + 1] // plus null terminator
|
|
447
445
|
* int str_len = fpconv_dtoa(d, buf);
|
|
448
446
|
*
|
|
449
447
|
* buf[str_len] = '\0';
|
|
@@ -451,7 +449,7 @@ static int filter_special(double fp, char* dest)
|
|
|
451
449
|
* }
|
|
452
450
|
*
|
|
453
451
|
*/
|
|
454
|
-
static int fpconv_dtoa(double d, char dest[
|
|
452
|
+
static int fpconv_dtoa(double d, char dest[32])
|
|
455
453
|
{
|
|
456
454
|
char digits[18];
|
|
457
455
|
|
|
@@ -474,6 +472,9 @@ static int fpconv_dtoa(double d, char dest[24])
|
|
|
474
472
|
int ndigits = grisu2(d, digits, &K);
|
|
475
473
|
|
|
476
474
|
str_len += emit_digits(digits, ndigits, dest + str_len, K, neg);
|
|
475
|
+
#if JSON_DEBUG
|
|
476
|
+
assert(str_len <= 32);
|
|
477
|
+
#endif
|
|
477
478
|
|
|
478
479
|
return str_len;
|
|
479
480
|
}
|
data/json.gemspec
CHANGED
|
@@ -44,15 +44,14 @@ spec = Gem::Specification.new do |s|
|
|
|
44
44
|
"LEGAL",
|
|
45
45
|
"README.md",
|
|
46
46
|
"json.gemspec",
|
|
47
|
-
|
|
48
|
-
]
|
|
47
|
+
] + Dir.glob("lib/**/*.rb", base: File.expand_path("..", __FILE__))
|
|
49
48
|
|
|
50
49
|
if java_ext
|
|
51
50
|
s.platform = 'java'
|
|
52
51
|
s.files += Dir["lib/json/ext/**/*.jar"]
|
|
53
52
|
else
|
|
54
53
|
s.extensions = Dir["ext/json/**/extconf.rb"]
|
|
55
|
-
s.files += Dir["ext/json/**/*.{c,h}"]
|
|
54
|
+
s.files += Dir["ext/json/**/*.{c,h,rb}"]
|
|
56
55
|
end
|
|
57
56
|
end
|
|
58
57
|
|
data/lib/json/add/core.rb
CHANGED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
|
3
|
+
require 'json'
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
class String
|
|
7
|
+
# call-seq: json_create(o)
|
|
8
|
+
#
|
|
9
|
+
# Raw Strings are JSON Objects (the raw bytes are stored in an array for the
|
|
10
|
+
# key "raw"). The Ruby String can be created by this class method.
|
|
11
|
+
def self.json_create(object)
|
|
12
|
+
object["raw"].pack("C*")
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# call-seq: to_json_raw_object()
|
|
16
|
+
#
|
|
17
|
+
# This method creates a raw object hash, that can be nested into
|
|
18
|
+
# other data structures and will be generated as a raw string. This
|
|
19
|
+
# method should be used, if you want to convert raw strings to JSON
|
|
20
|
+
# instead of UTF-8 strings, e. g. binary data.
|
|
21
|
+
def to_json_raw_object
|
|
22
|
+
{
|
|
23
|
+
JSON.create_id => self.class.name,
|
|
24
|
+
"raw" => unpack("C*"),
|
|
25
|
+
}
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# call-seq: to_json_raw(*args)
|
|
29
|
+
#
|
|
30
|
+
# This method creates a JSON text from the result of a call to
|
|
31
|
+
# to_json_raw_object of this String.
|
|
32
|
+
def to_json_raw(...)
|
|
33
|
+
to_json_raw_object.to_json(...)
|
|
34
|
+
end
|
|
35
|
+
end
|
data/lib/json/common.rb
CHANGED
|
@@ -48,7 +48,7 @@ module JSON
|
|
|
48
48
|
end
|
|
49
49
|
end
|
|
50
50
|
|
|
51
|
-
# TODO:
|
|
51
|
+
# TODO: extract :create_additions support to another gem for version 3.0
|
|
52
52
|
def create_additions_proc(opts)
|
|
53
53
|
if opts[:symbolize_names]
|
|
54
54
|
raise ArgumentError, "options :symbolize_names and :create_additions cannot be used in conjunction"
|
|
@@ -71,9 +71,14 @@ module JSON
|
|
|
71
71
|
end
|
|
72
72
|
when object_class
|
|
73
73
|
if opts[:create_additions] != false
|
|
74
|
-
if
|
|
75
|
-
klass =
|
|
76
|
-
|
|
74
|
+
if class_path = object[JSON.create_id]
|
|
75
|
+
klass = begin
|
|
76
|
+
Object.const_get(class_path)
|
|
77
|
+
rescue NameError => e
|
|
78
|
+
raise ArgumentError, "can't get const #{class_path}: #{e}"
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
if klass.respond_to?(:json_creatable?) ? klass.json_creatable? : klass.respond_to?(:json_create)
|
|
77
82
|
create_additions_warning if create_additions.nil?
|
|
78
83
|
object = klass.json_create(object)
|
|
79
84
|
end
|
|
@@ -87,31 +92,32 @@ module JSON
|
|
|
87
92
|
opts
|
|
88
93
|
end
|
|
89
94
|
|
|
90
|
-
GEM_ROOT = File.expand_path("../../../", __FILE__) + "/"
|
|
91
95
|
def create_additions_warning
|
|
92
|
-
|
|
96
|
+
JSON.deprecation_warning "JSON.load implicit support for `create_additions: true` is deprecated " \
|
|
93
97
|
"and will be removed in 3.0, use JSON.unsafe_load or explicitly " \
|
|
94
98
|
"pass `create_additions: true`"
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
95
102
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
end
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
if RUBY_VERSION >= "3.0"
|
|
106
|
-
warn(message, uplevel: uplevel - 1, category: :deprecated)
|
|
103
|
+
class << self
|
|
104
|
+
def deprecation_warning(message, uplevel = 3) # :nodoc:
|
|
105
|
+
gem_root = File.expand_path("..", __dir__) + "/"
|
|
106
|
+
caller_locations(uplevel, 10).each do |frame|
|
|
107
|
+
if frame.path.nil? || frame.path.start_with?(gem_root) || frame.path.end_with?("/truffle/cext_ruby.rb", ".c")
|
|
108
|
+
uplevel += 1
|
|
107
109
|
else
|
|
108
|
-
|
|
110
|
+
break
|
|
109
111
|
end
|
|
110
112
|
end
|
|
113
|
+
|
|
114
|
+
if RUBY_VERSION >= "3.0"
|
|
115
|
+
warn(message, uplevel: uplevel, category: :deprecated)
|
|
116
|
+
else
|
|
117
|
+
warn(message, uplevel: uplevel)
|
|
118
|
+
end
|
|
111
119
|
end
|
|
112
|
-
end
|
|
113
120
|
|
|
114
|
-
class << self
|
|
115
121
|
# :call-seq:
|
|
116
122
|
# JSON[object] -> new_array or new_string
|
|
117
123
|
#
|
|
@@ -146,33 +152,34 @@ module JSON
|
|
|
146
152
|
const_set :Parser, parser
|
|
147
153
|
end
|
|
148
154
|
|
|
149
|
-
# Return the constant located at _path_. The format of _path_ has to be
|
|
150
|
-
# either ::A::B::C or A::B::C. In any case, A has to be located at the top
|
|
151
|
-
# level (absolute namespace path?). If there doesn't exist a constant at
|
|
152
|
-
# the given path, an ArgumentError is raised.
|
|
153
|
-
def deep_const_get(path) # :nodoc:
|
|
154
|
-
Object.const_get(path)
|
|
155
|
-
rescue NameError => e
|
|
156
|
-
raise ArgumentError, "can't get const #{path}: #{e}"
|
|
157
|
-
end
|
|
158
|
-
|
|
159
155
|
# Set the module _generator_ to be used by JSON.
|
|
160
156
|
def generator=(generator) # :nodoc:
|
|
161
157
|
old, $VERBOSE = $VERBOSE, nil
|
|
158
|
+
|
|
159
|
+
# The default proc used when the +sort_keys+ generation option is +true+.
|
|
160
|
+
# It returns a new hash with the entries sorted by their keys.
|
|
161
|
+
sort_keys_proc = ->(hash) { hash.sort.to_h }
|
|
162
|
+
if defined?(::Ractor) && Ractor.respond_to?(:shareable_lambda)
|
|
163
|
+
sort_keys_proc = Ractor.shareable_lambda(&sort_keys_proc)
|
|
164
|
+
end
|
|
165
|
+
generator::State.default_sort_keys_proc = sort_keys_proc
|
|
166
|
+
|
|
162
167
|
@generator = generator
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
168
|
+
if generator.const_defined?(:GeneratorMethods)
|
|
169
|
+
generator_methods = generator::GeneratorMethods
|
|
170
|
+
for const in generator_methods.constants
|
|
171
|
+
klass = const_get(const)
|
|
172
|
+
modul = generator_methods.const_get(const)
|
|
173
|
+
klass.class_eval do
|
|
174
|
+
instance_methods(false).each do |m|
|
|
175
|
+
m.to_s == 'to_json' and remove_method m
|
|
176
|
+
end
|
|
177
|
+
include modul
|
|
170
178
|
end
|
|
171
|
-
include modul
|
|
172
179
|
end
|
|
173
180
|
end
|
|
174
181
|
self.state = generator::State
|
|
175
|
-
const_set :State,
|
|
182
|
+
const_set :State, state
|
|
176
183
|
ensure
|
|
177
184
|
$VERBOSE = old
|
|
178
185
|
end
|
|
@@ -185,6 +192,25 @@ module JSON
|
|
|
185
192
|
|
|
186
193
|
private
|
|
187
194
|
|
|
195
|
+
# Called from the extension when a hash has both string and symbol keys
|
|
196
|
+
def on_mixed_keys_hash(hash, do_raise)
|
|
197
|
+
set = {}
|
|
198
|
+
hash.each_key do |key|
|
|
199
|
+
key_str = key.to_s
|
|
200
|
+
|
|
201
|
+
if set[key_str]
|
|
202
|
+
message = "detected duplicate key #{key_str.inspect} in #{hash.inspect}"
|
|
203
|
+
if do_raise
|
|
204
|
+
raise GeneratorError, message
|
|
205
|
+
else
|
|
206
|
+
deprecation_warning("#{message}.\nThis will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`")
|
|
207
|
+
end
|
|
208
|
+
else
|
|
209
|
+
set[key_str] = true
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
|
|
188
214
|
def deprecated_singleton_attr_accessor(*attrs)
|
|
189
215
|
args = RUBY_VERSION >= "3.0" ? ", category: :deprecated" : ""
|
|
190
216
|
attrs.each do |attr|
|
|
@@ -230,7 +256,9 @@ module JSON
|
|
|
230
256
|
class JSONError < StandardError; end
|
|
231
257
|
|
|
232
258
|
# This exception is raised if a parser error occurs.
|
|
233
|
-
class ParserError < JSONError
|
|
259
|
+
class ParserError < JSONError
|
|
260
|
+
attr_reader :line, :column
|
|
261
|
+
end
|
|
234
262
|
|
|
235
263
|
# This exception is raised if the nesting of parsed data structures is too
|
|
236
264
|
# deep.
|
|
@@ -266,7 +294,7 @@ module JSON
|
|
|
266
294
|
# to string interpolation.
|
|
267
295
|
#
|
|
268
296
|
# Note: no validation is performed on the provided string. It is the
|
|
269
|
-
#
|
|
297
|
+
# responsibility of the caller to ensure the string contains valid JSON.
|
|
270
298
|
Fragment = Struct.new(:json) do
|
|
271
299
|
def initialize(json)
|
|
272
300
|
unless string = String.try_convert(json)
|
|
@@ -388,7 +416,7 @@ module JSON
|
|
|
388
416
|
#
|
|
389
417
|
# Returns a \String containing the generated \JSON data.
|
|
390
418
|
#
|
|
391
|
-
# See also JSON.
|
|
419
|
+
# See also JSON.pretty_generate.
|
|
392
420
|
#
|
|
393
421
|
# Argument +obj+ is the Ruby object to be converted to \JSON.
|
|
394
422
|
#
|
|
@@ -488,7 +516,7 @@ module JSON
|
|
|
488
516
|
# }
|
|
489
517
|
#
|
|
490
518
|
def pretty_generate(obj, opts = nil)
|
|
491
|
-
return
|
|
519
|
+
return opts.generate(obj) if State === opts
|
|
492
520
|
|
|
493
521
|
options = PRETTY_GENERATE_OPTIONS
|
|
494
522
|
|
|
@@ -533,6 +561,7 @@ module JSON
|
|
|
533
561
|
:create_additions => nil,
|
|
534
562
|
}
|
|
535
563
|
# :call-seq:
|
|
564
|
+
# JSON.unsafe_load(source, options = {}) -> object
|
|
536
565
|
# JSON.unsafe_load(source, proc = nil, options = {}) -> object
|
|
537
566
|
#
|
|
538
567
|
# Returns the Ruby objects created by parsing the given +source+.
|
|
@@ -640,6 +669,7 @@ module JSON
|
|
|
640
669
|
# when Array
|
|
641
670
|
# obj.map! {|v| deserialize_obj v }
|
|
642
671
|
# end
|
|
672
|
+
# obj
|
|
643
673
|
# })
|
|
644
674
|
# pp ruby
|
|
645
675
|
# Output:
|
|
@@ -663,7 +693,12 @@ module JSON
|
|
|
663
693
|
#
|
|
664
694
|
def unsafe_load(source, proc = nil, options = nil)
|
|
665
695
|
opts = if options.nil?
|
|
666
|
-
|
|
696
|
+
if proc && proc.is_a?(Hash)
|
|
697
|
+
options, proc = proc, nil
|
|
698
|
+
options
|
|
699
|
+
else
|
|
700
|
+
_unsafe_load_default_options
|
|
701
|
+
end
|
|
667
702
|
else
|
|
668
703
|
_unsafe_load_default_options.merge(options)
|
|
669
704
|
end
|
|
@@ -681,12 +716,17 @@ module JSON
|
|
|
681
716
|
if opts[:allow_blank] && (source.nil? || source.empty?)
|
|
682
717
|
source = 'null'
|
|
683
718
|
end
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
719
|
+
|
|
720
|
+
if proc
|
|
721
|
+
opts = opts.dup
|
|
722
|
+
opts[:on_load] = proc.to_proc
|
|
723
|
+
end
|
|
724
|
+
|
|
725
|
+
parse(source, opts)
|
|
687
726
|
end
|
|
688
727
|
|
|
689
728
|
# :call-seq:
|
|
729
|
+
# JSON.load(source, options = {}) -> object
|
|
690
730
|
# JSON.load(source, proc = nil, options = {}) -> object
|
|
691
731
|
#
|
|
692
732
|
# Returns the Ruby objects created by parsing the given +source+.
|
|
@@ -800,6 +840,7 @@ module JSON
|
|
|
800
840
|
# when Array
|
|
801
841
|
# obj.map! {|v| deserialize_obj v }
|
|
802
842
|
# end
|
|
843
|
+
# obj
|
|
803
844
|
# })
|
|
804
845
|
# pp ruby
|
|
805
846
|
# Output:
|
|
@@ -822,8 +863,18 @@ module JSON
|
|
|
822
863
|
# @attributes={"type"=>"Admin", "password"=>"0wn3d"}>}
|
|
823
864
|
#
|
|
824
865
|
def load(source, proc = nil, options = nil)
|
|
866
|
+
if proc && options.nil? && proc.is_a?(Hash)
|
|
867
|
+
options = proc
|
|
868
|
+
proc = nil
|
|
869
|
+
end
|
|
870
|
+
|
|
825
871
|
opts = if options.nil?
|
|
826
|
-
|
|
872
|
+
if proc && proc.is_a?(Hash)
|
|
873
|
+
options, proc = proc, nil
|
|
874
|
+
options
|
|
875
|
+
else
|
|
876
|
+
_load_default_options
|
|
877
|
+
end
|
|
827
878
|
else
|
|
828
879
|
_load_default_options.merge(options)
|
|
829
880
|
end
|
|
@@ -838,7 +889,7 @@ module JSON
|
|
|
838
889
|
end
|
|
839
890
|
end
|
|
840
891
|
|
|
841
|
-
if opts[:allow_blank] && (source.nil? || source.empty?)
|
|
892
|
+
if opts[:allow_blank] && (source.nil? || (String === source && source.empty?))
|
|
842
893
|
source = 'null'
|
|
843
894
|
end
|
|
844
895
|
|
|
@@ -996,10 +1047,11 @@ module JSON
|
|
|
996
1047
|
# JSON.new(options = nil, &block)
|
|
997
1048
|
#
|
|
998
1049
|
# Argument +options+, if given, contains a \Hash of options for both parsing and generating.
|
|
999
|
-
# See {Parsing Options}[
|
|
1050
|
+
# See {Parsing Options}[rdoc-ref:JSON@Parsing+Options],
|
|
1051
|
+
# and {Generating Options}[rdoc-ref:JSON@Generating+Options].
|
|
1000
1052
|
#
|
|
1001
1053
|
# For generation, the <tt>strict: true</tt> option is always set. When a Ruby object with no native \JSON counterpart is
|
|
1002
|
-
#
|
|
1054
|
+
# encountered, the block provided to the initialize method is invoked, and must return a Ruby object that has a native
|
|
1003
1055
|
# \JSON counterpart:
|
|
1004
1056
|
#
|
|
1005
1057
|
# module MyApp
|
|
@@ -1025,7 +1077,7 @@ module JSON
|
|
|
1025
1077
|
options[:as_json] = as_json if as_json
|
|
1026
1078
|
|
|
1027
1079
|
@state = State.new(options).freeze
|
|
1028
|
-
@parser_config = Ext::Parser::Config.new(ParserOptions.prepare(options))
|
|
1080
|
+
@parser_config = Ext::Parser::Config.new(ParserOptions.prepare(options)).freeze
|
|
1029
1081
|
end
|
|
1030
1082
|
|
|
1031
1083
|
# call-seq:
|
|
@@ -1034,7 +1086,7 @@ module JSON
|
|
|
1034
1086
|
#
|
|
1035
1087
|
# Serialize the given object into a \JSON document.
|
|
1036
1088
|
def dump(object, io = nil)
|
|
1037
|
-
@state.
|
|
1089
|
+
@state.generate(object, io)
|
|
1038
1090
|
end
|
|
1039
1091
|
alias_method :generate, :dump
|
|
1040
1092
|
|
|
@@ -1055,6 +1107,30 @@ module JSON
|
|
|
1055
1107
|
load(File.read(path, encoding: Encoding::UTF_8))
|
|
1056
1108
|
end
|
|
1057
1109
|
end
|
|
1110
|
+
|
|
1111
|
+
module GeneratorMethods
|
|
1112
|
+
# call-seq: to_json(*)
|
|
1113
|
+
#
|
|
1114
|
+
# Converts this object into a JSON string.
|
|
1115
|
+
# If this object doesn't directly maps to a JSON native type,
|
|
1116
|
+
# first convert it to a string (calling #to_s), then converts
|
|
1117
|
+
# it to a JSON string, and returns the result.
|
|
1118
|
+
# This is a fallback, if no special method #to_json was defined for some object.
|
|
1119
|
+
def to_json(state = nil, *)
|
|
1120
|
+
obj = case self
|
|
1121
|
+
when nil, false, true, Integer, Float, Array, Hash
|
|
1122
|
+
self
|
|
1123
|
+
else
|
|
1124
|
+
"#{self}"
|
|
1125
|
+
end
|
|
1126
|
+
|
|
1127
|
+
if state.nil?
|
|
1128
|
+
JSON::State._generate_no_fallback(obj, nil, nil)
|
|
1129
|
+
else
|
|
1130
|
+
JSON::State.from_state(state)._generate_no_fallback(obj)
|
|
1131
|
+
end
|
|
1132
|
+
end
|
|
1133
|
+
end
|
|
1058
1134
|
end
|
|
1059
1135
|
|
|
1060
1136
|
module ::Kernel
|
|
@@ -1070,7 +1146,7 @@ module ::Kernel
|
|
|
1070
1146
|
end
|
|
1071
1147
|
|
|
1072
1148
|
objs.each do |obj|
|
|
1073
|
-
puts JSON
|
|
1149
|
+
puts JSON.generate(obj, :allow_nan => true, :max_nesting => false)
|
|
1074
1150
|
end
|
|
1075
1151
|
nil
|
|
1076
1152
|
end
|
|
@@ -1085,7 +1161,7 @@ module ::Kernel
|
|
|
1085
1161
|
end
|
|
1086
1162
|
|
|
1087
1163
|
objs.each do |obj|
|
|
1088
|
-
puts JSON
|
|
1164
|
+
puts JSON.pretty_generate(obj, :allow_nan => true, :max_nesting => false)
|
|
1089
1165
|
end
|
|
1090
1166
|
nil
|
|
1091
1167
|
end
|
|
@@ -1100,3 +1176,7 @@ module ::Kernel
|
|
|
1100
1176
|
JSON[object, opts]
|
|
1101
1177
|
end
|
|
1102
1178
|
end
|
|
1179
|
+
|
|
1180
|
+
class Object
|
|
1181
|
+
include JSON::GeneratorMethods
|
|
1182
|
+
end
|
|
@@ -8,20 +8,8 @@ module JSON
|
|
|
8
8
|
#
|
|
9
9
|
# Instantiates a new State object, configured by _opts_.
|
|
10
10
|
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
# * *indent*: a string used to indent levels (default: ''),
|
|
14
|
-
# * *space*: a string that is put after, a : or , delimiter (default: ''),
|
|
15
|
-
# * *space_before*: a string that is put before a : pair delimiter (default: ''),
|
|
16
|
-
# * *object_nl*: a string that is put at the end of a JSON object (default: ''),
|
|
17
|
-
# * *array_nl*: a string that is put at the end of a JSON array (default: ''),
|
|
18
|
-
# * *allow_nan*: true if NaN, Infinity, and -Infinity should be
|
|
19
|
-
# generated, otherwise an exception is thrown, if these values are
|
|
20
|
-
# encountered. This options defaults to false.
|
|
21
|
-
# * *ascii_only*: true if only ASCII characters should be generated. This
|
|
22
|
-
# option defaults to false.
|
|
23
|
-
# * *buffer_initial_length*: sets the initial length of the generator's
|
|
24
|
-
# internal buffer.
|
|
11
|
+
# Argument +opts+, if given, contains a \Hash of options for the generation.
|
|
12
|
+
# See {Generating Options}[rdoc-ref:JSON@Generating+Options].
|
|
25
13
|
def initialize(opts = nil)
|
|
26
14
|
if opts && !opts.empty?
|
|
27
15
|
configure(opts)
|
|
@@ -66,8 +54,14 @@ module JSON
|
|
|
66
54
|
strict: strict?,
|
|
67
55
|
depth: depth,
|
|
68
56
|
buffer_initial_length: buffer_initial_length,
|
|
57
|
+
sort_keys: sort_keys
|
|
69
58
|
}
|
|
70
59
|
|
|
60
|
+
allow_duplicate_key = allow_duplicate_key?
|
|
61
|
+
unless allow_duplicate_key.nil?
|
|
62
|
+
result[:allow_duplicate_key] = allow_duplicate_key
|
|
63
|
+
end
|
|
64
|
+
|
|
71
65
|
instance_variables.each do |iv|
|
|
72
66
|
iv = iv.to_s[1..-1]
|
|
73
67
|
result[iv.to_sym] = self[iv]
|
|
@@ -82,6 +76,8 @@ module JSON
|
|
|
82
76
|
#
|
|
83
77
|
# Returns the value returned by method +name+.
|
|
84
78
|
def [](name)
|
|
79
|
+
::JSON.deprecation_warning("JSON::State#[] is deprecated and will be removed in json 3.0.0")
|
|
80
|
+
|
|
85
81
|
if respond_to?(name)
|
|
86
82
|
__send__(name)
|
|
87
83
|
else
|
|
@@ -94,6 +90,8 @@ module JSON
|
|
|
94
90
|
#
|
|
95
91
|
# Sets the attribute name to value.
|
|
96
92
|
def []=(name, value)
|
|
93
|
+
::JSON.deprecation_warning("JSON::State#[]= is deprecated and will be removed in json 3.0.0")
|
|
94
|
+
|
|
97
95
|
if respond_to?(name_writer = "#{name}=")
|
|
98
96
|
__send__ name_writer, value
|
|
99
97
|
else
|
data/lib/json/ext.rb
CHANGED
|
@@ -34,12 +34,38 @@ 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
|
-
|
|
44
|
+
if defined?(ResumableParser) # Not yet available on JRuby
|
|
45
|
+
class ResumableParser
|
|
46
|
+
# Returns whether the parser is entirely done: no unconsumed bytes in
|
|
47
|
+
# the buffer, no document under construction and no parsed value
|
|
48
|
+
# awaiting retrieval.
|
|
49
|
+
#
|
|
50
|
+
# The main use case is detecting a truncated stream once the input is
|
|
51
|
+
# exhausted:
|
|
52
|
+
#
|
|
53
|
+
# loop do
|
|
54
|
+
# begin
|
|
55
|
+
# parser << socket.readpartial(4096)
|
|
56
|
+
# rescue EOFError
|
|
57
|
+
# break
|
|
58
|
+
# end
|
|
59
|
+
# while parser.parse
|
|
60
|
+
# process(parser.value)
|
|
61
|
+
# end
|
|
62
|
+
# end
|
|
63
|
+
# warn "stream was truncated" unless parser.empty?
|
|
64
|
+
def empty?
|
|
65
|
+
eos? && !partial_value? && !value?
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
JSON_LOADED = true unless defined?(JSON::JSON_LOADED)
|
|
45
71
|
end
|
data/lib/json/generic_object.rb
CHANGED
|
@@ -52,14 +52,6 @@ module JSON
|
|
|
52
52
|
table
|
|
53
53
|
end
|
|
54
54
|
|
|
55
|
-
def [](name)
|
|
56
|
-
__send__(name)
|
|
57
|
-
end unless method_defined?(:[])
|
|
58
|
-
|
|
59
|
-
def []=(name, value)
|
|
60
|
-
__send__("#{name}=", value)
|
|
61
|
-
end unless method_defined?(:[]=)
|
|
62
|
-
|
|
63
55
|
def |(other)
|
|
64
56
|
self.class[other.to_hash.merge(to_hash)]
|
|
65
57
|
end
|