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