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

Changeset 5486

Show
Ignore:
Timestamp:
11/11/06 18:25:26 (2 years ago)
Author:
sam
Message:

Don't quote hash keys in Hash#to_json if they're valid JavaScript identifiers.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r5476 r5486  
    11*SVN* 
     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] 
    24 
    35* Lazily load the Unicode Database in the UTF-8 Handler [Rick Olson] 
  • trunk/activesupport/lib/active_support/json.rb

    r4081 r5486  
    55    class CircularReferenceError < StandardError #:nodoc: 
    66    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. 
    109    class Variable < String #:nodoc: 
    1110      def to_json 
     
    1312      end 
    1413    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 
    1521 
    1622    class << self 
     
    2127          Encoders[value.class].call(value) 
    2228        end 
     29      end 
     30       
     31      def can_unquote_identifier?(key) 
     32        return false unless unquote_hash_key_identifiers 
     33        key.to_s =~ /^[[:alpha:]_$][[:alnum:]_$]*$/ 
    2334      end 
    2435       
  • trunk/activesupport/lib/active_support/json/encoders/core.rb

    r5432 r5486  
    5252      define_encoder Hash do |hash| 
    5353        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}" 
    5658          end * ', ' 
    5759          result << '}' 
  • trunk/activesupport/test/json.rb

    r4795 r5486  
    3737    end 
    3838  end 
     39 
     40  def setup 
     41    unquote(false) 
     42  end 
     43   
     44  def teardown 
     45    unquote(true) 
     46  end 
    3947   
    4048  def test_hash_encoding 
     
    6169    assert_raises(ActiveSupport::JSON::CircularReferenceError) { a.to_json } 
    6270  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     
    6387end