Skip to content

Instantly share code, notes, and snippets.

@brianmario
Created April 29, 2009 18:04
Show Gist options
  • Save brianmario/103929 to your computer and use it in GitHub Desktop.
Save brianmario/103929 to your computer and use it in GitHub Desktop.
module Yajl
module Stream
def self.encode(obj)
case obj.class.name
when "Hash"
val = "{"
val << obj.keys.map do |key|
"\"#{key}\": #{encode(obj[key])}"
end * ", "
val << "}"
when "Array"
"[#{obj.map{|val| encode(val)} * ', '}]"
when "NilClass"
"null"
when "TrueClass", "FalseClass", "Fixnum", "Float"
obj.to_s
else
"\"#{obj.to_s}\""
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment