From ee46ffedb88f812467485167036c7d254d0ce757 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 26 Apr 2009 20:04:47 -0700 Subject: Now that we have a separate internal rails_to_json, use a separate circular reference stack instead of sticking it in the options hash --- actionpack/lib/action_view/helpers/prototype_helper.rb | 4 ++-- activesupport/lib/active_support/json/encoders/date.rb | 2 +- activesupport/lib/active_support/json/encoders/date_time.rb | 2 +- .../lib/active_support/json/encoders/enumerable.rb | 4 ++-- .../lib/active_support/json/encoders/false_class.rb | 2 +- activesupport/lib/active_support/json/encoders/hash.rb | 4 ++-- activesupport/lib/active_support/json/encoders/nil_class.rb | 2 +- activesupport/lib/active_support/json/encoders/numeric.rb | 2 +- activesupport/lib/active_support/json/encoders/object.rb | 6 +++--- activesupport/lib/active_support/json/encoders/regexp.rb | 2 +- activesupport/lib/active_support/json/encoders/string.rb | 2 +- activesupport/lib/active_support/json/encoders/symbol.rb | 4 ++-- activesupport/lib/active_support/json/encoders/time.rb | 2 +- .../lib/active_support/json/encoders/true_class.rb | 2 +- activesupport/lib/active_support/json/encoding.rb | 13 +++++++------ activesupport/lib/active_support/json/variable.rb | 2 +- activesupport/lib/active_support/time_with_zone.rb | 2 +- 17 files changed, 29 insertions(+), 28 deletions(-) diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index a44c144d53..1fbe012a95 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -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 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 :only => :name 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 :include => :posts option to users, # 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 diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index c02ee1a524..069842c6c3 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -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 -- cgit v1.2.3