ActiveSupport JSON working?


#1

Hi,

I am trying to convert a hash to JSON using ActiveSupports built in
.to_json. I am doing similar to the example below but are there
certain strings that could go in place of ‘bar’ or ‘rez’ that will
break the to_json method? I am using a very long string from rjs
output in place of ‘bar’.

Perhaps .to_json is not ready yet?

Thanks,
Peter

def MyController < ApplicationController
def give_me_json
# make sure not to send html but text/plain
@headers[“Content-Type”] = “text/plain; charset=utf-8”

data = { :foo => 'bar', :etc => 'rez' }
render_text data.to_json

end
end


#2

On 5/27/06, Peter M. removed_email_address@domain.invalid wrote:

Thanks,
end
end

Does it break on characters that require utf-8 encoding? If so, it
might be that your controller-file isn’t in utf-8 itself, or that it’s
not stored as utf-8 in the database (in case you grab the data from
there). I’ve been using the to_json function a bit myself and it seems
to work properly on everything I’ve tried (once I got everything utf-8
encoded so it worked with the Swedish characters I tossed at it).

Hope that helps,
Mathias.


#3

On May 27, 2006, at 6:21 PM, Mathias W. wrote:

render_text data.to_json

Hope that helps,
Mathias.

I could be totally wrong here, but aren't you supposed to send json

as text/javascript instead of text/plain?

-Ezra


#4

On 5/27/06, Ezra Z. removed_email_address@domain.invalid wrote:

    I could be totally wrong here, but aren't you supposed to send json

as text/javascript instead of text/plain?

I think that if you did return as type=text/javascript then
Prototype.js would automatically eval() the response. But since JSON
is just an object notation, Prototype would create an object, assign
it to nothing and nothing would happen. It’s my guess.

Peter


#5

On 5/27/06, Mathias W. removed_email_address@domain.invalid wrote:

Does it break on characters that require utf-8 encoding? If so, it
might be that your controller-file isn’t in utf-8 itself, or that it’s
not stored as utf-8 in the database (in case you grab the data from
there). I’ve been using the to_json function a bit myself and it seems
to work properly on everything I’ve tried (once I got everything utf-8
encoded so it worked with the Swedish characters I tossed at it).

Thanks for the response. Partly I asked because I wasn’t sure if
anyone used the .to_json from ActiveSupport yet. In the online docs I
don’t see anything so I wasn’t sure if it was ready. Since you use it
successfully I thought a little harder and I think I was the problem,
of course.

Thanks
Peter


#6

On 5/27/06, Peter M. removed_email_address@domain.invalid wrote:

Hi,

I am trying to convert a hash to JSON using ActiveSupports built in
.to_json. I am doing similar to the example below but are there
certain strings that could go in place of ‘bar’ or ‘rez’ that will
break the to_json method? I am using a very long string from rjs
output in place of ‘bar’.

Perhaps .to_json is not ready yet?

I think I was trying to overcomplicate things. I now doubt any
problems are with ActiveSupport’s JSON support. I found a nice
solution using XML and may look back at this problem in the future.
Thanks to those that read.

Peter