Skip to content
This repository
Browse code

Fixed JSON encoding to use quoted keys according to the JSON standard…

… (closes #8762) [choonkat/chuyeow]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7697 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 7275d2749cb829d89bffe7e6aa87c99084351f6a 1 parent 66d05f5
David Heinemeier Hansson authored September 30, 2007
6  actionpack/test/controller/render_test.rb
@@ -209,19 +209,19 @@ def test_do_with_render_text
209 209
 
210 210
   def test_do_with_render_json
211 211
     get :render_json_hello_world
212  
-    assert_equal '{hello: "world"}', @response.body
  212
+    assert_equal '{"hello": "world"}', @response.body
213 213
     assert_equal 'application/json', @response.content_type
214 214
   end
215 215
 
216 216
   def test_do_with_render_json_with_callback
217 217
     get :render_json_hello_world_with_callback
218  
-    assert_equal 'alert({hello: "world"})', @response.body
  218
+    assert_equal 'alert({"hello": "world"})', @response.body
219 219
     assert_equal 'application/json', @response.content_type
220 220
   end
221 221
 
222 222
   def test_do_with_render_symbol_json
223 223
     get :render_symbol_json
224  
-    assert_equal '{hello: "world"}', @response.body
  224
+    assert_equal '{"hello": "world"}', @response.body
225 225
     assert_equal 'application/json', @response.content_type
226 226
   end
227 227
 
5  activerecord/test/json_serialization_test.rb
@@ -8,9 +8,6 @@
8 8
 
9 9
 class JsonSerializationTest < Test::Unit::TestCase
10 10
   def setup
11  
-    # Quote all keys (so that we can test against strictly valid JSON).
12  
-    ActiveSupport::JSON.unquote_hash_key_identifiers = false
13  
-
14 11
     @contact = Contact.new(
15 12
       :name        => 'Konata Izumi',
16 13
       :age         => 16,
@@ -70,8 +67,6 @@ class DatabaseConnectedJsonEncodingTest < Test::Unit::TestCase
70 67
   fixtures :authors, :posts, :comments, :tags, :taggings
71 68
 
72 69
   def setup
73  
-    ActiveSupport::JSON.unquote_hash_key_identifiers = false
74  
-
75 70
     @david = authors(:david)
76 71
   end
77 72
 
2  activesupport/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *2.0.0 [Preview Release]* (September 29th, 2007)
2 2
 
  3
+* Fixed JSON encoding to use quoted keys according to the JSON standard #8762 [choonkat/chuyeow]
  4
+
3 5
 * Alias Object#send to send! for Ruby 1.9 forward compatibility.  [Jeremy Kemper]
4 6
 
5 7
 * Backport Object#instance_variable_defined? for Ruby < 1.8.6.  [Jeremy Kemper]
2  activesupport/lib/active_support/json/encoders/hash.rb
@@ -2,8 +2,6 @@ class Hash
2 2
   def to_json #:nodoc:
3 3
     returning result = '{' do
4 4
       result << map do |key, value|
5  
-        key = ActiveSupport::JSON::Variable.new(key.to_s) if 
6  
-          ActiveSupport::JSON.can_unquote_identifier?(key)
7 5
         "#{ActiveSupport::JSON.encode(key)}: #{ActiveSupport::JSON.encode(value)}"
8 6
       end * ', '
9 7
       result << '}'
12  activesupport/lib/active_support/json/encoding.rb
@@ -10,13 +10,6 @@
10 10
 
11 11
 module ActiveSupport
12 12
   module JSON
13  
-    # When +true+, Hash#to_json will omit quoting string or symbol keys
14  
-    # if the keys are valid JavaScript identifiers.  Note that this is
15  
-    # technically improper JSON (all object keys must be quoted), so if
16  
-    # you need strict JSON compliance, set this option to +false+.
17  
-    mattr_accessor :unquote_hash_key_identifiers
18  
-    @@unquote_hash_key_identifiers = true
19  
-
20 13
     class CircularReferenceError < StandardError
21 14
     end
22 15
 
@@ -30,11 +23,6 @@ def encode(value)
30 23
         end
31 24
       end
32 25
 
33  
-      def can_unquote_identifier?(key) #:nodoc:
34  
-        unquote_hash_key_identifiers && 
35  
-          ActiveSupport::JSON.valid_identifier?(key)
36  
-      end
37  
-
38 26
       protected
39 27
         def raise_on_circular_reference(value) #:nodoc:
40 28
           stack = Thread.current[REFERENCE_STACK_VARIABLE] ||= []
2  activesupport/lib/active_support/json/variable.rb
... ...
@@ -1,6 +1,6 @@
1 1
 module ActiveSupport
2 2
   module JSON
3  
-    # A string that returns itself as as its JSON-encoded form.
  3
+    # A string that returns itself as its JSON-encoded form.
4 4
     class Variable < String
5 5
       def to_json
6 6
         self
34  activesupport/test/json/encoding_test.rb
@@ -41,21 +41,12 @@ def initialize(a, b)
41 41
     end
42 42
   end
43 43
 
44  
-  def setup
45  
-    unquote(false)
46  
-  end
47  
-  
48  
-  def teardown
49  
-    unquote(true)
50  
-  end
51  
-  
52 44
   def test_hash_encoding
53 45
     assert_equal %({\"a\": \"b\"}), { :a => :b }.to_json
54 46
     assert_equal %({\"a\": 1}), { 'a' => 1  }.to_json
55 47
     assert_equal %({\"a\": [1, 2]}), { 'a' => [1,2] }.to_json
56  
-    
57  
-    sorted_json  = 
58  
-      '{' + {:a => :b, :c => :d}.to_json[1..-2].split(', ').sort.join(', ') + '}'
  48
+
  49
+    sorted_json = '{' + {:a => :b, :c => :d}.to_json[1..-2].split(', ').sort.join(', ') + '}'
59 50
     assert_equal %({\"a\": \"b\", \"c\": \"d\"}), sorted_json
60 51
   end
61 52
 
@@ -72,29 +63,14 @@ def test_exception_raised_when_encoding_circular_reference
72 63
     a << a
73 64
     assert_raises(ActiveSupport::JSON::CircularReferenceError) { a.to_json }
74 65
   end
75  
-  
76  
-  def test_unquote_hash_key_identifiers
  66
+
  67
+  def test_hash_key_identifiers_are_always_quoted
77 68
     values = {0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B"}
78 69
     assert_equal %w( "$" "A" "A0" "A0B" "_" "a" 0 1 ), object_keys(values.to_json)
79  
-    unquote(true) { assert_equal %w( $ 0 1 A A0 A0B _ a ), object_keys(values.to_json) }
80 70
   end
81  
-  
82  
-  def test_unquote_hash_key_identifiers_ignores_javascript_reserved_words
83  
-    values = {"hello" => "world", "this" => "that", "with" => "foo"}
84  
-    unquote(true) { assert_equal %w( "this" "with" hello ), object_keys(values.to_json) }
85  
-  end
86  
-  
  71
+
87 72
   protected
88  
-    def unquote(value)
89  
-      previous_value = ActiveSupport::JSON.unquote_hash_key_identifiers
90  
-      ActiveSupport::JSON.unquote_hash_key_identifiers = value
91  
-      yield if block_given?
92  
-    ensure
93  
-      ActiveSupport::JSON.unquote_hash_key_identifiers = previous_value if block_given?
94  
-    end
95  
-    
96 73
     def object_keys(json_object)
97 74
       json_object[1..-2].scan(/([^{}:,\s]+):/).flatten.sort
98 75
     end
99  
-    
100 76
 end

0 notes on commit 7275d27

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