json 2.13.2 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGES.md +208 -8
- data/LEGAL +12 -0
- data/README.md +34 -87
- data/ext/json/ext/fbuffer/fbuffer.h +75 -86
- data/ext/json/ext/generator/extconf.rb +4 -1
- data/ext/json/ext/generator/generator.c +494 -596
- data/ext/json/ext/json.h +196 -0
- data/ext/json/ext/parser/extconf.rb +29 -1
- data/ext/json/ext/parser/parser.c +2091 -723
- data/ext/json/ext/simd/simd.h +42 -22
- data/ext/json/ext/vendor/fast_float_parser.h +814 -0
- data/ext/json/ext/vendor/fpconv.c +13 -12
- data/lib/json/common.rb +199 -389
- data/lib/json/ext/generator/state.rb +8 -37
- data/lib/json/ext.rb +25 -1
- data/lib/json/truffle_ruby/generator.rb +186 -128
- data/lib/json/version.rb +1 -1
- data/lib/json.rb +84 -252
- metadata +5 -18
- data/lib/json/add/bigdecimal.rb +0 -58
- data/lib/json/add/complex.rb +0 -51
- data/lib/json/add/core.rb +0 -12
- data/lib/json/add/date.rb +0 -54
- data/lib/json/add/date_time.rb +0 -67
- data/lib/json/add/exception.rb +0 -49
- data/lib/json/add/ostruct.rb +0 -54
- data/lib/json/add/range.rb +0 -54
- data/lib/json/add/rational.rb +0 -49
- data/lib/json/add/regexp.rb +0 -48
- data/lib/json/add/set.rb +0 -48
- data/lib/json/add/struct.rb +0 -52
- data/lib/json/add/symbol.rb +0 -52
- data/lib/json/add/time.rb +0 -52
- data/lib/json/generic_object.rb +0 -75
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b4a457db7bd0eba155736808c796e81741e7ea72438768e183a7b7a795e8de4e
|
|
4
|
+
data.tar.gz: 61bb013e78224c5c91f620d3097986735411befd5139f33eaecb2c3c510ababe
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6e0b4b4a8160235dcef28c7924ecda754a7ae792a714e6b5fbc5baa810fd358129c4340dd1056d31df823bf139f2b42bccb9c2d400c7282079d3dcf3b2f8c2c8
|
|
7
|
+
data.tar.gz: e67782447e1934dcada56b848982e6bb15708997b9eca220f7b631470734e0b14b2b227ac82612b03b8eb6b3d54b7686d4627a6d551d05b8252da40203f07087
|
data/CHANGES.md
CHANGED
|
@@ -2,6 +2,205 @@
|
|
|
2
2
|
|
|
3
3
|
### Unreleased
|
|
4
4
|
|
|
5
|
+
### 2026-09-07 (3.0.0)
|
|
6
|
+
|
|
7
|
+
* Add `JSON::ParserError#json_path` to locate parse errors in the document as a JSONPath-style string (e.g. `$.foo[0].bar`). For duplicate key errors it points at the duplicated key itself.
|
|
8
|
+
* Fix the parser to also reject lone trailing UTF-16 surrogates (`\uDCxx` with no leading partner), symmetric to the leading-surrogate case. The Java parser already rejected these; this closes the CRuby/JRuby parity gap.
|
|
9
|
+
|
|
10
|
+
### 2026-08-11 (3.0.0.rc1)
|
|
11
|
+
|
|
12
|
+
With the removal of the insecure `create_additions` option, `JSON.load` and `JSON.dump` are
|
|
13
|
+
now safe to use. Them being unsafe by default caused multiple security vulnerabilites in the past.
|
|
14
|
+
|
|
15
|
+
If you did depend on `create_additions`, the recommended migration is to [implement a custom serializer using
|
|
16
|
+
`JSON::Coder`](https://byroot.github.io/ruby/json/2025/08/02/whats-wrong-with-the-json-gem-api.html#the-create_additions-option).
|
|
17
|
+
|
|
18
|
+
All the mutable default options, such as `JSON.load_default_options` have been removed.
|
|
19
|
+
They were preventing Ractor compatiblity, and causing bug in libraries using JSON expecting the default behavior.
|
|
20
|
+
`JSON` methods now always behave the same unless monkey patched.
|
|
21
|
+
|
|
22
|
+
All methods options are now either keyword arguments or checked like keyword arguments, meaning
|
|
23
|
+
unknown options such as typos raise `ArgumentError`.
|
|
24
|
+
|
|
25
|
+
Duplicated keys are now rejected by default.
|
|
26
|
+
|
|
27
|
+
JavaScript comments in documents are no longer supported by default.
|
|
28
|
+
|
|
29
|
+
Numerous rarely used aliases have been removed.
|
|
30
|
+
|
|
31
|
+
* `JSON.load` defaults are now safe to use.
|
|
32
|
+
* All unknown options will now cause an `ArgumentError` rather than to be ignored.
|
|
33
|
+
* The `allow_comments` parsing option now defaults to `false`.
|
|
34
|
+
* The `allow_duplicate_key` option now defaults to `false`, for both parsing and generating JSON.
|
|
35
|
+
* Removed the `limit` positional argument of `JSON.dump`.
|
|
36
|
+
* Removed the `escape_slash` alias of `script_safe`.
|
|
37
|
+
* Removed `Kernel#j` and `Kernel#jj`.
|
|
38
|
+
* Removed `JSON.load_default_options`.
|
|
39
|
+
* Removed `JSON.unsafe_load_default_options`.
|
|
40
|
+
* Removed `JSON.dump_default_options`.
|
|
41
|
+
* Removed `JSON::State#[]` and `JSON::State#[]=`.
|
|
42
|
+
* Removed `JSON.unparse`.
|
|
43
|
+
* Removed `JSON.fast_generate`.
|
|
44
|
+
* Removed `JSON.fast_unparse`.
|
|
45
|
+
* Removed `JSON.pretty_unparse`.
|
|
46
|
+
* Removed `JSON.restore`.
|
|
47
|
+
* Removed `JSON::PRETTY_STATE_PROTOTYPE`.
|
|
48
|
+
* Removed the insecure `create_additions` option.
|
|
49
|
+
* Removed `JSON::GenericObject`.
|
|
50
|
+
|
|
51
|
+
### 2026-07-31 (2.21.2)
|
|
52
|
+
|
|
53
|
+
* Fix a use-after-free bug in `JSON::ResumableParser`. [GHSA-9hj4-r449-hfvc][CVE-2026-71847].
|
|
54
|
+
|
|
55
|
+
### 2026-07-13 (2.21.1)
|
|
56
|
+
|
|
57
|
+
* Fix a compilation issue on Window and Microsoft Visual C++.
|
|
58
|
+
|
|
59
|
+
### 2026-07-12 (2.21.0)
|
|
60
|
+
|
|
61
|
+
* `JSON.generate` now accept a `sort_keys` option, which takes either a boolean or a block.
|
|
62
|
+
* Added `#empty?` and `#partial_value?` methods on `JSON::ResumableParser`.
|
|
63
|
+
* Numerous correctness and performance fixes for `JSON::ResumableParser`.
|
|
64
|
+
* Avoid triggering Ruby's `float out of range` warning when parsing out of range numbers.
|
|
65
|
+
* Declare C types with Ruby 4.1 `RUBY_TYPED_THREAD_SAFE_FREE`.
|
|
66
|
+
|
|
67
|
+
### 2026-06-23 (2.20.0)
|
|
68
|
+
|
|
69
|
+
* Both C and Java parsers are no longer recursive, so parsing very deep documents with `max_nesting: false` will no longer
|
|
70
|
+
result in `SystemStackError stack level too deep` errors.
|
|
71
|
+
* The `:max_nesting` option still defaults to `100`.
|
|
72
|
+
* Optimized floating point number parsing further by replacing the ryu algorithm by a port of Eisel-Lemire Fast Float.
|
|
73
|
+
* Added `JSON::ResumableParser` to parse streams of JSON documents. Not yet available on JRuby.
|
|
74
|
+
* Deprecate default support of JavaScript comments in the parser and add `allow_comments: true` parsing option.
|
|
75
|
+
* Integrate with Ruby 4.1 `ruby_sized_xfree`.
|
|
76
|
+
|
|
77
|
+
### 2026-06-11 (2.19.9)
|
|
78
|
+
|
|
79
|
+
* Fix buffer overflow that could lead to a crash when writing JSON directly into an IO
|
|
80
|
+
with `JSON.generate(object, io)`. [CVE-2026-54696].
|
|
81
|
+
|
|
82
|
+
### 2026-06-03 (2.19.8)
|
|
83
|
+
|
|
84
|
+
* Fix 1-byte buffer overread on EOS errors.
|
|
85
|
+
* Handle invalid types passed as `max_nesting` option.
|
|
86
|
+
|
|
87
|
+
### 2026-05-28 (2.19.7)
|
|
88
|
+
|
|
89
|
+
* Fix some more edge cases with out of range floats.
|
|
90
|
+
* Ensure the string provided to `JSON.parse` can't be mutated during parsing.
|
|
91
|
+
* Add missing write barriers in `State#dup`.
|
|
92
|
+
* Further validate generator `depth` config.
|
|
93
|
+
|
|
94
|
+
### 2026-05-28 (2.19.6)
|
|
95
|
+
|
|
96
|
+
* Cleanly handle overly large `depth` generator argument.
|
|
97
|
+
* Add missing write barrier in `ParserConfig`.
|
|
98
|
+
|
|
99
|
+
### 2026-05-04 (2.19.5)
|
|
100
|
+
|
|
101
|
+
* Cap the parser to emit a maximum of 5 deprecation warnings per document. Emitting more is not helpful.
|
|
102
|
+
|
|
103
|
+
### 2026-04-19 (2.19.4)
|
|
104
|
+
|
|
105
|
+
* Fix parsing of out of range floats (very large exponents that lead to either `0.0` or `Inf`).
|
|
106
|
+
|
|
107
|
+
### 2026-03-25 (2.19.3)
|
|
108
|
+
|
|
109
|
+
* Fix handling of unescaped control characters preceeded by a backslash.
|
|
110
|
+
|
|
111
|
+
### 2026-03-18 (2.19.2)
|
|
112
|
+
|
|
113
|
+
* Fix a format string injection vulnerability in `JSON.parse(doc, allow_duplicate_key: false)`. `CVE-2026-33210`.
|
|
114
|
+
|
|
115
|
+
### 2026-03-08 (2.19.1)
|
|
116
|
+
|
|
117
|
+
* Fix a compiler dependent GC bug introduced in `2.18.0`.
|
|
118
|
+
|
|
119
|
+
### 2026-03-06 (2.19.0)
|
|
120
|
+
|
|
121
|
+
* Fix `allow_blank` parsing option to no longer allow invalid types (e.g. `load([], allow_blank: true)` now raise a type error).
|
|
122
|
+
* Add `allow_invalid_escape` parsing option to ignore backslashes that aren't followed by one of the valid escape characters.
|
|
123
|
+
|
|
124
|
+
### 2026-02-03 (2.18.1)
|
|
125
|
+
|
|
126
|
+
* Fix a potential crash in very specific circumstance if GC triggers during a call to `to_json`
|
|
127
|
+
without first invoking a user defined `#to_json` method.
|
|
128
|
+
|
|
129
|
+
### 2025-12-11 (2.18.0)
|
|
130
|
+
|
|
131
|
+
* Add `:allow_control_characters` parser options, to allow JSON strings containing unescaped ASCII control characters (e.g. newlines).
|
|
132
|
+
|
|
133
|
+
### 2026-03-18 (2.17.1.2) - Security Backport
|
|
134
|
+
|
|
135
|
+
* Fix a format string injection vulnerability in `JSON.parse(doc, allow_duplicate_key: false)`. `CVE-2026-33210`.
|
|
136
|
+
|
|
137
|
+
### 2025-12-04 (2.17.1)
|
|
138
|
+
|
|
139
|
+
* Fix a regression in parsing of unicode surogate pairs (`\uXX\uXX`) that could cause an invalid string to be returned.
|
|
140
|
+
|
|
141
|
+
### 2025-12-03 (2.17.0)
|
|
142
|
+
|
|
143
|
+
* Improve `JSON.load` and `JSON.unsafe_load` to allow passing options as second argument.
|
|
144
|
+
* Fix the parser to no longer ignore invalid escapes in strings.
|
|
145
|
+
Only `\"`, `\\`, `\b`, `\f`, `\n`, `\r`, `\t` and `\u` are valid JSON escapes.
|
|
146
|
+
* Fixed `JSON::Coder` to use the depth it was initialized with.
|
|
147
|
+
* On TruffleRuby, fix the generator to not call `to_json` on the return value of `as_json` for `Float::NAN`.
|
|
148
|
+
* Fixed handling of `state.depth`: when `to_json` changes `state.depth` but does not restore it, it is reset
|
|
149
|
+
automatically to its initial value.
|
|
150
|
+
In particular, when a `NestingError` is raised, `depth` is no longer equal to `max_nesting` after the call to
|
|
151
|
+
generate, and is reset to its initial value. Similarly when `to_json` raises an exception.
|
|
152
|
+
|
|
153
|
+
### 2025-11-07 (2.16.0)
|
|
154
|
+
|
|
155
|
+
* Deprecate `JSON::State#[]` and `JSON::State#[]=`. Consider using `JSON::Coder` instead.
|
|
156
|
+
* `JSON::Coder` now also yields to the block when encountering strings with invalid encoding.
|
|
157
|
+
* Fix GeneratorError messages to be UTF-8 encoded.
|
|
158
|
+
* Fix memory leak when `Exception` is raised, or `throw` is used during JSON generation.
|
|
159
|
+
* Optimized floating point number parsing by integrating the ryu algorithm (thanks to Josef Šimánek).
|
|
160
|
+
* Optimized numbers parsing using SWAR (thanks to Scott Myron).
|
|
161
|
+
* Optimized parsing of pretty printed documents using SWAR (thanks to Scott Myron).
|
|
162
|
+
|
|
163
|
+
### 2026-03-18 (2.15.2.1) - Security Backport
|
|
164
|
+
|
|
165
|
+
* Fix a format string injection vulnerability in `JSON.parse(doc, allow_duplicate_key: false)`. `CVE-2026-33210`.
|
|
166
|
+
|
|
167
|
+
### 2025-10-25 (2.15.2)
|
|
168
|
+
|
|
169
|
+
* Fix `JSON::Coder` to have one dedicated depth counter per invocation.
|
|
170
|
+
After encountering a circular reference in `JSON::Coder#dump`, any further `#dump` call would raise `JSON::NestingError`.
|
|
171
|
+
|
|
172
|
+
### 2025-10-07 (2.15.1)
|
|
173
|
+
|
|
174
|
+
* Fix incorrect escaping in the JRuby extension when encoding shared strings.
|
|
175
|
+
|
|
176
|
+
### 2025-09-22 (2.15.0)
|
|
177
|
+
|
|
178
|
+
* `JSON::Coder` callback now receive a second argument to convey whether the object is a hash key.
|
|
179
|
+
* Tuned the floating point number generator to not use scientific notation as aggressively.
|
|
180
|
+
|
|
181
|
+
### 2025-09-18 (2.14.1)
|
|
182
|
+
|
|
183
|
+
* Fix `IndexOutOfBoundsException` in the JRuby extension when encoding shared strings.
|
|
184
|
+
|
|
185
|
+
### 2025-09-18 (2.14.0)
|
|
186
|
+
|
|
187
|
+
* Add new `allow_duplicate_key` generator options. By default a warning is now emitted when a duplicated key is encountered.
|
|
188
|
+
In `json 3.0` an error will be raised.
|
|
189
|
+
```ruby
|
|
190
|
+
>> Warning[:deprecated] = true
|
|
191
|
+
>> puts JSON.generate({ foo: 1, "foo" => 2 })
|
|
192
|
+
(irb):2: warning: detected duplicate key "foo" in {foo: 1, "foo" => 2}.
|
|
193
|
+
This will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`
|
|
194
|
+
{"foo":1,"foo":2}
|
|
195
|
+
>> JSON.generate({ foo: 1, "foo" => 2 }, allow_duplicate_key: false)
|
|
196
|
+
detected duplicate key "foo" in {foo: 1, "foo" => 2} (JSON::GeneratorError)
|
|
197
|
+
```
|
|
198
|
+
* Fix `JSON.generate` `strict: true` mode to also restrict hash keys.
|
|
199
|
+
* Fix `JSON::Coder` to also invoke block for hash keys that aren't strings nor symbols.
|
|
200
|
+
* Fix `JSON.unsafe_load` usage with proc
|
|
201
|
+
* Fix the parser to more consistently reject invalid UTF-16 surogate pairs.
|
|
202
|
+
* Stop defining `String.json_create`, `String#to_json_raw`, `String#to_json_raw_object` when `json/add` isn't loaded.
|
|
203
|
+
|
|
5
204
|
### 2025-07-28 (2.13.2)
|
|
6
205
|
|
|
7
206
|
* Improve duplicate key warning and errors to include the key name and point to the right caller.
|
|
@@ -44,7 +243,7 @@
|
|
|
44
243
|
### 2025-04-24 (2.11.1)
|
|
45
244
|
|
|
46
245
|
* Add back `JSON.restore`, `JSON.unparse`, `JSON.fast_unparse` and `JSON.pretty_unparse`.
|
|
47
|
-
These were deprecated 16 years ago, but never
|
|
246
|
+
These were deprecated 16 years ago, but never emitted warnings, only undocumented, so are
|
|
48
247
|
still used by a few gems.
|
|
49
248
|
|
|
50
249
|
### 2025-04-24 (2.11.0)
|
|
@@ -71,7 +270,7 @@
|
|
|
71
270
|
### 2025-03-12 (2.10.2)
|
|
72
271
|
|
|
73
272
|
* Fix a potential crash in the C extension parser.
|
|
74
|
-
* Raise a ParserError on all incomplete unicode escape sequence. This was the behavior until `2.10.0`
|
|
273
|
+
* Raise a ParserError on all incomplete unicode escape sequence. This was the behavior until `2.10.0` inadvertently changed it.
|
|
75
274
|
* Ensure document snippets that are included in parser errors don't include truncated multibyte characters.
|
|
76
275
|
* Ensure parser error snippets are valid UTF-8.
|
|
77
276
|
* Fix `JSON::GeneratorError#detailed_message` on Ruby < 3.2
|
|
@@ -102,7 +301,7 @@
|
|
|
102
301
|
|
|
103
302
|
### 2024-11-14 (2.8.2)
|
|
104
303
|
|
|
105
|
-
* `JSON.load_file`
|
|
304
|
+
* `JSON.load_file` explicitly read the file as UTF-8.
|
|
106
305
|
|
|
107
306
|
### 2024-11-06 (2.8.1)
|
|
108
307
|
|
|
@@ -110,7 +309,7 @@
|
|
|
110
309
|
|
|
111
310
|
### 2024-11-06 (2.8.0)
|
|
112
311
|
|
|
113
|
-
* Emit a deprecation warning when `JSON.load` create custom types without the `create_additions` option being
|
|
312
|
+
* Emit a deprecation warning when `JSON.load` create custom types without the `create_additions` option being explicitly enabled.
|
|
114
313
|
* Prefer to use `JSON.unsafe_load(string)` or `JSON.load(string, create_additions: true)`.
|
|
115
314
|
* Emit a deprecation warning when serializing valid UTF-8 strings encoded in `ASCII_8BIT` aka `BINARY`.
|
|
116
315
|
* Bump required Ruby version to 2.7.
|
|
@@ -118,7 +317,7 @@
|
|
|
118
317
|
pre-existing support for comments, make it suitable to parse `jsonc` documents.
|
|
119
318
|
* Many performance improvements to `JSON.parse` and `JSON.load`, up to `1.7x` faster on real world documents.
|
|
120
319
|
* Some minor performance improvements to `JSON.dump` and `JSON.generate`.
|
|
121
|
-
* `JSON.pretty_generate` no longer
|
|
320
|
+
* `JSON.pretty_generate` no longer includes newlines inside empty object and arrays.
|
|
122
321
|
|
|
123
322
|
### 2024-11-04 (2.7.6)
|
|
124
323
|
|
|
@@ -135,13 +334,13 @@
|
|
|
135
334
|
* Workaround a bug in 3.4.8 and older https://github.com/rubygems/rubygems/pull/6490.
|
|
136
335
|
This bug would cause some gems with native extension to fail during compilation.
|
|
137
336
|
* Workaround different versions of `json` and `json_pure` being loaded (not officially supported).
|
|
138
|
-
* Make `json_pure` Ractor compatible.
|
|
337
|
+
* Make `json_pure` Ractor compatible.
|
|
139
338
|
|
|
140
339
|
### 2024-10-24 (2.7.3)
|
|
141
340
|
|
|
142
341
|
* Numerous performance optimizations in `JSON.generate` and `JSON.dump` (up to 2 times faster).
|
|
143
|
-
* Limit the size of ParserError exception messages, only include up to 32 bytes of the
|
|
144
|
-
* Fix json-pure's `Object#to_json` to accept non
|
|
342
|
+
* Limit the size of ParserError exception messages, only include up to 32 bytes of the unparsable source.
|
|
343
|
+
* Fix json-pure's `Object#to_json` to accept non-state arguments.
|
|
145
344
|
* Fix multiline comment support in `json-pure`.
|
|
146
345
|
* Fix `JSON.parse` to no longer mutate the argument encoding when passed an ASCII-8BIT string.
|
|
147
346
|
* Fix `String#to_json` to raise on invalid encoding in `json-pure`.
|
|
@@ -286,6 +485,7 @@
|
|
|
286
485
|
## 2015-09-11 (2.0.0)
|
|
287
486
|
* Now complies to newest JSON RFC 7159.
|
|
288
487
|
* Implements compatibility to ruby 2.4 integer unification.
|
|
488
|
+
* Removed support for `quirks_mode` option.
|
|
289
489
|
* Drops support for old rubies whose life has ended, that is rubies < 2.0.
|
|
290
490
|
Also see https://www.ruby-lang.org/en/news/2014/07/01/eol-for-1-8-7-and-1-9-2/
|
|
291
491
|
* There were still some mentions of dual GPL licensing in the source, but JSON
|
data/LEGAL
CHANGED
|
@@ -6,3 +6,15 @@
|
|
|
6
6
|
All the files in this distribution are covered under either the Ruby's
|
|
7
7
|
license (see the file COPYING) or public-domain except some files
|
|
8
8
|
mentioned below.
|
|
9
|
+
|
|
10
|
+
ext/json/ext/vendor/fpconv.h::
|
|
11
|
+
This file is adapted from https://github.com/night-shift/fpconv
|
|
12
|
+
It is licensed under Boost Software License 1.0.
|
|
13
|
+
|
|
14
|
+
ext/json/ext/vendor/jeaiii-ltoa.h::
|
|
15
|
+
This file is adapted from https://github.com/jeaiii/itoa
|
|
16
|
+
It is licensed under the MIT License
|
|
17
|
+
|
|
18
|
+
ext/json/ext/vendor/fast_float_parser.h::
|
|
19
|
+
This file is adapted from the Fast Float C++ library by The fast_float authors https://github.com/fastfloat/fast_float
|
|
20
|
+
It is licensed under the MIT License
|
data/README.md
CHANGED
|
@@ -14,10 +14,7 @@ UTF-16 surrogate pairs in order to be able to generate the whole range of
|
|
|
14
14
|
unicode code points.
|
|
15
15
|
|
|
16
16
|
All strings, that are to be encoded as JSON strings, should be UTF-8 byte
|
|
17
|
-
sequences on the Ruby side.
|
|
18
|
-
encoded, please use the to\_json\_raw\_object method of String (which produces
|
|
19
|
-
an object, that contains a byte array) and decode the result on the receiving
|
|
20
|
-
endpoint.
|
|
17
|
+
sequences on the Ruby side.
|
|
21
18
|
|
|
22
19
|
## Installation
|
|
23
20
|
|
|
@@ -49,8 +46,7 @@ JSON.generate(data)
|
|
|
49
46
|
```
|
|
50
47
|
|
|
51
48
|
You can also use the `pretty_generate` method (which formats the output more
|
|
52
|
-
verbosely and nicely)
|
|
53
|
-
checks generate performs, e. g. nesting deepness checks).
|
|
49
|
+
verbosely and nicely).
|
|
54
50
|
|
|
55
51
|
## Casting non native types
|
|
56
52
|
|
|
@@ -85,7 +81,7 @@ Both of these behavior can be disabled using the `strict: true` option:
|
|
|
85
81
|
|
|
86
82
|
```ruby
|
|
87
83
|
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)
|
|
84
|
+
JSON.generate(Position.new(1, 2), strict: true) # => Position not allowed in JSON (JSON::GeneratorError)
|
|
89
85
|
```
|
|
90
86
|
|
|
91
87
|
## JSON::Coder
|
|
@@ -97,7 +93,7 @@ Instead it is recommended to use the newer `JSON::Coder` API:
|
|
|
97
93
|
|
|
98
94
|
```ruby
|
|
99
95
|
module MyApp
|
|
100
|
-
API_JSON_CODER = JSON::Coder.new do |object|
|
|
96
|
+
API_JSON_CODER = JSON::Coder.new do |object, is_object_key|
|
|
101
97
|
case object
|
|
102
98
|
when Time
|
|
103
99
|
object.iso8601(3)
|
|
@@ -113,96 +109,42 @@ puts MyApp::API_JSON_CODER.dump(Time.now.utc) # => "2025-01-21T08:41:44.286Z"
|
|
|
113
109
|
The provided block is called for all objects that don't have a native JSON equivalent, and
|
|
114
110
|
must return a Ruby object that has a native JSON equivalent.
|
|
115
111
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
To combine JSON fragments into a bigger JSON document, you can use `JSON::Fragment`:
|
|
119
|
-
|
|
120
|
-
```ruby
|
|
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 })
|
|
126
|
-
```
|
|
127
|
-
|
|
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.
|
|
133
|
-
|
|
134
|
-
To create a JSON document from a ruby data structure, you can call
|
|
135
|
-
`JSON.generate` like that:
|
|
136
|
-
|
|
137
|
-
```ruby
|
|
138
|
-
json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
|
139
|
-
# => "[1,2,{\"a\":3.141},false,true,null,\"4..10\"]"
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
To get back a ruby data structure from a JSON document, you have to call
|
|
143
|
-
JSON.parse on it:
|
|
144
|
-
|
|
145
|
-
```ruby
|
|
146
|
-
JSON.parse json
|
|
147
|
-
# => [1, 2, {"a"=>3.141}, false, true, nil, "4..10"]
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
Note, that the range from the original data structure is a simple
|
|
151
|
-
string now. The reason for this is, that JSON doesn't support ranges
|
|
152
|
-
or arbitrary classes. In this case the json library falls back to call
|
|
153
|
-
`Object#to_json`, which is the same as `#to_s.to_json`.
|
|
154
|
-
|
|
155
|
-
It's possible to add JSON support serialization to arbitrary classes by
|
|
156
|
-
simply implementing a more specialized version of the `#to_json method`, that
|
|
157
|
-
should return a JSON object (a hash converted to JSON with `#to_json`) like
|
|
158
|
-
this (don't forget the `*a` for all the arguments):
|
|
112
|
+
It is also called for objects that do have a JSON equivalent, but are used as Hash keys, for instance `{ 1 => 2}`,
|
|
113
|
+
as well as for strings that aren't valid UTF-8:
|
|
159
114
|
|
|
160
115
|
```ruby
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
116
|
+
coder = JSON::Coder.new do |object, is_object_key|
|
|
117
|
+
case object
|
|
118
|
+
when String
|
|
119
|
+
if !object.valid_encoding? || object.encoding != Encoding::UTF_8
|
|
120
|
+
Base64.encode64(object)
|
|
121
|
+
else
|
|
122
|
+
object
|
|
123
|
+
end
|
|
124
|
+
else
|
|
125
|
+
object
|
|
167
126
|
end
|
|
168
127
|
end
|
|
169
128
|
```
|
|
170
129
|
|
|
171
|
-
|
|
172
|
-
JSON representation later. In this case it's `Range`, but any namespace of
|
|
173
|
-
the form `A::B` or `::A::B` will do. All other keys are arbitrary and can be
|
|
174
|
-
used to store the necessary data to configure the object to be deserialised.
|
|
130
|
+
## Combining JSON fragments
|
|
175
131
|
|
|
176
|
-
|
|
177
|
-
if the given class responds to the `json_create` class method. If so, it is
|
|
178
|
-
called with the JSON object converted to a Ruby hash. So a range can
|
|
179
|
-
be deserialised by implementing `Range.json_create` like this:
|
|
132
|
+
To combine JSON fragments into a bigger JSON document, you can use `JSON::Fragment`:
|
|
180
133
|
|
|
181
134
|
```ruby
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
new(*o['data'])
|
|
185
|
-
end
|
|
135
|
+
posts_json = cache.fetch_multi(post_ids) do |post_id|
|
|
136
|
+
JSON.generate(Post.find(post_id))
|
|
186
137
|
end
|
|
138
|
+
posts_json.map! { |post_json| JSON::Fragment.new(post_json) }
|
|
139
|
+
JSON.generate({ posts: posts_json, count: posts_json.count })
|
|
187
140
|
```
|
|
188
141
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
```ruby
|
|
192
|
-
json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
|
193
|
-
# => "[1,2,{\"a\":3.141},false,true,null,{\"json_class\":\"Range\",\"data\":[4,10,false]}]"
|
|
194
|
-
JSON.parse json
|
|
195
|
-
# => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
|
196
|
-
json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
|
197
|
-
# => "[1,2,{\"a\":3.141},false,true,null,{\"json_class\":\"Range\",\"data\":[4,10,false]}]"
|
|
198
|
-
JSON.unsafe_load json
|
|
199
|
-
# => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
|
200
|
-
```
|
|
142
|
+
## Pretty Printing
|
|
201
143
|
|
|
202
144
|
`JSON.generate` always creates the shortest possible string representation of a
|
|
203
145
|
ruby data structure in one line. This is good for data storage or network
|
|
204
146
|
protocols, but not so good for humans to read. Fortunately there's also
|
|
205
|
-
`JSON.pretty_generate`
|
|
147
|
+
`JSON.pretty_generate` that creates a more readable
|
|
206
148
|
output:
|
|
207
149
|
|
|
208
150
|
```ruby
|
|
@@ -227,9 +169,16 @@ output:
|
|
|
227
169
|
]
|
|
228
170
|
```
|
|
229
171
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
172
|
+
## Security
|
|
173
|
+
|
|
174
|
+
When parsing or serializing untrusted input, parser and generator options should never be user controlled.
|
|
175
|
+
|
|
176
|
+
```ruby
|
|
177
|
+
# Dangerous, DO NOT DO THIS.
|
|
178
|
+
JSON.generate(params[:data], params[:options])
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Security vulnerability reports relying on attacker controlled parsing or generator options will be handled as regular bug fixes.
|
|
233
182
|
|
|
234
183
|
## Development
|
|
235
184
|
|
|
@@ -277,5 +226,3 @@ The latest version of this library can be downloaded at
|
|
|
277
226
|
Online Documentation should be located at
|
|
278
227
|
|
|
279
228
|
* https://www.rubydoc.info/gems/json
|
|
280
|
-
|
|
281
|
-
[Ragel]: http://www.colm.net/open-source/ragel/
|