json 2.15.2 → 2.18.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.
data/lib/json/common.rb CHANGED
@@ -71,8 +71,13 @@ module JSON
71
71
  end
72
72
  when object_class
73
73
  if opts[:create_additions] != false
74
- if class_name = object[JSON.create_id]
75
- klass = JSON.deep_const_get(class_name)
74
+ if class_path = object[JSON.create_id]
75
+ klass = begin
76
+ Object.const_get(class_path)
77
+ rescue NameError => e
78
+ raise ArgumentError, "can't get const #{class_path}: #{e}"
79
+ end
80
+
76
81
  if klass.respond_to?(:json_creatable?) ? klass.json_creatable? : klass.respond_to?(:json_create)
77
82
  create_additions_warning if create_additions.nil?
78
83
  object = klass.json_create(object)
@@ -147,16 +152,6 @@ module JSON
147
152
  const_set :Parser, parser
148
153
  end
149
154
 
150
- # Return the constant located at _path_. The format of _path_ has to be
151
- # either ::A::B::C or A::B::C. In any case, A has to be located at the top
152
- # level (absolute namespace path?). If there doesn't exist a constant at
153
- # the given path, an ArgumentError is raised.
154
- def deep_const_get(path) # :nodoc:
155
- Object.const_get(path)
156
- rescue NameError => e
157
- raise ArgumentError, "can't get const #{path}: #{e}"
158
- end
159
-
160
155
  # Set the module _generator_ to be used by JSON.
161
156
  def generator=(generator) # :nodoc:
162
157
  old, $VERBOSE = $VERBOSE, nil
@@ -555,6 +550,7 @@ module JSON
555
550
  :create_additions => nil,
556
551
  }
557
552
  # :call-seq:
553
+ # JSON.unsafe_load(source, options = {}) -> object
558
554
  # JSON.unsafe_load(source, proc = nil, options = {}) -> object
559
555
  #
560
556
  # Returns the Ruby objects created by parsing the given +source+.
@@ -686,7 +682,12 @@ module JSON
686
682
  #
687
683
  def unsafe_load(source, proc = nil, options = nil)
688
684
  opts = if options.nil?
689
- _unsafe_load_default_options
685
+ if proc && proc.is_a?(Hash)
686
+ options, proc = proc, nil
687
+ options
688
+ else
689
+ _unsafe_load_default_options
690
+ end
690
691
  else
691
692
  _unsafe_load_default_options.merge(options)
692
693
  end
@@ -714,6 +715,7 @@ module JSON
714
715
  end
715
716
 
716
717
  # :call-seq:
718
+ # JSON.load(source, options = {}) -> object
717
719
  # JSON.load(source, proc = nil, options = {}) -> object
718
720
  #
719
721
  # Returns the Ruby objects created by parsing the given +source+.
@@ -850,8 +852,18 @@ module JSON
850
852
  # @attributes={"type"=>"Admin", "password"=>"0wn3d"}>}
851
853
  #
852
854
  def load(source, proc = nil, options = nil)
855
+ if proc && options.nil? && proc.is_a?(Hash)
856
+ options = proc
857
+ proc = nil
858
+ end
859
+
853
860
  opts = if options.nil?
854
- _load_default_options
861
+ if proc && proc.is_a?(Hash)
862
+ options, proc = proc, nil
863
+ options
864
+ else
865
+ _load_default_options
866
+ end
855
867
  else
856
868
  _load_default_options.merge(options)
857
869
  end
@@ -1053,7 +1065,7 @@ module JSON
1053
1065
  options[:as_json] = as_json if as_json
1054
1066
 
1055
1067
  @state = State.new(options).freeze
1056
- @parser_config = Ext::Parser::Config.new(ParserOptions.prepare(options))
1068
+ @parser_config = Ext::Parser::Config.new(ParserOptions.prepare(options)).freeze
1057
1069
  end
1058
1070
 
1059
1071
  # call-seq:
@@ -1062,7 +1074,7 @@ module JSON
1062
1074
  #
1063
1075
  # Serialize the given object into a \JSON document.
1064
1076
  def dump(object, io = nil)
1065
- @state.generate_new(object, io)
1077
+ @state.generate(object, io)
1066
1078
  end
1067
1079
  alias_method :generate, :dump
1068
1080
 
@@ -75,6 +75,8 @@ module JSON
75
75
  #
76
76
  # Returns the value returned by method +name+.
77
77
  def [](name)
78
+ ::JSON.deprecation_warning("JSON::State#[] is deprecated and will be removed in json 3.0.0")
79
+
78
80
  if respond_to?(name)
79
81
  __send__(name)
80
82
  else
@@ -87,6 +89,8 @@ module JSON
87
89
  #
88
90
  # Sets the attribute name to value.
89
91
  def []=(name, value)
92
+ ::JSON.deprecation_warning("JSON::State#[]= is deprecated and will be removed in json 3.0.0")
93
+
90
94
  if respond_to?(name_writer = "#{name}=")
91
95
  __send__ name_writer, value
92
96
  else
@@ -55,6 +55,11 @@ module JSON
55
55
  (Symbol === key || String === key)
56
56
  end
57
57
 
58
+ def self.valid_encoding?(string) # :nodoc:
59
+ return false unless string.encoding == ::Encoding::UTF_8 || string.encoding == ::Encoding::US_ASCII
60
+ string.is_a?(Symbol) || string.valid_encoding?
61
+ end
62
+
58
63
  # Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
59
64
  # UTF16 big endian characters as \u????, and return it.
60
65
  def self.utf8_to_json(string, script_safe = false) # :nodoc:
@@ -307,8 +312,8 @@ module JSON
307
312
  def to_h
308
313
  result = {}
309
314
  instance_variables.each do |iv|
310
- iv = iv.to_s[1..-1]
311
- result[iv.to_sym] = self[iv]
315
+ key = iv.to_s[1..-1]
316
+ result[key.to_sym] = instance_variable_get(iv)
312
317
  end
313
318
 
314
319
  if result[:allow_duplicate_key].nil?
@@ -325,6 +330,9 @@ module JSON
325
330
  # created this method raises a
326
331
  # GeneratorError exception.
327
332
  def generate(obj, anIO = nil)
333
+ return dup.generate(obj, anIO) if frozen?
334
+
335
+ depth = @depth
328
336
  if @indent.empty? and @space.empty? and @space_before.empty? and @object_nl.empty? and @array_nl.empty? and
329
337
  !@ascii_only and !@script_safe and @max_nesting == 0 and (!@strict || Symbol === obj)
330
338
  result = generate_json(obj, ''.dup)
@@ -341,14 +349,8 @@ module JSON
341
349
  else
342
350
  result
343
351
  end
344
- end
345
-
346
- def generate_new(obj, anIO = nil) # :nodoc:
347
- dup.generate(obj, anIO)
348
- end
349
-
350
- private def initialize_copy(_orig)
351
- @depth = 0
352
+ ensure
353
+ @depth = depth unless frozen?
352
354
  end
353
355
 
354
356
  # Handles @allow_nan, @buffer_initial_length, other ivars must be the default value (see above)
@@ -434,6 +436,8 @@ module JSON
434
436
 
435
437
  # Return the value returned by method +name+.
436
438
  def [](name)
439
+ ::JSON.deprecation_warning("JSON::State#[] is deprecated and will be removed in json 3.0.0")
440
+
437
441
  if respond_to?(name)
438
442
  __send__(name)
439
443
  else
@@ -443,6 +447,8 @@ module JSON
443
447
  end
444
448
 
445
449
  def []=(name, value)
450
+ ::JSON.deprecation_warning("JSON::State#[]= is deprecated and will be removed in json 3.0.0")
451
+
446
452
  if respond_to?(name_writer = "#{name}=")
447
453
  __send__ name_writer, value
448
454
  else
@@ -485,17 +491,15 @@ module JSON
485
491
  # _depth_ is used to find out nesting depth, to indent accordingly.
486
492
  def to_json(state = nil, *)
487
493
  state = State.from_state(state)
494
+ depth = state.depth
488
495
  state.check_max_nesting
489
496
  json_transform(state)
497
+ ensure
498
+ state.depth = depth
490
499
  end
491
500
 
492
501
  private
493
502
 
494
- def json_shift(state)
495
- state.object_nl.empty? or return ''
496
- state.indent * state.depth
497
- end
498
-
499
503
  def json_transform(state)
500
504
  depth = state.depth += 1
501
505
 
@@ -521,13 +525,17 @@ module JSON
521
525
  end
522
526
  result << state.indent * depth if indent
523
527
 
524
- if state.strict? && !Generator.native_key?(key)
525
- if state.as_json
528
+ if state.strict?
529
+ if state.as_json && (!Generator.native_key?(key) || !Generator.valid_encoding?(key))
526
530
  key = state.as_json.call(key, true)
527
531
  end
528
532
 
529
533
  unless Generator.native_key?(key)
530
- raise GeneratorError.new("#{key.class} not allowed as object key in JSON", value)
534
+ raise GeneratorError.new("#{key.class} not allowed as object key in JSON", key)
535
+ end
536
+
537
+ unless Generator.valid_encoding?(key)
538
+ raise GeneratorError.new("source sequence is illegal/malformed utf-8", key)
531
539
  end
532
540
  end
533
541
 
@@ -546,17 +554,19 @@ module JSON
546
554
  raise GeneratorError.new("#{value.class} returned by #{state.as_json} not allowed in JSON", value)
547
555
  end
548
556
  result << value.to_json(state)
557
+ state.depth = depth
549
558
  else
550
559
  raise GeneratorError.new("#{value.class} not allowed in JSON", value)
551
560
  end
552
561
  elsif value.respond_to?(:to_json)
553
562
  result << value.to_json(state)
563
+ state.depth = depth
554
564
  else
555
565
  result << %{"#{String(value)}"}
556
566
  end
557
567
  first = false
558
568
  }
559
- depth = state.depth -= 1
569
+ depth -= 1
560
570
  unless first
561
571
  result << state.object_nl
562
572
  result << state.indent * depth if indent
@@ -573,8 +583,11 @@ module JSON
573
583
  # produced JSON string output further.
574
584
  def to_json(state = nil, *)
575
585
  state = State.from_state(state)
586
+ depth = state.depth
576
587
  state.check_max_nesting
577
588
  json_transform(state)
589
+ ensure
590
+ state.depth = depth
578
591
  end
579
592
 
580
593
  private
@@ -612,12 +625,13 @@ module JSON
612
625
  end
613
626
  elsif value.respond_to?(:to_json)
614
627
  result << value.to_json(state)
628
+ state.depth = depth
615
629
  else
616
630
  result << %{"#{String(value)}"}
617
631
  end
618
632
  first = false
619
633
  }
620
- depth = state.depth -= 1
634
+ depth -= 1
621
635
  result << state.array_nl
622
636
  result << state.indent * depth if indent
623
637
  result << ']'
@@ -642,6 +656,9 @@ module JSON
642
656
  if casted_value.equal?(self)
643
657
  raise GeneratorError.new("#{self} not allowed in JSON", self)
644
658
  end
659
+ unless Generator.native_type?(casted_value)
660
+ raise GeneratorError.new("#{casted_value.class} returned by #{state.as_json} not allowed in JSON", casted_value)
661
+ end
645
662
 
646
663
  state.check_max_nesting
647
664
  state.depth += 1
@@ -674,14 +691,25 @@ module JSON
674
691
  # \u????.
675
692
  def to_json(state = nil, *args)
676
693
  state = State.from_state(state)
677
- if encoding == ::Encoding::UTF_8
678
- unless valid_encoding?
694
+ string = self
695
+
696
+ if state.strict? && state.as_json
697
+ unless Generator.valid_encoding?(string)
698
+ string = state.as_json.call(string, false)
699
+ unless string.is_a?(::String)
700
+ return string.to_json(state, *args)
701
+ end
702
+ end
703
+ end
704
+
705
+ if string.encoding == ::Encoding::UTF_8
706
+ unless string.valid_encoding?
679
707
  raise GeneratorError.new("source sequence is illegal/malformed utf-8", self)
680
708
  end
681
- string = self
682
709
  else
683
- string = encode(::Encoding::UTF_8)
710
+ string = string.encode(::Encoding::UTF_8)
684
711
  end
712
+
685
713
  if state.ascii_only?
686
714
  %("#{JSON::TruffleRuby::Generator.utf8_to_json_ascii(string, state.script_safe)}")
687
715
  else
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.15.2'
4
+ VERSION = '2.18.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.15.2
4
+ version: 2.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank
@@ -26,12 +26,14 @@ files:
26
26
  - ext/json/ext/fbuffer/fbuffer.h
27
27
  - ext/json/ext/generator/extconf.rb
28
28
  - ext/json/ext/generator/generator.c
29
+ - ext/json/ext/json.h
29
30
  - ext/json/ext/parser/extconf.rb
30
31
  - ext/json/ext/parser/parser.c
31
32
  - ext/json/ext/simd/conf.rb
32
33
  - ext/json/ext/simd/simd.h
33
34
  - ext/json/ext/vendor/fpconv.c
34
35
  - ext/json/ext/vendor/jeaiii-ltoa.h
36
+ - ext/json/ext/vendor/ryu.h
35
37
  - json.gemspec
36
38
  - lib/json.rb
37
39
  - lib/json/add/bigdecimal.rb