Hi guys, Hoping to learn something here. (folks at #8762<http://dev.rubyonrails.org/ticket/8762>would be interested as well) Currently Hash#to_json produces text with unquoted keys, which is invalid JSON. Asking developers to set ActiveSupport:: JSON.unquote_hash_key_identifiers to false if they want valid JSON doesn't seem in character of Rails, i.e. "smart defaults"... or is valid JSON the rarer requirement in common cases? This doesn't appear to be a bug nor an accident. In fact, thought and effort has gone into making to_json to consistently produce invalid JSON: [5486<http://dev.rubyonrails.org/changeset/5486#file2>], [6443 <http://dev.rubyonrails.org/changeset/6443#file5>]. i.e. [6443] fights away ticket #7714 <http://dev.rubyonrails.org/ticket/7714> by bringing in an elaborate ActiveSupport::JSON::RESERVED_WORDS instead of *returning* to produce valid JSON (as requested in ticket). Hope somebody could shed some light on this design decision and I'll learn some Ruby. Cheers -- choonkeat
on 2007-09-17 13:24
on 2007-09-25 23:01
On 9/17/07, choonkeat <choonkeat@gmail.com> wrote: > > > Currently Hash#to_json produces text with unquoted keys, which is invalid > JSON. Asking developers to set ActiveSupport:: > JSON.unquote_hash_key_identifiers to false if they want valid JSON doesn't > seem in character of Rails, i.e. "smart defaults"... or is valid JSON the > rarer requirement in common cases? Didn't even know Rails did that. Since #to_json produces valid JavaScript, obviously we that are using JSON from Rails didn't realize that in fact it isn't valid JSON. There isn't any explanation to unquote the hash keys except to save a few bytes, which is hardly worth invalidating the spec. After all, JSON data is used in far more places than just eval() in JavaScript.
on 2007-09-28 18:53
I want to know why too, and I think we should get an answer soon considering how DHH seems to want to achieve feature parity between JSON and XML for Rails 2.0 (you can, for example, use ARes while speaking JSON and AR objects now convert nicely to JSON). I agree it just doesn't make much sense to me either to have invalid JSON emitted by default. Cheers, Chu Yeow Mislav Marohni�? wrote: > On 9/17/07, choonkeat <choonkeat@gmail.com> wrote: >> >> >> Currently Hash#to_json produces text with unquoted keys, which is invalid >> JSON. Asking developers to set ActiveSupport:: >> JSON.unquote_hash_key_identifiers to false if they want valid JSON doesn't >> seem in character of Rails, i.e. "smart defaults"... or is valid JSON the >> rarer requirement in common cases? > > > Didn't even know Rails did that. Since #to_json produces valid > JavaScript, > obviously we that are using JSON from Rails didn't realize that in fact > it > isn't valid JSON. > > There isn't any explanation to unquote the hash keys except to save a > few > bytes, which is hardly worth invalidating the spec. After all, JSON data > is > used in far more places than just eval() in JavaScript.
on 2007-09-28 22:13
> Currently Hash#to_json produces text with unquoted keys, which is invalid > JSON. Asking developers to set ActiveSupport:: > JSON.unquote_hash_key_identifiers to false if they want > valid JSON doesn't seem in character of Rails, i.e. "smart defaults"... or > is valid JSON the rarer requirement in common cases? I've dug around the history and asked a few folks an it seems there's no particularly valid reason for us to continue to support the current behaviour. Send in a patch which switches the default and perhaps removes the option, and I'll see it gets applied. -- Cheers Koz
on 2007-09-29 10:31
On 9/28/07, Michael Koziarski <michael@koziarski.com> wrote: > > > I've dug around the history and asked a few folks an it seems there's > no particularly valid reason for us to continue to support the current > behaviour. Send in a patch which switches the default and perhaps > removes the option, and I'll see it gets applied. Maybe the option should stay but have no effect and should trigger a deprecation warning?
on 2007-09-29 19:44
Awesome. Thanks for digging around! I've submitted a patch here: http://dev.rubyonrails.org/ticket/8762 (please ignore the first patch, that's the wrong file). Please take a look. Cheers, Chu Yeow Michael Koziarski wrote: >> Currently Hash#to_json produces text with unquoted keys, which is invalid >> JSON. Asking developers to set ActiveSupport:: >> JSON.unquote_hash_key_identifiers to false if they want >> valid JSON doesn't seem in character of Rails, i.e. "smart defaults"... or >> is valid JSON the rarer requirement in common cases? > > I've dug around the history and asked a few folks an it seems there's > no particularly valid reason for us to continue to support the current > behaviour. Send in a patch which switches the default and perhaps > removes the option, and I'll see it gets applied. > > -- > Cheers > > Koz
on 2007-09-30 08:10
+1 and patching test in actionpack as well Looks like this is rolling along :-) -- choonkeat
on 2007-10-01 05:42
DHH wrote:
> Applied.
Awesome. Valid JSON at last :) Thanks Choon Keat, Michael and DHH for
making this happen!