| 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 96
96: def self.from_state(opts)
97: case opts
98: when self
99: opts
100: when Hash
101: new(opts)
102: else
103: new
104: end
105: end
Instantiates a new State object, configured by opts.
opts can have the following keys:
# File lib/json/pure/generator.rb, line 123
123: def initialize(opts = {})
124: @seen = {}
125: @indent = ''
126: @space = ''
127: @space_before = ''
128: @object_nl = ''
129: @array_nl = ''
130: @check_circular = true
131: @allow_nan = false
132: configure opts
133: end
Returns true, if circular data structures should be checked, otherwise returns false.
# File lib/json/pure/generator.rb, line 165
165: def check_circular?
166: @check_circular
167: end
Configure this State instance with the Hash opts, and return itself.
# File lib/json/pure/generator.rb, line 194
194: def configure(opts)
195: @indent = opts[:indent] if opts.key?(:indent)
196: @space = opts[:space] if opts.key?(:space)
197: @space_before = opts[:space_before] if opts.key?(:space_before)
198: @object_nl = opts[:object_nl] if opts.key?(:object_nl)
199: @array_nl = opts[:array_nl] if opts.key?(:array_nl)
200: @check_circular = !!opts[:check_circular] if opts.key?(:check_circular)
201: @allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
202: if !opts.key?(:max_nesting) # defaults to 19
203: @max_nesting = 19
204: elsif opts[:max_nesting]
205: @max_nesting = opts[:max_nesting]
206: else
207: @max_nesting = 0
208: end
209: self
210: end
Forget object for this generating run.
# File lib/json/pure/generator.rb, line 188
188: def forget(object)
189: @seen.delete object.__id__
190: 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 183
183: def remember(object)
184: @seen[object.__id__] = true
185: end
Returns true, if object was already seen during this generating run.
# File lib/json/pure/generator.rb, line 177
177: def seen?(object)
178: @seen.key?(object.__id__)
179: end
Returns the configuration instance variables as a hash, that can be passed to the configure method.
# File lib/json/pure/generator.rb, line 214
214: def to_h
215: result = {}
216: for iv in %w[indent space space_before object_nl array_nl check_circular allow_nan max_nesting]
217: result[iv.intern] = instance_variable_get("@#{iv}")
218: end
219: result
220: end