json 2.7.2 → 2.10.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 +98 -17
- data/LEGAL +8 -0
- data/README.md +68 -216
- data/ext/json/ext/fbuffer/fbuffer.h +110 -92
- data/ext/json/ext/generator/extconf.rb +8 -2
- data/ext/json/ext/generator/generator.c +952 -833
- data/ext/json/ext/parser/extconf.rb +7 -27
- data/ext/json/ext/parser/parser.c +1207 -1940
- data/json.gemspec +44 -49
- data/lib/json/add/bigdecimal.rb +2 -2
- data/lib/json/add/complex.rb +1 -1
- data/lib/json/add/core.rb +1 -1
- data/lib/json/add/date.rb +1 -1
- data/lib/json/add/date_time.rb +1 -1
- data/lib/json/add/exception.rb +1 -1
- data/lib/json/add/ostruct.rb +1 -1
- data/lib/json/add/range.rb +1 -1
- data/lib/json/add/rational.rb +1 -1
- data/lib/json/add/regexp.rb +1 -1
- data/lib/json/add/struct.rb +1 -1
- data/lib/json/add/symbol.rb +8 -4
- data/lib/json/add/time.rb +3 -10
- data/lib/json/common.rb +401 -106
- data/lib/json/ext/generator/state.rb +106 -0
- data/lib/json/ext.rb +34 -4
- data/lib/json/generic_object.rb +1 -1
- data/lib/json/{pure → truffle_ruby}/generator.rb +322 -145
- data/lib/json/version.rb +3 -7
- data/lib/json.rb +16 -21
- metadata +15 -22
- data/ext/json/ext/generator/depend +0 -1
- data/ext/json/ext/generator/generator.h +0 -177
- 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 -971
- data/ext/json/extconf.rb +0 -3
- data/lib/json/pure/parser.rb +0 -337
- data/lib/json/pure.rb +0 -15
- /data/{LICENSE → COPYING} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc08bd5e3c83c4e731d37e18b5a32f9bf0049d896c483f1beb96af96f2daad18
|
4
|
+
data.tar.gz: ff215943fdccc5a5ed3f7390719aa5c43a18664e19f86d94226ff14c154cc354
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3bc85653be385e76842fa3f69671fccdc4235a32806f554cf8ec4a9ad697996be97d3aef52823b3c1f8839e1cd9a7893ab87360ca76d59b4ff704c75fcb655e
|
7
|
+
data.tar.gz: f2f09188afd38972e15e1c80c2c8c4efd14aee954d18ff83d4a6a993bd0f9d9c84362232c17abbc9bf3ffdfce062c79c75a6fccf124bdbd3345a202b76d50a26
|
data/BSDL
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved.
|
2
|
+
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
4
|
+
modification, are permitted provided that the following conditions
|
5
|
+
are met:
|
6
|
+
1. Redistributions of source code must retain the above copyright
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
|
+
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
13
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
14
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
15
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
16
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
17
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
18
|
+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
19
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
20
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
21
|
+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
22
|
+
SUCH DAMAGE.
|
data/CHANGES.md
CHANGED
@@ -1,5 +1,87 @@
|
|
1
1
|
# Changes
|
2
2
|
|
3
|
+
### 2025-02-10 (2.10.1)
|
4
|
+
|
5
|
+
* Fix a compatibility issue with `MultiJson.dump(obj, pretty: true)`: `no implicit conversion of false into Proc (TypeError)`.
|
6
|
+
|
7
|
+
### 2025-02-10 (2.10.0)
|
8
|
+
|
9
|
+
* `strict: true` now accept symbols as values. Previously they'd only be accepted as hash keys.
|
10
|
+
* The C extension Parser has been entirely reimplemented from scratch.
|
11
|
+
* Introduced `JSON::Coder` as a new API allowing to customize how non native types are serialized in a non-global way.
|
12
|
+
* Introduced `JSON::Fragment` to allow assembling cached fragments in a safe way.
|
13
|
+
* The Java implementation of the generator received many optimizations.
|
14
|
+
|
15
|
+
### 2024-12-18 (2.9.1)
|
16
|
+
|
17
|
+
* Fix support for Solaris 10.
|
18
|
+
|
19
|
+
### 2024-12-03 (2.9.0)
|
20
|
+
|
21
|
+
* Fix C implementation of `script_safe` escaping to not confuse some other 3 wide characters with `\u2028` and `\u2029`.
|
22
|
+
e.g. `JSON.generate(["倩", "瀨"], script_safe: true)` would generate the wrong JSON.
|
23
|
+
* `JSON.dump(object, some_io)` now write into the IO in chunks while previously it would buffer the entire JSON before writing.
|
24
|
+
* `JSON::GeneratorError` now has a `#invalid_object` attribute, making it easier to understand why an object tree cannot be serialized.
|
25
|
+
* Numerous improvements to the JRuby extension.
|
26
|
+
|
27
|
+
### 2024-11-14 (2.8.2)
|
28
|
+
|
29
|
+
* `JSON.load_file` explictly read the file as UTF-8.
|
30
|
+
|
31
|
+
### 2024-11-06 (2.8.1)
|
32
|
+
|
33
|
+
* Fix the java packages to include the extension.
|
34
|
+
|
35
|
+
### 2024-11-06 (2.8.0)
|
36
|
+
|
37
|
+
* Emit a deprecation warning when `JSON.load` create custom types without the `create_additions` option being explictly enabled.
|
38
|
+
* Prefer to use `JSON.unsafe_load(string)` or `JSON.load(string, create_additions: true)`.
|
39
|
+
* Emit a deprecation warning when serializing valid UTF-8 strings encoded in `ASCII_8BIT` aka `BINARY`.
|
40
|
+
* Bump required Ruby version to 2.7.
|
41
|
+
* Add support for optionally parsing trailing commas, via `allow_trailing_comma: true`, which in cunjunction with the
|
42
|
+
pre-existing support for comments, make it suitable to parse `jsonc` documents.
|
43
|
+
* Many performance improvements to `JSON.parse` and `JSON.load`, up to `1.7x` faster on real world documents.
|
44
|
+
* Some minor performance improvements to `JSON.dump` and `JSON.generate`.
|
45
|
+
* `JSON.pretty_generate` no longer include newline inside empty object and arrays.
|
46
|
+
|
47
|
+
### 2024-11-04 (2.7.6)
|
48
|
+
|
49
|
+
* Fix a regression in JSON.generate when dealing with Hash keys that are string subclasses, call `to_json` on them.
|
50
|
+
|
51
|
+
### 2024-10-25 (2.7.5)
|
52
|
+
|
53
|
+
* Fix a memory leak when `#to_json` methods raise an exception.
|
54
|
+
* Gracefully handle formatting configs being set to `nil` instead of `""`.
|
55
|
+
* Workaround another issue caused by conflicting versions of both `json_pure` and `json` being loaded.
|
56
|
+
|
57
|
+
### 2024-10-25 (2.7.4)
|
58
|
+
|
59
|
+
* Workaround a bug in 3.4.8 and older https://github.com/rubygems/rubygems/pull/6490.
|
60
|
+
This bug would cause some gems with native extension to fail during compilation.
|
61
|
+
* Workaround different versions of `json` and `json_pure` being loaded (not officially supported).
|
62
|
+
* Make `json_pure` Ractor compatible.
|
63
|
+
|
64
|
+
### 2024-10-24 (2.7.3)
|
65
|
+
|
66
|
+
* Numerous performance optimizations in `JSON.generate` and `JSON.dump` (up to 2 times faster).
|
67
|
+
* Limit the size of ParserError exception messages, only include up to 32 bytes of the unparseable source.
|
68
|
+
* Fix json-pure's `Object#to_json` to accept non state arguments
|
69
|
+
* Fix multiline comment support in `json-pure`.
|
70
|
+
* Fix `JSON.parse` to no longer mutate the argument encoding when passed an ASCII-8BIT string.
|
71
|
+
* Fix `String#to_json` to raise on invalid encoding in `json-pure`.
|
72
|
+
* Delete code that was based on CVTUTF.
|
73
|
+
* Use the pure-Ruby generator on TruffleRuby.
|
74
|
+
* Fix `strict` mode in `json-pure` to not break on Integer.
|
75
|
+
|
76
|
+
### 2024-04-04 (2.7.2)
|
77
|
+
|
78
|
+
* Use rb_sym2str instead of SYM2ID #561
|
79
|
+
* Fix memory leak when exception is raised during JSON generation #574
|
80
|
+
* Remove references to "19" methods in JRuby #576
|
81
|
+
* Make OpenStruct support as optional by @hsbt in #565
|
82
|
+
* Autoload JSON::GenericObject to avoid require ostruct warning in Ruby 3.4 #577
|
83
|
+
* Warn to install ostruct if json couldn't load it by @hsbt #578
|
84
|
+
|
3
85
|
### 2023-12-05 (2.7.1)
|
4
86
|
|
5
87
|
* JSON.dump: handle unenclosed hashes regression #554
|
@@ -168,7 +250,7 @@
|
|
168
250
|
* Remove Rubinius exception since transcoding should be working now.
|
169
251
|
|
170
252
|
## 2013-05-13 (1.8.0)
|
171
|
-
* Fix https://github.com/
|
253
|
+
* Fix https://github.com/ruby/json/issues/162 reported by Marc-Andre
|
172
254
|
Lafortune <github_rocks@marc-andre.ca>. Thanks!
|
173
255
|
* Applied patches by Yui NARUSE <naruse@airemix.jp> to suppress warning with
|
174
256
|
-Wchar-subscripts and better validate UTF-8 strings.
|
@@ -188,7 +270,7 @@
|
|
188
270
|
JSON::GenericObject.json_creatable = true
|
189
271
|
as well.
|
190
272
|
* Remove useless assert in fbuffer implementation.
|
191
|
-
* Apply patch attached to https://github.com/
|
273
|
+
* Apply patch attached to https://github.com/ruby/json/issues#issue/155
|
192
274
|
provided by John Shahid <jvshahid@gmail.com>, Thx!
|
193
275
|
* Add license information to rubygems spec data, reported by Jordi Massaguer Pla <jmassaguerpla@suse.de>.
|
194
276
|
* Improve documentation, thx to Zachary Scott <zachary@zacharyscott.net>.
|
@@ -202,7 +284,7 @@
|
|
202
284
|
* Fix compilation of extension on older rubies.
|
203
285
|
|
204
286
|
## 2012-07-26 (1.7.4)
|
205
|
-
* Fix compilation problem on AIX, see https://github.com/
|
287
|
+
* Fix compilation problem on AIX, see https://github.com/ruby/json/issues/142
|
206
288
|
|
207
289
|
## 2012-05-12 (1.7.3)
|
208
290
|
* Work around Rubinius encoding issues using iconv for conversion instead.
|
@@ -224,9 +306,9 @@
|
|
224
306
|
* Propagate src encoding to values made from it (fixes 1.9 mode converting
|
225
307
|
everything to ascii-8bit; harmless for 1.8 mode too) (Thomas E. Enebo
|
226
308
|
<tom.enebo@gmail.com>), should fix
|
227
|
-
https://github.com/
|
228
|
-
* Fix https://github.com/
|
229
|
-
* Fix https://github.com/
|
309
|
+
https://github.com/ruby/json/issues#issue/119.
|
310
|
+
* Fix https://github.com/ruby/json/issues#issue/124 Thx to Jason Hutchens.
|
311
|
+
* Fix https://github.com/ruby/json/issues#issue/117
|
230
312
|
|
231
313
|
## 2012-01-15 (1.6.5)
|
232
314
|
* Vit Ondruch <v.ondruch@tiscali.cz> reported a bug that shows up when using
|
@@ -253,7 +335,7 @@
|
|
253
335
|
patch go to Josh Partlow (jpartlow@github).
|
254
336
|
* Improve parsing speed for JSON numbers (integers and floats) in a similar way to
|
255
337
|
what Evan Phoenix <evan@phx.io> suggested in:
|
256
|
-
https://github.com/
|
338
|
+
https://github.com/ruby/json/pull/103
|
257
339
|
|
258
340
|
## 2011-09-18 (1.6.1)
|
259
341
|
* Using -target 1.5 to force Java bits to compile with 1.5.
|
@@ -266,15 +348,14 @@
|
|
266
348
|
* Fix memory leak when used from multiple JRuby. (Patch by
|
267
349
|
jfirebaugh@github).
|
268
350
|
* Apply patch by Eric Wong <nocode@yhbt.net> that fixes garbage collection problem
|
269
|
-
reported in https://github.com/
|
351
|
+
reported in https://github.com/ruby/json/issues/46.
|
270
352
|
* Add :quirks_mode option to parser and generator.
|
271
353
|
* Add support for Rational and Complex number additions via json/add/complex
|
272
354
|
and json/add/rational requires.
|
273
355
|
|
274
356
|
## 2011-06-20 (1.5.3)
|
275
357
|
* Alias State#configure method as State#merge to increase duck type synonymy with Hash.
|
276
|
-
|
277
|
-
the new way.
|
358
|
+
* Add `as_json` methods in json/add/core, so rails can create its json objects the new way.
|
278
359
|
|
279
360
|
## 2011-05-11 (1.5.2)
|
280
361
|
* Apply documentation patch by Cory Monty <cory.monty@gmail.com>.
|
@@ -285,7 +366,7 @@
|
|
285
366
|
|
286
367
|
## 2011-01-24 (1.5.1)
|
287
368
|
* Made rake-compiler build a fat binary gem. This should fix issue
|
288
|
-
https://github.com/
|
369
|
+
https://github.com/ruby/json/issues#issue/54.
|
289
370
|
|
290
371
|
## 2011-01-22 (1.5.0)
|
291
372
|
* Included Java source codes for the Jruby extension made by Daniel Luz
|
@@ -295,7 +376,7 @@
|
|
295
376
|
reported by Riley Goodside.
|
296
377
|
|
297
378
|
## 2010-08-09 (1.4.6)
|
298
|
-
* Fixed oversight reported in http://github.com/
|
379
|
+
* Fixed oversight reported in http://github.com/ruby/json/issues/closed#issue/23,
|
299
380
|
always create a new object from the state prototype.
|
300
381
|
* Made pure and ext api more similar again.
|
301
382
|
|
@@ -305,12 +386,12 @@
|
|
305
386
|
argument.
|
306
387
|
* Some fixes in the state objects and additional tests.
|
307
388
|
## 2010-08-06 (1.4.4)
|
308
|
-
* Fixes build problem for rubinius under OS X, http://github.com/
|
309
|
-
* Fixes crashes described in http://github.com/
|
310
|
-
http://github.com/
|
389
|
+
* Fixes build problem for rubinius under OS X, http://github.com/ruby/json/issues/closed#issue/25
|
390
|
+
* Fixes crashes described in http://github.com/ruby/json/issues/closed#issue/21 and
|
391
|
+
http://github.com/ruby/json/issues/closed#issue/23
|
311
392
|
## 2010-05-05 (1.4.3)
|
312
393
|
* Fixed some test assertions, from Ruby r27587 and r27590, patch by nobu.
|
313
|
-
* Fixed issue http://github.com/
|
394
|
+
* Fixed issue http://github.com/ruby/json/issues/#issue/20 reported by
|
314
395
|
electronicwhisper@github. Thx!
|
315
396
|
|
316
397
|
## 2010-04-26 (1.4.2)
|
@@ -332,7 +413,7 @@
|
|
332
413
|
* Extension should at least be compatible with MRI, YARV and Rubinius.
|
333
414
|
|
334
415
|
## 2010-04-07 (1.2.4)
|
335
|
-
*
|
416
|
+
* Trigger const_missing callback to make Rails' dynamic class loading work.
|
336
417
|
|
337
418
|
## 2010-03-11 (1.2.3)
|
338
419
|
* Added a `State#[]` method which returns an attribute's value in order to
|
data/LEGAL
ADDED
data/README.md
CHANGED
@@ -1,21 +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
|
-
|
17
|
-
Both variants of the JSON generator generate UTF-8 character sequences by
|
18
|
-
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
|
19
12
|
non-ASCII and control characters with \uXXXX escape sequences, and support
|
20
13
|
UTF-16 surrogate pairs in order to be able to generate the whole range of
|
21
14
|
unicode code points.
|
@@ -28,51 +21,15 @@ endpoint.
|
|
28
21
|
|
29
22
|
## Installation
|
30
23
|
|
31
|
-
|
32
|
-
the pure ruby variant. If you cannot build it on your system, you can settle
|
33
|
-
for the latter.
|
34
|
-
|
35
|
-
Just type into the command line as root:
|
36
|
-
|
37
|
-
```
|
38
|
-
# rake install
|
39
|
-
```
|
40
|
-
|
41
|
-
The above command will build the extensions and install them on your system.
|
42
|
-
|
43
|
-
```
|
44
|
-
# rake install_pure
|
45
|
-
```
|
46
|
-
|
47
|
-
or
|
48
|
-
|
49
|
-
```
|
50
|
-
# ruby install.rb
|
51
|
-
```
|
52
|
-
|
53
|
-
will just install the pure ruby implementation of JSON.
|
54
|
-
|
55
|
-
If you use Rubygems you can type
|
56
|
-
|
57
|
-
```
|
58
|
-
# gem install json
|
59
|
-
```
|
60
|
-
|
61
|
-
instead, to install the newest JSON version.
|
62
|
-
|
63
|
-
There is also a pure ruby json only variant of the gem, that can be installed
|
64
|
-
with:
|
24
|
+
Install the gem and add to the application's Gemfile by executing:
|
65
25
|
|
66
|
-
|
67
|
-
# gem install json_pure
|
68
|
-
```
|
26
|
+
$ bundle add json
|
69
27
|
|
70
|
-
|
28
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
71
29
|
|
72
|
-
|
73
|
-
graphviz images of the state machines, you need [Ragel].
|
30
|
+
$ gem install json
|
74
31
|
|
75
|
-
## Usage
|
32
|
+
## Basic Usage
|
76
33
|
|
77
34
|
To use JSON you can
|
78
35
|
|
@@ -80,20 +37,6 @@ To use JSON you can
|
|
80
37
|
require 'json'
|
81
38
|
```
|
82
39
|
|
83
|
-
to load the installed variant (either the extension `'json'` or the pure
|
84
|
-
variant `'json_pure'`). If you have installed the extension variant, you can
|
85
|
-
pick either the extension variant or the pure variant by typing
|
86
|
-
|
87
|
-
```ruby
|
88
|
-
require 'json/ext'
|
89
|
-
```
|
90
|
-
|
91
|
-
or
|
92
|
-
|
93
|
-
```ruby
|
94
|
-
require 'json/pure'
|
95
|
-
```
|
96
|
-
|
97
40
|
Now you can parse a JSON document into a ruby data structure by calling
|
98
41
|
|
99
42
|
```ruby
|
@@ -109,50 +52,84 @@ You can also use the `pretty_generate` method (which formats the output more
|
|
109
52
|
verbosely and nicely) or `fast_generate` (which doesn't do any of the security
|
110
53
|
checks generate performs, e. g. nesting deepness checks).
|
111
54
|
|
112
|
-
|
113
|
-
|
55
|
+
## Casting non native types
|
56
|
+
|
57
|
+
JSON documents can only support Hashes, Arrays, Strings, Integers and Floats.
|
58
|
+
|
59
|
+
By default if you attempt to serialize something else, `JSON.generate` will
|
60
|
+
search for a `#to_json` method on that object:
|
114
61
|
|
115
62
|
```ruby
|
116
|
-
|
117
|
-
|
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}]
|
118
76
|
```
|
119
77
|
|
120
|
-
|
78
|
+
If a `#to_json` method isn't defined on the object, `JSON.generate` will fallback to call `#to_s`:
|
121
79
|
|
122
80
|
```ruby
|
123
|
-
|
124
|
-
data = JSON['{"test":23}'] # => {"test"=>23}
|
81
|
+
JSON.generate(Object.new) # => "#<Object:0x000000011e768b98>"
|
125
82
|
```
|
126
83
|
|
127
|
-
|
128
|
-
you
|
84
|
+
Both of these behavior can be disabled using the `strict: true` option:
|
129
85
|
|
130
86
|
```ruby
|
131
|
-
|
87
|
+
JSON.generate(Object.new, strict: true) # => Object not allowed in JSON (JSON::GeneratorError)
|
88
|
+
JSON.generate(Position.new(1, 2)) # => Position not allowed in JSON (JSON::GeneratorError)
|
132
89
|
```
|
133
90
|
|
134
|
-
|
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.
|
95
|
+
|
96
|
+
Instead it is recommended to use the newer `JSON::Coder` API:
|
135
97
|
|
136
98
|
```ruby
|
137
|
-
|
138
|
-
|
99
|
+
module MyApp
|
100
|
+
API_JSON_CODER = JSON::Coder.new do |object|
|
101
|
+
case object
|
102
|
+
when Time
|
103
|
+
object.iso8601(3)
|
104
|
+
else
|
105
|
+
object
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
139
109
|
|
140
|
-
|
141
|
-
|
110
|
+
puts MyApp::API_JSON_CODER.dump(Time.now.utc) # => "2025-01-21T08:41:44.286Z"
|
111
|
+
```
|
142
112
|
|
143
|
-
|
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.
|
144
115
|
|
145
|
-
|
146
|
-
object to JSON, it will by default only include the exception message.
|
116
|
+
## Combining JSON fragments
|
147
117
|
|
148
|
-
To
|
149
|
-
above, or specifically load the exception addition:
|
118
|
+
To combine JSON fragments into a bigger JSON document, you can use `JSON::Fragment`:
|
150
119
|
|
151
120
|
```ruby
|
152
|
-
|
121
|
+
posts_json = cache.fetch_multi(post_ids) do |post_id|
|
122
|
+
JSON.generate(Post.find(post_id))
|
123
|
+
end
|
124
|
+
posts_json.map! { |post_json| JSON::Fragment.new(post_json) }
|
125
|
+
JSON.generate({ posts: posts_json, count: posts_json.count })
|
153
126
|
```
|
154
127
|
|
155
|
-
##
|
128
|
+
## Round-tripping arbitrary types
|
129
|
+
|
130
|
+
> [!CAUTION]
|
131
|
+
> You should never use `JSON.unsafe_load` nor `JSON.parse(str, create_additions: true)` to parse untrusted user input,
|
132
|
+
> as it can lead to remote code execution vulnerabilities.
|
156
133
|
|
157
134
|
To create a JSON document from a ruby data structure, you can call
|
158
135
|
`JSON.generate` like that:
|
@@ -218,7 +195,7 @@ JSON.parse json
|
|
218
195
|
# => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
219
196
|
json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
220
197
|
# => "[1,2,{\"a\":3.141},false,true,null,{\"json_class\":\"Range\",\"data\":[4,10,false]}]"
|
221
|
-
JSON.
|
198
|
+
JSON.unsafe_load json
|
222
199
|
# => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
223
200
|
```
|
224
201
|
|
@@ -254,136 +231,11 @@ There are also the methods `Kernel#j` for generate, and `Kernel#jj` for
|
|
254
231
|
`pretty_generate` output to the console, that work analogous to Core Ruby's `p` and
|
255
232
|
the `pp` library's `pp` methods.
|
256
233
|
|
257
|
-
The script `tools/server.rb` contains a small example if you want to test, how
|
258
|
-
receiving a JSON object from a webrick server in your browser with the
|
259
|
-
JavaScript prototype library http://www.prototypejs.org works.
|
260
|
-
|
261
|
-
## Speed Comparisons
|
262
|
-
|
263
|
-
I have created some benchmark results (see the benchmarks/data-p4-3Ghz
|
264
|
-
subdir of the package) for the JSON-parser to estimate the speed up in the C
|
265
|
-
extension:
|
266
|
-
|
267
|
-
```
|
268
|
-
Comparing times (call_time_mean):
|
269
|
-
1 ParserBenchmarkExt#parser 900 repeats:
|
270
|
-
553.922304770 ( real) -> 21.500x
|
271
|
-
0.001805307
|
272
|
-
2 ParserBenchmarkYAML#parser 1000 repeats:
|
273
|
-
224.513358139 ( real) -> 8.714x
|
274
|
-
0.004454078
|
275
|
-
3 ParserBenchmarkPure#parser 1000 repeats:
|
276
|
-
26.755020642 ( real) -> 1.038x
|
277
|
-
0.037376163
|
278
|
-
4 ParserBenchmarkRails#parser 1000 repeats:
|
279
|
-
25.763381731 ( real) -> 1.000x
|
280
|
-
0.038814780
|
281
|
-
calls/sec ( time) -> speed covers
|
282
|
-
secs/call
|
283
|
-
```
|
284
|
-
|
285
|
-
In the table above 1 is `JSON::Ext::Parser`, 2 is `YAML.load` with YAML
|
286
|
-
compatible JSON document, 3 is is `JSON::Pure::Parser`, and 4 is
|
287
|
-
`ActiveSupport::JSON.decode`. The ActiveSupport JSON-decoder converts the
|
288
|
-
input first to YAML and then uses the YAML-parser, the conversion seems to
|
289
|
-
slow it down so much that it is only as fast as the `JSON::Pure::Parser`!
|
290
|
-
|
291
|
-
If you look at the benchmark data you can see that this is mostly caused by
|
292
|
-
the frequent high outliers - the median of the Rails-parser runs is still
|
293
|
-
overall smaller than the median of the `JSON::Pure::Parser` runs:
|
294
|
-
|
295
|
-
```
|
296
|
-
Comparing times (call_time_median):
|
297
|
-
1 ParserBenchmarkExt#parser 900 repeats:
|
298
|
-
800.592479481 ( real) -> 26.936x
|
299
|
-
0.001249075
|
300
|
-
2 ParserBenchmarkYAML#parser 1000 repeats:
|
301
|
-
271.002390644 ( real) -> 9.118x
|
302
|
-
0.003690004
|
303
|
-
3 ParserBenchmarkRails#parser 1000 repeats:
|
304
|
-
30.227910865 ( real) -> 1.017x
|
305
|
-
0.033082008
|
306
|
-
4 ParserBenchmarkPure#parser 1000 repeats:
|
307
|
-
29.722384421 ( real) -> 1.000x
|
308
|
-
0.033644676
|
309
|
-
calls/sec ( time) -> speed covers
|
310
|
-
secs/call
|
311
|
-
```
|
312
|
-
|
313
|
-
I have benchmarked the `JSON-Generator` as well. This generated a few more
|
314
|
-
values, because there are different modes that also influence the achieved
|
315
|
-
speed:
|
316
|
-
|
317
|
-
```
|
318
|
-
Comparing times (call_time_mean):
|
319
|
-
1 GeneratorBenchmarkExt#generator_fast 1000 repeats:
|
320
|
-
547.354332608 ( real) -> 15.090x
|
321
|
-
0.001826970
|
322
|
-
2 GeneratorBenchmarkExt#generator_safe 1000 repeats:
|
323
|
-
443.968212317 ( real) -> 12.240x
|
324
|
-
0.002252414
|
325
|
-
3 GeneratorBenchmarkExt#generator_pretty 900 repeats:
|
326
|
-
375.104545883 ( real) -> 10.341x
|
327
|
-
0.002665923
|
328
|
-
4 GeneratorBenchmarkPure#generator_fast 1000 repeats:
|
329
|
-
49.978706968 ( real) -> 1.378x
|
330
|
-
0.020008521
|
331
|
-
5 GeneratorBenchmarkRails#generator 1000 repeats:
|
332
|
-
38.531868759 ( real) -> 1.062x
|
333
|
-
0.025952543
|
334
|
-
6 GeneratorBenchmarkPure#generator_safe 1000 repeats:
|
335
|
-
36.927649925 ( real) -> 1.018x 7 (>=3859)
|
336
|
-
0.027079979
|
337
|
-
7 GeneratorBenchmarkPure#generator_pretty 1000 repeats:
|
338
|
-
36.272134441 ( real) -> 1.000x 6 (>=3859)
|
339
|
-
0.027569373
|
340
|
-
calls/sec ( time) -> speed covers
|
341
|
-
secs/call
|
342
|
-
```
|
343
|
-
|
344
|
-
In the table above 1-3 are `JSON::Ext::Generator` methods. 4, 6, and 7 are
|
345
|
-
`JSON::Pure::Generator` methods and 5 is the Rails JSON generator. It is now a
|
346
|
-
bit faster than the `generator_safe` and `generator_pretty` methods of the pure
|
347
|
-
variant but slower than the others.
|
348
|
-
|
349
|
-
To achieve the fastest JSON document output, you can use the `fast_generate`
|
350
|
-
method. Beware, that this will disable the checking for circular Ruby data
|
351
|
-
structures, which may cause JSON to go into an infinite loop.
|
352
|
-
|
353
|
-
Here are the median comparisons for completeness' sake:
|
354
|
-
|
355
|
-
```
|
356
|
-
Comparing times (call_time_median):
|
357
|
-
1 GeneratorBenchmarkExt#generator_fast 1000 repeats:
|
358
|
-
708.258020939 ( real) -> 16.547x
|
359
|
-
0.001411915
|
360
|
-
2 GeneratorBenchmarkExt#generator_safe 1000 repeats:
|
361
|
-
569.105020353 ( real) -> 13.296x
|
362
|
-
0.001757145
|
363
|
-
3 GeneratorBenchmarkExt#generator_pretty 900 repeats:
|
364
|
-
482.825371244 ( real) -> 11.280x
|
365
|
-
0.002071142
|
366
|
-
4 GeneratorBenchmarkPure#generator_fast 1000 repeats:
|
367
|
-
62.717626652 ( real) -> 1.465x
|
368
|
-
0.015944481
|
369
|
-
5 GeneratorBenchmarkRails#generator 1000 repeats:
|
370
|
-
43.965681162 ( real) -> 1.027x
|
371
|
-
0.022745013
|
372
|
-
6 GeneratorBenchmarkPure#generator_safe 1000 repeats:
|
373
|
-
43.929073409 ( real) -> 1.026x 7 (>=3859)
|
374
|
-
0.022763968
|
375
|
-
7 GeneratorBenchmarkPure#generator_pretty 1000 repeats:
|
376
|
-
42.802514491 ( real) -> 1.000x 6 (>=3859)
|
377
|
-
0.023363113
|
378
|
-
calls/sec ( time) -> speed covers
|
379
|
-
secs/call
|
380
|
-
```
|
381
|
-
|
382
234
|
## Development
|
383
235
|
|
384
236
|
### Release
|
385
237
|
|
386
|
-
Update the json.
|
238
|
+
Update the `lib/json/version.rb` file.
|
387
239
|
|
388
240
|
```
|
389
241
|
rbenv shell 2.6.5
|