json 2.18.1 → 2.20.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
@@ -156,15 +156,17 @@ module JSON
156
156
  def generator=(generator) # :nodoc:
157
157
  old, $VERBOSE = $VERBOSE, nil
158
158
  @generator = generator
159
- generator_methods = generator::GeneratorMethods
160
- for const in generator_methods.constants
161
- klass = const_get(const)
162
- modul = generator_methods.const_get(const)
163
- klass.class_eval do
164
- instance_methods(false).each do |m|
165
- m.to_s == 'to_json' and remove_method m
159
+ if generator.const_defined?(:GeneratorMethods)
160
+ generator_methods = generator::GeneratorMethods
161
+ for const in generator_methods.constants
162
+ klass = const_get(const)
163
+ modul = generator_methods.const_get(const)
164
+ klass.class_eval do
165
+ instance_methods(false).each do |m|
166
+ m.to_s == 'to_json' and remove_method m
167
+ end
168
+ include modul
166
169
  end
167
- include modul
168
170
  end
169
171
  end
170
172
  self.state = generator::State
@@ -878,7 +880,7 @@ module JSON
878
880
  end
879
881
  end
880
882
 
881
- if opts[:allow_blank] && (source.nil? || source.empty?)
883
+ if opts[:allow_blank] && (source.nil? || (String === source && source.empty?))
882
884
  source = 'null'
883
885
  end
884
886
 
@@ -1036,7 +1038,8 @@ module JSON
1036
1038
  # JSON.new(options = nil, &block)
1037
1039
  #
1038
1040
  # Argument +options+, if given, contains a \Hash of options for both parsing and generating.
1039
- # See {Parsing Options}[#module-JSON-label-Parsing+Options], and {Generating Options}[#module-JSON-label-Generating+Options].
1041
+ # See {Parsing Options}[rdoc-ref:JSON@Parsing+Options],
1042
+ # and {Generating Options}[rdoc-ref:JSON@Generating+Options].
1040
1043
  #
1041
1044
  # For generation, the <tt>strict: true</tt> option is always set. When a Ruby object with no native \JSON counterpart is
1042
1045
  # encountered, the block provided to the initialize method is invoked, and must return a Ruby object that has a native
@@ -1095,6 +1098,30 @@ module JSON
1095
1098
  load(File.read(path, encoding: Encoding::UTF_8))
1096
1099
  end
1097
1100
  end
1101
+
1102
+ module GeneratorMethods
1103
+ # call-seq: to_json(*)
1104
+ #
1105
+ # Converts this object into a JSON string.
1106
+ # If this object doesn't directly maps to a JSON native type,
1107
+ # first convert it to a string (calling #to_s), then converts
1108
+ # it to a JSON string, and returns the result.
1109
+ # This is a fallback, if no special method #to_json was defined for some object.
1110
+ def to_json(state = nil, *)
1111
+ obj = case self
1112
+ when nil, false, true, Integer, Float, Array, Hash
1113
+ self
1114
+ else
1115
+ "#{self}"
1116
+ end
1117
+
1118
+ if state.nil?
1119
+ JSON::State._generate_no_fallback(obj, nil, nil)
1120
+ else
1121
+ JSON::State.from_state(state)._generate_no_fallback(obj)
1122
+ end
1123
+ end
1124
+ end
1098
1125
  end
1099
1126
 
1100
1127
  module ::Kernel
@@ -1140,3 +1167,7 @@ module ::Kernel
1140
1167
  JSON[object, opts]
1141
1168
  end
1142
1169
  end
1170
+
1171
+ class Object
1172
+ include JSON::GeneratorMethods
1173
+ end
@@ -9,7 +9,7 @@ module JSON
9
9
  # Instantiates a new State object, configured by _opts_.
10
10
  #
11
11
  # Argument +opts+, if given, contains a \Hash of options for the generation.
12
- # See {Generating Options}[#module-JSON-label-Generating+Options].
12
+ # See {Generating Options}[rdoc-ref:JSON@Generating+Options].
13
13
  def initialize(opts = nil)
14
14
  if opts && !opts.empty?
15
15
  configure(opts)
@@ -48,7 +48,7 @@ module JSON
48
48
  SCRIPT_SAFE_ESCAPE_PATTERN = /[\/"\\\x0-\x1f\u2028-\u2029]/
49
49
 
50
50
  def self.native_type?(value) # :nodoc:
51
- (false == value || true == value || nil == value || String === value || Array === value || Hash === value || Integer === value || Float === value || Fragment === value)
51
+ (false == value || true == value || nil == value || String === value || Symbol === value || Array === value || Hash === value || Integer === value || Float === value || Fragment === value)
52
52
  end
53
53
 
54
54
  def self.native_key?(key) # :nodoc:
@@ -211,7 +211,14 @@ module JSON
211
211
 
212
212
  # This integer returns the current depth data structure nesting in the
213
213
  # generated JSON.
214
- attr_accessor :depth
214
+ attr_reader :depth
215
+
216
+ def depth=(depth)
217
+ if depth.negative?
218
+ raise ArgumentError, "depth must be >= 0 (got #{depth})"
219
+ end
220
+ @depth = depth
221
+ end
215
222
 
216
223
  def check_max_nesting # :nodoc:
217
224
  return if @max_nesting.zero?
@@ -260,6 +267,11 @@ module JSON
260
267
  else
261
268
  raise TypeError, "can't convert #{opts.class} into Hash"
262
269
  end
270
+
271
+ if opts[:depth]&.negative?
272
+ raise ArgumentError, "depth must be >= 0 (got #{opts[:depth]})"
273
+ end
274
+
263
275
  opts.each do |key, value|
264
276
  instance_variable_set "@#{key}", value
265
277
  end
@@ -295,6 +307,9 @@ module JSON
295
307
  if !opts.key?(:max_nesting) # defaults to 100
296
308
  @max_nesting = 100
297
309
  elsif opts[:max_nesting]
310
+ unless opts[:max_nesting].is_a?(Integer)
311
+ raise TypeError, ":max_nesting must be an Integer, got: #{opts[:max_nesting].class}"
312
+ end
298
313
  @max_nesting = opts[:max_nesting]
299
314
  else
300
315
  @max_nesting = 0
@@ -505,11 +520,11 @@ module JSON
505
520
 
506
521
  if empty?
507
522
  state.depth -= 1
508
- return '{}'
523
+ return +'{}'
509
524
  end
510
525
 
511
526
  delim = ",#{state.object_nl}"
512
- result = +"{#{state.object_nl}"
527
+ result = "{#{state.object_nl}"
513
528
  first = true
514
529
  key_type = nil
515
530
  indent = !state.object_nl.empty?
@@ -546,7 +561,7 @@ module JSON
546
561
  raise TypeError, "#{key.class}#to_s returns an instance of #{key_str.class}, expected a String"
547
562
  end
548
563
 
549
- result = +"#{result}#{key_json}#{state.space_before}:#{state.space}"
564
+ result = "#{result}#{key_json}#{state.space_before}:#{state.space}"
550
565
  if state.strict? && !Generator.native_type?(value)
551
566
  if state.as_json
552
567
  value = state.as_json.call(value, false)
@@ -597,7 +612,7 @@ module JSON
597
612
 
598
613
  if empty?
599
614
  state.depth -= 1
600
- return '[]'
615
+ return +'[]'
601
616
  end
602
617
 
603
618
  result = '['.dup
@@ -722,17 +737,17 @@ module JSON
722
737
 
723
738
  module TrueClass
724
739
  # Returns a JSON string for true: 'true'.
725
- def to_json(*) 'true' end
740
+ def to_json(*) +'true' end
726
741
  end
727
742
 
728
743
  module FalseClass
729
744
  # Returns a JSON string for false: 'false'.
730
- def to_json(*) 'false' end
745
+ def to_json(*) +'false' end
731
746
  end
732
747
 
733
748
  module NilClass
734
749
  # Returns a JSON string for nil: 'null'.
735
- def to_json(*) 'null' end
750
+ def to_json(*) +'null' end
736
751
  end
737
752
  end
738
753
  end
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.18.1'
4
+ VERSION = '2.20.0'
5
5
  end
data/lib/json.rb CHANGED
@@ -145,11 +145,11 @@ require 'json/common'
145
145
  # # warning: detected duplicate keys in JSON object.
146
146
  # # This will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`
147
147
  #
148
- # When set to `+true+`
148
+ # When set to +true+:
149
149
  # # The last value is used.
150
150
  # JSON.parse('{"a": 1, "a":2}') => {"a" => 2}
151
151
  #
152
- # When set to `+false+`, the future default:
152
+ # When set to +false+, the future default:
153
153
  # JSON.parse('{"a": 1, "a":2}') => duplicate key at line 1 column 1 (JSON::ParserError)
154
154
  #
155
155
  # ---
@@ -184,6 +184,20 @@ require 'json/common'
184
184
  #
185
185
  # ---
186
186
  #
187
+ # Option +allow_comments+ (boolean) specifies whether to allow
188
+ # JavaScript style comments (either <tt>// comment</tt> or <tt>/* comment */</tt>);
189
+ # defaults to +false+.
190
+ #
191
+ # When not specified, a deprecation warning is emitted if a comment is encountered.
192
+ #
193
+ # When set to +true+, comments are ignored:
194
+ # JSON.parse('/* comment */ {"a": 1, "a":2}') # => {"a" => 2}
195
+ #
196
+ # When set to +false+, the future default:
197
+ # JSON.parse('/* comment */ {"a": 1, "a":2}') # unexpected character: '/' at line 1 column 1 (JSON::ParserError)
198
+ #
199
+ # ---
200
+ #
187
201
  # Option +allow_control_characters+ (boolean) specifies whether to allow
188
202
  # unescaped ASCII control characters, such as newlines, in strings;
189
203
  # defaults to +false+.
@@ -194,6 +208,18 @@ require 'json/common'
194
208
  # When enabled:
195
209
  # JSON.parse(%{"Hello\nWorld"}, allow_control_characters: true) # => "Hello\nWorld"
196
210
  #
211
+ # ---
212
+ #
213
+ # Option +allow_invalid_escape+ (boolean) specifies whether to ignore backslahes that are followed
214
+ # by an invalid escape character in strings;
215
+ # defaults to +false+.
216
+ #
217
+ # With the default, +false+:
218
+ # JSON.parse('"Hell\o"') # invalid escape character in string (JSON::ParserError)
219
+ #
220
+ # When enabled:
221
+ # JSON.parse('"Hell\o"', allow_invalid_escape: true) # => "Hello"
222
+ #
197
223
  # ====== Output Options
198
224
  #
199
225
  # Option +freeze+ (boolean) specifies whether the returned objects will be frozen;
@@ -323,8 +349,8 @@ require 'json/common'
323
349
  # JSON.generate(JSON::MinusInfinity)
324
350
  #
325
351
  # Allow:
326
- # ruby = [Float::NaN, Float::Infinity, Float::MinusInfinity]
327
- # JSON.generate(ruby, allow_nan: true) # => '[NaN,Infinity,-Infinity]'
352
+ # ruby = [Float::NAN, Float::INFINITY, JSON::NaN, JSON::Infinity, JSON::MinusInfinity]
353
+ # JSON.generate(ruby, allow_nan: true) # => '[NaN,Infinity,NaN,Infinity,-Infinity]'
328
354
  #
329
355
  # ---
330
356
  #
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.18.1
4
+ version: 2.20.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank
@@ -31,9 +31,9 @@ files:
31
31
  - ext/json/ext/parser/parser.c
32
32
  - ext/json/ext/simd/conf.rb
33
33
  - ext/json/ext/simd/simd.h
34
+ - ext/json/ext/vendor/fast_float_parser.h
34
35
  - ext/json/ext/vendor/fpconv.c
35
36
  - ext/json/ext/vendor/jeaiii-ltoa.h
36
- - ext/json/ext/vendor/ryu.h
37
37
  - json.gemspec
38
38
  - lib/json.rb
39
39
  - lib/json/add/bigdecimal.rb
@@ -84,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
84
  - !ruby/object:Gem::Version
85
85
  version: '0'
86
86
  requirements: []
87
- rubygems_version: 4.1.0.dev
87
+ rubygems_version: 4.0.12
88
88
  specification_version: 4
89
89
  summary: JSON Implementation for Ruby
90
90
  test_files: []