json 2.21.2 → 3.0.2

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.
@@ -71,33 +71,6 @@ module JSON
71
71
  end
72
72
 
73
73
  alias_method :to_hash, :to_h
74
-
75
- # call-seq: [](name)
76
- #
77
- # Returns the value returned by method +name+.
78
- def [](name)
79
- ::JSON.deprecation_warning("JSON::State#[] is deprecated and will be removed in json 3.0.0")
80
-
81
- if respond_to?(name)
82
- __send__(name)
83
- else
84
- instance_variable_get("@#{name}") if
85
- instance_variables.include?("@#{name}".to_sym) # avoid warning
86
- end
87
- end
88
-
89
- # call-seq: []=(name, value)
90
- #
91
- # Sets the attribute name to value.
92
- def []=(name, value)
93
- ::JSON.deprecation_warning("JSON::State#[]= is deprecated and will be removed in json 3.0.0")
94
-
95
- if respond_to?(name_writer = "#{name}=")
96
- __send__ name_writer, value
97
- else
98
- instance_variable_set "@#{name}", value
99
- end
100
- end
101
74
  end
102
75
  end
103
76
  end
data/lib/json/ext.rb CHANGED
@@ -66,6 +66,4 @@ module JSON
66
66
  end
67
67
  end
68
68
  end
69
-
70
- JSON_LOADED = true unless defined?(JSON::JSON_LOADED)
71
69
  end
@@ -167,7 +167,8 @@ module JSON
167
167
  @strict = false
168
168
  @max_nesting = 100
169
169
  @sort_keys = false
170
- configure(opts) if opts
170
+ @allow_duplicate_key = false
171
+ _configure(**opts) if opts
171
172
  end
172
173
 
173
174
  # This string is used to indent levels in the JSON text.
@@ -289,65 +290,48 @@ module JSON
289
290
 
290
291
  # Configure this State instance with the Hash _opts_, and return
291
292
  # itself.
292
- def configure(opts)
293
- if opts.respond_to?(:to_hash)
294
- opts = opts.to_hash
295
- elsif opts.respond_to?(:to_h)
296
- opts = opts.to_h
297
- else
298
- raise TypeError, "can't convert #{opts.class} into Hash"
299
- end
300
-
301
- if opts[:depth]&.negative?
302
- raise ArgumentError, "depth must be >= 0 (got #{opts[:depth]})"
303
- end
304
-
305
- opts.each do |key, value|
306
- instance_variable_set "@#{key}", value
293
+ def configure(options)
294
+ unless Hash == options
295
+ if options.respond_to?(:to_hash)
296
+ options = options.to_hash
297
+ elsif options.respond_to?(:to_h)
298
+ options = options.to_h
299
+ end
307
300
  end
301
+ _configure(**options)
302
+ end
303
+ alias merge configure
308
304
 
309
- # NOTE: If adding new instance variables here, check whether #generate should check them for #generate_json
310
- @indent = opts[:indent] || '' if opts.key?(:indent)
311
- @space = opts[:space] || '' if opts.key?(:space)
312
- @space_before = opts[:space_before] || '' if opts.key?(:space_before)
313
- @object_nl = opts[:object_nl] || '' if opts.key?(:object_nl)
314
- @array_nl = opts[:array_nl] || '' if opts.key?(:array_nl)
315
- @allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
316
- @as_json = opts[:as_json].to_proc if opts[:as_json]
317
- @ascii_only = opts[:ascii_only] if opts.key?(:ascii_only)
318
- self.sort_keys = opts[:sort_keys] if opts.key?(:sort_keys)
319
- @depth = opts[:depth] || 0
320
- @buffer_initial_length ||= opts[:buffer_initial_length]
321
-
322
- @script_safe = if opts.key?(:script_safe)
323
- !!opts[:script_safe]
324
- elsif opts.key?(:escape_slash)
325
- !!opts[:escape_slash]
326
- else
327
- false
305
+ private def _configure(
306
+ indent: '', space: '', space_before: '', object_nl: '', array_nl: '', allow_nan: false,
307
+ as_json: false, ascii_only: false, sort_keys: false, depth: 0, buffer_initial_length: 1024,
308
+ allow_duplicate_key: false, script_safe: false, strict: false, max_nesting: 100
309
+ )
310
+ if depth.negative?
311
+ raise ArgumentError, "depth must be >= 0 (got #{depth})"
328
312
  end
329
313
 
330
- if opts.key?(:allow_duplicate_key)
331
- @allow_duplicate_key = !!opts[:allow_duplicate_key]
332
- else
333
- @allow_duplicate_key = nil # nil is deprecation
314
+ if max_nesting && !(Integer === max_nesting)
315
+ raise TypeError, ":max_nesting must be `false` or an Integer, got: #{max_nesting.class}"
334
316
  end
335
317
 
336
- @strict = !!opts[:strict] if opts.key?(:strict)
337
-
338
- if !opts.key?(:max_nesting) # defaults to 100
339
- @max_nesting = 100
340
- elsif opts[:max_nesting]
341
- unless opts[:max_nesting].is_a?(Integer)
342
- raise TypeError, ":max_nesting must be an Integer, got: #{opts[:max_nesting].class}"
343
- end
344
- @max_nesting = opts[:max_nesting]
345
- else
346
- @max_nesting = 0
347
- end
318
+ @indent = indent || ''
319
+ @space = space || ''
320
+ @space_before = space_before || ''
321
+ @object_nl = object_nl || ''
322
+ @array_nl = array_nl || ''
323
+ @allow_nan = allow_nan || false
324
+ @as_json = as_json || false
325
+ @ascii_only = ascii_only || false
326
+ self.sort_keys = sort_keys
327
+ @depth = depth
328
+ @buffer_initial_length = buffer_initial_length
329
+ @allow_duplicate_key = allow_duplicate_key
330
+ @script_safe = script_safe
331
+ @strict = strict
332
+ @max_nesting = max_nesting || 0
348
333
  self
349
334
  end
350
- alias merge configure
351
335
 
352
336
  def allow_duplicate_key? # :nodoc:
353
337
  @allow_duplicate_key
@@ -362,10 +346,6 @@ module JSON
362
346
  result[key.to_sym] = instance_variable_get(iv)
363
347
  end
364
348
 
365
- if result[:allow_duplicate_key].nil?
366
- result.delete(:allow_duplicate_key)
367
- end
368
-
369
349
  result
370
350
  end
371
351
 
@@ -416,7 +396,7 @@ module JSON
416
396
  else
417
397
  if key_type && !@allow_duplicate_key && key_type != k.class
418
398
  key_type = nil # stop checking
419
- JSON.send(:on_mixed_keys_hash, obj, !@allow_duplicate_key.nil?)
399
+ JSON.send(:on_mixed_keys_hash, obj)
420
400
  end
421
401
  buf << ','
422
402
  end
@@ -483,28 +463,6 @@ module JSON
483
463
  end
484
464
  buf << '"'
485
465
  end
486
-
487
- # Return the value returned by method +name+.
488
- def [](name)
489
- ::JSON.deprecation_warning("JSON::State#[] is deprecated and will be removed in json 3.0.0")
490
-
491
- if respond_to?(name)
492
- __send__(name)
493
- else
494
- instance_variable_get("@#{name}") if
495
- instance_variables.include?("@#{name}".to_sym) # avoid warning
496
- end
497
- end
498
-
499
- def []=(name, value)
500
- ::JSON.deprecation_warning("JSON::State#[]= is deprecated and will be removed in json 3.0.0")
501
-
502
- if respond_to?(name_writer = "#{name}=")
503
- __send__ name_writer, value
504
- else
505
- instance_variable_set "@#{name}", value
506
- end
507
- end
508
466
  end
509
467
 
510
468
  module GeneratorMethods
@@ -569,7 +527,7 @@ module JSON
569
527
  else
570
528
  if key_type && !state.allow_duplicate_key? && key_type != key.class
571
529
  key_type = nil # stop checking
572
- JSON.send(:on_mixed_keys_hash, self, state.allow_duplicate_key? == false)
530
+ JSON.send(:on_mixed_keys_hash, self)
573
531
  end
574
532
  result << delim
575
533
  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.21.2'
4
+ VERSION = '3.0.2'
5
5
  end