class Hash
Public Instance Methods
from_json(lexer)
click to toggle source
This method will parse a JSON
object from the passed lexer object. It takes a lexer object which is about to read a JSON
object. It raises a runtime error otherwise. It returns the original JSON
object. This method probably shouldn’t be used directly.
Parameters¶ ↑
# File lib/json/objects.rb, line 168 def from_json(lexer) lexer.unescape if (lexer.nextclean == '%') lexer.back raise "A JSON Object must begin with '{'" if (lexer.nextclean != "{") loop { c = lexer.nextclean key = nil case c when '\0' raise "A JSON Object must end with '}'" when '}' return (self); else lexer.back key = lexer.nextvalue().to_s() end raise "Expected a ':' after a key" if (lexer.nextclean() != ':') self[key] = lexer.nextvalue() case lexer.nextclean() when ',' return(self) if (lexer.nextclean() == '}') lexer.back when '}' return(self) else raise "Expected a ',' or '}'" end } return(self) end
to_json()
click to toggle source
This method will serialize the hash into regular JSON
format.
# File lib/json/objects.rb, line 147 def to_json retval = "{" first = true self.each {|key, val| retval << "," unless first key = key.to_s #keys in json hashes need to be strings, nothing else. retval << key.to_json + ":" retval << val.to_json first = false } retval << "}" return(retval) end