From: Michael Guterl Date: 2008-09-07T12:30:24+09:00 Subject: Re: json for extjs (without rails) On Sat, Sep 6, 2008 at 11:06 PM, Gurpal 2000 wrote: > Ara Howard wrote: >> On Sep 6, 2008, at 6:52 PM, Gurpal 2000 wrote: >> >>> class car >>> into >>> an EXTJS grid. >>> >>> Thanks >> >> >> gem install json >> ri json >> >> a @ http://codeforpeople.com/ > > an example would actually be helpful. i already looked at the rdoc, i'm > a ruby beginner. No example anywhere on google either... There seem to > be examples to parse and unparse but those are simple one liners with no > solid object conversions. > There's an example of serializing a custom class (Range) on http://json.rubyforge.org/ Here's something based off of that: require 'rubygems' require 'json' class Car attr_reader :make, :color def initialize(make, color) @make, @color = make, color end def to_json(*a) { 'json_class' => self.class.name, 'data' => { :make => @make, :color => @color } }.to_json(*a) end def self.json_create(o) new(*o['data'].values) end end car = JSON.parse(Car.new('VW', 'White').to_json) puts car.make puts car.color puts car.to_json