Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Dan http://www.dan-gottlieb.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Quick and Dirty JSON Serializer

// quick and dirty JOSN serializer

   1  
   2  def jsonize(d)
   3    def surround(d, i=0); '' << '"[{'[i] << d << '"]}'[i]; end
   4    alias j jsonize; alias s surround
   5    if d.kind_of?(String) then s(d)
   6    elsif d.kind_of?(Symbol) then s(d.to_s)
   7    elsif d.kind_of?(Array) then s(d.collect {|v| j(v)}.join(', '), 1)
   8    elsif d.kind_of?(Hash) then s(d.to_a.collect { |v| "#{j(v[0])} : #{j(v[1])}" }.join(', '),2)
   9    # add support for other types of objects here if needed...
  10    else d.to_s; end
  11  end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS