Class: JSON5::Limits

Inherits:
Object
  • Object
show all
Defined in:
lib/json5/limits.rb

Constant Summary collapse

ATTRIBUTES =
%i[
  max_input_bytes
  max_depth
  max_string_code_units
  max_number_bytes
  max_comment_bytes
  max_container_entries
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_input_bytes: 64 * 1024 * 1024, max_depth: 512, max_string_code_units: 16 * 1024 * 1024, max_number_bytes: 1 * 1024 * 1024, max_comment_bytes: 16 * 1024 * 1024, max_container_entries: 10_000_000) ⇒ Limits

Returns a new instance of Limits.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/json5/limits.rb', line 16

def initialize(max_input_bytes: 64 * 1024 * 1024, max_depth: 512,
               max_string_code_units: 16 * 1024 * 1024,
               max_number_bytes: 1 * 1024 * 1024,
               max_comment_bytes: 16 * 1024 * 1024,
               max_container_entries: 10_000_000)
  values = {
    max_input_bytes: max_input_bytes,
    max_depth: max_depth,
    max_string_code_units: max_string_code_units,
    max_number_bytes: max_number_bytes,
    max_comment_bytes: max_comment_bytes,
    max_container_entries: max_container_entries
  }
  values.each do |name, value|
    next if value.nil?
    unless value.is_a?(Integer) && value >= 0
      raise ArgumentError, "#{name} must be non-negative"
    end
  end
  values.each { |name, value| instance_variable_set("@#{name}", value) }
  freeze
end

Class Method Details

.defaultObject



39
40
41
# File 'lib/json5/limits.rb', line 39

def self.default
  @default ||= new
end

.unboundedObject



43
44
45
# File 'lib/json5/limits.rb', line 43

def self.unbounded
  @unbounded ||= new(**ATTRIBUTES.to_h { |name| [name, nil] })
end