This project is archived and is in readonly mode.

#1755 ✓committed

JSON encoding of hashes does not always properly double quote keys

Reported by Noah | January 14th, 2009 @ 02:06 AM | in 2.x

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.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket


Referenced by