Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

JSON encoding of hashes does not always properly double quote keys

#1755

I noticed that as of rails 2.x all hash keys were intended to be double quoted in JSON encodings. Indeed there's an ActiveSupport test method called "test_hash_key_identifiers_are_always_quoted". I noticed however that this isn't actually the case -- numeric keys for example are not double quoted:

{1 => 2}.to_json

=> "{1: 2}"

The proper JSON encoding would encode this numeric key as a doubly quoted string as follows:

{1 => 2}.to_json

=> "{"1": 2}"

I've attached a patch which address this problem by checking to see if the hash key is properly quoted - if it's not, it adds double quotes to the hash key.

Reported by Noah · January 14th, 2009 @ 02:06 AM

State: committed
Milestone: 2.x
Assigned to: Michael Koziarski Michael Koziarski
Importance: none

Activity

  1. Michael Koziarski
    Michael Koziarski
    • Assigned user set to Michael Koziarski

    Is there any reason we couldn't do this instead?

    January 15th, 2009 @ 08:43 PM

  2. Noah
    Noah

    Heh - no reason I can think of. Thanks for fixing that up.

    January 16th, 2009 @ 02:03 AM

  3. Repository
    Repository
    • State changed from new to committed

    (from [0bed5bdb213ea68e2f167ac4f61f698f37cf2d69]) Properly quote json keys.

    According to the RFC and the json.org site all json keys must be strings, and those strings must be quoted with double quotes. [#1755 JSON encoding of hashes does not always properly double quote keys state:committed] http://github.com/rails/rails/co...

    January 16th, 2009 @ 04:40 AM