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

Comments

    No one has commented yet.