Now that we have a separate internal rails_to_json, use a separate circular reference stack instead of sticking it in the options hash

This commit is contained in:
Jeremy Kemper
2009-04-26 20:04:47 -07:00
parent 678385d307
commit ee46ffedb8
17 changed files with 29 additions and 28 deletions

View File

@@ -1180,11 +1180,11 @@ module ActionView
# The JSON Encoder calls this to check for the +to_json+ method
# Since it's a blank slate object, I suppose it responds to anything.
def respond_to?(method)
def respond_to?(*)
true
end
def rails_to_json(options = nil)
def rails_to_json(*)
@variable
end

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -1,6 +1,6 @@
class FalseClass
private
def rails_to_json(options = nil)
def rails_to_json(*)
'false'
end
end

View File

@@ -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

View File

@@ -1,6 +1,6 @@
class NilClass
private
def rails_to_json(options = nil)
def rails_to_json(*)
'null'
end
end

View File

@@ -1,6 +1,6 @@
class Numeric
private
def rails_to_json(options = nil)
def rails_to_json(*)
to_s
end
end

View File

@@ -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

View File

@@ -1,6 +1,6 @@
class Regexp
private
def rails_to_json(options = nil)
def rails_to_json(*)
inspect
end
end

View File

@@ -1,6 +1,6 @@
class String
private
def rails_to_json(options = nil)
def rails_to_json(*)
ActiveSupport::JSON::Encoding.escape(self)
end
end

View File

@@ -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

View File

@@ -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

View File

@@ -1,6 +1,6 @@
class TrueClass
private
def rails_to_json(options = nil)
def rails_to_json(*)
'true'
end
end

View File

@@ -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

View File

@@ -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

View File

@@ -313,7 +313,7 @@ module ActiveSupport
# # With ActiveSupport.use_standard_json_time_format = false
# Time.utc(2005,2,1,15,15,10).in_time_zone.to_json
# # => "2005/02/01 15:15:10 +0000"
def rails_to_json(options = nil)
def rails_to_json(*)
if !ActiveSupport.respond_to?(:use_standard_json_time_format) || ActiveSupport.use_standard_json_time_format
xmlschema.inspect
else