json 2.7.2 → 2.12.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/BSDL +22 -0
- data/CHANGES.md +160 -17
- data/LEGAL +8 -0
- data/README.md +76 -211
- data/ext/json/ext/fbuffer/fbuffer.h +178 -95
- data/ext/json/ext/generator/extconf.rb +38 -2
- data/ext/json/ext/generator/generator.c +1311 -826
- data/ext/json/ext/generator/simd.h +112 -0
- data/ext/json/ext/parser/extconf.rb +6 -27
- data/ext/json/ext/parser/parser.c +1176 -1971
- data/ext/json/ext/vendor/fpconv.c +479 -0
- data/ext/json/ext/vendor/jeaiii-ltoa.h +267 -0
- 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 +647 -241
- data/lib/json/ext/generator/state.rb +106 -0
- data/lib/json/ext.rb +35 -5
- 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 +18 -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: b76bc749fd1bc82cd84df8c7d14317305d5426e0962d0e034864b218e952e474
|
4
|
+
data.tar.gz: 8e8d9179b6ca7ce69c7a281691973dc9cb1e6b147a5812b96d02c3b41d8f8cec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a241873428f8de2106604f3a31484aa243b6f62c6c6f38f8e381669f937b19f03b35add48ddf4f1d24c5a9f469c2dbdc8bafca36d8f3bcf678313ad8d9ea11d3
|
7
|
+
data.tar.gz: 99d07dbf3dcacda3cf662ee05bc181aa3ca6e43ce8779b9f5edc454fba8e718abe876eb81b4918e0c0bb1a3b4286e1d85df086425e552743420942a8e0098f0e
|
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,149 @@
|
|
1
1
|
# Changes
|
2
2
|
|
3
|
+
### Unreleased
|
4
|
+
|
5
|
+
### 2025-05-23 (2.12.2)
|
6
|
+
|
7
|
+
* Fix compiler optimization level.
|
8
|
+
|
9
|
+
### 2025-05-23 (2.12.1)
|
10
|
+
|
11
|
+
* Fix a potential crash in large negative floating point number generation.
|
12
|
+
* Fix for JSON.pretty_generate to use passed state object's generate instead of state class as the required parameters aren't available.
|
13
|
+
|
14
|
+
### 2025-05-12 (2.12.0)
|
15
|
+
|
16
|
+
* Improve floating point generation to not use scientific notation as much.
|
17
|
+
* Include line and column in parser errors. Both in the message and as exception attributes.
|
18
|
+
* Handle non-string hash keys with broken `to_s` implementations.
|
19
|
+
* `JSON.generate` now uses SSE2 (x86) or NEON (arm64) instructions when available to escape strings.
|
20
|
+
|
21
|
+
### 2025-04-25 (2.11.3)
|
22
|
+
|
23
|
+
* Fix a regression in `JSON.pretty_generate` that could cause indentation to be off once some `#to_json` has been called.
|
24
|
+
|
25
|
+
### 2025-04-24 (2.11.2)
|
26
|
+
|
27
|
+
* Add back `JSON::PRETTY_STATE_PROTOTYPE`. This constant was private API but is used by popular gems like `multi_json`.
|
28
|
+
It now emits a deprecation warning.
|
29
|
+
|
30
|
+
### 2025-04-24 (2.11.1)
|
31
|
+
|
32
|
+
* Add back `JSON.restore`, `JSON.unparse`, `JSON.fast_unparse` and `JSON.pretty_unparse`.
|
33
|
+
These were deprecated 16 years ago, but never emited warnings, only undocumented, so are
|
34
|
+
still used by a few gems.
|
35
|
+
|
36
|
+
### 2025-04-24 (2.11.0)
|
37
|
+
|
38
|
+
* Optimize Integer generation to be ~1.8x faster.
|
39
|
+
* Optimize Float generation to be ~10x faster.
|
40
|
+
* Fix `JSON.load` proc argument to substitute the parsed object with the return value.
|
41
|
+
This better match `Marshal.load` behavior.
|
42
|
+
* Deprecate `JSON.fast_generate` (it's not any faster, so pointless).
|
43
|
+
* Deprecate `JSON.load_default_options`.
|
44
|
+
* Deprecate `JSON.unsafe_load_default_options`.
|
45
|
+
* Deprecate `JSON.dump_default_options`.
|
46
|
+
* Deprecate `Kernel#j`
|
47
|
+
* Deprecate `Kernel#jj`
|
48
|
+
* Remove outdated `JSON.iconv`.
|
49
|
+
* Remove `Class#json_creatable?` monkey patch.
|
50
|
+
* Remove deprecated `JSON.restore` method.
|
51
|
+
* Remove deprecated `JSON.unparse` method.
|
52
|
+
* Remove deprecated `JSON.fast_unparse` method.
|
53
|
+
* Remove deprecated `JSON.pretty_unparse` method.
|
54
|
+
* Remove deprecated `JSON::UnparserError` constant.
|
55
|
+
* Remove outdated `JSON::MissingUnicodeSupport` constant.
|
56
|
+
|
57
|
+
### 2025-03-12 (2.10.2)
|
58
|
+
|
59
|
+
* Fix a potential crash in the C extension parser.
|
60
|
+
* Raise a ParserError on all incomplete unicode escape sequence. This was the behavior until `2.10.0` unadvertently changed it.
|
61
|
+
* Ensure document snippets that are included in parser errors don't include truncated multibyte characters.
|
62
|
+
* Ensure parser error snippets are valid UTF-8.
|
63
|
+
* Fix `JSON::GeneratorError#detailed_message` on Ruby < 3.2
|
64
|
+
|
65
|
+
### 2025-02-10 (2.10.1)
|
66
|
+
|
67
|
+
* Fix a compatibility issue with `MultiJson.dump(obj, pretty: true)`: `no implicit conversion of false into Proc (TypeError)`.
|
68
|
+
|
69
|
+
### 2025-02-10 (2.10.0)
|
70
|
+
|
71
|
+
* `strict: true` now accept symbols as values. Previously they'd only be accepted as hash keys.
|
72
|
+
* The C extension Parser has been entirely reimplemented from scratch.
|
73
|
+
* Introduced `JSON::Coder` as a new API allowing to customize how non native types are serialized in a non-global way.
|
74
|
+
* Introduced `JSON::Fragment` to allow assembling cached fragments in a safe way.
|
75
|
+
* The Java implementation of the generator received many optimizations.
|
76
|
+
|
77
|
+
### 2024-12-18 (2.9.1)
|
78
|
+
|
79
|
+
* Fix support for Solaris 10.
|
80
|
+
|
81
|
+
### 2024-12-03 (2.9.0)
|
82
|
+
|
83
|
+
* Fix C implementation of `script_safe` escaping to not confuse some other 3 wide characters with `\u2028` and `\u2029`.
|
84
|
+
e.g. `JSON.generate(["倩", "瀨"], script_safe: true)` would generate the wrong JSON.
|
85
|
+
* `JSON.dump(object, some_io)` now write into the IO in chunks while previously it would buffer the entire JSON before writing.
|
86
|
+
* `JSON::GeneratorError` now has a `#invalid_object` attribute, making it easier to understand why an object tree cannot be serialized.
|
87
|
+
* Numerous improvements to the JRuby extension.
|
88
|
+
|
89
|
+
### 2024-11-14 (2.8.2)
|
90
|
+
|
91
|
+
* `JSON.load_file` explictly read the file as UTF-8.
|
92
|
+
|
93
|
+
### 2024-11-06 (2.8.1)
|
94
|
+
|
95
|
+
* Fix the java packages to include the extension.
|
96
|
+
|
97
|
+
### 2024-11-06 (2.8.0)
|
98
|
+
|
99
|
+
* Emit a deprecation warning when `JSON.load` create custom types without the `create_additions` option being explictly enabled.
|
100
|
+
* Prefer to use `JSON.unsafe_load(string)` or `JSON.load(string, create_additions: true)`.
|
101
|
+
* Emit a deprecation warning when serializing valid UTF-8 strings encoded in `ASCII_8BIT` aka `BINARY`.
|
102
|
+
* Bump required Ruby version to 2.7.
|
103
|
+
* Add support for optionally parsing trailing commas, via `allow_trailing_comma: true`, which in cunjunction with the
|
104
|
+
pre-existing support for comments, make it suitable to parse `jsonc` documents.
|
105
|
+
* Many performance improvements to `JSON.parse` and `JSON.load`, up to `1.7x` faster on real world documents.
|
106
|
+
* Some minor performance improvements to `JSON.dump` and `JSON.generate`.
|
107
|
+
* `JSON.pretty_generate` no longer include newline inside empty object and arrays.
|
108
|
+
|
109
|
+
### 2024-11-04 (2.7.6)
|
110
|
+
|
111
|
+
* Fix a regression in JSON.generate when dealing with Hash keys that are string subclasses, call `to_json` on them.
|
112
|
+
|
113
|
+
### 2024-10-25 (2.7.5)
|
114
|
+
|
115
|
+
* Fix a memory leak when `#to_json` methods raise an exception.
|
116
|
+
* Gracefully handle formatting configs being set to `nil` instead of `""`.
|
117
|
+
* Workaround another issue caused by conflicting versions of both `json_pure` and `json` being loaded.
|
118
|
+
|
119
|
+
### 2024-10-25 (2.7.4)
|
120
|
+
|
121
|
+
* Workaround a bug in 3.4.8 and older https://github.com/rubygems/rubygems/pull/6490.
|
122
|
+
This bug would cause some gems with native extension to fail during compilation.
|
123
|
+
* Workaround different versions of `json` and `json_pure` being loaded (not officially supported).
|
124
|
+
* Make `json_pure` Ractor compatible.
|
125
|
+
|
126
|
+
### 2024-10-24 (2.7.3)
|
127
|
+
|
128
|
+
* Numerous performance optimizations in `JSON.generate` and `JSON.dump` (up to 2 times faster).
|
129
|
+
* Limit the size of ParserError exception messages, only include up to 32 bytes of the unparseable source.
|
130
|
+
* Fix json-pure's `Object#to_json` to accept non state arguments
|
131
|
+
* Fix multiline comment support in `json-pure`.
|
132
|
+
* Fix `JSON.parse` to no longer mutate the argument encoding when passed an ASCII-8BIT string.
|
133
|
+
* Fix `String#to_json` to raise on invalid encoding in `json-pure`.
|
134
|
+
* Delete code that was based on CVTUTF.
|
135
|
+
* Use the pure-Ruby generator on TruffleRuby.
|
136
|
+
* Fix `strict` mode in `json-pure` to not break on Integer.
|
137
|
+
|
138
|
+
### 2024-04-04 (2.7.2)
|
139
|
+
|
140
|
+
* Use rb_sym2str instead of SYM2ID #561
|
141
|
+
* Fix memory leak when exception is raised during JSON generation #574
|
142
|
+
* Remove references to "19" methods in JRuby #576
|
143
|
+
* Make OpenStruct support as optional by @hsbt in #565
|
144
|
+
* Autoload JSON::GenericObject to avoid require ostruct warning in Ruby 3.4 #577
|
145
|
+
* Warn to install ostruct if json couldn't load it by @hsbt #578
|
146
|
+
|
3
147
|
### 2023-12-05 (2.7.1)
|
4
148
|
|
5
149
|
* JSON.dump: handle unenclosed hashes regression #554
|
@@ -168,7 +312,7 @@
|
|
168
312
|
* Remove Rubinius exception since transcoding should be working now.
|
169
313
|
|
170
314
|
## 2013-05-13 (1.8.0)
|
171
|
-
* Fix https://github.com/
|
315
|
+
* Fix https://github.com/ruby/json/issues/162 reported by Marc-Andre
|
172
316
|
Lafortune <github_rocks@marc-andre.ca>. Thanks!
|
173
317
|
* Applied patches by Yui NARUSE <naruse@airemix.jp> to suppress warning with
|
174
318
|
-Wchar-subscripts and better validate UTF-8 strings.
|
@@ -188,7 +332,7 @@
|
|
188
332
|
JSON::GenericObject.json_creatable = true
|
189
333
|
as well.
|
190
334
|
* Remove useless assert in fbuffer implementation.
|
191
|
-
* Apply patch attached to https://github.com/
|
335
|
+
* Apply patch attached to https://github.com/ruby/json/issues#issue/155
|
192
336
|
provided by John Shahid <jvshahid@gmail.com>, Thx!
|
193
337
|
* Add license information to rubygems spec data, reported by Jordi Massaguer Pla <jmassaguerpla@suse.de>.
|
194
338
|
* Improve documentation, thx to Zachary Scott <zachary@zacharyscott.net>.
|
@@ -202,7 +346,7 @@
|
|
202
346
|
* Fix compilation of extension on older rubies.
|
203
347
|
|
204
348
|
## 2012-07-26 (1.7.4)
|
205
|
-
* Fix compilation problem on AIX, see https://github.com/
|
349
|
+
* Fix compilation problem on AIX, see https://github.com/ruby/json/issues/142
|
206
350
|
|
207
351
|
## 2012-05-12 (1.7.3)
|
208
352
|
* Work around Rubinius encoding issues using iconv for conversion instead.
|
@@ -224,9 +368,9 @@
|
|
224
368
|
* Propagate src encoding to values made from it (fixes 1.9 mode converting
|
225
369
|
everything to ascii-8bit; harmless for 1.8 mode too) (Thomas E. Enebo
|
226
370
|
<tom.enebo@gmail.com>), should fix
|
227
|
-
https://github.com/
|
228
|
-
* Fix https://github.com/
|
229
|
-
* Fix https://github.com/
|
371
|
+
https://github.com/ruby/json/issues#issue/119.
|
372
|
+
* Fix https://github.com/ruby/json/issues#issue/124 Thx to Jason Hutchens.
|
373
|
+
* Fix https://github.com/ruby/json/issues#issue/117
|
230
374
|
|
231
375
|
## 2012-01-15 (1.6.5)
|
232
376
|
* Vit Ondruch <v.ondruch@tiscali.cz> reported a bug that shows up when using
|
@@ -253,7 +397,7 @@
|
|
253
397
|
patch go to Josh Partlow (jpartlow@github).
|
254
398
|
* Improve parsing speed for JSON numbers (integers and floats) in a similar way to
|
255
399
|
what Evan Phoenix <evan@phx.io> suggested in:
|
256
|
-
https://github.com/
|
400
|
+
https://github.com/ruby/json/pull/103
|
257
401
|
|
258
402
|
## 2011-09-18 (1.6.1)
|
259
403
|
* Using -target 1.5 to force Java bits to compile with 1.5.
|
@@ -266,15 +410,14 @@
|
|
266
410
|
* Fix memory leak when used from multiple JRuby. (Patch by
|
267
411
|
jfirebaugh@github).
|
268
412
|
* Apply patch by Eric Wong <nocode@yhbt.net> that fixes garbage collection problem
|
269
|
-
reported in https://github.com/
|
413
|
+
reported in https://github.com/ruby/json/issues/46.
|
270
414
|
* Add :quirks_mode option to parser and generator.
|
271
415
|
* Add support for Rational and Complex number additions via json/add/complex
|
272
416
|
and json/add/rational requires.
|
273
417
|
|
274
418
|
## 2011-06-20 (1.5.3)
|
275
419
|
* Alias State#configure method as State#merge to increase duck type synonymy with Hash.
|
276
|
-
|
277
|
-
the new way.
|
420
|
+
* Add `as_json` methods in json/add/core, so rails can create its json objects the new way.
|
278
421
|
|
279
422
|
## 2011-05-11 (1.5.2)
|
280
423
|
* Apply documentation patch by Cory Monty <cory.monty@gmail.com>.
|
@@ -285,7 +428,7 @@
|
|
285
428
|
|
286
429
|
## 2011-01-24 (1.5.1)
|
287
430
|
* Made rake-compiler build a fat binary gem. This should fix issue
|
288
|
-
https://github.com/
|
431
|
+
https://github.com/ruby/json/issues#issue/54.
|
289
432
|
|
290
433
|
## 2011-01-22 (1.5.0)
|
291
434
|
* Included Java source codes for the Jruby extension made by Daniel Luz
|
@@ -295,7 +438,7 @@
|
|
295
438
|
reported by Riley Goodside.
|
296
439
|
|
297
440
|
## 2010-08-09 (1.4.6)
|
298
|
-
* Fixed oversight reported in http://github.com/
|
441
|
+
* Fixed oversight reported in http://github.com/ruby/json/issues/closed#issue/23,
|
299
442
|
always create a new object from the state prototype.
|
300
443
|
* Made pure and ext api more similar again.
|
301
444
|
|
@@ -305,12 +448,12 @@
|
|
305
448
|
argument.
|
306
449
|
* Some fixes in the state objects and additional tests.
|
307
450
|
## 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/
|
451
|
+
* Fixes build problem for rubinius under OS X, http://github.com/ruby/json/issues/closed#issue/25
|
452
|
+
* Fixes crashes described in http://github.com/ruby/json/issues/closed#issue/21 and
|
453
|
+
http://github.com/ruby/json/issues/closed#issue/23
|
311
454
|
## 2010-05-05 (1.4.3)
|
312
455
|
* Fixed some test assertions, from Ruby r27587 and r27590, patch by nobu.
|
313
|
-
* Fixed issue http://github.com/
|
456
|
+
* Fixed issue http://github.com/ruby/json/issues/#issue/20 reported by
|
314
457
|
electronicwhisper@github. Thx!
|
315
458
|
|
316
459
|
## 2010-04-26 (1.4.2)
|
@@ -332,7 +475,7 @@
|
|
332
475
|
* Extension should at least be compatible with MRI, YARV and Rubinius.
|
333
476
|
|
334
477
|
## 2010-04-07 (1.2.4)
|
335
|
-
*
|
478
|
+
* Trigger const_missing callback to make Rails' dynamic class loading work.
|
336
479
|
|
337
480
|
## 2010-03-11 (1.2.3)
|
338
481
|
* 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
|
24
|
+
Install the gem and add to the application's Gemfile by executing:
|
56
25
|
|
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:
|
26
|
+
$ bundle add json
|
65
27
|
|
66
|
-
|
67
|
-
# gem install json_pure
|
68
|
-
```
|
28
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
69
29
|
|
70
|
-
|
30
|
+
$ gem install json
|
71
31
|
|
72
|
-
|
73
|
-
graphviz images of the state machines, you need [Ragel].
|
74
|
-
|
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,24 @@ 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
|
-
|
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:
|
234
|
+
## Development
|
316
235
|
|
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
|
-
```
|
236
|
+
### Prerequisites
|
343
237
|
|
344
|
-
|
345
|
-
|
346
|
-
bit faster than the `generator_safe` and `generator_pretty` methods of the pure
|
347
|
-
variant but slower than the others.
|
238
|
+
1. Clone the repository
|
239
|
+
2. Install dependencies with `bundle install`
|
348
240
|
|
349
|
-
|
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.
|
241
|
+
### Testing
|
352
242
|
|
353
|
-
|
243
|
+
The full test suite can be run with:
|
354
244
|
|
245
|
+
```bash
|
246
|
+
bundle exec rake test
|
355
247
|
```
|
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
|
-
## Development
|
383
248
|
|
384
249
|
### Release
|
385
250
|
|
386
|
-
Update the json.
|
251
|
+
Update the `lib/json/version.rb` file.
|
387
252
|
|
388
253
|
```
|
389
254
|
rbenv shell 2.6.5
|