Politics, Programming and Possibilities
21 May
While the dev.rubyonrails.org site is down for the time being, I thought I’d post this patch in case the bug bites anyone else. There’s a bug in the Rails ActiveSupport::JSON.decode method (edge rails) that causes parsing to fail if you have alternating quote types in a string. For example:
{"title": "That's great!"}
will fail because the single quote inside the double quotes is taken to mean “the start of a new single-quoted string” rather than “a character inside the double-quoted string.”
Here is a diff and a ruby patch (require it in your environment.rb file).
Update: Now available as ticket 8425
3 Responses for "Json Decoding Bug in Rails"
Meanwhile, I’ve just been bitten by Ragel based JSON gem deciding that
{"title": "That\\'s great"}is illegal despite the spec saying that any character may be escaped. Which would be fine if I had control over the feed, but I was trying to parse a del.icio.us.feed…The regular expression for matching a JSON string looks like this:
/”((?:[^\x00-\x1f\\"]|\\(?:u[\da-fA-F]{4}|.))*)”/
It’s not the most efficient variant that will do the match, but it’ll serve. Unescaping the matched string is a little bit trickier, but, it’s not really rocket science.
I do wonder why the Rails even *cares* about single quotes in this context though; they don’t appear to be legal string delimiters in the JSON spec.
True, it’s not part of the JSON spec, but single-quoted strings *would* technically parse just fine when eval’ed in javascript—so maybe that’s why the Rails parser (among others) is a little lenient there.
the ruby+C JSON parser has serious issues, like not being able to parse anything thats not inside a [] and discarding it. im about to dig into the ragel since im sick of the slowness of the pure-ruby gem
Leave a reply