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 integer returns the current depth data structure nesting in the generated JSON.
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.
This string is put at the end of a line that holds a JSON object (or Hash).
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 lib/json/truffle_ruby/generator.rb, line 113 def self.from_state(opts) case when self === opts opts when opts.respond_to?(:to_hash) new(opts.to_hash) when opts.respond_to?(:to_h) new(opts.to_h) else SAFE_STATE_PROTOTYPE.dup end end
Creates a State object from opts, which ought to be Hash to create a new State instance configured by opts, something else to create an unconfigured instance. If opts is a State object, it is just returned.
Source
# File lib/json/truffle_ruby/generator.rb, line 99 def self.generate(obj, opts = nil, io = nil) string = new(opts).generate(obj) if io io.write(string) io else string end end
Source
# File lib/json/truffle_ruby/generator.rb, line 143 def initialize(opts = nil) @indent = '' @space = '' @space_before = '' @object_nl = '' @array_nl = '' @allow_nan = false @ascii_only = false @depth = 0 @buffer_initial_length = 1024 @script_safe = false @strict = false @max_nesting = 100 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 lib/json/truffle_ruby/generator.rb, line 383 def [](name) if respond_to?(name) __send__(name) else instance_variable_get("@#{name}") if instance_variables.include?("@#{name}".to_sym) # avoid warning end end
Return the value returned by method name.
Source
# File lib/json/truffle_ruby/generator.rb, line 392 def []=(name, value) if respond_to?(name_writer = "#{name}=") __send__ name_writer, value else instance_variable_set "@#{name}", value end end
Source
# File lib/json/truffle_ruby/generator.rb, line 217 def allow_nan? @allow_nan end
Returns true if NaN, Infinity, and -Infinity should be considered as valid JSON and output.
Source
# File lib/json/truffle_ruby/generator.rb, line 223 def ascii_only? @ascii_only end
Returns true, if only ASCII characters should be generated. Otherwise returns false.
Source
# File lib/json/truffle_ruby/generator.rb, line 211 def check_circular? !@max_nesting.zero? end
Returns true, if circular data structures are checked, otherwise returns false.
Source
# File lib/json/truffle_ruby/generator.rb, line 241 def configure(opts) if opts.respond_to?(:to_hash) opts = opts.to_hash elsif opts.respond_to?(:to_h) opts = opts.to_h else raise TypeError, "can't convert #{opts.class} into Hash" end opts.each do |key, value| instance_variable_set "@#{key}", value end # NOTE: If adding new instance variables here, check whether #generate should check them for #generate_json @indent = opts[:indent] || '' if opts.key?(:indent) @space = opts[:space] || '' if opts.key?(:space) @space_before = opts[:space_before] || '' if opts.key?(:space_before) @object_nl = opts[:object_nl] || '' if opts.key?(:object_nl) @array_nl = opts[:array_nl] || '' if opts.key?(:array_nl) @allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan) @ascii_only = opts[:ascii_only] if opts.key?(:ascii_only) @depth = opts[:depth] || 0 @buffer_initial_length ||= opts[:buffer_initial_length] @script_safe = if opts.key?(:script_safe) !!opts[:script_safe] elsif opts.key?(:escape_slash) !!opts[:escape_slash] else false end @strict = !!opts[:strict] if opts.key?(:strict) if !opts.key?(:max_nesting) # defaults to 100 @max_nesting = 100 elsif opts[:max_nesting] @max_nesting = opts[:max_nesting] else @max_nesting = 0 end self end
Configure this State instance with the Hash opts, and return itself.
Source
# File lib/json/truffle_ruby/generator.rb, line 302 def generate(obj) 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 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 ) result 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 lib/json/truffle_ruby/generator.rb, line 228 def script_safe? @script_safe end
Returns true, if forward slashes are escaped. Otherwise returns false.
Source
# File lib/json/truffle_ruby/generator.rb, line 235 def strict? @strict end
Returns true, if strict mode is enabled. Otherwise returns false. Strict mode only allow serializing JSON native types: Hash, Array, String, Integer, Float, true, false and nil.
Source
# File lib/json/truffle_ruby/generator.rb, line 287 def to_h result = {} instance_variables.each do |iv| iv = iv.to_s[1..-1] result[iv.to_sym] = self[iv] end result end
Returns the configuration instance variables as a hash, that can be passed to the configure method.
Private Instance Methods
Source
# File lib/json/truffle_ruby/generator.rb, line 317 def generate_json(obj, buf) case obj when Hash buf << '{' first = true obj.each_pair do |k,v| buf << ',' unless first key_str = k.to_s if key_str.class == String fast_serialize_string(key_str, buf) elsif key_str.is_a?(String) generate_json(key_str, buf) else raise TypeError, "#{k.class}#to_s returns an instance of #{key_str.class}, expected a String" end buf << ':' generate_json(v, buf) first = false end buf << '}' when Array buf << '[' first = true obj.each do |e| buf << ',' unless first generate_json(e, buf) first = false end buf << ']' when String if obj.class == String fast_serialize_string(obj, buf) else buf << obj.to_json(self) end when Integer buf << obj.to_s else # Note: Float is handled this way since Float#to_s is slow anyway buf << obj.to_json(self) end end
Handles @allow_nan, @buffer_initial_length, other ivars must be the default value (see above)