Json for extjs (without rails)

Hi

I trying to generate some simple json for extjs use without rails.

Now say if i have a array of MyObject. Do i have to define my own
to_json?

Is there a library that would say convert an array of objects to json
WITH the param names?

class car

registration

make

color

end

so the json should say something like

{ registration: “blah”, make: “sdsdd”, color: “oikooi” }

and if it’s an array the json should look like something i can feed into
an EXTJS grid.

Thanks

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/

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.

thanks

On Sat, Sep 6, 2008 at 11:06 PM, Gurpal 2000 [email protected] wrote:

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

On Sep 6, 2008, at 9:06 PM, Gurpal 2000 wrote:

gem install json

thanks

Posted via http://www.ruby-forum.com/.

cfp:~ > cat a.rb

i prefer the pure-ruby version of json

require ‘rubygems’
require ‘json’ # gem install json_pure OR gem install json

it’s easiest to just use mixtures of hash, arrays, strings, and

numbers

because this translates 1-1 with javascript

hash = { ‘key’ => 42 }

hash2 = { ‘another key’ => 42.0 }

data = [
hash, hash2
]

puts data.to_json
puts

even if you defined your own classes i personally prefer to

translate to

‘simple’ javascript structures

class C
def initialize a, b
@a, @b = a, b
end

 def to_json
   { 'a' => @a, 'b' => @b }.to_json
 end

end

puts C.new(4, 2).to_json

cfp:~ > ruby a.rb
[{“key”:42},{“another key”:42.0}]

{“a”:4,“b”:2}

a @ http://codeforpeople.com/

On Sat, Sep 6, 2008 at 11:36 PM, Michael G. [email protected]
wrote:

There’s an example of serializing a custom class (Range) on
@make, @color = make, color
end

def self.json_create(o)
new(*o[‘data’].values)
I just realized this code is flawed due to the nature of unordered
hashes in ruby 1.8, but you get the idea…

ara.t.howard wrote:

i prefer the pure-ruby version of json

Why’s that?

even if you defined your own classes i personally prefer to translate to

‘simple’ javascript structures

Gives up the possibility reverse translation though… but I think I
agree that building everything out of the 4 native types is best.

On Sep 6, 2008, at 11:36 PM, Joel VanderWerf wrote:

ara.t.howard wrote:

i prefer the pure-ruby version of json

Why’s that?

simply because it’s easy to drop in a rails project’s lib dir and
forget about it - also because it’s simple to hack if needed.

even if you defined your own classes i personally prefer to

translate to

‘simple’ javascript structures

Gives up the possibility reverse translation though… but I think I
agree that building everything out of the 4 native types is best.

yeah, it’s so easy to unpack a simple data structure in js into
objects anyhow and, best of all, you can always read simple json.

cheers.

a @ http://codeforpeople.com/

Ara Howard wrote:

On Sep 6, 2008, at 9:06 PM, Gurpal 2000 wrote:

gem install json

thanks

Posted via http://www.ruby-forum.com/.

cfp:~ > cat a.rb

i prefer the pure-ruby version of json

require ‘rubygems’
require ‘json’ # gem install json_pure OR gem install json

it’s easiest to just use mixtures of hash, arrays, strings, and

numbers

because this translates 1-1 with javascript

hash = { ‘key’ => 42 }

hash2 = { ‘another key’ => 42.0 }

data = [
hash, hash2
]

puts data.to_json
puts

even if you defined your own classes i personally prefer to

translate to

‘simple’ javascript structures

class C
def initialize a, b
@a, @b = a, b
end

 def to_json
   { 'a' => @a, 'b' => @b }.to_json
 end

end

puts C.new(4, 2).to_json

cfp:~ > ruby a.rb
[{“key”:42},{“another key”:42.0}]

{“a”:4,“b”:2}

a @ http://codeforpeople.com/

I have another question. If I do something like this:

mydata = [ C.new(4, 2).to_json, C.new(1, 3).to_json, C.new(5, 6).to_json
]
myhash = { ‘data’ => mydata }
puts myhash.to_json

I see that the output will double escape the values (they are actually
strings with all sorts of characters in my code).

I guess what i need is the to_json on the hash to call to_json in each
element of the array. What’s the ruby-esque way to do this? I could do
it in long code but doesn’t feel right. This should easy right?

Thanks

On Sep 14, 2008, at 4:19 AM, Gurpal 2000 wrote:

I have another question. If I do something like this:

mydata = [ C.new(4, 2).to_json, C.new(1, 3).to_json, C.new(5,
6).to_json
]
myhash = { ‘data’ => mydata }
puts myhash.to_json

mydata = [ C.new(4, 2), C.new(1, 3), C.new(5, 6) ]
myhash = { ‘data’ => mydata }.to_json

a @ http://codeforpeople.com/

Ara Howard wrote:

On Sep 14, 2008, at 4:19 AM, Gurpal 2000 wrote:

I have another question. If I do something like this:

mydata = [ C.new(4, 2).to_json, C.new(1, 3).to_json, C.new(5,
6).to_json
]
myhash = { ‘data’ => mydata }
puts myhash.to_json

mydata = [ C.new(4, 2), C.new(1, 3), C.new(5, 6) ]
myhash = { ‘data’ => mydata }.to_json

a @ http://codeforpeople.com/

The funny thing is i’ve been trying that and it keeps on crapping out:

U:/ruby/lib/ruby/gems/1.8/gems/json_pure-1.1.3/lib/json/pure/generator.rb:300:in
to_json': wrong number of arguments (2 for 0) (ArgumentError) from U:/ruby/lib/ruby/gems/1.8/gems/json_pure-1.1.3/lib/json/pure/generator.rb:300:in json_transform’
from
U:/ruby/lib/ruby/gems/1.8/gems/json_pure-1.1.3/lib/json/pure/generator.rb:299:in
map' from U:/ruby/lib/ruby/gems/1.8/gems/json_pure-1.1.3/lib/json/pure/generator.rb:299:in json_transform’
from
U:/ruby/lib/ruby/gems/1.8/gems/json_pure-1.1.3/lib/json/pure/generator.rb:272:in
to_json' from U:/ruby/lib/ruby/gems/1.8/gems/json_pure-1.1.3/lib/json/pure/generator.rb:251:in json_transform’
from U:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
map' from U:/ruby/lib/ruby/gems/1.8/gems/json_pure-1.1.3/lib/json/pure/generator.rb:245:in each’
from
U:/ruby/lib/ruby/gems/1.8/gems/json_pure-1.1.3/lib/json/pure/generator.rb:245:in
map' from U:/ruby/lib/ruby/gems/1.8/gems/json_pure-1.1.3/lib/json/pure/generator.rb:245:in json_transform’
from
U:/ruby/lib/ruby/gems/1.8/gems/json_pure-1.1.3/lib/json/pure/generator.rb:218:in
`to_json’
from Z:/test.rb:116

Gurpal 2000 wrote:

The funny thing is i’ve been trying that and it keeps on crapping out:

U:/ruby/lib/ruby/gems/1.8/gems/json_pure-1.1.3/lib/json/pureuby/gems/1.8//generator.rb:251:in
json_transform' from U:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:inmap’
from
U:/ruby/lib/ruby/gems/1.8/gems/json_pure-1.1.3/lib/json/pure/generator.rb:245:in
each' from U:/ruby/lib/ruby/gems/1.8/gems/json_pure-1.1.3/lib/json/pure/generator.rb:245:inmap’
from
U:/ruby/lib/ruby/gems/1.8/gems/json_pure-1.1.3/lib/json/pure/generator.rb:245:in
json_transform' from U:/ruby/lib/ruby/gems/1.8/gems/json_pure-1.1.3/lib/json/pure/generator.rb:218:into_json’
from Z:/test.rb:116

BUMP! i’m still stuck here. Any ideas most appreciated.

Gurpal 2000 wrote:

cfp:~ > ruby a.rb
[{“key”:42},{“another key”:42.0}]

{“a”:4,“b”:2}

a @ http://codeforpeople.com/

I have another question. If I do something like this:

mydata = [ C.new(4, 2).to_json, C.new(1, 3).to_json, C.new(5, 6).to_json
]
myhash = { ‘data’ => mydata }
puts myhash.to_json

I see that the output will double escape the values (they are actually
strings with all sorts of characters in my code).

I guess what i need is the to_json on the hash to call to_json in each
element of the array. What’s the ruby-esque way to do this? I could do
it in long code but doesn’t feel right. This should easy right?

Thanks

example if i put multiple Cars in a hash or asrray and then wrap that in
a “data” hash i get double quotes:

{“data”:[“{"a":"acar","b":2}”,“{"a":"ccar","b":6}”,“{"a":"bcar","b":3}”]}

what i need is something like

{ data: [ { data1 }, { data2 }, … ] }

thanks

On Sep 16, 2:44 am, Gurpal 2000 [email protected] wrote:

mydata = [ C.new(4, 2), C.new(1, 3), C.new(5, 6) ]
myhash = { ‘data’ => mydata }.to_json

a @http://codeforpeople.com/

The funny thing is i’ve been trying that and it keeps on crapping out:

U:/ruby/lib/ruby/gems/1.8/gems/json_pure-1.1.3/lib/json/pure/generator.rb:3­00:in
`to_json’: wrong number of arguments (2 for 0) (ArgumentError)

This means that you are trying to supply two arguments to to_json,
when in fact you aren’t supposed to have any. My guess is that the
example you’re trying doesn’t exactly match the above.

– Mark.