| Class | JSON::Pure::Generator::State |
| In: |
lib/json/pure/generator.rb
|
| Parent: | Object |
| array_nl | [RW] | This string is put at the end of a line that holds a JSON array. |
| 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). |
| 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. |
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 71 def self.from_state(opts) case opts when self opts when Hash new(opts) else new end end
Instantiates a new State object, configured by opts.
opts can have the following keys:
# File lib/json/pure/generator.rb, line 98 def initialize(opts = {}) @seen = {} @indent = '' @space = '' @space_before = '' @object_nl = '' @array_nl = '' @check_circular = true @allow_nan = false configure opts end
Returns true, if circular data structures should be checked, otherwise returns false.
# File lib/json/pure/generator.rb, line 140 def check_circular? @check_circular end
Configure this State instance with the Hash opts, and return itself.
# File lib/json/pure/generator.rb, line 169 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) @check_circular = !!opts[:check_circular] if opts.key?(:check_circular) @allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan) 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
Forget object for this generating run.
# File lib/json/pure/generator.rb, line 163 def forget(object) @seen.delete object.__id__ end
Remember object, to find out if it was already encountered (if a cyclic data structure is if a cyclic data structure is rendered).
# File lib/json/pure/generator.rb, line 158 def remember(object) @seen[object.__id__] = true end
Returns true, if object was already seen during this generating run.
# File lib/json/pure/generator.rb, line 152 def seen?(object) @seen.key?(object.__id__) end
Returns the configuration instance variables as a hash, that can be passed to the configure method.
# File lib/json/pure/generator.rb, line 189 def to_h result = {} for iv in %w[indent space space_before object_nl array_nl check_circular allow_nan max_nesting] result[iv.intern] = instance_variable_get("@#{iv}") end result end