json 2.8.1-java → 2.9.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: 22cd415f4f4cf98d59f767f269ab163b88c015719cd974a55bdfc83f087170b9
4
- data.tar.gz: 545b0d5a6ca3873e6db17b9854c425e96f59719c050a39a749a7bd99f4109f8e
3
+ metadata.gz: 74fb4885db76820e52ae4f7bbd590e4adfeef0209e313b1383361ed4160d5fad
4
+ data.tar.gz: 9800ac4ee5384282c67ed29ffbc82b91cc0f4ef1caf63e1a9dd573c4ef9cc040
5
5
  SHA512:
6
- metadata.gz: 432356f2ce4c10efd568c7fa09596f318f70ce942cd66dfeaaf6469b3f8ad58a2cf751b2d0d9b8bef3e662335142e4ec9b9cd4172338021fb7d8a6d3585b94af
7
- data.tar.gz: 329ef4f74d36e80bd5339532a22175b44c2f2eaeb25c902e7ce5c6da3f4b1c1daf7fee2446105d2f89f35dc41450f9dd3bdcfb9562fb83a7a406d98c33a2ea72
6
+ metadata.gz: 6492a64b044bd77298366f70ed86e8afa4500faa21c083b68389fd76219ea1fce927b743cf0640f4ba7a5793436ac64f64dc4652eb1b31422782f510f636bea7
7
+ data.tar.gz: 7625eb024f4756644e84ebfffd14aa371a9e0d1d9be36942e2df1f3c44dd383c444fa4535670f2758430a1c2a3e847b7e48a09c806b32b506f24876145512ae2
data/CHANGES.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changes
2
2
 
3
+ ### 2024-12-03 (2.9.0)
4
+
5
+ * Fix C implementation of `script_safe` escaping to not confuse some other 3 wide characters with `\u2028` and `\u2029`.
6
+ e.g. `JSON.generate(["倩", "瀨"], script_safe: true)` would generate the wrong JSON.
7
+ * `JSON.dump(object, some_io)` now write into the IO in chunks while previously it would buffer the entire JSON before writing.
8
+ * `JSON::GeneratorError` now has a `#invalid_object` attribute, making it easier to understand why an object tree cannot be serialized.
9
+ * Numerous improvements to the JRuby extension.
10
+
11
+ ### 2024-11-14 (2.8.2)
12
+
13
+ * `JSON.load_file` explictly read the file as UTF-8.
14
+
3
15
  ### 2024-11-06 (2.8.1)
4
16
 
5
17
  * Fix the java packages to include the extension.
@@ -14,6 +26,7 @@
14
26
  pre-existing support for comments, make it suitable to parse `jsonc` documents.
15
27
  * Many performance improvements to `JSON.parse` and `JSON.load`, up to `1.7x` faster on real world documents.
16
28
  * Some minor performance improvements to `JSON.dump` and `JSON.generate`.
29
+ * `JSON.pretty_generate` no longer include newline inside empty object and arrays.
17
30
 
18
31
  ### 2024-11-04 (2.7.6)
19
32
 
data/lib/json/common.rb CHANGED
@@ -1,4 +1,5 @@
1
- #frozen_string_literal: true
1
+ # frozen_string_literal: true
2
+
2
3
  require 'json/version'
3
4
 
4
5
  module JSON
@@ -25,7 +26,7 @@ module JSON
25
26
  elsif object.respond_to?(:to_str)
26
27
  str = object.to_str
27
28
  if str.is_a?(String)
28
- return JSON.parse(object.to_str, opts)
29
+ return JSON.parse(str, opts)
29
30
  end
30
31
  end
31
32
 
@@ -142,7 +143,23 @@ module JSON
142
143
  # :startdoc:
143
144
 
144
145
  # This exception is raised if a generator or unparser error occurs.
145
- class GeneratorError < JSONError; end
146
+ class GeneratorError < JSONError
147
+ attr_reader :invalid_object
148
+
149
+ def initialize(message, invalid_object = nil)
150
+ super(message)
151
+ @invalid_object = invalid_object
152
+ end
153
+
154
+ def detailed_message(...)
155
+ if @invalid_object.nil?
156
+ super
157
+ else
158
+ "#{super}\nInvalid object: #{@invalid_object.inspect}"
159
+ end
160
+ end
161
+ end
162
+
146
163
  # For backwards compatibility
147
164
  UnparserError = GeneratorError # :nodoc:
148
165
 
@@ -230,8 +247,8 @@ module JSON
230
247
  # parse(File.read(path), opts)
231
248
  #
232
249
  # See method #parse.
233
- def load_file(filespec, opts = {})
234
- parse(File.read(filespec), opts)
250
+ def load_file(filespec, opts = nil)
251
+ parse(File.read(filespec, encoding: Encoding::UTF_8), opts)
235
252
  end
236
253
 
237
254
  # :call-seq:
@@ -242,7 +259,7 @@ module JSON
242
259
  #
243
260
  # See method #parse!
244
261
  def load_file!(filespec, opts = {})
245
- parse!(File.read(filespec), opts)
262
+ parse!(File.read(filespec, encoding: Encoding::UTF_8), opts)
246
263
  end
247
264
 
248
265
  # :call-seq:
@@ -285,7 +302,7 @@ module JSON
285
302
  if State === opts
286
303
  opts.generate(obj)
287
304
  else
288
- State.generate(obj, opts)
305
+ State.generate(obj, opts, nil)
289
306
  end
290
307
  end
291
308
 
@@ -410,6 +427,10 @@ module JSON
410
427
  #
411
428
  # Returns the Ruby objects created by parsing the given +source+.
412
429
  #
430
+ # BEWARE: This method is meant to serialise data from trusted user input,
431
+ # like from your own database server or clients under your control, it could
432
+ # be dangerous to allow untrusted users to pass JSON sources into it.
433
+ #
413
434
  # - Argument +source+ must be, or be convertible to, a \String:
414
435
  # - If +source+ responds to instance method +to_str+,
415
436
  # <tt>source.to_str</tt> becomes the source.
@@ -424,9 +445,6 @@ module JSON
424
445
  # - Argument +proc+, if given, must be a \Proc that accepts one argument.
425
446
  # It will be called recursively with each result (depth-first order).
426
447
  # See details below.
427
- # BEWARE: This method is meant to serialise data from trusted user input,
428
- # like from your own database server or clients under your control, it could
429
- # be dangerous to allow untrusted users to pass JSON sources into it.
430
448
  # - Argument +opts+, if given, contains a \Hash of options for the parsing.
431
449
  # See {Parsing Options}[#module-JSON-label-Parsing+Options].
432
450
  # The default options can be changed via method JSON.unsafe_load_default_options=.
@@ -563,6 +581,16 @@ module JSON
563
581
  #
564
582
  # Returns the Ruby objects created by parsing the given +source+.
565
583
  #
584
+ # BEWARE: This method is meant to serialise data from trusted user input,
585
+ # like from your own database server or clients under your control, it could
586
+ # be dangerous to allow untrusted users to pass JSON sources into it.
587
+ # If you must use it, use JSON.unsafe_load instead to make it clear.
588
+ #
589
+ # Since JSON version 2.8.0, `load` emits a deprecation warning when a
590
+ # non native type is deserialized, without `create_additions` being explicitly
591
+ # enabled, and in JSON version 3.0, `load` will have `create_additions` disabled
592
+ # by default.
593
+ #
566
594
  # - Argument +source+ must be, or be convertible to, a \String:
567
595
  # - If +source+ responds to instance method +to_str+,
568
596
  # <tt>source.to_str</tt> becomes the source.
@@ -577,10 +605,6 @@ module JSON
577
605
  # - Argument +proc+, if given, must be a \Proc that accepts one argument.
578
606
  # It will be called recursively with each result (depth-first order).
579
607
  # See details below.
580
- # BEWARE: This method is meant to serialise data from trusted user input,
581
- # like from your own database server or clients under your control, it could
582
- # be dangerous to allow untrusted users to pass JSON sources into it.
583
- # If you must use it, use JSON.unsafe_load instead to make it clear.
584
608
  # - Argument +opts+, if given, contains a \Hash of options for the parsing.
585
609
  # See {Parsing Options}[#module-JSON-label-Parsing+Options].
586
610
  # The default options can be changed via method JSON.load_default_options=.
@@ -793,18 +817,15 @@ module JSON
793
817
  opts = opts.merge(:max_nesting => limit) if limit
794
818
  opts = merge_dump_options(opts, **kwargs) if kwargs
795
819
 
796
- result = begin
797
- generate(obj, opts)
820
+ begin
821
+ if State === opts
822
+ opts.generate(obj, anIO)
823
+ else
824
+ State.generate(obj, opts, anIO)
825
+ end
798
826
  rescue JSON::NestingError
799
827
  raise ArgumentError, "exceed depth limit"
800
828
  end
801
-
802
- if anIO.nil?
803
- result
804
- else
805
- anIO.write result
806
- anIO
807
- end
808
829
  end
809
830
 
810
831
  # Encodes string using String.encode.
@@ -47,6 +47,17 @@ module JSON
47
47
 
48
48
  alias_method :merge, :configure
49
49
 
50
+ # call-seq:
51
+ # generate(obj) -> String
52
+ # generate(obj, anIO) -> anIO
53
+ #
54
+ # Generates a valid JSON document from object +obj+ and returns the
55
+ # result. If no valid JSON document can be created this method raises a
56
+ # GeneratorError exception.
57
+ def generate(obj, io = nil)
58
+ _generate(obj, io)
59
+ end
60
+
50
61
  # call-seq: to_h
51
62
  #
52
63
  # Returns the configuration instance variables as a hash, that can be
Binary file
Binary file
@@ -62,8 +62,8 @@ module JSON
62
62
  string
63
63
  end
64
64
 
65
- def utf8_to_json_ascii(string, script_safe = false) # :nodoc:
66
- string = string.b
65
+ def utf8_to_json_ascii(original_string, script_safe = false) # :nodoc:
66
+ string = original_string.b
67
67
  map = script_safe ? SCRIPT_SAFE_MAP : MAP
68
68
  string.gsub!(/[\/"\\\x0-\x1f]/n) { map[$&] || $& }
69
69
  string.gsub!(/(
@@ -74,7 +74,7 @@ module JSON
74
74
  )+ |
75
75
  [\x80-\xc1\xf5-\xff] # invalid
76
76
  )/nx) { |c|
77
- c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
77
+ c.size == 1 and raise GeneratorError.new("invalid utf8 byte: '#{c}'", original_string)
78
78
  s = c.encode(::Encoding::UTF_16BE, ::Encoding::UTF_8).unpack('H*')[0]
79
79
  s.force_encoding(::Encoding::BINARY)
80
80
  s.gsub!(/.{4}/n, '\\\\u\&')
@@ -83,7 +83,7 @@ module JSON
83
83
  string.force_encoding(::Encoding::UTF_8)
84
84
  string
85
85
  rescue => e
86
- raise GeneratorError.wrap(e)
86
+ raise GeneratorError.new(e.message, original_string)
87
87
  end
88
88
 
89
89
  def valid_utf8?(string)
@@ -96,8 +96,14 @@ module JSON
96
96
  # This class is used to create State instances, that are use to hold data
97
97
  # while generating a JSON text from a Ruby data structure.
98
98
  class State
99
- def self.generate(obj, opts = nil)
100
- new(opts).generate(obj)
99
+ def self.generate(obj, opts = nil, io = nil)
100
+ string = new(opts).generate(obj)
101
+ if io
102
+ io.write(string)
103
+ io
104
+ else
105
+ string
106
+ end
101
107
  end
102
108
 
103
109
  # Creates a State object from _opts_, which ought to be Hash to create
@@ -300,8 +306,10 @@ module JSON
300
306
  else
301
307
  result = obj.to_json(self)
302
308
  end
303
- JSON::TruffleRuby::Generator.valid_utf8?(result) or raise GeneratorError,
304
- "source sequence #{result.inspect} is illegal/malformed utf-8"
309
+ JSON::TruffleRuby::Generator.valid_utf8?(result) or raise GeneratorError.new(
310
+ "source sequence #{result.inspect} is illegal/malformed utf-8",
311
+ obj
312
+ )
305
313
  result
306
314
  end
307
315
 
@@ -358,10 +366,10 @@ module JSON
358
366
  begin
359
367
  string = string.encode(::Encoding::UTF_8)
360
368
  rescue Encoding::UndefinedConversionError => error
361
- raise GeneratorError, error.message
369
+ raise GeneratorError.new(error.message, string)
362
370
  end
363
371
  end
364
- raise GeneratorError, "source sequence is illegal/malformed utf-8" unless string.valid_encoding?
372
+ raise GeneratorError.new("source sequence is illegal/malformed utf-8", string) unless string.valid_encoding?
365
373
 
366
374
  if /["\\\x0-\x1f]/n.match?(string)
367
375
  buf << string.gsub(/["\\\x0-\x1f]/n, MAP)
@@ -397,7 +405,7 @@ module JSON
397
405
  # special method #to_json was defined for some object.
398
406
  def to_json(state = nil, *)
399
407
  if state && State.from_state(state).strict?
400
- raise GeneratorError, "#{self.class} not allowed in JSON"
408
+ raise GeneratorError.new("#{self.class} not allowed in JSON", self)
401
409
  else
402
410
  to_s.to_json
403
411
  end
@@ -448,7 +456,7 @@ module JSON
448
456
 
449
457
  result = +"#{result}#{key_json}#{state.space_before}:#{state.space}"
450
458
  if state.strict? && !(false == value || true == value || nil == value || String === value || Array === value || Hash === value || Integer === value || Float === value)
451
- raise GeneratorError, "#{value.class} not allowed in JSON"
459
+ raise GeneratorError.new("#{value.class} not allowed in JSON", value)
452
460
  elsif value.respond_to?(:to_json)
453
461
  result << value.to_json(state)
454
462
  else
@@ -501,7 +509,7 @@ module JSON
501
509
  result << delim unless first
502
510
  result << state.indent * depth if indent
503
511
  if state.strict? && !(false == value || true == value || nil == value || String === value || Array === value || Hash === value || Integer === value || Float === value)
504
- raise GeneratorError, "#{value.class} not allowed in JSON"
512
+ raise GeneratorError.new("#{value.class} not allowed in JSON", value)
505
513
  elsif value.respond_to?(:to_json)
506
514
  result << value.to_json(state)
507
515
  else
@@ -530,13 +538,13 @@ module JSON
530
538
  if state.allow_nan?
531
539
  to_s
532
540
  else
533
- raise GeneratorError, "#{self} not allowed in JSON"
541
+ raise GeneratorError.new("#{self} not allowed in JSON", self)
534
542
  end
535
543
  when nan?
536
544
  if state.allow_nan?
537
545
  to_s
538
546
  else
539
- raise GeneratorError, "#{self} not allowed in JSON"
547
+ raise GeneratorError.new("#{self} not allowed in JSON", self)
540
548
  end
541
549
  else
542
550
  to_s
@@ -552,7 +560,7 @@ module JSON
552
560
  state = State.from_state(state)
553
561
  if encoding == ::Encoding::UTF_8
554
562
  unless valid_encoding?
555
- raise GeneratorError, "source sequence is illegal/malformed utf-8"
563
+ raise GeneratorError.new("source sequence is illegal/malformed utf-8", self)
556
564
  end
557
565
  string = self
558
566
  else
@@ -564,7 +572,7 @@ module JSON
564
572
  %("#{JSON::TruffleRuby::Generator.utf8_to_json(string, state.script_safe)}")
565
573
  end
566
574
  rescue Encoding::UndefinedConversionError => error
567
- raise ::JSON::GeneratorError, error.message
575
+ raise ::JSON::GeneratorError.new(error.message, self)
568
576
  end
569
577
 
570
578
  # Module that holds the extending methods if, the String module is
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.8.1'
4
+ VERSION = '2.9.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.8.1
4
+ version: 2.9.0
5
5
  platform: java
6
6
  authors:
7
7
  - Daniel Luz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-11-06 00:00:00.000000000 Z
11
+ date: 2024-12-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A JSON implementation as a JRuby extension.
14
14
  email: dev+ruby@mernen.com