Class JSON::Pure::Generator::State
In: lib/json/pure/generator.rb
Parent: Object
ParserError NestingError GeneratorError CircularDatastructure StandardError JSONError MissingUnicodeSupport StringScanner Parser Gtk State lib/json/common.rb Ext lib/json/pure/parser.rb lib/json/pure/generator.rb Integer FalseClass Array Hash Float NilClass Object TrueClass Extend String GeneratorMethods Generator Pure Editor JSON dot/m_9_0.png

This class is used to create State instances, that are use to hold data while generating a JSON text from a a Ruby data structure.

Methods

Attributes

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.

Public Class methods

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/pure/generator.rb, line 71
71:         def self.from_state(opts)
72:           case opts
73:           when self
74:             opts
75:           when Hash
76:             new(opts)
77:           else
78:             new
79:           end
80:         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 JSON object (default: ’’),
  • array_nl: a string that is put at the end of a JSON array (default: ’’),
  • check_circular: true if checking for circular data structures should be done (the default), false otherwise.
  • check_circular: true if checking for circular data structures should be done, false (the default) otherwise.
  • 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.

[Source]

     # File lib/json/pure/generator.rb, line 98
 98:         def initialize(opts = {})
 99:           @seen = {}
100:           @indent         = ''
101:           @space          = ''
102:           @space_before   = ''
103:           @object_nl      = ''
104:           @array_nl       = ''
105:           @check_circular = true
106:           @allow_nan      = false
107:           configure opts
108:         end

Public Instance methods

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

[Source]

     # File lib/json/pure/generator.rb, line 146
146:         def allow_nan?
147:           @allow_nan
148:         end

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

[Source]

     # File lib/json/pure/generator.rb, line 140
140:         def check_circular?
141:           @check_circular
142:         end

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

[Source]

     # File lib/json/pure/generator.rb, line 169
169:         def configure(opts)
170:           @indent         = opts[:indent] if opts.key?(:indent)
171:           @space          = opts[:space] if opts.key?(:space)
172:           @space_before   = opts[:space_before] if opts.key?(:space_before)
173:           @object_nl      = opts[:object_nl] if opts.key?(:object_nl)
174:           @array_nl       = opts[:array_nl] if opts.key?(:array_nl)
175:           @check_circular = !!opts[:check_circular] if opts.key?(:check_circular)
176:           @allow_nan      = !!opts[:allow_nan] if opts.key?(:allow_nan)
177:           if !opts.key?(:max_nesting) # defaults to 19
178:             @max_nesting = 19
179:           elsif opts[:max_nesting]
180:             @max_nesting = opts[:max_nesting]
181:           else
182:             @max_nesting = 0
183:           end
184:           self
185:         end

Forget object for this generating run.

[Source]

     # File lib/json/pure/generator.rb, line 163
163:         def forget(object)
164:           @seen.delete object.__id__
165:         end

Remember object, to find out if it was already encountered (if a cyclic data structure is if a cyclic data structure is rendered).

[Source]

     # File lib/json/pure/generator.rb, line 158
158:         def remember(object)
159:           @seen[object.__id__] = true
160:         end

Returns true, if object was already seen during this generating run.

[Source]

     # File lib/json/pure/generator.rb, line 152
152:         def seen?(object)
153:           @seen.key?(object.__id__)
154:         end

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

[Source]

     # File lib/json/pure/generator.rb, line 189
189:         def to_h
190:           result = {}
191:           for iv in %w[indent space space_before object_nl array_nl check_circular allow_nan max_nesting]
192:             result[iv.intern] = instance_variable_get("@#{iv}")
193:           end
194:           result
195:         end

[Validate]