json 2.12.2 → 2.16.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b76bc749fd1bc82cd84df8c7d14317305d5426e0962d0e034864b218e952e474
4
- data.tar.gz: 8e8d9179b6ca7ce69c7a281691973dc9cb1e6b147a5812b96d02c3b41d8f8cec
3
+ metadata.gz: d8bd00bbc63367659eb90ee7e3f4f1394fecdf713fc724f7908c6a2c7b79bbf7
4
+ data.tar.gz: efa1d81f687277247649de777f25be01e56ad8dfac1bdd9ce61111e10231185e
5
5
  SHA512:
6
- metadata.gz: a241873428f8de2106604f3a31484aa243b6f62c6c6f38f8e381669f937b19f03b35add48ddf4f1d24c5a9f469c2dbdc8bafca36d8f3bcf678313ad8d9ea11d3
7
- data.tar.gz: 99d07dbf3dcacda3cf662ee05bc181aa3ca6e43ce8779b9f5edc454fba8e718abe876eb81b4918e0c0bb1a3b4286e1d85df086425e552743420942a8e0098f0e
6
+ metadata.gz: cb8b505c08b88115a87aa0b6ab8b7f34d6e0611af7d37277cef243131990db6cb77ca2508045104588607d54bd58b0c2e67ce0728bd72c4c32d2d77a56b9762e
7
+ data.tar.gz: 8dec0d5ba99f33e68049328f060a600da2fe0545caf47c44c2e812a8316cbe64a99152f365be8654cb4af45493de3bed97baa61a0b2a71ae71a7466d0ee11e8c
data/CHANGES.md CHANGED
@@ -2,6 +2,67 @@
2
2
 
3
3
  ### Unreleased
4
4
 
5
+ ### 2025-11-07 (2.16.0)
6
+
7
+ * Deprecate `JSON::State#[]` and `JSON::State#[]=`. Consider using `JSON::Coder` instead.
8
+ * `JSON::Coder` now also yields to the block when encountering strings with invalid encoding.
9
+ * Fix GeneratorError messages to be UTF-8 encoded.
10
+ * Fix memory leak when `Exception` is raised, or `throw` is used during JSON generation.
11
+ * Optimized floating point number parsing by integrating the ryu algorithm (thanks to Josef Šimánek).
12
+ * Optimized numbers parsing using SWAR (thanks to Scott Myron).
13
+ * Optimized parsing of pretty printed documents using SWAR (thanks to Scott Myron).
14
+
15
+ ### 2025-10-25 (2.15.2)
16
+
17
+ * Fix `JSON::Coder` to have one dedicated depth counter per invocation.
18
+ After encountering a circular reference in `JSON::Coder#dump`, any further `#dump` call would raise `JSON::NestingError`.
19
+
20
+ ### 2025-10-07 (2.15.1)
21
+
22
+ * Fix incorrect escaping in the JRuby extension when encoding shared strings.
23
+
24
+ ### 2025-09-22 (2.15.0)
25
+
26
+ * `JSON::Coder` callback now receive a second argument to convey whether the object is a hash key.
27
+ * Tuned the floating point number generator to not use scientific notation as aggressively.
28
+
29
+ ### 2025-09-18 (2.14.1)
30
+
31
+ * Fix `IndexOutOfBoundsException` in the JRuby extension when encoding shared strings.
32
+
33
+ ### 2025-09-18 (2.14.0)
34
+
35
+ * Add new `allow_duplicate_key` generator options. By default a warning is now emitted when a duplicated key is encountered.
36
+ In `json 3.0` an error will be raised.
37
+ ```ruby
38
+ >> Warning[:deprecated] = true
39
+ >> puts JSON.generate({ foo: 1, "foo" => 2 })
40
+ (irb):2: warning: detected duplicate key "foo" in {foo: 1, "foo" => 2}.
41
+ This will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`
42
+ {"foo":1,"foo":2}
43
+ >> JSON.generate({ foo: 1, "foo" => 2 }, allow_duplicate_key: false)
44
+ detected duplicate key "foo" in {foo: 1, "foo" => 2} (JSON::GeneratorError)
45
+ ```
46
+ * Fix `JSON.generate` `strict: true` mode to also restrict hash keys.
47
+ * Fix `JSON::Coder` to also invoke block for hash keys that aren't strings nor symbols.
48
+ * Fix `JSON.unsafe_load` usage with proc
49
+ * Fix the parser to more consistently reject invalid UTF-16 surogate pairs.
50
+ * Stop defining `String.json_create`, `String#to_json_raw`, `String#to_json_raw_object` when `json/add` isn't loaded.
51
+
52
+ ### 2025-07-28 (2.13.2)
53
+
54
+ * Improve duplicate key warning and errors to include the key name and point to the right caller.
55
+
56
+ ### 2025-07-24 (2.13.1)
57
+
58
+ * Fix support for older compilers without `__builtin_cpu_supports`.
59
+
60
+ ### 2025-07-17 (2.13.0)
61
+
62
+ * Add new `allow_duplicate_key` parsing options. By default a warning is now emitted when a duplicated key is encountered.
63
+ In `json 3.0` an error will be raised.
64
+ * Optimize parsing further using SIMD to scan strings.
65
+
5
66
  ### 2025-05-23 (2.12.2)
6
67
 
7
68
  * Fix compiler optimization level.
@@ -30,7 +91,7 @@
30
91
  ### 2025-04-24 (2.11.1)
31
92
 
32
93
  * 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
94
+ These were deprecated 16 years ago, but never emitted warnings, only undocumented, so are
34
95
  still used by a few gems.
35
96
 
36
97
  ### 2025-04-24 (2.11.0)
@@ -57,7 +118,7 @@
57
118
  ### 2025-03-12 (2.10.2)
58
119
 
59
120
  * 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.
121
+ * Raise a ParserError on all incomplete unicode escape sequence. This was the behavior until `2.10.0` inadvertently changed it.
61
122
  * Ensure document snippets that are included in parser errors don't include truncated multibyte characters.
62
123
  * Ensure parser error snippets are valid UTF-8.
63
124
  * Fix `JSON::GeneratorError#detailed_message` on Ruby < 3.2
@@ -88,7 +149,7 @@
88
149
 
89
150
  ### 2024-11-14 (2.8.2)
90
151
 
91
- * `JSON.load_file` explictly read the file as UTF-8.
152
+ * `JSON.load_file` explicitly read the file as UTF-8.
92
153
 
93
154
  ### 2024-11-06 (2.8.1)
94
155
 
@@ -96,7 +157,7 @@
96
157
 
97
158
  ### 2024-11-06 (2.8.0)
98
159
 
99
- * Emit a deprecation warning when `JSON.load` create custom types without the `create_additions` option being explictly enabled.
160
+ * Emit a deprecation warning when `JSON.load` create custom types without the `create_additions` option being explicitly enabled.
100
161
  * Prefer to use `JSON.unsafe_load(string)` or `JSON.load(string, create_additions: true)`.
101
162
  * Emit a deprecation warning when serializing valid UTF-8 strings encoded in `ASCII_8BIT` aka `BINARY`.
102
163
  * Bump required Ruby version to 2.7.
@@ -104,7 +165,7 @@
104
165
  pre-existing support for comments, make it suitable to parse `jsonc` documents.
105
166
  * Many performance improvements to `JSON.parse` and `JSON.load`, up to `1.7x` faster on real world documents.
106
167
  * Some minor performance improvements to `JSON.dump` and `JSON.generate`.
107
- * `JSON.pretty_generate` no longer include newline inside empty object and arrays.
168
+ * `JSON.pretty_generate` no longer includes newlines inside empty object and arrays.
108
169
 
109
170
  ### 2024-11-04 (2.7.6)
110
171
 
@@ -121,13 +182,13 @@
121
182
  * Workaround a bug in 3.4.8 and older https://github.com/rubygems/rubygems/pull/6490.
122
183
  This bug would cause some gems with native extension to fail during compilation.
123
184
  * Workaround different versions of `json` and `json_pure` being loaded (not officially supported).
124
- * Make `json_pure` Ractor compatible.
185
+ * Make `json_pure` Ractor compatible.
125
186
 
126
187
  ### 2024-10-24 (2.7.3)
127
188
 
128
189
  * 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
190
+ * Limit the size of ParserError exception messages, only include up to 32 bytes of the unparsable source.
191
+ * Fix json-pure's `Object#to_json` to accept non-state arguments.
131
192
  * Fix multiline comment support in `json-pure`.
132
193
  * Fix `JSON.parse` to no longer mutate the argument encoding when passed an ASCII-8BIT string.
133
194
  * Fix `String#to_json` to raise on invalid encoding in `json-pure`.
@@ -272,6 +333,7 @@
272
333
  ## 2015-09-11 (2.0.0)
273
334
  * Now complies to newest JSON RFC 7159.
274
335
  * Implements compatibility to ruby 2.4 integer unification.
336
+ * Removed support for `quirks_mode` option.
275
337
  * Drops support for old rubies whose life has ended, that is rubies < 2.0.
276
338
  Also see https://www.ruby-lang.org/en/news/2014/07/01/eol-for-1-8-7-and-1-9-2/
277
339
  * 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/ryu.h::
19
+ This file is adapted from the Ryu algorithm by Ulf Adams https://github.com/ulfjack/ryu.
20
+ It is dual-licensed under Apache License 2.0 OR Boost Software License 1.0.
data/README.md CHANGED
@@ -97,7 +97,7 @@ Instead it is recommended to use the newer `JSON::Coder` API:
97
97
 
98
98
  ```ruby
99
99
  module MyApp
100
- API_JSON_CODER = JSON::Coder.new do |object|
100
+ API_JSON_CODER = JSON::Coder.new do |object, is_object_key|
101
101
  case object
102
102
  when Time
103
103
  object.iso8601(3)
@@ -113,6 +113,24 @@ puts MyApp::API_JSON_CODER.dump(Time.now.utc) # => "2025-01-21T08:41:44.286Z"
113
113
  The provided block is called for all objects that don't have a native JSON equivalent, and
114
114
  must return a Ruby object that has a native JSON equivalent.
115
115
 
116
+ It is also called for objects that do have a JSON equivalent, but are used as Hash keys, for instance `{ 1 => 2}`,
117
+ as well as for strings that aren't valid UTF-8:
118
+
119
+ ```ruby
120
+ coder = JSON::Combining.new do |object, is_object_key|
121
+ case object
122
+ when String
123
+ if !string.valid_encoding? || string.encoding != Encoding::UTF_8
124
+ Base64.encode64(string)
125
+ else
126
+ string
127
+ end
128
+ else
129
+ object
130
+ end
131
+ end
132
+ ```
133
+
116
134
  ## Combining JSON fragments
117
135
 
118
136
  To combine JSON fragments into a bigger JSON document, you can use `JSON::Fragment`:
@@ -1,47 +1,9 @@
1
1
  #ifndef _FBUFFER_H_
2
2
  #define _FBUFFER_H_
3
3
 
4
- #include "ruby.h"
5
- #include "ruby/encoding.h"
4
+ #include "../json.h"
6
5
  #include "../vendor/jeaiii-ltoa.h"
7
6
 
8
- /* shims */
9
- /* This is the fallback definition from Ruby 3.4 */
10
-
11
- #ifndef RBIMPL_STDBOOL_H
12
- #if defined(__cplusplus)
13
- # if defined(HAVE_STDBOOL_H) && (__cplusplus >= 201103L)
14
- # include <cstdbool>
15
- # endif
16
- #elif defined(HAVE_STDBOOL_H)
17
- # include <stdbool.h>
18
- #elif !defined(HAVE__BOOL)
19
- typedef unsigned char _Bool;
20
- # define bool _Bool
21
- # define true ((_Bool)+1)
22
- # define false ((_Bool)+0)
23
- # define __bool_true_false_are_defined
24
- #endif
25
- #endif
26
-
27
- #ifndef RB_UNLIKELY
28
- #define RB_UNLIKELY(expr) expr
29
- #endif
30
-
31
- #ifndef RB_LIKELY
32
- #define RB_LIKELY(expr) expr
33
- #endif
34
-
35
- #ifndef MAYBE_UNUSED
36
- # define MAYBE_UNUSED(x) x
37
- #endif
38
-
39
- #ifdef RUBY_DEBUG
40
- #ifndef JSON_DEBUG
41
- #define JSON_DEBUG RUBY_DEBUG
42
- #endif
43
- #endif
44
-
45
7
  enum fbuffer_type {
46
8
  FBUFFER_HEAP_ALLOCATED = 0,
47
9
  FBUFFER_STACK_ALLOCATED = 1,
@@ -169,12 +131,17 @@ static inline void fbuffer_inc_capa(FBuffer *fb, unsigned long requested)
169
131
  }
170
132
  }
171
133
 
172
- static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
134
+ static inline void fbuffer_append_reserved(FBuffer *fb, const char *newstr, unsigned long len)
135
+ {
136
+ MEMCPY(fb->ptr + fb->len, newstr, char, len);
137
+ fbuffer_consumed(fb, len);
138
+ }
139
+
140
+ static inline void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
173
141
  {
174
142
  if (len > 0) {
175
143
  fbuffer_inc_capa(fb, len);
176
- MEMCPY(fb->ptr + fb->len, newstr, char, len);
177
- fbuffer_consumed(fb, len);
144
+ fbuffer_append_reserved(fb, newstr, len);
178
145
  }
179
146
  }
180
147
 
@@ -197,11 +164,24 @@ static void fbuffer_append_str(FBuffer *fb, VALUE str)
197
164
  const char *newstr = StringValuePtr(str);
198
165
  unsigned long len = RSTRING_LEN(str);
199
166
 
200
- RB_GC_GUARD(str);
201
-
202
167
  fbuffer_append(fb, newstr, len);
203
168
  }
204
169
 
170
+ static void fbuffer_append_str_repeat(FBuffer *fb, VALUE str, size_t repeat)
171
+ {
172
+ const char *newstr = StringValuePtr(str);
173
+ unsigned long len = RSTRING_LEN(str);
174
+
175
+ fbuffer_inc_capa(fb, repeat * len);
176
+ while (repeat) {
177
+ #ifdef JSON_DEBUG
178
+ fb->requested = len;
179
+ #endif
180
+ fbuffer_append_reserved(fb, newstr, len);
181
+ repeat--;
182
+ }
183
+ }
184
+
205
185
  static inline void fbuffer_append_char(FBuffer *fb, char newchr)
206
186
  {
207
187
  fbuffer_inc_capa(fb, 1);
@@ -257,14 +237,11 @@ static VALUE fbuffer_finalize(FBuffer *fb)
257
237
  {
258
238
  if (fb->io) {
259
239
  fbuffer_flush(fb);
260
- fbuffer_free(fb);
261
240
  rb_io_flush(fb->io);
262
241
  return fb->io;
263
242
  } else {
264
- VALUE result = rb_utf8_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
265
- fbuffer_free(fb);
266
- return result;
243
+ return rb_utf8_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
267
244
  }
268
245
  }
269
246
 
270
- #endif
247
+ #endif // _FBUFFER_H_
@@ -9,31 +9,7 @@ else
9
9
  $defs << "-DJSON_DEBUG" if ENV["JSON_DEBUG"]
10
10
 
11
11
  if enable_config('generator-use-simd', default=!ENV["JSON_DISABLE_SIMD"])
12
- if RbConfig::CONFIG['host_cpu'] =~ /^(arm.*|aarch64.*)/
13
- # Try to compile a small program using NEON instructions
14
- if have_header('arm_neon.h')
15
- have_type('uint8x16_t', headers=['arm_neon.h']) && try_compile(<<~'SRC')
16
- #include <arm_neon.h>
17
- int main() {
18
- uint8x16_t test = vdupq_n_u8(32);
19
- return 0;
20
- }
21
- SRC
22
- $defs.push("-DJSON_ENABLE_SIMD")
23
- end
24
- end
25
-
26
- if have_header('x86intrin.h') && have_type('__m128i', headers=['x86intrin.h']) && try_compile(<<~'SRC')
27
- #include <x86intrin.h>
28
- int main() {
29
- __m128i test = _mm_set1_epi8(32);
30
- return 0;
31
- }
32
- SRC
33
- $defs.push("-DJSON_ENABLE_SIMD")
34
- end
35
-
36
- have_header('cpuid.h')
12
+ load __dir__ + "/../simd/conf.rb"
37
13
  end
38
14
 
39
15
  create_makefile 'json/ext/generator'