class JSON::Pure::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

array_nl[RW]

This string is put at the end of a line that holds a JSON array.

depth[RW]

This integer returns the current depth data structure nesting in the generated JSON.

indent[RW]

This string is used to indent levels in the JSON text.

max_nesting[RW]

This integer returns the maximum level of data structure nesting in the generated JSON, #max_nesting = 0 if no maximum is checked.

object_nl[RW]

This string is put at the end of a line that holds a JSON object (or Hash).

quirks_mode[RW]

If this attribute is set to true, quirks mode is enabled, otherwise it's disabled.

space[RW]

This string is used to insert a space between the tokens in a JSON string.

space_before[RW]

This string is used to insert a space before the ':' in JSON objects.

Public Class Methods

from_state(opts) click to toggle source

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.

# File lib/json/pure/generator.rb, line 108
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
new(opts = {}) click to toggle source

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 JSON object (default: ''),

  • array_nl: a string that is put at the end of a JSON array (default: ''),

  • 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.

  • quirks_mode: Enables #quirks_mode for parser, that is for example generating single JSON values instead of documents is possible.

# File lib/json/pure/generator.rb, line 138
def initialize(opts = {})
  @indent                = ''
  @space                 = ''
  @space_before          = ''
  @object_nl             = ''
  @array_nl              = ''
  @allow_nan             = false
  @ascii_only            = false
  @quirks_mode           = false
  @buffer_initial_length = 1024
  configure opts
end

Public Instance Methods

[](name) click to toggle source

Return the value returned by method name.

# File lib/json/pure/generator.rb, line 265
def [](name)
  __send__ name
end
allow_nan?() click to toggle source

Returns true if NaN, Infinity, and -Infinity should be considered as valid JSON and output.

# File lib/json/pure/generator.rb, line 205
def allow_nan?
  @allow_nan
end
ascii_only?() click to toggle source

Returns true, if only ASCII characters should be generated. Otherwise returns false.

# File lib/json/pure/generator.rb, line 211
def ascii_only?
  @ascii_only
end
check_circular?() click to toggle source

Returns true, if circular data structures are checked, otherwise returns false.

# File lib/json/pure/generator.rb, line 199
def check_circular?
  !@max_nesting.zero?
end
configure(opts) click to toggle source

Configure this State instance with the Hash opts, and return itself.

# File lib/json/pure/generator.rb, line 222
def configure(opts)
  @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
  @quirks_mode    = opts[:quirks_mode] if opts.key?(:quirks_mode)
  if !opts.key?(:max_nesting) # defaults to 19
    @max_nesting = 19
  elsif opts[:max_nesting]
    @max_nesting = opts[:max_nesting]
  else
    @max_nesting = 0
  end
  self
end
Also aliased as: merge
generate(obj) click to toggle source

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.

# File lib/json/pure/generator.rb, line 256
def generate(obj)
  result = obj.to_json(self)
  if !@quirks_mode && result !~ /\A\s*(?:\[.*\]|\{.*\})\s*\Z/
    raise GeneratorError, "only generation of JSON objects or arrays allowed"
  end
  result
end
merge(opts)
Alias for: configure
quirks_mode?() click to toggle source

Returns true, if quirks mode is enabled. Otherwise returns false.

# File lib/json/pure/generator.rb, line 216
def quirks_mode?
  @quirks_mode
end
to_h() click to toggle source

Returns the configuration instance variables as a hash, that can be passed to the configure method.

# File lib/json/pure/generator.rb, line 245
def to_h
  result = {}
  for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting ascii_only quirks_mode buffer_initial_length depth]
    result[iv.intern] = instance_variable_get("@#{iv}")
  end
  result
end