Parsing JSON -> Ruby in Rails?

Hi,

I know that Rails has to_json method in 1.1, but what do I use to
convert json -> ruby objects in my controllers? I’ve tried the JSON
ruby parser, but when I include that in my controller it overwrites
rail’s to_json method with it’s own that doesn’t work right. When I
don’t include json library the following renders:

render( :text => output.to_json )

“{“attributes”: {“text”: “Redirection is done using either the
\342\200\234>\342\200\235 (greater-than symbol), or using the
\342\200\234|\342\200\235 (pipe) operator which sends the standard
output of one command to another command as standard input.”,
“paragraph”: “7”, “page_id”: “36”, “id”: “1”, “user_id”:
“1”}}”

This is strange to include the attributes array by default, but it
works. When I include the library I get:

“”#Highlight:0x365b410""

If I’m not supposed to use json library, then what does rails provide
out of the box for going from json -> ruby?

Charlie

Charlie H. wrote:

If I’m not supposed to use json library, then what does rails provide
out of the box for going from json → ruby?

I can’t find anything currently built-in, but I’ve been successfully
using SafeJSON.rb (put it in the lib directory):

http://www.xerial.org/projects/Xerial/browser/ruby/trunk/aqua/mixi/SafeJSON.rb?format=txt


We develop, watch us RoR, in numbers too big to ignore.

Charlie H. wrote:

I know that Rails has to_json method in 1.1, but what do I use to
convert json → ruby objects in my controllers?

Except for some whitespace all JSON content can be loaded using the
native
ruby YAML parser. If your JSON outputer is not YAML safe (needs to
insert
whitespace after colons) use the following snippet:

YAML.load(json_str.gsub(/,“(\w+)”/, ‘, “\1”’).gsub(/“(\w+)”:/, '“\1”: '))

Hopefully in the future this extra step will no longer be needed and
JSON
will be a true subset of YAML.

zsombor


Company - http://primalgrasp.com
Thoughts - http://deezsombor.blogspot.com