json 2.20.0-java → 2.21.0-java

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: f01681908b8ed67cdfbfb53e8f7a580259cd6c5ea973217292233aee473a2bcb
4
- data.tar.gz: 6cb81cbffd79eeff08ad709da839556c18492423a8980c50cd79c72a3720b022
3
+ metadata.gz: 1afa1e3890c8c870016c243fc967533cb0a0ef3061e95d6ba29262c7564809b9
4
+ data.tar.gz: f8123182d86d00979ae0fadf3df150b7a2766e5e92a9323d97550ac13bd85769
5
5
  SHA512:
6
- metadata.gz: 32142742b1ccfe813374514fa2e51fbe99280ec08f67b611177c10c2bee032e6ec5725e1ee826f7a0ff484ab33e0fa40c40ee28240cbda11619ddb4127f6d748
7
- data.tar.gz: d33bdd91bc38c61a82bcadc58bd8b4a085d77943945d31d5ff1f1de14d01a42a7fe7c2088c9fea76c3e10b00ee24cfe89ccafd3df9eace5d2e7f761dd007ebac
6
+ metadata.gz: 83be4643e044a17198169036eb49eb2ae2dced41f26fe26335cd1feef90acb9b7c56be5a6a986de25dcde2f919a5d587c208f477e65d41783d347f84bf0fdeed
7
+ data.tar.gz: d748f91e351cea5fc38a02c174ca8909bc31e4ef0b611e409936a0248429040e7a70daf6fcdca9781c0fe99a41558ff021dbf127098fb65bbddb4c6853a6a0b0
data/CHANGES.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  ### Unreleased
4
4
 
5
+ ### 2026-07-12 (2.21.0)
6
+
7
+ * `JSON.generate` now accept a `sort_keys` option, which takes either a boolean or a block.
8
+ * Added `#empty?` and `#partial_value?` methods on `JSON::ResumableParser`.
9
+ * Numerous correctness and performance fixes for `JSON::ResumableParser`.
10
+ * Avoid triggering Ruby's `float out of range` warning when parsing out of range numbers.
11
+ * Declare C types with Ruby 4.1 `RUBY_TYPED_THREAD_SAFE_FREE`.
12
+
5
13
  ### 2026-06-23 (2.20.0)
6
14
 
7
15
  * Both C and Java parsers are no longer recursive, so parsing very deep documents with `max_nesting: false` will no longer
data/README.md CHANGED
@@ -85,7 +85,7 @@ Both of these behavior can be disabled using the `strict: true` option:
85
85
 
86
86
  ```ruby
87
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)
88
+ JSON.generate(Position.new(1, 2), strict: true) # => Position not allowed in JSON (JSON::GeneratorError)
89
89
  ```
90
90
 
91
91
  ## JSON::Coder
@@ -117,13 +117,13 @@ It is also called for objects that do have a JSON equivalent, but are used as Ha
117
117
  as well as for strings that aren't valid UTF-8:
118
118
 
119
119
  ```ruby
120
- coder = JSON::Combining.new do |object, is_object_key|
120
+ coder = JSON::Coder.new do |object, is_object_key|
121
121
  case object
122
122
  when String
123
- if !string.valid_encoding? || string.encoding != Encoding::UTF_8
124
- Base64.encode64(string)
123
+ if !object.valid_encoding? || object.encoding != Encoding::UTF_8
124
+ Base64.encode64(object)
125
125
  else
126
- string
126
+ object
127
127
  end
128
128
  else
129
129
  object
data/lib/json/common.rb CHANGED
@@ -155,6 +155,15 @@ module JSON
155
155
  # Set the module _generator_ to be used by JSON.
156
156
  def generator=(generator) # :nodoc:
157
157
  old, $VERBOSE = $VERBOSE, nil
158
+
159
+ # The default proc used when the +sort_keys+ generation option is +true+.
160
+ # It returns a new hash with the entries sorted by their keys.
161
+ sort_keys_proc = ->(hash) { hash.sort.to_h }
162
+ if defined?(::Ractor) && Ractor.respond_to?(:shareable_lambda)
163
+ sort_keys_proc = Ractor.shareable_lambda(&sort_keys_proc)
164
+ end
165
+ generator::State.default_sort_keys_proc = sort_keys_proc
166
+
158
167
  @generator = generator
159
168
  if generator.const_defined?(:GeneratorMethods)
160
169
  generator_methods = generator::GeneratorMethods
@@ -54,6 +54,7 @@ module JSON
54
54
  strict: strict?,
55
55
  depth: depth,
56
56
  buffer_initial_length: buffer_initial_length,
57
+ sort_keys: sort_keys
57
58
  }
58
59
 
59
60
  allow_duplicate_key = allow_duplicate_key?
Binary file
Binary file
data/lib/json/ext.rb CHANGED
@@ -41,5 +41,31 @@ module JSON
41
41
  end
42
42
  end
43
43
 
44
+ if defined?(ResumableParser) # Not yet available on JRuby
45
+ class ResumableParser
46
+ # Returns whether the parser is entirely done: no unconsumed bytes in
47
+ # the buffer, no document under construction and no parsed value
48
+ # awaiting retrieval.
49
+ #
50
+ # The main use case is detecting a truncated stream once the input is
51
+ # exhausted:
52
+ #
53
+ # loop do
54
+ # begin
55
+ # parser << socket.readpartial(4096)
56
+ # rescue EOFError
57
+ # break
58
+ # end
59
+ # while parser.parse
60
+ # process(parser.value)
61
+ # end
62
+ # end
63
+ # warn "stream was truncated" unless parser.empty?
64
+ def empty?
65
+ eos? && !partial_value? && !value?
66
+ end
67
+ end
68
+ end
69
+
44
70
  JSON_LOADED = true unless defined?(JSON::JSON_LOADED)
45
71
  end
@@ -111,6 +111,8 @@ module JSON
111
111
  # This class is used to create State instances, that are use to hold data
112
112
  # while generating a JSON text from a Ruby data structure.
113
113
  class State
114
+ singleton_class.attr_accessor :default_sort_keys_proc # :nodoc:
115
+
114
116
  def self.generate(obj, opts = nil, io = nil)
115
117
  new(opts).generate(obj, io)
116
118
  end
@@ -164,6 +166,7 @@ module JSON
164
166
  @script_safe = false
165
167
  @strict = false
166
168
  @max_nesting = 100
169
+ @sort_keys = false
167
170
  configure(opts) if opts
168
171
  end
169
172
 
@@ -199,6 +202,33 @@ module JSON
199
202
  # supported by the JSON spec will raise a JSON::GeneratorError
200
203
  attr_accessor :strict
201
204
 
205
+ # Controls key sorting in the generated JSON. If set to +true+, object
206
+ # keys are sorted by key lexicographically. If set to a Proc, it
207
+ # receives the entire Hash and must return a Hash with its pairs in the
208
+ # desired order.
209
+ attr_reader :sort_keys
210
+
211
+ def sort_keys=(value) # :nodoc:
212
+ type_error = false
213
+ @sort_keys = case value
214
+ when Proc
215
+ value
216
+ when true
217
+ State.default_sort_keys_proc
218
+ when nil, false
219
+ false
220
+ else
221
+ type_error = true
222
+ false
223
+ end
224
+
225
+ if type_error
226
+ raise TypeError, "The `sort_keys` argument must be a boolean or a Proc"
227
+ end
228
+
229
+ @sort_keys
230
+ end
231
+
202
232
  # :stopdoc:
203
233
  attr_reader :buffer_initial_length
204
234
 
@@ -285,6 +315,7 @@ module JSON
285
315
  @allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
286
316
  @as_json = opts[:as_json].to_proc if opts[:as_json]
287
317
  @ascii_only = opts[:ascii_only] if opts.key?(:ascii_only)
318
+ self.sort_keys = opts[:sort_keys] if opts.key?(:sort_keys)
288
319
  @depth = opts[:depth] || 0
289
320
  @buffer_initial_length ||= opts[:buffer_initial_length]
290
321
 
@@ -349,9 +380,13 @@ module JSON
349
380
 
350
381
  depth = @depth
351
382
  if @indent.empty? and @space.empty? and @space_before.empty? and @object_nl.empty? and @array_nl.empty? and
352
- !@ascii_only and !@script_safe and @max_nesting == 0 and (!@strict || Symbol === obj)
383
+ !@ascii_only and !@script_safe and @max_nesting == 0 and (!@strict || Symbol === obj) and !@sort_keys
353
384
  result = generate_json(obj, ''.dup)
354
385
  else
386
+ if @sort_keys
387
+ obj = @sort_keys.call(obj)
388
+ end
389
+
355
390
  result = obj.to_json(self)
356
391
  end
357
392
  JSON::TruffleRuby::Generator.valid_utf8?(result) or raise GeneratorError.new(
data/lib/json/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JSON
4
- VERSION = '2.20.0'
4
+ VERSION = '2.21.0'
5
5
  end
data/lib/json.rb CHANGED
@@ -408,7 +408,6 @@ require 'json/common'
408
408
  # to be inserted after each \JSON object; defaults to the empty \String, <tt>''</tt>.
409
409
  # - Option +indent+ (\String) specifies the string (usually spaces) to be
410
410
  # used for indentation; defaults to the empty \String, <tt>''</tt>;
411
- # defaults to the empty \String, <tt>''</tt>;
412
411
  # has no effect unless options +array_nl+ or +object_nl+ specify newlines.
413
412
  # - Option +space+ (\String) specifies a string (usually a space) to be
414
413
  # inserted after the colon in each \JSON object's pair;
@@ -416,6 +415,11 @@ require 'json/common'
416
415
  # - Option +space_before+ (\String) specifies a string (usually a space) to be
417
416
  # inserted before the colon in each \JSON object's pair;
418
417
  # defaults to the empty \String, <tt>''</tt>.
418
+ # - Option +sort_keys+ (boolean or \Proc) controls whether and how the keys of a
419
+ # hash are sorted when generating the output; defaults to <tt>false</tt>.
420
+ # When +true+, keys are sorted lexicographically. When a \Proc, it receives
421
+ # the entire \Hash and must return a \Hash with its pairs in the desired
422
+ # order, allowing for arbitrary sort orders.
419
423
  #
420
424
  # In this example, +obj+ is used first to generate the shortest
421
425
  # \JSON data (no whitespace), then again with all formatting options
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.20.0
4
+ version: 2.21.0
5
5
  platform: java
6
6
  authors:
7
7
  - Daniel Luz
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2026-06-23 00:00:00.000000000 Z
10
+ date: 2026-07-12 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: A JSON implementation as a JRuby extension.
13
13
  email: dev+ruby@mernen.com