Method: JSON::Pure::Generator::State#generate

Defined in:
lib/json/pure/generator.rb

#generate(obj) ⇒ Object

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.



256
257
258
259
260
261
262
263
264
265
266
# File 'lib/json/pure/generator.rb', line 256

def generate(obj)
  result = obj.to_json(self)
  unless @quirks_mode
    unless result =~ /\A\s*\[/ && result =~ /\]\s*\Z/ ||
      result =~ /\A\s*\{/ && result =~ /\}\s*\Z/
    then
      raise GeneratorError, "only generation of JSON objects or arrays allowed"
    end
  end
  result
end