aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/json.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/json.rb')
-rw-r--r--activesupport/lib/active_support/json.rb17
1 files changed, 14 insertions, 3 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] ||= []