aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/json
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2009-04-26 20:04:47 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2009-04-26 20:04:47 -0700
commitee46ffedb88f812467485167036c7d254d0ce757 (patch)
tree3a2024e2edff6a61822a12c6308a1d917fa683fe /activesupport/lib/active_support/json
parent678385d307559b2b5737bb2b07f633d2b9a0802c (diff)
downloadrails-ee46ffedb88f812467485167036c7d254d0ce757.tar.gz
rails-ee46ffedb88f812467485167036c7d254d0ce757.tar.bz2
rails-ee46ffedb88f812467485167036c7d254d0ce757.zip
Now that we have a separate internal rails_to_json, use a separate circular reference stack instead of sticking it in the options hash
Diffstat (limited to 'activesupport/lib/active_support/json')
-rw-r--r--activesupport/lib/active_support/json/encoders/date.rb2
-rw-r--r--activesupport/lib/active_support/json/encoders/date_time.rb2
-rw-r--r--activesupport/lib/active_support/json/encoders/enumerable.rb4
-rw-r--r--activesupport/lib/active_support/json/encoders/false_class.rb2
-rw-r--r--activesupport/lib/active_support/json/encoders/hash.rb4
-rw-r--r--activesupport/lib/active_support/json/encoders/nil_class.rb2
-rw-r--r--activesupport/lib/active_support/json/encoders/numeric.rb2
-rw-r--r--activesupport/lib/active_support/json/encoders/object.rb6
-rw-r--r--activesupport/lib/active_support/json/encoders/regexp.rb2
-rw-r--r--activesupport/lib/active_support/json/encoders/string.rb2
-rw-r--r--activesupport/lib/active_support/json/encoders/symbol.rb4
-rw-r--r--activesupport/lib/active_support/json/encoders/time.rb2
-rw-r--r--activesupport/lib/active_support/json/encoders/true_class.rb2
-rw-r--r--activesupport/lib/active_support/json/encoding.rb13
-rw-r--r--activesupport/lib/active_support/json/variable.rb2
15 files changed, 26 insertions, 25 deletions
diff --git a/activesupport/lib/active_support/json/encoders/date.rb b/activesupport/lib/active_support/json/encoders/date.rb
index f1479e11e0..9adb3c20e2 100644
--- a/activesupport/lib/active_support/json/encoders/date.rb
+++ b/activesupport/lib/active_support/json/encoders/date.rb
@@ -12,7 +12,7 @@ class Date
# # With ActiveSupport.use_standard_json_time_format = false
# Date.new(2005,2,1).to_json
# # => "2005/02/01"
- def rails_to_json(options = nil)
+ def rails_to_json(*)
if ActiveSupport.use_standard_json_time_format
%("#{strftime("%Y-%m-%d")}")
else
diff --git a/activesupport/lib/active_support/json/encoders/date_time.rb b/activesupport/lib/active_support/json/encoders/date_time.rb
index 343612afe9..3a29292b24 100644
--- a/activesupport/lib/active_support/json/encoders/date_time.rb
+++ b/activesupport/lib/active_support/json/encoders/date_time.rb
@@ -12,7 +12,7 @@ class DateTime
# # With ActiveSupport.use_standard_json_time_format = false
# DateTime.civil(2005,2,1,15,15,10).to_json
# # => "2005/02/01 15:15:10 +0000"
- def rails_to_json(options = nil)
+ def rails_to_json(*)
if ActiveSupport.use_standard_json_time_format
xmlschema.inspect
else
diff --git a/activesupport/lib/active_support/json/encoders/enumerable.rb b/activesupport/lib/active_support/json/encoders/enumerable.rb
index adde7445b3..898990a59c 100644
--- a/activesupport/lib/active_support/json/encoders/enumerable.rb
+++ b/activesupport/lib/active_support/json/encoders/enumerable.rb
@@ -7,7 +7,7 @@ module Enumerable
# # => users.to_json(:only => :name)
#
# will pass the <tt>:only => :name</tt> option to each user.
- def rails_to_json(options = nil) #:nodoc:
- "[#{map { |value| ActiveSupport::JSON.encode(value, options) } * ','}]"
+ def rails_to_json(options = nil, *args) #:nodoc:
+ "[#{map { |value| ActiveSupport::JSON.encode(value, options, *args) } * ','}]"
end
end
diff --git a/activesupport/lib/active_support/json/encoders/false_class.rb b/activesupport/lib/active_support/json/encoders/false_class.rb
index 673465860a..eb975fe542 100644
--- a/activesupport/lib/active_support/json/encoders/false_class.rb
+++ b/activesupport/lib/active_support/json/encoders/false_class.rb
@@ -1,6 +1,6 @@
class FalseClass
private
- def rails_to_json(options = nil)
+ def rails_to_json(*)
'false'
end
end
diff --git a/activesupport/lib/active_support/json/encoders/hash.rb b/activesupport/lib/active_support/json/encoders/hash.rb
index 5aec547b9b..4771484843 100644
--- a/activesupport/lib/active_support/json/encoders/hash.rb
+++ b/activesupport/lib/active_support/json/encoders/hash.rb
@@ -31,7 +31,7 @@ class Hash
# would pass the <tt>:include => :posts</tt> option to <tt>users</tt>,
# allowing the posts association in the User model to be converted to JSON
# as well.
- def rails_to_json(options = nil) #:nodoc:
+ def rails_to_json(options = nil, *args) #:nodoc:
hash_keys = self.keys
if options
@@ -44,7 +44,7 @@ class Hash
result = '{'
result << hash_keys.map do |key|
- "#{ActiveSupport::JSON.encode(key.to_s)}:#{ActiveSupport::JSON.encode(self[key], options)}"
+ "#{ActiveSupport::JSON.encode(key.to_s)}:#{ActiveSupport::JSON.encode(self[key], options, *args)}"
end * ','
result << '}'
end
diff --git a/activesupport/lib/active_support/json/encoders/nil_class.rb b/activesupport/lib/active_support/json/encoders/nil_class.rb
index 5343badc9c..8c51dba384 100644
--- a/activesupport/lib/active_support/json/encoders/nil_class.rb
+++ b/activesupport/lib/active_support/json/encoders/nil_class.rb
@@ -1,6 +1,6 @@
class NilClass
private
- def rails_to_json(options = nil)
+ def rails_to_json(*)
'null'
end
end
diff --git a/activesupport/lib/active_support/json/encoders/numeric.rb b/activesupport/lib/active_support/json/encoders/numeric.rb
index 42de2f4ea7..c7cd0df1d7 100644
--- a/activesupport/lib/active_support/json/encoders/numeric.rb
+++ b/activesupport/lib/active_support/json/encoders/numeric.rb
@@ -1,6 +1,6 @@
class Numeric
private
- def rails_to_json(options = nil)
+ def rails_to_json(*)
to_s
end
end
diff --git a/activesupport/lib/active_support/json/encoders/object.rb b/activesupport/lib/active_support/json/encoders/object.rb
index 4722feb7db..9cc12d91ac 100644
--- a/activesupport/lib/active_support/json/encoders/object.rb
+++ b/activesupport/lib/active_support/json/encoders/object.rb
@@ -3,11 +3,11 @@ require 'active_support/core_ext/object/instance_variables'
class Object
# Dumps object in JSON (JavaScript Object Notation). See www.json.org for more info.
def to_json(options = nil)
- rails_to_json(options)
+ ActiveSupport::JSON.encode(self, options)
end
private
- def rails_to_json(options = nil)
- ActiveSupport::JSON.encode(instance_values, options)
+ def rails_to_json(*args)
+ ActiveSupport::JSON.encode(instance_values, *args)
end
end
diff --git a/activesupport/lib/active_support/json/encoders/regexp.rb b/activesupport/lib/active_support/json/encoders/regexp.rb
index 62be810be5..ee42db4d02 100644
--- a/activesupport/lib/active_support/json/encoders/regexp.rb
+++ b/activesupport/lib/active_support/json/encoders/regexp.rb
@@ -1,6 +1,6 @@
class Regexp
private
- def rails_to_json(options = nil)
+ def rails_to_json(*)
inspect
end
end
diff --git a/activesupport/lib/active_support/json/encoders/string.rb b/activesupport/lib/active_support/json/encoders/string.rb
index 6b9dcd97bf..4a6b21c1c0 100644
--- a/activesupport/lib/active_support/json/encoders/string.rb
+++ b/activesupport/lib/active_support/json/encoders/string.rb
@@ -1,6 +1,6 @@
class String
private
- def rails_to_json(options = nil)
+ def rails_to_json(*)
ActiveSupport::JSON::Encoding.escape(self)
end
end
diff --git a/activesupport/lib/active_support/json/encoders/symbol.rb b/activesupport/lib/active_support/json/encoders/symbol.rb
index 333cf773c1..d575350a4e 100644
--- a/activesupport/lib/active_support/json/encoders/symbol.rb
+++ b/activesupport/lib/active_support/json/encoders/symbol.rb
@@ -1,6 +1,6 @@
class Symbol
private
- def rails_to_json(options = nil)
- ActiveSupport::JSON.encode(to_s, options)
+ def rails_to_json(*args)
+ ActiveSupport::JSON.encode(to_s, *args)
end
end
diff --git a/activesupport/lib/active_support/json/encoders/time.rb b/activesupport/lib/active_support/json/encoders/time.rb
index c1cf3398c2..d434b9aace 100644
--- a/activesupport/lib/active_support/json/encoders/time.rb
+++ b/activesupport/lib/active_support/json/encoders/time.rb
@@ -14,7 +14,7 @@ class Time
# # With ActiveSupport.use_standard_json_time_format = false
# Time.utc(2005,2,1,15,15,10).to_json
# # => "2005/02/01 15:15:10 +0000"
- def rails_to_json(options = nil)
+ def rails_to_json(*)
if ActiveSupport.use_standard_json_time_format
xmlschema.inspect
else
diff --git a/activesupport/lib/active_support/json/encoders/true_class.rb b/activesupport/lib/active_support/json/encoders/true_class.rb
index 6d5d063254..bc25a6db78 100644
--- a/activesupport/lib/active_support/json/encoders/true_class.rb
+++ b/activesupport/lib/active_support/json/encoders/true_class.rb
@@ -1,6 +1,6 @@
class TrueClass
private
- def rails_to_json(options = nil)
+ def rails_to_json(*)
'true'
end
end
diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb
index acccb62aea..5441145dfd 100644
--- a/activesupport/lib/active_support/json/encoding.rb
+++ b/activesupport/lib/active_support/json/encoding.rb
@@ -4,12 +4,13 @@ module ActiveSupport
end
# Converts a Ruby object into a JSON string.
- def self.encode(value, options = nil)
- options ||= {}
- seen = (options[:seen] ||= [])
- raise CircularReferenceError, 'object references itself' if seen.include?(value.object_id)
- seen << value.object_id
- value.send(:rails_to_json, options)
+ def self.encode(value, options = nil, seen = nil)
+ seen ||= []
+ if seen.any? { |object| object.equal?(value) }
+ raise CircularReferenceError, 'object references itself'
+ end
+ seen << value
+ value.send(:rails_to_json, options, seen)
ensure
seen.pop
end
diff --git a/activesupport/lib/active_support/json/variable.rb b/activesupport/lib/active_support/json/variable.rb
index 2c7b449a50..daa7449b71 100644
--- a/activesupport/lib/active_support/json/variable.rb
+++ b/activesupport/lib/active_support/json/variable.rb
@@ -3,7 +3,7 @@ module ActiveSupport
# A string that returns itself as its JSON-encoded form.
class Variable < String
private
- def rails_to_json(options = nil)
+ def rails_to_json(*)
self
end
end