Class JSON::Pure::Generator::State
In: lib/json/pure/generator.rb
Parent: Object
JSONError GeneratorError ParserError MissingUnicodeSupport CircularDatastructure NestingError StandardError Gtk StringScanner Parser State lib/json/common.rb Ext Editor lib/json/pure/parser.rb lib/json/pure/generator.rb Object Integer FalseClass Array Hash Float NilClass TrueClass Extend String GeneratorMethods Generator Pure JSON dot/m_9_3.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 95
 95:         def self.from_state(opts)
 96:           case opts
 97:           when self
 98:             opts
 99:           when Hash
100:             new(opts)
101:           else
102:             new
103:           end
104:         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 122
122:         def initialize(opts = {})
123:           @seen = {}
124:           @indent         = ''
125:           @space          = ''
126:           @space_before   = ''
127:           @object_nl      = ''
128:           @array_nl       = ''
129:           @check_circular = true
130:           @allow_nan      = false
131:           configure opts
132:         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 170
170:         def allow_nan?
171:           @allow_nan
172:         end

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

[Source]

     # File lib/json/pure/generator.rb, line 164
164:         def check_circular?
165:           @check_circular
166:         end

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

[Source]

     # File lib/json/pure/generator.rb, line 193
193:         def configure(opts)
194:           @indent         = opts[:indent] if opts.key?(:indent)
195:           @space          = opts[:space] if opts.key?(:space)
196:           @space_before   = opts[:space_before] if opts.key?(:space_before)
197:           @object_nl      = opts[:object_nl] if opts.key?(:object_nl)
198:           @array_nl       = opts[:array_nl] if opts.key?(:array_nl)
199:           @check_circular = !!opts[:check_circular] if opts.key?(:check_circular)
200:           @allow_nan      = !!opts[:allow_nan] if opts.key?(:allow_nan)
201:           if !opts.key?(:max_nesting) # defaults to 19
202:             @max_nesting = 19
203:           elsif opts[:max_nesting]
204:             @max_nesting = opts[:max_nesting]
205:           else
206:             @max_nesting = 0
207:           end
208:           self
209:         end

Forget object for this generating run.

[Source]

     # File lib/json/pure/generator.rb, line 187
187:         def forget(object)
188:           @seen.delete object.__id__
189:         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 182
182:         def remember(object)
183:           @seen[object.__id__] = true
184:         end

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

[Source]

     # File lib/json/pure/generator.rb, line 176
176:         def seen?(object)
177:           @seen.key?(object.__id__)
178:         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 213
213:         def to_h
214:           result = {}
215:           for iv in %w[indent space space_before object_nl array_nl check_circular allow_nan max_nesting]
216:             result[iv.intern] = instance_variable_get("@#{iv}")
217:           end
218:           result
219:         end

[Validate]