json 2.6.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 +144 -17
- data/LEGAL +8 -0
- data/README.md +67 -224
- 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 +1020 -806
- data/ext/json/ext/parser/extconf.rb +7 -27
- data/ext/json/ext/parser/parser.c +1343 -3212
- data/json.gemspec +48 -52
- data/lib/json/add/bigdecimal.rb +39 -10
- data/lib/json/add/complex.rb +29 -6
- data/lib/json/add/core.rb +1 -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/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 +418 -128
- data/lib/json/ext/generator/state.rb +106 -0
- data/lib/json/ext.rb +34 -4
- data/lib/json/generic_object.rb +7 -3
- data/lib/json/truffle_ruby/generator.rb +690 -0
- data/lib/json/version.rb +3 -7
- data/lib/json.rb +25 -21
- metadata +15 -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 -986
- 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
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,120 @@
|
|
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
|
+
|
85
|
+
### 2023-12-05 (2.7.1)
|
86
|
+
|
87
|
+
* JSON.dump: handle unenclosed hashes regression #554
|
88
|
+
* Overload kwargs in JSON.dump #556
|
89
|
+
* [DOC] RDoc for additions #557
|
90
|
+
* Fix JSON.dump overload combination #558
|
91
|
+
|
92
|
+
### 2023-12-01 (2.7.0)
|
93
|
+
|
94
|
+
* Add a strict option to Generator #519
|
95
|
+
* `escape_slash` option was renamed as `script_safe` and now also escape U+2028 and U+2029. `escape_slash` is now an alias of `script_safe` #525
|
96
|
+
* Remove unnecessary initialization of create_id in JSON.parse() #454
|
97
|
+
* Improvements to Hash#to_json in pure implementation generator #203
|
98
|
+
* Use ruby_xfree to free buffers #518
|
99
|
+
* Fix "unexpected token" offset for Infinity #507
|
100
|
+
* Avoid using deprecated BigDecimal.new on JRuby #546
|
101
|
+
* Removed code for Ruby 1.8 #540
|
102
|
+
* Rename JSON::ParseError to JSON:ParserError #530
|
103
|
+
* Call super in included hook #486
|
104
|
+
* JRuby requires a minimum of Java 8 #516
|
105
|
+
* Always indent even if empty #517
|
106
|
+
|
107
|
+
### 2022-11-30 (2.6.3)
|
108
|
+
|
109
|
+
* bugfix json/pure mixing escaped with literal unicode raises Encoding::CompatibilityError #483
|
110
|
+
* Stop including the parser source __LINE__ in exceptions #470
|
111
|
+
|
112
|
+
### 2022-11-17 (2.6.2)
|
113
|
+
|
114
|
+
* Remove unknown keyword arg from DateTime.parse #488
|
115
|
+
* Ignore java artifacts by @hsbt #489
|
116
|
+
* Fix parser bug for empty string allocation #496
|
117
|
+
|
3
118
|
### 2021-10-24 (2.6.1)
|
4
119
|
|
5
120
|
* Restore version.rb with 2.6.1
|
@@ -105,6 +220,19 @@
|
|
105
220
|
I changed these mentions to be consistent with the Ruby license setting in
|
106
221
|
the gemspec files which were already correct now.
|
107
222
|
|
223
|
+
## 2017-01-13 (1.8.6)
|
224
|
+
* Be compatible with ancient ruby 1.8 (maybe?)
|
225
|
+
|
226
|
+
## 2015-09-11 (1.8.5)
|
227
|
+
* Be compatible with ruby 2.4.0
|
228
|
+
* There were still some mentions of dual GPL licensing in the source, but JSON
|
229
|
+
has just the Ruby license that itself includes an explicit dual-licensing
|
230
|
+
clause that allows covered software to be distributed under the terms of
|
231
|
+
the Simplified BSD License instead for all ruby versions >= 1.9.3. This is
|
232
|
+
however a GPL compatible license according to the Free Software Foundation.
|
233
|
+
I changed these mentions to be consistent with the Ruby license setting in
|
234
|
+
the gemspec files which were already correct now.
|
235
|
+
|
108
236
|
## 2015-06-01 (1.8.3)
|
109
237
|
* Fix potential memory leak, thx to nobu.
|
110
238
|
|
@@ -122,7 +250,7 @@
|
|
122
250
|
* Remove Rubinius exception since transcoding should be working now.
|
123
251
|
|
124
252
|
## 2013-05-13 (1.8.0)
|
125
|
-
* Fix https://github.com/
|
253
|
+
* Fix https://github.com/ruby/json/issues/162 reported by Marc-Andre
|
126
254
|
Lafortune <github_rocks@marc-andre.ca>. Thanks!
|
127
255
|
* Applied patches by Yui NARUSE <naruse@airemix.jp> to suppress warning with
|
128
256
|
-Wchar-subscripts and better validate UTF-8 strings.
|
@@ -142,7 +270,7 @@
|
|
142
270
|
JSON::GenericObject.json_creatable = true
|
143
271
|
as well.
|
144
272
|
* Remove useless assert in fbuffer implementation.
|
145
|
-
* Apply patch attached to https://github.com/
|
273
|
+
* Apply patch attached to https://github.com/ruby/json/issues#issue/155
|
146
274
|
provided by John Shahid <jvshahid@gmail.com>, Thx!
|
147
275
|
* Add license information to rubygems spec data, reported by Jordi Massaguer Pla <jmassaguerpla@suse.de>.
|
148
276
|
* Improve documentation, thx to Zachary Scott <zachary@zacharyscott.net>.
|
@@ -156,7 +284,7 @@
|
|
156
284
|
* Fix compilation of extension on older rubies.
|
157
285
|
|
158
286
|
## 2012-07-26 (1.7.4)
|
159
|
-
* Fix compilation problem on AIX, see https://github.com/
|
287
|
+
* Fix compilation problem on AIX, see https://github.com/ruby/json/issues/142
|
160
288
|
|
161
289
|
## 2012-05-12 (1.7.3)
|
162
290
|
* Work around Rubinius encoding issues using iconv for conversion instead.
|
@@ -178,9 +306,9 @@
|
|
178
306
|
* Propagate src encoding to values made from it (fixes 1.9 mode converting
|
179
307
|
everything to ascii-8bit; harmless for 1.8 mode too) (Thomas E. Enebo
|
180
308
|
<tom.enebo@gmail.com>), should fix
|
181
|
-
https://github.com/
|
182
|
-
* Fix https://github.com/
|
183
|
-
* 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
|
184
312
|
|
185
313
|
## 2012-01-15 (1.6.5)
|
186
314
|
* Vit Ondruch <v.ondruch@tiscali.cz> reported a bug that shows up when using
|
@@ -207,7 +335,7 @@
|
|
207
335
|
patch go to Josh Partlow (jpartlow@github).
|
208
336
|
* Improve parsing speed for JSON numbers (integers and floats) in a similar way to
|
209
337
|
what Evan Phoenix <evan@phx.io> suggested in:
|
210
|
-
https://github.com/
|
338
|
+
https://github.com/ruby/json/pull/103
|
211
339
|
|
212
340
|
## 2011-09-18 (1.6.1)
|
213
341
|
* Using -target 1.5 to force Java bits to compile with 1.5.
|
@@ -220,15 +348,14 @@
|
|
220
348
|
* Fix memory leak when used from multiple JRuby. (Patch by
|
221
349
|
jfirebaugh@github).
|
222
350
|
* Apply patch by Eric Wong <nocode@yhbt.net> that fixes garbage collection problem
|
223
|
-
reported in https://github.com/
|
351
|
+
reported in https://github.com/ruby/json/issues/46.
|
224
352
|
* Add :quirks_mode option to parser and generator.
|
225
353
|
* Add support for Rational and Complex number additions via json/add/complex
|
226
354
|
and json/add/rational requires.
|
227
355
|
|
228
356
|
## 2011-06-20 (1.5.3)
|
229
357
|
* Alias State#configure method as State#merge to increase duck type synonymy with Hash.
|
230
|
-
|
231
|
-
the new way.
|
358
|
+
* Add `as_json` methods in json/add/core, so rails can create its json objects the new way.
|
232
359
|
|
233
360
|
## 2011-05-11 (1.5.2)
|
234
361
|
* Apply documentation patch by Cory Monty <cory.monty@gmail.com>.
|
@@ -239,7 +366,7 @@
|
|
239
366
|
|
240
367
|
## 2011-01-24 (1.5.1)
|
241
368
|
* Made rake-compiler build a fat binary gem. This should fix issue
|
242
|
-
https://github.com/
|
369
|
+
https://github.com/ruby/json/issues#issue/54.
|
243
370
|
|
244
371
|
## 2011-01-22 (1.5.0)
|
245
372
|
* Included Java source codes for the Jruby extension made by Daniel Luz
|
@@ -249,7 +376,7 @@
|
|
249
376
|
reported by Riley Goodside.
|
250
377
|
|
251
378
|
## 2010-08-09 (1.4.6)
|
252
|
-
* Fixed oversight reported in http://github.com/
|
379
|
+
* Fixed oversight reported in http://github.com/ruby/json/issues/closed#issue/23,
|
253
380
|
always create a new object from the state prototype.
|
254
381
|
* Made pure and ext api more similar again.
|
255
382
|
|
@@ -259,12 +386,12 @@
|
|
259
386
|
argument.
|
260
387
|
* Some fixes in the state objects and additional tests.
|
261
388
|
## 2010-08-06 (1.4.4)
|
262
|
-
* Fixes build problem for rubinius under OS X, http://github.com/
|
263
|
-
* Fixes crashes described in http://github.com/
|
264
|
-
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
|
265
392
|
## 2010-05-05 (1.4.3)
|
266
393
|
* Fixed some test assertions, from Ruby r27587 and r27590, patch by nobu.
|
267
|
-
* Fixed issue http://github.com/
|
394
|
+
* Fixed issue http://github.com/ruby/json/issues/#issue/20 reported by
|
268
395
|
electronicwhisper@github. Thx!
|
269
396
|
|
270
397
|
## 2010-04-26 (1.4.2)
|
@@ -286,7 +413,7 @@
|
|
286
413
|
* Extension should at least be compatible with MRI, YARV and Rubinius.
|
287
414
|
|
288
415
|
## 2010-04-07 (1.2.4)
|
289
|
-
*
|
416
|
+
* Trigger const_missing callback to make Rails' dynamic class loading work.
|
290
417
|
|
291
418
|
## 2010-03-11 (1.2.3)
|
292
419
|
* Added a `State#[]` method which returns an attribute's value in order to
|
data/LEGAL
ADDED
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
|
57
|
-
|
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:
|
24
|
+
Install the gem and add to the application's Gemfile by executing:
|
66
25
|
|
67
|
-
|
68
|
-
# gem install json_pure
|
69
|
-
```
|
26
|
+
$ bundle add json
|
70
27
|
|
71
|
-
|
28
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
72
29
|
|
73
|
-
|
74
|
-
graphviz images of the state machines, you need ragel from:
|
75
|
-
http://www.complang.org/ragel/
|
30
|
+
$ gem install json
|
76
31
|
|
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,84 @@ 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)) # => 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
|
-
|
149
|
-
|
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
|
150
109
|
|
151
|
-
|
152
|
-
|
110
|
+
puts MyApp::API_JSON_CODER.dump(Time.now.utc) # => "2025-01-21T08:41:44.286Z"
|
111
|
+
```
|
153
112
|
|
154
|
-
|
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.
|
155
115
|
|
156
|
-
|
157
|
-
object to JSON, it will by default only include the exception message.
|
116
|
+
## Combining JSON fragments
|
158
117
|
|
159
|
-
To
|
160
|
-
above, or specifically load the exception addition:
|
118
|
+
To combine JSON fragments into a bigger JSON document, you can use `JSON::Fragment`:
|
161
119
|
|
162
120
|
```ruby
|
163
|
-
|
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 })
|
164
126
|
```
|
165
127
|
|
166
|
-
##
|
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.
|
167
133
|
|
168
134
|
To create a JSON document from a ruby data structure, you can call
|
169
135
|
`JSON.generate` like that:
|
@@ -229,7 +195,7 @@ JSON.parse json
|
|
229
195
|
# => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
230
196
|
json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
231
197
|
# => "[1,2,{\"a\":3.141},false,true,null,{\"json_class\":\"Range\",\"data\":[4,10,false]}]"
|
232
|
-
JSON.
|
198
|
+
JSON.unsafe_load json
|
233
199
|
# => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
234
200
|
```
|
235
201
|
|
@@ -265,136 +231,11 @@ There are also the methods `Kernel#j` for generate, and `Kernel#jj` for
|
|
265
231
|
`pretty_generate` output to the console, that work analogous to Core Ruby's `p` and
|
266
232
|
the `pp` library's `pp` methods.
|
267
233
|
|
268
|
-
The script `tools/server.rb` contains a small example if you want to test, how
|
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
|
273
|
-
|
274
|
-
I have created some benchmark results (see the benchmarks/data-p4-3Ghz
|
275
|
-
subdir of the package) for the JSON-parser to estimate the speed up in the C
|
276
|
-
extension:
|
277
|
-
|
278
|
-
```
|
279
|
-
Comparing times (call_time_mean):
|
280
|
-
1 ParserBenchmarkExt#parser 900 repeats:
|
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
|
294
|
-
```
|
295
|
-
|
296
|
-
In the table above 1 is `JSON::Ext::Parser`, 2 is `YAML.load` with YAML
|
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
|
-
```
|
323
|
-
|
324
|
-
I have benchmarked the `JSON-Generator` as well. This generated a few more
|
325
|
-
values, because there are different modes that also influence the achieved
|
326
|
-
speed:
|
327
|
-
|
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
|
-
```
|
354
|
-
|
355
|
-
In the table above 1-3 are `JSON::Ext::Generator` methods. 4, 6, and 7 are
|
356
|
-
`JSON::Pure::Generator` methods and 5 is the Rails JSON generator. It is now a
|
357
|
-
bit faster than the `generator_safe` and `generator_pretty` methods of the pure
|
358
|
-
variant but slower than the others.
|
359
|
-
|
360
|
-
To achieve the fastest JSON document output, you can use the `fast_generate`
|
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.
|
363
|
-
|
364
|
-
Here are the median comparisons for completeness' sake:
|
365
|
-
|
366
|
-
```
|
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
234
|
## Development
|
394
235
|
|
395
236
|
### Release
|
396
237
|
|
397
|
-
Update the json.
|
238
|
+
Update the `lib/json/version.rb` file.
|
398
239
|
|
399
240
|
```
|
400
241
|
rbenv shell 2.6.5
|
@@ -423,3 +264,5 @@ The latest version of this library can be downloaded at
|
|
423
264
|
Online Documentation should be located at
|
424
265
|
|
425
266
|
* https://www.rubydoc.info/gems/json
|
267
|
+
|
268
|
+
[Ragel]: http://www.colm.net/open-source/ragel/
|