Class: Hash

Inherits:
Object show all
Defined in:
lib/json/objects.rb

Direct Known Subclasses

Amazon::Coral::QueryStringMap

Instance Method Summary collapse

Instance Method Details

#from_json(lexer) ⇒ Object

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


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# 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_jsonObject

This method will serialize the hash into regular JSON format.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# 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