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

···

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

gem install json
ri json

a @ http://codeforpeople.com/

···

On Sep 6, 2008, at 6:52 PM, Gurpal 2000 wrote:

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

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

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
--
Posted via http://www.ruby-forum.com/\.

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 Sat, Sep 6, 2008 at 11:06 PM, Gurpal 2000 <gurpal@gmx.com> 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.

cfp:~ > cat a.rb

# i prefer the pure-ruby version of json

···

On Sep 6, 2008, at 9: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.

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

#
   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/
--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

Ara Howard 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)

I just realized this code is flawed due to the nature of unordered
hashes in ruby 1.8, but you get the idea...

···

On Sat, Sep 6, 2008 at 11:36 PM, Michael Guterl <mguterl@gmail.com> wrote:

On Sat, Sep 6, 2008 at 11:06 PM, Gurpal 2000 <gurpal@gmx.com> wrote:

On Sep 6, 2008, at 6:52 PM, Gurpal 2000 wrote:

end
end

car = JSON.parse(Car.new('VW', 'White').to_json)
puts car.make
puts car.color
puts car.to_json

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.

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Ara Howard 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 6, 2008, at 9:06 PM, Gurpal 2000 wrote:

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

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/

···

On Sep 6, 2008, at 11:36 PM, Joel VanderWerf wrote:
--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

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

···

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

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

a @ http://codeforpeople.com/

···

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

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

Ara Howard 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

···

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

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

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: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.

···

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

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.

···

On Sep 16, 2:44 am, Gurpal 2000 <gur...@gmx.com> 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)