Changeset 5486
- Timestamp:
- 11/11/06 18:25:26 (2 years ago)
- Files:
-
- trunk/activesupport/CHANGELOG (modified) (1 diff)
- trunk/activesupport/lib/active_support/json.rb (modified) (3 diffs)
- trunk/activesupport/lib/active_support/json/encoders/core.rb (modified) (1 diff)
- trunk/activesupport/test/json.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activesupport/CHANGELOG
r5476 r5486 1 1 *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] 2 4 3 5 * Lazily load the Unicode Database in the UTF-8 Handler [Rick Olson] trunk/activesupport/lib/active_support/json.rb
r4081 r5486 5 5 class CircularReferenceError < StandardError #:nodoc: 6 6 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. 10 9 class Variable < String #:nodoc: 11 10 def to_json … … 13 12 end 14 13 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 15 21 16 22 class << self … … 21 27 Encoders[value.class].call(value) 22 28 end 29 end 30 31 def can_unquote_identifier?(key) 32 return false unless unquote_hash_key_identifiers 33 key.to_s =~ /^[[:alpha:]_$][[:alnum:]_$]*$/ 23 34 end 24 35 trunk/activesupport/lib/active_support/json/encoders/core.rb
r5432 r5486 52 52 define_encoder Hash do |hash| 53 53 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}" 56 58 end * ', ' 57 59 result << '}' trunk/activesupport/test/json.rb
r4795 r5486 37 37 end 38 38 end 39 40 def setup 41 unquote(false) 42 end 43 44 def teardown 45 unquote(true) 46 end 39 47 40 48 def test_hash_encoding … … 61 69 assert_raises(ActiveSupport::JSON::CircularReferenceError) { a.to_json } 62 70 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 63 87 end