json 2.6.1 → 2.21.1
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/BSDL +22 -0
- data/CHANGES.md +370 -17
- data/LEGAL +20 -0
- data/README.md +96 -213
- data/ext/json/ext/fbuffer/fbuffer.h +190 -118
- data/ext/json/ext/generator/extconf.rb +17 -2
- data/ext/json/ext/generator/generator.c +1532 -1074
- data/ext/json/ext/json.h +183 -0
- data/ext/json/ext/parser/extconf.rb +30 -25
- data/ext/json/ext/parser/parser.c +2837 -3258
- 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 +480 -0
- data/ext/json/ext/vendor/jeaiii-ltoa.h +267 -0
- data/json.gemspec +48 -53
- data/lib/json/add/bigdecimal.rb +39 -10
- data/lib/json/add/complex.rb +29 -6
- data/lib/json/add/core.rb +2 -1
- data/lib/json/add/date.rb +27 -7
- data/lib/json/add/date_time.rb +26 -9
- data/lib/json/add/exception.rb +25 -7
- data/lib/json/add/ostruct.rb +32 -9
- data/lib/json/add/range.rb +33 -8
- data/lib/json/add/rational.rb +28 -6
- data/lib/json/add/regexp.rb +26 -8
- data/lib/json/add/set.rb +25 -6
- data/lib/json/add/string.rb +35 -0
- data/lib/json/add/struct.rb +29 -7
- data/lib/json/add/symbol.rb +34 -7
- data/lib/json/add/time.rb +29 -15
- data/lib/json/common.rb +746 -267
- data/lib/json/ext/generator/state.rb +104 -0
- data/lib/json/ext.rb +61 -5
- data/lib/json/generic_object.rb +7 -11
- data/lib/json/truffle_ruby/generator.rb +790 -0
- data/lib/json/version.rb +3 -7
- data/lib/json.rb +134 -24
- metadata +22 -26
- data/VERSION +0 -1
- data/ext/json/ext/generator/depend +0 -1
- data/ext/json/ext/generator/generator.h +0 -174
- data/ext/json/ext/parser/depend +0 -1
- data/ext/json/ext/parser/parser.h +0 -96
- data/ext/json/ext/parser/parser.rl +0 -977
- data/ext/json/extconf.rb +0 -3
- data/lib/json/pure/generator.rb +0 -479
- data/lib/json/pure/parser.rb +0 -337
- data/lib/json/pure.rb +0 -15
- /data/{LICENSE → COPYING} +0 -0
data/README.md
CHANGED
|
@@ -1,22 +1,14 @@
|
|
|
1
1
|
# JSON implementation for Ruby
|
|
2
2
|
|
|
3
|
-
[](https://github.com/ruby/json/actions/workflows/ci.yml)
|
|
4
4
|
|
|
5
5
|
## Description
|
|
6
6
|
|
|
7
|
-
This is
|
|
8
|
-
http://www.ietf.org/rfc/rfc7159.txt .
|
|
9
|
-
will be two variants available:
|
|
7
|
+
This is an implementation of the JSON specification according to RFC 7159
|
|
8
|
+
http://www.ietf.org/rfc/rfc7159.txt .
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
* The quite a bit faster native extension variant, which is in parts
|
|
14
|
-
implemented in C or Java and comes with its own unicode conversion
|
|
15
|
-
functions and a parser generated by the ragel state machine compiler
|
|
16
|
-
http://www.complang.org/ragel/ .
|
|
17
|
-
|
|
18
|
-
Both variants of the JSON generator generate UTF-8 character sequences by
|
|
19
|
-
default. If an :ascii\_only option with a true value is given, they escape all
|
|
10
|
+
The JSON generator generate UTF-8 character sequences by default.
|
|
11
|
+
If an :ascii\_only option with a true value is given, they escape all
|
|
20
12
|
non-ASCII and control characters with \uXXXX escape sequences, and support
|
|
21
13
|
UTF-16 surrogate pairs in order to be able to generate the whole range of
|
|
22
14
|
unicode code points.
|
|
@@ -29,52 +21,15 @@ endpoint.
|
|
|
29
21
|
|
|
30
22
|
## Installation
|
|
31
23
|
|
|
32
|
-
|
|
33
|
-
the pure ruby variant. If you cannot build it on your system, you can settle
|
|
34
|
-
for the latter.
|
|
35
|
-
|
|
36
|
-
Just type into the command line as root:
|
|
37
|
-
|
|
38
|
-
```
|
|
39
|
-
# rake install
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
The above command will build the extensions and install them on your system.
|
|
43
|
-
|
|
44
|
-
```
|
|
45
|
-
# rake install_pure
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
or
|
|
49
|
-
|
|
50
|
-
```
|
|
51
|
-
# ruby install.rb
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
will just install the pure ruby implementation of JSON.
|
|
55
|
-
|
|
56
|
-
If you use Rubygems you can type
|
|
24
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
57
25
|
|
|
58
|
-
|
|
59
|
-
# gem install json
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
instead, to install the newest JSON version.
|
|
63
|
-
|
|
64
|
-
There is also a pure ruby json only variant of the gem, that can be installed
|
|
65
|
-
with:
|
|
26
|
+
$ bundle add json
|
|
66
27
|
|
|
67
|
-
|
|
68
|
-
# gem install json_pure
|
|
69
|
-
```
|
|
28
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
70
29
|
|
|
71
|
-
|
|
30
|
+
$ gem install json
|
|
72
31
|
|
|
73
|
-
|
|
74
|
-
graphviz images of the state machines, you need ragel from:
|
|
75
|
-
http://www.complang.org/ragel/
|
|
76
|
-
|
|
77
|
-
## Usage
|
|
32
|
+
## Basic Usage
|
|
78
33
|
|
|
79
34
|
To use JSON you can
|
|
80
35
|
|
|
@@ -82,20 +37,6 @@ To use JSON you can
|
|
|
82
37
|
require 'json'
|
|
83
38
|
```
|
|
84
39
|
|
|
85
|
-
to load the installed variant (either the extension `'json'` or the pure
|
|
86
|
-
variant `'json_pure'`). If you have installed the extension variant, you can
|
|
87
|
-
pick either the extension variant or the pure variant by typing
|
|
88
|
-
|
|
89
|
-
```ruby
|
|
90
|
-
require 'json/ext'
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
or
|
|
94
|
-
|
|
95
|
-
```ruby
|
|
96
|
-
require 'json/pure'
|
|
97
|
-
```
|
|
98
|
-
|
|
99
40
|
Now you can parse a JSON document into a ruby data structure by calling
|
|
100
41
|
|
|
101
42
|
```ruby
|
|
@@ -111,59 +52,102 @@ You can also use the `pretty_generate` method (which formats the output more
|
|
|
111
52
|
verbosely and nicely) or `fast_generate` (which doesn't do any of the security
|
|
112
53
|
checks generate performs, e. g. nesting deepness checks).
|
|
113
54
|
|
|
114
|
-
|
|
115
|
-
generate a JSON document from an array or hash:
|
|
55
|
+
## Casting non native types
|
|
116
56
|
|
|
117
|
-
|
|
118
|
-
document = JSON 'test' => 23 # => "{\"test\":23}"
|
|
119
|
-
document = JSON['test' => 23] # => "{\"test\":23}"
|
|
120
|
-
```
|
|
57
|
+
JSON documents can only support Hashes, Arrays, Strings, Integers and Floats.
|
|
121
58
|
|
|
122
|
-
|
|
59
|
+
By default if you attempt to serialize something else, `JSON.generate` will
|
|
60
|
+
search for a `#to_json` method on that object:
|
|
123
61
|
|
|
124
62
|
```ruby
|
|
125
|
-
|
|
126
|
-
|
|
63
|
+
Position = Struct.new(:latitude, :longitude) do
|
|
64
|
+
def to_json(state = nil, *)
|
|
65
|
+
JSON::State.from_state(state).generate({
|
|
66
|
+
latitude: latitude,
|
|
67
|
+
longitude: longitude,
|
|
68
|
+
})
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
JSON.generate([
|
|
73
|
+
Position.new(12323.234, 435345.233),
|
|
74
|
+
Position.new(23434.676, 159435.324),
|
|
75
|
+
]) # => [{"latitude":12323.234,"longitude":435345.233},{"latitude":23434.676,"longitude":159435.324}]
|
|
127
76
|
```
|
|
128
77
|
|
|
129
|
-
|
|
130
|
-
you
|
|
78
|
+
If a `#to_json` method isn't defined on the object, `JSON.generate` will fallback to call `#to_s`:
|
|
131
79
|
|
|
132
80
|
```ruby
|
|
133
|
-
|
|
81
|
+
JSON.generate(Object.new) # => "#<Object:0x000000011e768b98>"
|
|
134
82
|
```
|
|
135
83
|
|
|
136
|
-
|
|
84
|
+
Both of these behavior can be disabled using the `strict: true` option:
|
|
137
85
|
|
|
138
86
|
```ruby
|
|
139
|
-
JSON
|
|
87
|
+
JSON.generate(Object.new, strict: true) # => Object not allowed in JSON (JSON::GeneratorError)
|
|
88
|
+
JSON.generate(Position.new(1, 2), strict: true) # => Position not allowed in JSON (JSON::GeneratorError)
|
|
140
89
|
```
|
|
141
90
|
|
|
142
|
-
|
|
143
|
-
|
|
91
|
+
## JSON::Coder
|
|
92
|
+
|
|
93
|
+
Since `#to_json` methods are global, it can sometimes be problematic if you need a given type to be
|
|
94
|
+
serialized in different ways in different locations.
|
|
144
95
|
|
|
145
|
-
|
|
96
|
+
Instead it is recommended to use the newer `JSON::Coder` API:
|
|
146
97
|
|
|
147
98
|
```ruby
|
|
148
|
-
|
|
99
|
+
module MyApp
|
|
100
|
+
API_JSON_CODER = JSON::Coder.new do |object, is_object_key|
|
|
101
|
+
case object
|
|
102
|
+
when Time
|
|
103
|
+
object.iso8601(3)
|
|
104
|
+
else
|
|
105
|
+
object
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
puts MyApp::API_JSON_CODER.dump(Time.now.utc) # => "2025-01-21T08:41:44.286Z"
|
|
149
111
|
```
|
|
150
112
|
|
|
151
|
-
|
|
152
|
-
|
|
113
|
+
The provided block is called for all objects that don't have a native JSON equivalent, and
|
|
114
|
+
must return a Ruby object that has a native JSON equivalent.
|
|
153
115
|
|
|
154
|
-
|
|
116
|
+
It is also called for objects that do have a JSON equivalent, but are used as Hash keys, for instance `{ 1 => 2}`,
|
|
117
|
+
as well as for strings that aren't valid UTF-8:
|
|
155
118
|
|
|
156
|
-
|
|
157
|
-
|
|
119
|
+
```ruby
|
|
120
|
+
coder = JSON::Coder.new do |object, is_object_key|
|
|
121
|
+
case object
|
|
122
|
+
when String
|
|
123
|
+
if !object.valid_encoding? || object.encoding != Encoding::UTF_8
|
|
124
|
+
Base64.encode64(object)
|
|
125
|
+
else
|
|
126
|
+
object
|
|
127
|
+
end
|
|
128
|
+
else
|
|
129
|
+
object
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Combining JSON fragments
|
|
158
135
|
|
|
159
|
-
To
|
|
160
|
-
above, or specifically load the exception addition:
|
|
136
|
+
To combine JSON fragments into a bigger JSON document, you can use `JSON::Fragment`:
|
|
161
137
|
|
|
162
138
|
```ruby
|
|
163
|
-
|
|
139
|
+
posts_json = cache.fetch_multi(post_ids) do |post_id|
|
|
140
|
+
JSON.generate(Post.find(post_id))
|
|
141
|
+
end
|
|
142
|
+
posts_json.map! { |post_json| JSON::Fragment.new(post_json) }
|
|
143
|
+
JSON.generate({ posts: posts_json, count: posts_json.count })
|
|
164
144
|
```
|
|
165
145
|
|
|
166
|
-
##
|
|
146
|
+
## Round-tripping arbitrary types
|
|
147
|
+
|
|
148
|
+
> [!CAUTION]
|
|
149
|
+
> You should never use `JSON.unsafe_load` nor `JSON.parse(str, create_additions: true)` to parse untrusted user input,
|
|
150
|
+
> as it can lead to remote code execution vulnerabilities.
|
|
167
151
|
|
|
168
152
|
To create a JSON document from a ruby data structure, you can call
|
|
169
153
|
`JSON.generate` like that:
|
|
@@ -229,7 +213,7 @@ JSON.parse json
|
|
|
229
213
|
# => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
|
230
214
|
json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
|
231
215
|
# => "[1,2,{\"a\":3.141},false,true,null,{\"json_class\":\"Range\",\"data\":[4,10,false]}]"
|
|
232
|
-
JSON.
|
|
216
|
+
JSON.unsafe_load json
|
|
233
217
|
# => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
|
234
218
|
```
|
|
235
219
|
|
|
@@ -265,136 +249,35 @@ There are also the methods `Kernel#j` for generate, and `Kernel#jj` for
|
|
|
265
249
|
`pretty_generate` output to the console, that work analogous to Core Ruby's `p` and
|
|
266
250
|
the `pp` library's `pp` methods.
|
|
267
251
|
|
|
268
|
-
|
|
269
|
-
receiving a JSON object from a webrick server in your browser with the
|
|
270
|
-
JavaScript prototype library http://www.prototypejs.org works.
|
|
271
|
-
|
|
272
|
-
## Speed Comparisons
|
|
252
|
+
## Security
|
|
273
253
|
|
|
274
|
-
|
|
275
|
-
subdir of the package) for the JSON-parser to estimate the speed up in the C
|
|
276
|
-
extension:
|
|
254
|
+
When parsing or serializing untrusted input, parser and generator options should never be user controlled.
|
|
277
255
|
|
|
278
|
-
```
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
553.922304770 ( real) -> 21.500x
|
|
282
|
-
0.001805307
|
|
283
|
-
2 ParserBenchmarkYAML#parser 1000 repeats:
|
|
284
|
-
224.513358139 ( real) -> 8.714x
|
|
285
|
-
0.004454078
|
|
286
|
-
3 ParserBenchmarkPure#parser 1000 repeats:
|
|
287
|
-
26.755020642 ( real) -> 1.038x
|
|
288
|
-
0.037376163
|
|
289
|
-
4 ParserBenchmarkRails#parser 1000 repeats:
|
|
290
|
-
25.763381731 ( real) -> 1.000x
|
|
291
|
-
0.038814780
|
|
292
|
-
calls/sec ( time) -> speed covers
|
|
293
|
-
secs/call
|
|
256
|
+
```ruby
|
|
257
|
+
# Dangerous, DO NOT DO THIS.
|
|
258
|
+
JSON.generate(params[:data], params[:options])
|
|
294
259
|
```
|
|
295
260
|
|
|
296
|
-
|
|
297
|
-
compatible JSON document, 3 is is `JSON::Pure::Parser`, and 4 is
|
|
298
|
-
`ActiveSupport::JSON.decode`. The ActiveSupport JSON-decoder converts the
|
|
299
|
-
input first to YAML and then uses the YAML-parser, the conversion seems to
|
|
300
|
-
slow it down so much that it is only as fast as the `JSON::Pure::Parser`!
|
|
301
|
-
|
|
302
|
-
If you look at the benchmark data you can see that this is mostly caused by
|
|
303
|
-
the frequent high outliers - the median of the Rails-parser runs is still
|
|
304
|
-
overall smaller than the median of the `JSON::Pure::Parser` runs:
|
|
305
|
-
|
|
306
|
-
```
|
|
307
|
-
Comparing times (call_time_median):
|
|
308
|
-
1 ParserBenchmarkExt#parser 900 repeats:
|
|
309
|
-
800.592479481 ( real) -> 26.936x
|
|
310
|
-
0.001249075
|
|
311
|
-
2 ParserBenchmarkYAML#parser 1000 repeats:
|
|
312
|
-
271.002390644 ( real) -> 9.118x
|
|
313
|
-
0.003690004
|
|
314
|
-
3 ParserBenchmarkRails#parser 1000 repeats:
|
|
315
|
-
30.227910865 ( real) -> 1.017x
|
|
316
|
-
0.033082008
|
|
317
|
-
4 ParserBenchmarkPure#parser 1000 repeats:
|
|
318
|
-
29.722384421 ( real) -> 1.000x
|
|
319
|
-
0.033644676
|
|
320
|
-
calls/sec ( time) -> speed covers
|
|
321
|
-
secs/call
|
|
322
|
-
```
|
|
261
|
+
Security vulnerability reports relying on attacker controlled parsing or generator options will be handled as regular bug fixes.
|
|
323
262
|
|
|
324
|
-
|
|
325
|
-
values, because there are different modes that also influence the achieved
|
|
326
|
-
speed:
|
|
263
|
+
## Development
|
|
327
264
|
|
|
328
|
-
|
|
329
|
-
Comparing times (call_time_mean):
|
|
330
|
-
1 GeneratorBenchmarkExt#generator_fast 1000 repeats:
|
|
331
|
-
547.354332608 ( real) -> 15.090x
|
|
332
|
-
0.001826970
|
|
333
|
-
2 GeneratorBenchmarkExt#generator_safe 1000 repeats:
|
|
334
|
-
443.968212317 ( real) -> 12.240x
|
|
335
|
-
0.002252414
|
|
336
|
-
3 GeneratorBenchmarkExt#generator_pretty 900 repeats:
|
|
337
|
-
375.104545883 ( real) -> 10.341x
|
|
338
|
-
0.002665923
|
|
339
|
-
4 GeneratorBenchmarkPure#generator_fast 1000 repeats:
|
|
340
|
-
49.978706968 ( real) -> 1.378x
|
|
341
|
-
0.020008521
|
|
342
|
-
5 GeneratorBenchmarkRails#generator 1000 repeats:
|
|
343
|
-
38.531868759 ( real) -> 1.062x
|
|
344
|
-
0.025952543
|
|
345
|
-
6 GeneratorBenchmarkPure#generator_safe 1000 repeats:
|
|
346
|
-
36.927649925 ( real) -> 1.018x 7 (>=3859)
|
|
347
|
-
0.027079979
|
|
348
|
-
7 GeneratorBenchmarkPure#generator_pretty 1000 repeats:
|
|
349
|
-
36.272134441 ( real) -> 1.000x 6 (>=3859)
|
|
350
|
-
0.027569373
|
|
351
|
-
calls/sec ( time) -> speed covers
|
|
352
|
-
secs/call
|
|
353
|
-
```
|
|
265
|
+
### Prerequisites
|
|
354
266
|
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
bit faster than the `generator_safe` and `generator_pretty` methods of the pure
|
|
358
|
-
variant but slower than the others.
|
|
267
|
+
1. Clone the repository
|
|
268
|
+
2. Install dependencies with `bundle install`
|
|
359
269
|
|
|
360
|
-
|
|
361
|
-
method. Beware, that this will disable the checking for circular Ruby data
|
|
362
|
-
structures, which may cause JSON to go into an infinite loop.
|
|
270
|
+
### Testing
|
|
363
271
|
|
|
364
|
-
|
|
272
|
+
The full test suite can be run with:
|
|
365
273
|
|
|
274
|
+
```bash
|
|
275
|
+
bundle exec rake test
|
|
366
276
|
```
|
|
367
|
-
Comparing times (call_time_median):
|
|
368
|
-
1 GeneratorBenchmarkExt#generator_fast 1000 repeats:
|
|
369
|
-
708.258020939 ( real) -> 16.547x
|
|
370
|
-
0.001411915
|
|
371
|
-
2 GeneratorBenchmarkExt#generator_safe 1000 repeats:
|
|
372
|
-
569.105020353 ( real) -> 13.296x
|
|
373
|
-
0.001757145
|
|
374
|
-
3 GeneratorBenchmarkExt#generator_pretty 900 repeats:
|
|
375
|
-
482.825371244 ( real) -> 11.280x
|
|
376
|
-
0.002071142
|
|
377
|
-
4 GeneratorBenchmarkPure#generator_fast 1000 repeats:
|
|
378
|
-
62.717626652 ( real) -> 1.465x
|
|
379
|
-
0.015944481
|
|
380
|
-
5 GeneratorBenchmarkRails#generator 1000 repeats:
|
|
381
|
-
43.965681162 ( real) -> 1.027x
|
|
382
|
-
0.022745013
|
|
383
|
-
6 GeneratorBenchmarkPure#generator_safe 1000 repeats:
|
|
384
|
-
43.929073409 ( real) -> 1.026x 7 (>=3859)
|
|
385
|
-
0.022763968
|
|
386
|
-
7 GeneratorBenchmarkPure#generator_pretty 1000 repeats:
|
|
387
|
-
42.802514491 ( real) -> 1.000x 6 (>=3859)
|
|
388
|
-
0.023363113
|
|
389
|
-
calls/sec ( time) -> speed covers
|
|
390
|
-
secs/call
|
|
391
|
-
```
|
|
392
|
-
|
|
393
|
-
## Development
|
|
394
277
|
|
|
395
278
|
### Release
|
|
396
279
|
|
|
397
|
-
Update the json.
|
|
280
|
+
Update the `lib/json/version.rb` file.
|
|
398
281
|
|
|
399
282
|
```
|
|
400
283
|
rbenv shell 2.6.5
|