Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8762 (closed defect: fixed)

Opened 11 months ago

Last modified 8 months ago

[PATCH] to_json method violates JSON specifications

Reported by: Lars_G Assigned to: core
Priority: high Milestone: 1.x
Component: ActiveSupport Version: edge
Severity: normal Keywords: json
Cc: chuyeow

Description

the to_json method generates hashes invalid by JSON specifications as found in www.json.org or RFC-4627 Section 2, Page 5. Whereas a member in a Object consists of a pair, which consists of a STRING (the key) and a VALUE (of any valid JSON type). And strings are defined as null to many CHARs inside double quotes (ASCII %x22) to_json returns keys without quotes, which by the JSON specification would be invalid, as keys are strings, and forcefully wrapped in quotes.

Attachments

valid_json_patch.txt (205 bytes) - added by chuyeow on 09/29/07 17:34:15.
Emit valid JSON (removes ActiveSupport::JSON.unquote_hash_key_identifiers)
valid_json_only.diff (4.9 kB) - added by chuyeow on 09/29/07 17:45:52.
Emit valid JSON (removes ActiveSupport::JSON.unquote_hash_key_identifiers)
actionpack_test_patch_8762.diff (1.0 kB) - added by choonkeat on 09/30/07 06:06:18.
Patch for ActionPack test to validate against quoted json keys
valid_json_only.2.diff (5.9 kB) - added by chuyeow on 09/30/07 08:03:22.
Emit valid JSON (removes ActiveSupport::JSON.unquote_hash_key_identifiers) with ActionPack tests (against r7693)

Change History

(follow-up: ↓ 11 ) 06/27/07 01:39:29 changed by kamal

  • keywords set to json.
  • status changed from new to closed.
  • resolution set to wontfix.

You can turn on strictly valid JSON by setting

ActiveSupport::JSON.unquote_hash_key_identifiers = false

This will wrap the keys in double quotes.

See [5486] and http://blog.codefront.net/2007/06/20/how-to-get-strictly-valid-json-from-rails/

07/26/07 11:06:35 changed by Henrik N

  • status changed from closed to reopened.
  • resolution deleted.

Could someone please explain why the default for to_json is not (valid) JSON? The linked blog post only states that this is the case and mentions the configuration option, but it'd be interesting to know why non-(valid) JSON is an option, and indeed the default one.

It would make more sense to me to have valid JSON be the default. It'd make more sense still to have to_json only do (valid) JSON, and if there's an additional need for non-JSON Javascript representations, a to_js method could be added. The implementation of to_json could simply be to call to_js(:quoted_keys => true) or equivalent.

I'd be happy to submit a patch, but I don't want to spend time on it until I find out why it's not being done that way already.

08/13/07 10:20:15 changed by chuyeow

  • cc set to chuyeow.

Heh I wrote that blog post and I would like to know why it isn't generating valid JSON by default too. Must have been a decision somewhere. Maybe made by one of the Prototype developers?

Anyway, any light on this would be great!

09/27/07 16:23:02 changed by chuyeow

  • priority changed from normal to high.
  • version changed from 1.2.3 to edge.

With all the recent changes to add JSON serialization and deserialization support on edge (see #7519 and #7518), I think it's absolutely important that Rails outputs valid JSON by default (not the other way around). Would definitely be imperative for the Rails 2.0 release.

If there's a historical reason for it, can someone shed some light? Worst case is that Rails 2.0 goes out with a big fat JSON warning about the invalid JSON by default ;)

09/28/07 02:35:11 changed by chuyeow

Sorry for the bad links in the last comment. It should be r7519 and r7518.

09/29/07 17:34:15 changed by chuyeow

  • attachment valid_json_patch.txt added.

Emit valid JSON (removes ActiveSupport::JSON.unquote_hash_key_identifiers)

09/29/07 17:41:39 changed by chuyeow

  • summary changed from to_json method violates json specifications to [PATCH] to_json method violates JSON specifications.

Please ignore valid_json_patch.txt, I attached the wrong file!

On Michael Koziarsk's suggestion here: http://www.ruby-forum.com/topic/124797, I've attached a patch that makes Rails emit valid JSON only. I've removed the ActiveSupport::JSON.unquote_hash_key_identifiers option since I'm of a mind that there shouldn't need to be an option to produce invalid JSON but if anyone thinks deprecation is more prudent it's really easy to patch that too.

I've:

  • left JSON decoding unchanged (i.e. invalid JSON with unquoted hash keys can still be decoded),
  • left reserved words in ActiveSupport::JSON unchanged (small chance its useful in future, but can be removed to no detriment to tests too).

09/29/07 17:45:52 changed by chuyeow

  • attachment valid_json_only.diff added.

Emit valid JSON (removes ActiveSupport::JSON.unquote_hash_key_identifiers)

09/29/07 17:47:54 changed by chuyeow

Oh and please note that when you run the ActiveRecord tests, 2 of them will fail. These are already failing tests in r7676 w/o my patch (I'll see if I can figure out what broke them). The tests are:

  1) Failure:
test_belongs_to_counter_after_save(BelongsToAssociationsTest) [./test/associations_test.rb:1167]:
<1> expected but was
<0>.

  2) Failure:
test_belongs_to_counter_after_update_attributes(BelongsToAssociationsTest) [./test/associations_test.rb:1176]:
<1> expected but was
<0>.

09/30/07 06:06:18 changed by choonkeat

  • attachment actionpack_test_patch_8762.diff added.

Patch for ActionPack test to validate against quoted json keys

09/30/07 06:07:45 changed by choonkeat

+1 to valid_json_only.diff

Additionally, added actionpack_test_patch_8762.diff to update tests in ActionPack to validate against quoted json key

09/30/07 08:03:22 changed by chuyeow

  • attachment valid_json_only.2.diff added.

Emit valid JSON (removes ActiveSupport::JSON.unquote_hash_key_identifiers) with ActionPack tests (against r7693)

09/30/07 08:07:10 changed by chuyeow

Thanks for that choonkeat, I've merged your patch into valid_json_only.2.diff.

09/30/07 20:57:53 changed by david

  • status changed from reopened to closed.
  • resolution set to fixed.

(In [7697]) Fixed JSON encoding to use quoted keys according to the JSON standard (closes #8762) [choonkat/chuyeow]

(in reply to: ↑ 1 ) 10/04/07 03:10:28 changed by eggie5

I'm confused, why are we leaving invalid JSON as the default?

10/04/07 03:28:26 changed by david

We're not, that's why this ticket has now been closed.