class JSON::TruffleRuby::Generator::State
This class is used to create State instances, that are use to hold data while generating a JSON text from a Ruby data structure.
Attributes
This string is put at the end of a line that holds a JSON array.
This proc converts unsupported types into native JSON types.
This string is used to indent levels in the JSON text.
This integer returns the maximum level of data structure nesting in the generated JSON, max_nesting = 0 if no maximum is checked.
Controls whether RFC8785 (canonicalization) mode is enabled. Otherwise returns false.
If this attribute is set to true, forward slashes will be escaped in all json strings.
This string is used to insert a space between the tokens in a JSON string.
This string is used to insert a space before the ‘:’ in JSON objects.
If this attribute is set to true, attempting to serialize types not supported by the JSON spec will raise a JSON::GeneratorError
Public Class Methods
Source
# File vendor/bundle/ruby/4.0.0/gems/json-3.1.0/lib/json/truffle_ruby/generator.rb, line 126 def self.from_state(opts) if opts case when self === opts return opts.validate_rfc8785 when opts.respond_to?(:to_hash) return new(opts.to_hash) when opts.respond_to?(:to_h) return new(opts.to_h) end end new end
# File vendor/bundle/ruby/4.0.0/gems/json-3.1.0/lib/json/truffle_ruby/generator.rb, line 118 def self.generate(obj, opts = nil, io = nil) new(opts).generate(obj, io) end
Source
# File vendor/bundle/ruby/4.0.0/gems/json-3.1.0/lib/json/truffle_ruby/generator.rb, line 157 def initialize(opts = nil) @indent = '' @space = '' @space_before = '' @object_nl = '' @array_nl = '' @allow_nan = false @ascii_only = false @as_json = false @depth = 0 @buffer_initial_length = 1024 @script_safe = false @strict = false @max_nesting = 100 @sort_keys = false @allow_duplicate_key = false @rfc8785 = false _configure(**opts) if opts end
Instantiates a new State object, configured by opts.
opts can have the following keys:
-
indent: a string used to indent levels (default: ”),
-
space: a string that is put after, a : or , delimiter (default: ”),
-
space_before: a string that is put before a : pair delimiter (default: ”),
-
object_nl: a string that is put at the end of a
JSONobject (default: ”), -
array_nl: a string that is put at the end of a
JSONarray (default: ”), -
script_safe: true if U+2028, U+2029 and forward slash (/) should be escaped as to make the
JSONobject safe to interpolate in a script tag (default: false). -
check_circular: is deprecated now, use the :max_nesting option instead,
-
max_nesting: sets the maximum level of data structure nesting in the generated
JSON,max_nesting= 0 if no maximum should be checked. -
allow_nan: true if NaN, Infinity, and -Infinity should be generated, otherwise an exception is thrown, if these values are encountered. This options defaults to false.
Public Instance Methods
Source
# File vendor/bundle/ruby/4.0.0/gems/json-3.1.0/lib/json/truffle_ruby/generator.rb, line 284 def allow_nan? @allow_nan end
Returns true if NaN, Infinity, and -Infinity should be considered as valid JSON and output.
Source
# File vendor/bundle/ruby/4.0.0/gems/json-3.1.0/lib/json/truffle_ruby/generator.rb, line 197 def as_json=(value) raise FrozenError, "can't modify frozen #{self.class}: #{inspect}" if frozen? value = value.to_proc if !(Proc === value) && value.respond_to?(:to_proc) unless Proc === value raise TypeError, "as_json must be a Proc" end @as_json = value end
Source
# File vendor/bundle/ruby/4.0.0/gems/json-3.1.0/lib/json/truffle_ruby/generator.rb, line 290 def ascii_only? @ascii_only end
Returns true, if only ASCII characters should be generated. Otherwise returns false.
Source
# File vendor/bundle/ruby/4.0.0/gems/json-3.1.0/lib/json/truffle_ruby/generator.rb, line 278 def check_circular? !@max_nesting.zero? end
Returns true, if circular data structures are checked, otherwise returns false.
Source
# File vendor/bundle/ruby/4.0.0/gems/json-3.1.0/lib/json/truffle_ruby/generator.rb, line 329 def configure(options) unless Hash == options if options.respond_to?(:to_hash) options = options.to_hash elsif options.respond_to?(:to_h) options = options.to_h end end _configure(**options) end
Source
# File vendor/bundle/ruby/4.0.0/gems/json-3.1.0/lib/json/truffle_ruby/generator.rb, line 262 def depth=(depth) if depth.negative? raise ArgumentError, "depth must be >= 0 (got #{depth})" end @depth = depth end
Source
# File vendor/bundle/ruby/4.0.0/gems/json-3.1.0/lib/json/truffle_ruby/generator.rb, line 414 def generate(obj, anIO = nil) return dup.generate(obj, anIO) if frozen? depth = @depth validate_rfc8785 if @indent.empty? and @space.empty? and @space_before.empty? and @object_nl.empty? and @array_nl.empty? and !@ascii_only and !@script_safe and @max_nesting == 0 and (!@strict || Symbol === obj) and !@sort_keys result = generate_json(obj, ''.dup) else result = obj.to_json(self) end JSON::TruffleRuby::Generator.valid_utf8?(result) or raise GeneratorError.new( "source sequence #{result.inspect} is illegal/malformed utf-8", obj ) if anIO anIO.write(result) anIO else result end ensure @depth = depth unless frozen? end
Generates a valid JSON document from object obj and returns the result. If no valid JSON document can be created this method raises a GeneratorError exception.
Source
# File vendor/bundle/ruby/4.0.0/gems/json-3.1.0/lib/json/truffle_ruby/generator.rb, line 307 def rfc8785? @rfc8785 end
Returns true, if RFC8785 (canonicalization) mode is enabled. Otherwise returns false.
Source
# File vendor/bundle/ruby/4.0.0/gems/json-3.1.0/lib/json/truffle_ruby/generator.rb, line 295 def script_safe? @script_safe end
Returns true, if forward slashes are escaped. Otherwise returns false.
Source
# File vendor/bundle/ruby/4.0.0/gems/json-3.1.0/lib/json/truffle_ruby/generator.rb, line 302 def strict? @strict end
Source
# File vendor/bundle/ruby/4.0.0/gems/json-3.1.0/lib/json/truffle_ruby/generator.rb, line 387 def to_h { indent: @indent, space: @space, space_before: @space_before, object_nl: @object_nl, array_nl: @array_nl, as_json: @as_json, allow_nan: @allow_nan, ascii_only: @ascii_only, max_nesting: @max_nesting, script_safe: @script_safe, strict: @strict, depth: @depth, buffer_initial_length: @buffer_initial_length, sort_keys: @sort_keys, allow_duplicate_key: @allow_duplicate_key, rfc8785: @rfc8785, } end
Returns the configuration instance variables as a hash, that can be passed to the configure method.