aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/json/encoders/hash.rb2
-rw-r--r--activesupport/lib/active_support/json/encoding.rb12
-rw-r--r--activesupport/lib/active_support/json/variable.rb2
-rw-r--r--activesupport/test/json/encoding_test.rb34
5 files changed, 8 insertions, 44 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 272be35724..b0a3e6aaa6 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*2.0.0 [Preview Release]* (September 29th, 2007)
+* Fixed JSON encoding to use quoted keys according to the JSON standard #8762 [choonkat/chuyeow]
+
* Alias Object#send to send! for Ruby 1.9 forward compatibility. [Jeremy Kemper]
* Backport Object#instance_variable_defined? for Ruby < 1.8.6. [Jeremy Kemper]
diff --git a/activesupport/lib/active_support/json/encoders/hash.rb b/activesupport/lib/active_support/json/encoders/hash.rb
index 3654e10b2e..9a9f074847 100644
--- a/activesupport/lib/active_support/json/encoders/hash.rb
+++ b/activesupport/lib/active_support/json/encoders/hash.rb
@@ -2,8 +2,6 @@ class Hash
def to_json #:nodoc:
returning result = '{' do
result << map do |key, value|
- key = ActiveSupport::JSON::Variable.new(key.to_s) if
- ActiveSupport::JSON.can_unquote_identifier?(key)
"#{ActiveSupport::JSON.encode(key)}: #{ActiveSupport::JSON.encode(value)}"
end * ', '
result << '}'
diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb
index 64e5779b71..67656109c0 100644
--- a/activesupport/lib/active_support/json/encoding.rb
+++ b/activesupport/lib/active_support/json/encoding.rb
@@ -10,13 +10,6 @@ end
module ActiveSupport
module JSON
- # When +true+, Hash#to_json will omit quoting string or symbol keys
- # if the keys are valid JavaScript identifiers. Note that this is
- # technically improper JSON (all object keys must be quoted), so if
- # you need strict JSON compliance, set this option to +false+.
- mattr_accessor :unquote_hash_key_identifiers
- @@unquote_hash_key_identifiers = true
-
class CircularReferenceError < StandardError
end
@@ -30,11 +23,6 @@ module ActiveSupport
end
end
- def can_unquote_identifier?(key) #:nodoc:
- unquote_hash_key_identifiers &&
- ActiveSupport::JSON.valid_identifier?(key)
- end
-
protected
def raise_on_circular_reference(value) #:nodoc:
stack = Thread.current[REFERENCE_STACK_VARIABLE] ||= []
diff --git a/activesupport/lib/active_support/json/variable.rb b/activesupport/lib/active_support/json/variable.rb
index 325ac9b7a6..0af98f0968 100644
--- a/activesupport/lib/active_support/json/variable.rb
+++ b/activesupport/lib/active_support/json/variable.rb
@@ -1,6 +1,6 @@
module ActiveSupport
module JSON
- # A string that returns itself as as its JSON-encoded form.
+ # A string that returns itself as its JSON-encoded form.
class Variable < String
def to_json
self
diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb
index 9ed7683b86..ba468141b4 100644
--- a/activesupport/test/json/encoding_test.rb
+++ b/activesupport/test/json/encoding_test.rb
@@ -41,21 +41,12 @@ class TestJSONEncoding < Test::Unit::TestCase
end
end
- def setup
- unquote(false)
- end
-
- def teardown
- unquote(true)
- end
-
def test_hash_encoding
assert_equal %({\"a\": \"b\"}), { :a => :b }.to_json
assert_equal %({\"a\": 1}), { 'a' => 1 }.to_json
assert_equal %({\"a\": [1, 2]}), { 'a' => [1,2] }.to_json
-
- sorted_json =
- '{' + {:a => :b, :c => :d}.to_json[1..-2].split(', ').sort.join(', ') + '}'
+
+ sorted_json = '{' + {:a => :b, :c => :d}.to_json[1..-2].split(', ').sort.join(', ') + '}'
assert_equal %({\"a\": \"b\", \"c\": \"d\"}), sorted_json
end
@@ -72,29 +63,14 @@ class TestJSONEncoding < Test::Unit::TestCase
a << a
assert_raises(ActiveSupport::JSON::CircularReferenceError) { a.to_json }
end
-
- def test_unquote_hash_key_identifiers
+
+ def test_hash_key_identifiers_are_always_quoted
values = {0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B"}
assert_equal %w( "$" "A" "A0" "A0B" "_" "a" 0 1 ), object_keys(values.to_json)
- unquote(true) { assert_equal %w( $ 0 1 A A0 A0B _ a ), object_keys(values.to_json) }
end
-
- def test_unquote_hash_key_identifiers_ignores_javascript_reserved_words
- values = {"hello" => "world", "this" => "that", "with" => "foo"}
- unquote(true) { assert_equal %w( "this" "with" hello ), object_keys(values.to_json) }
- end
-
+
protected
- def unquote(value)
- previous_value = ActiveSupport::JSON.unquote_hash_key_identifiers
- ActiveSupport::JSON.unquote_hash_key_identifiers = value
- yield if block_given?
- ensure
- ActiveSupport::JSON.unquote_hash_key_identifiers = previous_value if block_given?
- end
-
def object_keys(json_object)
json_object[1..-2].scan(/([^{}:,\s]+):/).flatten.sort
end
-
end