json 2.18.1 → 2.19.7

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.
@@ -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
@@ -505,11 +517,11 @@ module JSON
505
517
 
506
518
  if empty?
507
519
  state.depth -= 1
508
- return '{}'
520
+ return +'{}'
509
521
  end
510
522
 
511
523
  delim = ",#{state.object_nl}"
512
- result = +"{#{state.object_nl}"
524
+ result = "{#{state.object_nl}"
513
525
  first = true
514
526
  key_type = nil
515
527
  indent = !state.object_nl.empty?
@@ -546,7 +558,7 @@ module JSON
546
558
  raise TypeError, "#{key.class}#to_s returns an instance of #{key_str.class}, expected a String"
547
559
  end
548
560
 
549
- result = +"#{result}#{key_json}#{state.space_before}:#{state.space}"
561
+ result = "#{result}#{key_json}#{state.space_before}:#{state.space}"
550
562
  if state.strict? && !Generator.native_type?(value)
551
563
  if state.as_json
552
564
  value = state.as_json.call(value, false)
@@ -597,7 +609,7 @@ module JSON
597
609
 
598
610
  if empty?
599
611
  state.depth -= 1
600
- return '[]'
612
+ return +'[]'
601
613
  end
602
614
 
603
615
  result = '['.dup
@@ -722,17 +734,17 @@ module JSON
722
734
 
723
735
  module TrueClass
724
736
  # Returns a JSON string for true: 'true'.
725
- def to_json(*) 'true' end
737
+ def to_json(*) +'true' end
726
738
  end
727
739
 
728
740
  module FalseClass
729
741
  # Returns a JSON string for false: 'false'.
730
- def to_json(*) 'false' end
742
+ def to_json(*) +'false' end
731
743
  end
732
744
 
733
745
  module NilClass
734
746
  # Returns a JSON string for nil: 'null'.
735
- def to_json(*) 'null' end
747
+ def to_json(*) +'null' end
736
748
  end
737
749
  end
738
750
  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.19.7'
5
5
  end
data/lib/json.rb CHANGED
@@ -194,6 +194,18 @@ require 'json/common'
194
194
  # When enabled:
195
195
  # JSON.parse(%{"Hello\nWorld"}, allow_control_characters: true) # => "Hello\nWorld"
196
196
  #
197
+ # ---
198
+ #
199
+ # Option +allow_invalid_escape+ (boolean) specifies whether to ignore backslahes that are followed
200
+ # by an invalid escape character in strings;
201
+ # defaults to +false+.
202
+ #
203
+ # With the default, +false+:
204
+ # JSON.parse('"Hell\o"') # invalid escape character in string (JSON::ParserError)
205
+ #
206
+ # When enabled:
207
+ # JSON.parse('"Hell\o"', allow_invalid_escape: true) # => "Hello"
208
+ #
197
209
  # ====== Output Options
198
210
  #
199
211
  # Option +freeze+ (boolean) specifies whether the returned objects will be frozen;
@@ -323,8 +335,8 @@ require 'json/common'
323
335
  # JSON.generate(JSON::MinusInfinity)
324
336
  #
325
337
  # Allow:
326
- # ruby = [Float::NaN, Float::Infinity, Float::MinusInfinity]
327
- # JSON.generate(ruby, allow_nan: true) # => '[NaN,Infinity,-Infinity]'
338
+ # ruby = [Float::NAN, Float::INFINITY, JSON::NaN, JSON::Infinity, JSON::MinusInfinity]
339
+ # JSON.generate(ruby, allow_nan: true) # => '[NaN,Infinity,NaN,Infinity,-Infinity]'
328
340
  #
329
341
  # ---
330
342
  #
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.19.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank
@@ -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: []