Skip to content
This repository
Browse code

Merge [5486] from trunk.

git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/1-2-pre-release@5498 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 9f8c805f5d45b93269e13249472586dea4bf651b 1 parent 46b845e
Sam Stephenson sstephenson authored
2  activesupport/CHANGELOG
... ... @@ -1,5 +1,7 @@
1 1 *SVN*
2 2
  3 +* Don't quote hash keys in Hash#to_json if they're valid JavaScript identifiers. Disable this with ActiveSupport::JSON.unquote_hash_key_identifiers = false if you need strict JSON compliance. [Sam Stephenson]
  4 +
3 5 * Lazily load the Unicode Database in the UTF-8 Handler [Rick Olson]
4 6
5 7 * Update dependencies to delete partially loaded constants. [Nicholas Seckar]
17 activesupport/lib/active_support/json.rb
@@ -4,14 +4,20 @@ module ActiveSupport
4 4 module JSON #:nodoc:
5 5 class CircularReferenceError < StandardError #:nodoc:
6 6 end
7   - # returns the literal string as its JSON encoded form. Useful for passing javascript variables into functions.
8   - #
9   - # page.call 'Element.show', ActiveSupport::JSON::Variable.new("$$(#items li)")
  7 +
  8 + # A string that returns itself as as its JSON-encoded form.
10 9 class Variable < String #:nodoc:
11 10 def to_json
12 11 self
13 12 end
14 13 end
  14 +
  15 + # When +true+, Hash#to_json will omit quoting string or symbol keys
  16 + # if the keys are valid JavaScript identifiers. Note that this is
  17 + # technically improper JSON (all object keys must be quoted), so if
  18 + # you need strict JSON compliance, set this option to +false+.
  19 + mattr_accessor :unquote_hash_key_identifiers
  20 + @@unquote_hash_key_identifiers = true
15 21
16 22 class << self
17 23 REFERENCE_STACK_VARIABLE = :json_reference_stack
@@ -22,6 +28,11 @@ def encode(value)
22 28 end
23 29 end
24 30
  31 + def can_unquote_identifier?(key)
  32 + return false unless unquote_hash_key_identifiers
  33 + key.to_s =~ /^[[:alpha:]_$][[:alnum:]_$]*$/
  34 + end
  35 +
25 36 protected
26 37 def raise_on_circular_reference(value)
27 38 stack = Thread.current[REFERENCE_STACK_VARIABLE] ||= []
6 activesupport/lib/active_support/json/encoders/core.rb
@@ -51,8 +51,10 @@ module Encoders #:nodoc:
51 51
52 52 define_encoder Hash do |hash|
53 53 returning result = '{' do
54   - result << hash.map do |pair|
55   - pair.map { |value| value.to_json } * ': '
  54 + result << hash.map do |key, value|
  55 + key = ActiveSupport::JSON::Variable.new(key.to_s) if
  56 + ActiveSupport::JSON.can_unquote_identifier?(key)
  57 + "#{key.to_json}: #{value.to_json}"
56 58 end * ', '
57 59 result << '}'
58 60 end
24 activesupport/test/json.rb
@@ -36,6 +36,14 @@ class TestJSONEmitters < Test::Unit::TestCase
36 36 end
37 37 end
38 38 end
  39 +
  40 + def setup
  41 + unquote(false)
  42 + end
  43 +
  44 + def teardown
  45 + unquote(true)
  46 + end
39 47
40 48 def test_hash_encoding
41 49 assert_equal %({\"a\": \"b\"}), { :a => :b }.to_json
@@ -60,4 +68,20 @@ def test_exception_raised_when_encoding_circular_reference
60 68 a << a
61 69 assert_raises(ActiveSupport::JSON::CircularReferenceError) { a.to_json }
62 70 end
  71 +
  72 + def test_unquote_hash_key_identifiers
  73 + values = {0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B"}
  74 + assert_equal %({"a": "a", 0: 0, "_": "_", 1: 1, "$": "$", "A": "A", "A0B": "A0B", "A0": "A0"}), values.to_json
  75 + unquote(true) { assert_equal %({a: "a", 0: 0, _: "_", 1: 1, $: "$", A: "A", A0B: "A0B", A0: "A0"}), values.to_json }
  76 + end
  77 +
  78 + protected
  79 + def unquote(value)
  80 + previous_value = ActiveSupport::JSON.unquote_hash_key_identifiers
  81 + ActiveSupport::JSON.unquote_hash_key_identifiers = value
  82 + yield if block_given?
  83 + ensure
  84 + ActiveSupport::JSON.unquote_hash_key_identifiers = previous_value if block_given?
  85 + end
  86 +
63 87 end

0 comments on commit 9f8c805

Please sign in to comment.
Something went wrong with that request. Please try again.