Object
This class is used to create State instances, that are use to hold data while generating a JSON text from a Ruby data structure.
This integer returns the maximum level of data structure nesting in the generated JSON, max_nesting = 0 if no maximum is checked.
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 108 108: def self.from_state(opts) 109: case 110: when self === opts 111: opts 112: when opts.respond_to?(:to_hash) 113: new(opts.to_hash) 114: when opts.respond_to?(:to_h) 115: new(opts.to_h) 116: else 117: SAFE_STATE_PROTOTYPE.dup 118: end 119: 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.
quirks_mode: Enables quirks_mode for parser, that is for example generating single JSON values instead of documents is possible.
# File lib/json/pure/generator.rb, line 138 138: def initialize(opts = {}) 139: @indent = '' 140: @space = '' 141: @space_before = '' 142: @object_nl = '' 143: @array_nl = '' 144: @allow_nan = false 145: @ascii_only = false 146: @quirks_mode = false 147: @buffer_initial_length = 1024 148: configure opts 149: end
Return the value returned by method name.
# File lib/json/pure/generator.rb, line 269 269: def [](name) 270: __send__ name 271: end
Returns true if NaN, Infinity, and -Infinity should be considered as valid JSON and output.
# File lib/json/pure/generator.rb, line 205 205: def allow_nan? 206: @allow_nan 207: end
Returns true, if only ASCII characters should be generated. Otherwise returns false.
# File lib/json/pure/generator.rb, line 211 211: def ascii_only? 212: @ascii_only 213: end
Returns true, if circular data structures are checked, otherwise returns false.
# File lib/json/pure/generator.rb, line 199 199: def check_circular? 200: !@max_nesting.zero? 201: end
Configure this State instance with the Hash opts, and return itself.
# File lib/json/pure/generator.rb, line 222 222: def configure(opts) 223: @indent = opts[:indent] if opts.key?(:indent) 224: @space = opts[:space] if opts.key?(:space) 225: @space_before = opts[:space_before] if opts.key?(:space_before) 226: @object_nl = opts[:object_nl] if opts.key?(:object_nl) 227: @array_nl = opts[:array_nl] if opts.key?(:array_nl) 228: @allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan) 229: @ascii_only = opts[:ascii_only] if opts.key?(:ascii_only) 230: @depth = opts[:depth] || 0 231: @quirks_mode = opts[:quirks_mode] if opts.key?(:quirks_mode) 232: if !opts.key?(:max_nesting) # defaults to 19 233: @max_nesting = 19 234: elsif opts[:max_nesting] 235: @max_nesting = opts[:max_nesting] 236: else 237: @max_nesting = 0 238: end 239: self 240: 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.
# File lib/json/pure/generator.rb, line 256 256: def generate(obj) 257: result = obj.to_json(self) 258: unless @quirks_mode 259: unless result =~ /\A\s*\[/ && result =~ /\]\s*\Z/ || 260: result =~ /\A\s*\{/ && result =~ /\}\s*\Z/ 261: then 262: raise GeneratorError, "only generation of JSON objects or arrays allowed" 263: end 264: end 265: result 266: end
Returns true, if quirks mode is enabled. Otherwise returns false.
# File lib/json/pure/generator.rb, line 216 216: def quirks_mode? 217: @quirks_mode 218: end
Returns the configuration instance variables as a hash, that can be passed to the configure method.
# File lib/json/pure/generator.rb, line 245 245: def to_h 246: result = {} 247: for iv in ]indent space space_before object_nl array_nl allow_nan max_nesting ascii_only quirks_mode buffer_initial_length depth] 248: result[iv.intern] = instance_variable_get("@#{iv}") 249: end 250: result 251: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.