aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/json.rb10
-rw-r--r--activesupport/lib/active_support/json/encoders/core.rb4
-rw-r--r--activesupport/test/json.rb34
3 files changed, 32 insertions, 16 deletions
diff --git a/activesupport/lib/active_support/json.rb b/activesupport/lib/active_support/json.rb
index 77c7225c66..943adebd35 100644
--- a/activesupport/lib/active_support/json.rb
+++ b/activesupport/lib/active_support/json.rb
@@ -3,7 +3,15 @@ require 'active_support/json/encoders'
module ActiveSupport
module JSON #:nodoc:
class CircularReferenceError < StandardError; 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)")
+ class Variable < String
+ def to_json
+ self
+ end
+ end
+
class << self
REFERENCE_STACK_VARIABLE = :json_reference_stack
diff --git a/activesupport/lib/active_support/json/encoders/core.rb b/activesupport/lib/active_support/json/encoders/core.rb
index 003c938be4..d3193af555 100644
--- a/activesupport/lib/active_support/json/encoders/core.rb
+++ b/activesupport/lib/active_support/json/encoders/core.rb
@@ -56,6 +56,10 @@ module ActiveSupport
result << '}'
end
end
+
+ define_encoder Regexp do |regexp|
+ regexp.inspect
+ end
end
end
end
diff --git a/activesupport/test/json.rb b/activesupport/test/json.rb
index c5b597abd7..fc4d7705b1 100644
--- a/activesupport/test/json.rb
+++ b/activesupport/test/json.rb
@@ -9,26 +9,30 @@ class Foo
end
class TestJSONEmitters < Test::Unit::TestCase
- TrueTests = [[ true, %(true) ]]
- FalseTests = [[ false, %(false) ]]
- NilTests = [[ nil, %(null) ]]
- NumericTests = [[ 1, %(1) ],
- [ 2.5, %(2.5) ]]
+ TrueTests = [[ true, %(true) ]]
+ FalseTests = [[ false, %(false) ]]
+ NilTests = [[ nil, %(null) ]]
+ NumericTests = [[ 1, %(1) ],
+ [ 2.5, %(2.5) ]]
- StringTests = [[ 'this is the string', %("this is the string") ],
- [ 'a "string" with quotes', %("a \\"string\\" with quotes") ]]
+ StringTests = [[ 'this is the string', %("this is the string") ],
+ [ 'a "string" with quotes', %("a \\"string\\" with quotes") ]]
- ArrayTests = [[ ['a', 'b', 'c'], %([\"a\", \"b\", \"c\"]) ],
- [ [1, 'a', :b, nil, false], %([1, \"a\", \"b\", null, false]) ]]
+ ArrayTests = [[ ['a', 'b', 'c'], %([\"a\", \"b\", \"c\"]) ],
+ [ [1, 'a', :b, nil, false], %([1, \"a\", \"b\", null, false]) ]]
- HashTests = [[ {:a => :b, :c => :d}, %({\"c\": \"d\", \"a\": \"b\"}) ]]
+ HashTests = [[ {:a => :b, :c => :d}, %({\"c\": \"d\", \"a\": \"b\"}) ]]
- SymbolTests = [[ :a, %("a") ],
- [ :this, %("this") ],
- [ :"a b", %("a b") ]]
+ SymbolTests = [[ :a, %("a") ],
+ [ :this, %("this") ],
+ [ :"a b", %("a b") ]]
+
+ ObjectTests = [[ Foo.new(1, 2), %({\"a\": 1, \"b\": 2}) ]]
+
+ VariableTests = [[ ActiveSupport::JSON::Variable.new('foo'), 'foo'],
+ [ ActiveSupport::JSON::Variable.new('alert("foo")'), 'alert("foo")']]
+ RegexpTests = [[ /^a/, '/^a/' ], /^\w{1,2}[a-z]+/ix, '/^\\w{1,2}[a-z]+/ix']
- ObjectTests = [[ Foo.new(1, 2), %({\"a\": 1, \"b\": 2}) ]]
-
constants.grep(/Tests$/).each do |class_tests|
define_method("test_#{class_tests[0..-6].downcase}") do
self.class.const_get(class_tests).each do |pair|