Permalink
Browse files

Now that we have a separate internal rails_to_json, use a separate ci…

…rcular reference stack instead of sticking it in the options hash
  • Loading branch information...
jeremy committed Apr 27, 2009
1 parent 678385d commit ee46ffedb88f812467485167036c7d254d0ce757
@@ -1180,11 +1180,11 @@ def initialize(generator, variable)
# 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
@@ -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
@@ -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
@@ -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
@@ -1,6 +1,6 @@
class FalseClass
private
- def rails_to_json(options = nil)
+ def rails_to_json(*)
'false'
end
end
@@ -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 @@ def rails_to_json(options = nil) #:nodoc:
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
@@ -1,6 +1,6 @@
class NilClass
private
- def rails_to_json(options = nil)
+ def rails_to_json(*)
'null'
end
end
@@ -1,6 +1,6 @@
class Numeric
private
- def rails_to_json(options = nil)
+ def rails_to_json(*)
to_s
end
end
@@ -3,11 +3,11 @@
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
@@ -1,6 +1,6 @@
class Regexp
private
- def rails_to_json(options = nil)
+ def rails_to_json(*)
inspect
end
end
@@ -1,6 +1,6 @@
class String
private
- def rails_to_json(options = nil)
+ def rails_to_json(*)
ActiveSupport::JSON::Encoding.escape(self)
end
end
@@ -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
@@ -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
@@ -1,6 +1,6 @@
class TrueClass
private
- def rails_to_json(options = nil)
+ def rails_to_json(*)
'true'
end
end
@@ -4,12 +4,13 @@ class CircularReferenceError < StandardError
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
@@ -3,7 +3,7 @@ module JSON
# 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
@@ -313,7 +313,7 @@ def method_missing(sym, *args, &block)
# # 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

0 comments on commit ee46ffe

Please sign in to comment.