| 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 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:
# 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
Returns true, if circular data structures should be checked, otherwise returns false.
# 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.
# 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.
# 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).
# 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.
# 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.
# 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