From 8d64085138b1a2ff36b94267d0236868b287610e Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 26 Apr 2009 15:18:33 -0700 Subject: Only Object to_json alias is needed. Prefer nil options. --- activesupport/lib/active_support/json/encoders/date.rb | 2 -- .../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 | 14 +++++++------- .../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 | 2 +- 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 | 3 ++- 14 files changed, 12 insertions(+), 33 deletions(-) (limited to 'activesupport/lib/active_support/json') diff --git a/activesupport/lib/active_support/json/encoders/date.rb b/activesupport/lib/active_support/json/encoders/date.rb index 79c3957362..1aebdd2764 100644 --- a/activesupport/lib/active_support/json/encoders/date.rb +++ b/activesupport/lib/active_support/json/encoders/date.rb @@ -18,6 +18,4 @@ class Date %("#{strftime("%Y/%m/%d")}") end end - - alias to_json rails_to_json end diff --git a/activesupport/lib/active_support/json/encoders/date_time.rb b/activesupport/lib/active_support/json/encoders/date_time.rb index cdfc39b9f3..096e0dd36b 100644 --- a/activesupport/lib/active_support/json/encoders/date_time.rb +++ b/activesupport/lib/active_support/json/encoders/date_time.rb @@ -18,6 +18,4 @@ class DateTime strftime('"%Y/%m/%d %H:%M:%S %z"') end end - - alias to_json rails_to_json end diff --git a/activesupport/lib/active_support/json/encoders/enumerable.rb b/activesupport/lib/active_support/json/encoders/enumerable.rb index e1c3ec249d..6e0c3b6b9c 100644 --- a/activesupport/lib/active_support/json/encoders/enumerable.rb +++ b/activesupport/lib/active_support/json/encoders/enumerable.rb @@ -6,9 +6,7 @@ module Enumerable # # => users.to_json(:only => :name) # # will pass the :only => :name option to each user. - def rails_to_json(options = {}) #:nodoc: + def rails_to_json(options = nil) #:nodoc: "[#{map { |value| ActiveSupport::JSON.encode(value, options) } * ','}]" end - - alias to_json rails_to_json end diff --git a/activesupport/lib/active_support/json/encoders/false_class.rb b/activesupport/lib/active_support/json/encoders/false_class.rb index a7657cca37..4c47b3372e 100644 --- a/activesupport/lib/active_support/json/encoders/false_class.rb +++ b/activesupport/lib/active_support/json/encoders/false_class.rb @@ -2,6 +2,4 @@ class FalseClass def rails_to_json(options = nil) #:nodoc: 'false' end - - alias to_json rails_to_json end diff --git a/activesupport/lib/active_support/json/encoders/hash.rb b/activesupport/lib/active_support/json/encoders/hash.rb index 19b97d7b8c..cd8639c1dd 100644 --- a/activesupport/lib/active_support/json/encoders/hash.rb +++ b/activesupport/lib/active_support/json/encoders/hash.rb @@ -30,13 +30,15 @@ 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 = {}) #:nodoc: + def rails_to_json(options = nil) #:nodoc: hash_keys = self.keys - if except = options[:except] - hash_keys = hash_keys - Array.wrap(except) - elsif only = options[:only] - hash_keys = hash_keys & Array.wrap(only) + if options + if except = options[:except] + hash_keys = hash_keys - Array.wrap(except) + elsif only = options[:only] + hash_keys = hash_keys & Array.wrap(only) + end end result = '{' @@ -45,6 +47,4 @@ class Hash end * ',' result << '}' end - - alias to_json rails_to_json end diff --git a/activesupport/lib/active_support/json/encoders/nil_class.rb b/activesupport/lib/active_support/json/encoders/nil_class.rb index b31e1dd249..b7b63e26d5 100644 --- a/activesupport/lib/active_support/json/encoders/nil_class.rb +++ b/activesupport/lib/active_support/json/encoders/nil_class.rb @@ -2,6 +2,4 @@ class NilClass def rails_to_json(options = nil) #:nodoc: 'null' end - - alias to_json rails_to_json end diff --git a/activesupport/lib/active_support/json/encoders/numeric.rb b/activesupport/lib/active_support/json/encoders/numeric.rb index 491b330c39..b969902da0 100644 --- a/activesupport/lib/active_support/json/encoders/numeric.rb +++ b/activesupport/lib/active_support/json/encoders/numeric.rb @@ -2,6 +2,4 @@ class Numeric def rails_to_json(options = nil) #:nodoc: to_s end - - alias to_json rails_to_json end diff --git a/activesupport/lib/active_support/json/encoders/object.rb b/activesupport/lib/active_support/json/encoders/object.rb index d68f50562e..57603bd1e6 100644 --- a/activesupport/lib/active_support/json/encoders/object.rb +++ b/activesupport/lib/active_support/json/encoders/object.rb @@ -2,7 +2,7 @@ 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 rails_to_json(options = {}) + def rails_to_json(options = nil) ActiveSupport::JSON.encode(instance_values, options) end diff --git a/activesupport/lib/active_support/json/encoders/regexp.rb b/activesupport/lib/active_support/json/encoders/regexp.rb index 63ccd7c490..ff7dd67544 100644 --- a/activesupport/lib/active_support/json/encoders/regexp.rb +++ b/activesupport/lib/active_support/json/encoders/regexp.rb @@ -2,6 +2,4 @@ class Regexp def rails_to_json(options = nil) #:nodoc: inspect end - - alias to_json rails_to_json end diff --git a/activesupport/lib/active_support/json/encoders/string.rb b/activesupport/lib/active_support/json/encoders/string.rb index 27bef3b9cc..da16111fd5 100644 --- a/activesupport/lib/active_support/json/encoders/string.rb +++ b/activesupport/lib/active_support/json/encoders/string.rb @@ -33,6 +33,4 @@ class String s.unpack("U*").pack("n*").unpack("H*")[0].gsub(/.{4}/, '\\\\u\&') } + '"' end - - alias to_json rails_to_json end diff --git a/activesupport/lib/active_support/json/encoders/symbol.rb b/activesupport/lib/active_support/json/encoders/symbol.rb index 6487bf8cb7..c39eda81c1 100644 --- a/activesupport/lib/active_support/json/encoders/symbol.rb +++ b/activesupport/lib/active_support/json/encoders/symbol.rb @@ -1,7 +1,5 @@ class Symbol - def rails_to_json(options = {}) #:nodoc: + def rails_to_json(options = nil) #:nodoc: ActiveSupport::JSON.encode(to_s, options) end - - alias to_json rails_to_json end diff --git a/activesupport/lib/active_support/json/encoders/time.rb b/activesupport/lib/active_support/json/encoders/time.rb index 2699672949..1cb0808490 100644 --- a/activesupport/lib/active_support/json/encoders/time.rb +++ b/activesupport/lib/active_support/json/encoders/time.rb @@ -20,6 +20,4 @@ class Time %("#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}") end end - - alias to_json rails_to_json end diff --git a/activesupport/lib/active_support/json/encoders/true_class.rb b/activesupport/lib/active_support/json/encoders/true_class.rb index ac7c7d1e87..a22c487a93 100644 --- a/activesupport/lib/active_support/json/encoders/true_class.rb +++ b/activesupport/lib/active_support/json/encoders/true_class.rb @@ -2,6 +2,4 @@ class TrueClass def rails_to_json(options = nil) #:nodoc: 'true' end - - alias to_json rails_to_json end diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb index e8e9308495..acccb62aea 100644 --- a/activesupport/lib/active_support/json/encoding.rb +++ b/activesupport/lib/active_support/json/encoding.rb @@ -4,7 +4,8 @@ module ActiveSupport end # Converts a Ruby object into a JSON string. - def self.encode(value, options = {}) + 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 -- cgit v1.2.3