Over a million developers have joined DZone.

Quick And Dirty JSON Serializer

DZone's Guide to

Quick And Dirty JSON Serializer

· ·
Free Resource
// quick and dirty JOSN serializer

def jsonize(d)
  def surround(d, i=0); '' << '"[{'[i] << d << '"]}'[i]; end
  alias j jsonize; alias s surround
  if d.kind_of?(String) then s(d)
  elsif d.kind_of?(Symbol) then s(d.to_s)
  elsif d.kind_of?(Array) then s(d.collect {|v| j(v)}.join(', '), 1)
  elsif d.kind_of?(Hash) then s(d.to_a.collect { |v| "#{j(v[0])} : #{j(v[1])}" }.join(', '),2)
  # add support for other types of objects here if needed...
  else d.to_s; end

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}