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

Changeset 7697

Show
Ignore:
Timestamp:
09/30/07 20:57:50 (2 years ago)
Author:
david
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/test/controller/render_test.rb

    r7403 r7697  
    210210  def test_do_with_render_json 
    211211    get :render_json_hello_world 
    212     assert_equal '{hello: "world"}', @response.body 
     212    assert_equal '{"hello": "world"}', @response.body 
    213213    assert_equal 'application/json', @response.content_type 
    214214  end 
     
    216216  def test_do_with_render_json_with_callback 
    217217    get :render_json_hello_world_with_callback 
    218     assert_equal 'alert({hello: "world"})', @response.body 
     218    assert_equal 'alert({"hello": "world"})', @response.body 
    219219    assert_equal 'application/json', @response.content_type 
    220220  end 
     
    222222  def test_do_with_render_symbol_json 
    223223    get :render_symbol_json 
    224     assert_equal '{hello: "world"}', @response.body 
     224    assert_equal '{"hello": "world"}', @response.body 
    225225    assert_equal 'application/json', @response.content_type 
    226226  end 
  • trunk/activerecord/test/json_serialization_test.rb

    r7674 r7697  
    99class JsonSerializationTest < Test::Unit::TestCase 
    1010  def setup 
    11     # Quote all keys (so that we can test against strictly valid JSON). 
    12     ActiveSupport::JSON.unquote_hash_key_identifiers = false 
    13  
    1411    @contact = Contact.new( 
    1512      :name        => 'Konata Izumi', 
     
    7168 
    7269  def setup 
    73     ActiveSupport::JSON.unquote_hash_key_identifiers = false 
    74  
    7570    @david = authors(:david) 
    7671  end 
  • trunk/activesupport/CHANGELOG

    r7685 r7697  
    11*2.0.0 [Preview Release]* (September 29th, 2007) 
     2 
     3* Fixed JSON encoding to use quoted keys according to the JSON standard #8762 [choonkat/chuyeow] 
    24 
    35* Alias Object#send to send! for Ruby 1.9 forward compatibility.  [Jeremy Kemper] 
  • trunk/activesupport/lib/active_support/json/encoders/hash.rb

    r6443 r7697  
    33    returning result = '{' do 
    44      result << map do |key, value| 
    5         key = ActiveSupport::JSON::Variable.new(key.to_s) if  
    6           ActiveSupport::JSON.can_unquote_identifier?(key) 
    75        "#{ActiveSupport::JSON.encode(key)}: #{ActiveSupport::JSON.encode(value)}" 
    86      end * ', ' 
  • trunk/activesupport/lib/active_support/json/encoding.rb

    r6772 r7697  
    1111module ActiveSupport 
    1212  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  
    2013    class CircularReferenceError < StandardError 
    2114    end 
     
    2922          value.send(:to_json) 
    3023        end 
    31       end 
    32  
    33       def can_unquote_identifier?(key) #:nodoc: 
    34         unquote_hash_key_identifiers &&  
    35           ActiveSupport::JSON.valid_identifier?(key) 
    3624      end 
    3725 
  • trunk/activesupport/lib/active_support/json/variable.rb

    r6443 r7697  
    11module ActiveSupport 
    22  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. 
    44    class Variable < String 
    55      def to_json 
  • trunk/activesupport/test/json/encoding_test.rb

    r6893 r7697  
    4242  end 
    4343 
    44   def setup 
    45     unquote(false) 
    46   end 
    47    
    48   def teardown 
    49     unquote(true) 
    50   end 
    51    
    5244  def test_hash_encoding 
    5345    assert_equal %({\"a\": \"b\"}), { :a => :b }.to_json 
    5446    assert_equal %({\"a\": 1}), { 'a' => 1  }.to_json 
    5547    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(', ') + '}' 
    5950    assert_equal %({\"a\": \"b\", \"c\": \"d\"}), sorted_json 
    6051  end 
     
    7364    assert_raises(ActiveSupport::JSON::CircularReferenceError) { a.to_json } 
    7465  end 
    75    
    76   def test_unquote_hash_key_identifiers 
     66 
     67  def test_hash_key_identifiers_are_always_quoted 
    7768    values = {0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B"} 
    7869    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) } 
    8070  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 
    8772  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      
    9673    def object_keys(json_object) 
    9774      json_object[1..-2].scan(/([^{}:,\s]+):/).flatten.sort 
    9875    end 
    99      
    10076end