aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
authorSam Stephenson <sam@37signals.com>2006-11-11 18:25:26 +0000
committerSam Stephenson <sam@37signals.com>2006-11-11 18:25:26 +0000
commit2516063f61cedf4eee36036a0b4bc9fb8c443ba1 (patch)
treea0a33d0550008ba582a3404e16ee727ce0533577 /activesupport/lib
parent2a92995d2ab33c630271746b7ec172bcd07daddf (diff)
downloadrails-2516063f61cedf4eee36036a0b4bc9fb8c443ba1.tar.gz
rails-2516063f61cedf4eee36036a0b4bc9fb8c443ba1.tar.bz2
rails-2516063f61cedf4eee36036a0b4bc9fb8c443ba1.zip
Don't quote hash keys in Hash#to_json if they're valid JavaScript identifiers.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5486 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/json.rb17
-rw-r--r--activesupport/lib/active_support/json/encoders/core.rb6
2 files changed, 18 insertions, 5 deletions
diff --git a/activesupport/lib/active_support/json.rb b/activesupport/lib/active_support/json.rb
index 7d5304ddd4..d1203bd21b 100644
--- a/activesupport/lib/active_support/json.rb
+++ b/activesupport/lib/active_support/json.rb
@@ -4,14 +4,20 @@ module ActiveSupport
module JSON #:nodoc:
class CircularReferenceError < StandardError #:nodoc:
end
- # returns the literal string as its JSON encoded form. Useful for passing javascript variables into functions.
- #
- # page.call 'Element.show', ActiveSupport::JSON::Variable.new("$$(#items li)")
+
+ # A string that returns itself as as its JSON-encoded form.
class Variable < String #:nodoc:
def to_json
self
end
end
+
+ # When +true+, Hash#to_json will omit quoting string or symbol keys
+ # if the keys are valid JavaScript identifiers. Note that this is
+ # technically improper JSON (all object keys must be quoted), so if
+ # you need strict JSON compliance, set this option to +false+.
+ mattr_accessor :unquote_hash_key_identifiers
+ @@unquote_hash_key_identifiers = true
class << self
REFERENCE_STACK_VARIABLE = :json_reference_stack
@@ -22,6 +28,11 @@ module ActiveSupport
end
end
+ def can_unquote_identifier?(key)
+ return false unless unquote_hash_key_identifiers
+ key.to_s =~ /^[[:alpha:]_$][[:alnum:]_$]*$/
+ end
+
protected
def raise_on_circular_reference(value)
stack = Thread.current[REFERENCE_STACK_VARIABLE] ||= []
diff --git a/activesupport/lib/active_support/json/encoders/core.rb b/activesupport/lib/active_support/json/encoders/core.rb
index d8571be9c8..f6cdfbcde5 100644
--- a/activesupport/lib/active_support/json/encoders/core.rb
+++ b/activesupport/lib/active_support/json/encoders/core.rb
@@ -51,8 +51,10 @@ module ActiveSupport
define_encoder Hash do |hash|
returning result = '{' do
- result << hash.map do |pair|
- pair.map { |value| value.to_json } * ': '
+ result << hash.map do |key, value|
+ key = ActiveSupport::JSON::Variable.new(key.to_s) if
+ ActiveSupport::JSON.can_unquote_identifier?(key)
+ "#{key.to_json}: #{value.to_json}"
end * ', '
result << '}'
end