Ruby Forum Rails-core > Mysterious design decision in Hash#to_json

Posted by choonkeat (Guest)
on 17.09.2007 13:24
(Received via mailing list)
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
Posted by Mislav MarohniÄ? (mislav)
on 25.09.2007 23:01
(Received via mailing list)
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.
Posted by Chu Yeow Cheah (chuyeow)
on 28.09.2007 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.
Posted by Michael Koziarski (Guest)
on 28.09.2007 22:13
(Received via mailing list)
> 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
Posted by Mislav MarohniÄ? (mislav)
on 29.09.2007 10:31
(Received via mailing list)
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?
Posted by Chu Yeow Cheah (chuyeow)
on 29.09.2007 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
Posted by choonkeat (Guest)
on 30.09.2007 08:10
(Received via mailing list)
+1 and patching test in actionpack as well

Looks like this is rolling along :-)

--
choonkeat
Posted by DHH (Guest)
on 30.09.2007 23:03
(Received via mailing list)
Applied.
Posted by choonkeat (Guest)
on 01.10.2007 05:40
(Received via mailing list)
Thanks!

--
choonkeat
Posted by Chu Yeow Cheah (chuyeow)
on 01.10.2007 05:42
DHH wrote:
> Applied.

Awesome. Valid JSON at last :) Thanks Choon Keat, Michael and DHH for 
making this happen!
Posted by eggie5 (Guest)
on 05.10.2007 19:41
(Received via mailing list)
Thank you again!

On Sep 30, 8:42 pm, Chu Yeow Cheah <ruby-forum-incom...@andreas-s.net>