Class JSON::Pure::Generator::State
In: lib/json/pure/generator.rb
Parent: Object
JSONError GeneratorError ParserError MissingUnicodeSupport 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.
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).
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 108
108:         def self.from_state(opts)
109:           case opts
110:           when self
111:             opts
112:           when Hash
113:             new(opts)
114:           else
115:             SAFE_STATE_PROTOTYPE.dup
116:           end
117:         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: 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.

[Source]

     # File lib/json/pure/generator.rb, line 134
134:         def initialize(opts = {})
135:           @indent         = ''
136:           @space          = ''
137:           @space_before   = ''
138:           @object_nl      = ''
139:           @array_nl       = ''
140:           @allow_nan      = false
141:           @ascii_only     = false
142:           configure opts
143:         end

Public Instance methods

Return the value returned by method name.

[Source]

     # File lib/json/pure/generator.rb, line 236
236:         def [](name)
237:           __send__ name
238:         end

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

[Source]

     # File lib/json/pure/generator.rb, line 185
185:         def allow_nan?
186:           @allow_nan
187:         end

[Source]

     # File lib/json/pure/generator.rb, line 189
189:         def ascii_only?
190:           @ascii_only
191:         end

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

[Source]

     # File lib/json/pure/generator.rb, line 179
179:         def check_circular?
180:           !@max_nesting.zero?
181:         end

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

[Source]

     # File lib/json/pure/generator.rb, line 195
195:         def configure(opts)
196:           @indent         = opts[:indent] if opts.key?(:indent)
197:           @space          = opts[:space] if opts.key?(:space)
198:           @space_before   = opts[:space_before] if opts.key?(:space_before)
199:           @object_nl      = opts[:object_nl] if opts.key?(:object_nl)
200:           @array_nl       = opts[:array_nl] if opts.key?(:array_nl)
201:           @allow_nan      = !!opts[:allow_nan] if opts.key?(:allow_nan)
202:           @ascii_only     = opts[:ascii_only] if opts.key?(:ascii_only)
203:           @depth          = opts[:depth] || 0
204:           if !opts.key?(:max_nesting) # defaults to 19
205:             @max_nesting = 19
206:           elsif opts[:max_nesting]
207:             @max_nesting = opts[:max_nesting]
208:           else
209:             @max_nesting = 0
210:           end
211:           self
212:         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/pure/generator.rb, line 227
227:         def generate(obj)
228:           result = obj.to_json(self)
229:           if result !~ /\A\s*(?:\[.*\]|\{.*\})\s*\Z/m
230:             raise GeneratorError, "only generation of JSON objects or arrays allowed"
231:           end
232:           result
233:         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 216
216:         def to_h
217:           result = {}
218:           for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting ascii_only depth]
219:             result[iv.intern] = instance_variable_get("@#{iv}")
220:           end
221:           result
222:         end

[Validate]