Forum: Ruby on Rails ActiveSupport JSON working?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Peter M. (Guest)
on 2006-05-28 01:18
(Received via mailing list)
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
Mathias W. (Guest)
on 2006-05-28 05:23
(Received via mailing list)
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.
Ezra Z. (Guest)
on 2006-05-28 10:01
(Received via mailing list)
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
Peter M. (Guest)
on 2006-05-28 10:11
(Received via mailing list)
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
Peter M. (Guest)
on 2006-05-28 10:11
(Received via mailing list)
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
Peter M. (Guest)
on 2006-05-28 10:14
(Received via mailing list)
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
This topic is locked and can not be replied to.