json for extjs (without rails)

G

Gurpal 2000

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
 
A

ara.t.howard

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


gem install json
ri json

a @ http://codeforpeople.com/
 
G

Gurpal 2000

Ara said:
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
 
M

Michael Guterl

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
 
A

ara.t.howard

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


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/
 
M

Michael Guterl

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)
I just realized this code is flawed due to the nature of unordered
hashes in ruby 1.8, but you get the idea...
 
J

Joel VanderWerf

ara.t.howard said:
# 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.
 
A

ara.t.howard

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.
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/
 
G

Gurpal 2000

Ara said:


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
 
G

Gurpal 2000

Gurpal said:
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
 
A

ara.t.howard

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/
 
G

Gurpal 2000

Ara said:
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
 
G

Gurpal 2000

Gurpal said:
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: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


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

Mark Thomas

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top