Skip to content
Browse files

Revert rails_to_json -> to_json so we don't break compatibility

[#2753 state:resolved]
  • Loading branch information...
1 parent 4b4164e commit 4a78dae2ab43b5ba94fa06b345e9edeb0305fe44 @jeremy jeremy committed Jun 8, 2009
View
2 activesupport/lib/active_support/json/encoders/date.rb
@@ -11,7 +11,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 to_json(options = nil)
if ActiveSupport.use_standard_json_time_format
%("#{strftime("%Y-%m-%d")}")
else
View
2 activesupport/lib/active_support/json/encoders/date_time.rb
@@ -11,7 +11,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 to_json(options = nil)
if ActiveSupport.use_standard_json_time_format
xmlschema.inspect
else
View
8 activesupport/lib/active_support/json/encoders/enumerable.rb
@@ -6,7 +6,13 @@ 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:
+ def to_json(options = nil) #:nodoc:
+ to_a.to_json(options)
+ end
+end
+
+class Array
+ def to_json(options = nil) #:nodoc:
"[#{map { |value| ActiveSupport::JSON.encode(value, options) } * ','}]"
end
end
View
2 activesupport/lib/active_support/json/encoders/false_class.rb
@@ -1,5 +1,5 @@
class FalseClass
- def rails_to_json(options = nil) #:nodoc:
+ def to_json(options = nil) #:nodoc:
'false'
end
end
View
2 activesupport/lib/active_support/json/encoders/hash.rb
@@ -28,7 +28,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 to_json(options = nil) #:nodoc:
hash_keys = self.keys
if options
View
2 activesupport/lib/active_support/json/encoders/nil_class.rb
@@ -1,5 +1,5 @@
class NilClass
- def rails_to_json(options = nil) #:nodoc:
+ def to_json(options = nil) #:nodoc:
'null'
end
end
View
14 activesupport/lib/active_support/json/encoders/numeric.rb
@@ -1,5 +1,17 @@
class Numeric
- def rails_to_json(options = nil) #:nodoc:
+ def to_json(options = nil) #:nodoc:
+ to_s
+ end
+end
+
+class Float
+ def to_json(options = nil) #:nodoc:
+ to_s
+ end
+end
+
+class Integer
+ def to_json(options = nil) #:nodoc:
to_s
end
end
View
6 activesupport/lib/active_support/json/encoders/object.rb
@@ -1,10 +1,6 @@
class Object
# Dumps object in JSON (JavaScript Object Notation). See www.json.org for more info.
- def rails_to_json(options = nil)
+ def to_json(options = nil)
ActiveSupport::JSON.encode(instance_values, options)
end
-
- def to_json(*args)
- rails_to_json(*args)
- end
end
View
2 activesupport/lib/active_support/json/encoders/regexp.rb
@@ -1,5 +1,5 @@
class Regexp
- def rails_to_json(options = nil) #:nodoc:
+ def to_json(options = nil) #:nodoc:
inspect
end
end
View
2 activesupport/lib/active_support/json/encoders/string.rb
@@ -1,5 +1,5 @@
class String
- def rails_to_json(options = nil) #:nodoc:
+ def to_json(options = nil) #:nodoc:
ActiveSupport::JSON::Encoding.escape(self)
end
end
View
2 activesupport/lib/active_support/json/encoders/symbol.rb
@@ -1,5 +1,5 @@
class Symbol
- def rails_to_json(options = nil) #:nodoc:
+ def to_json(options = nil) #:nodoc:
ActiveSupport::JSON.encode(to_s, options)
end
end
View
2 activesupport/lib/active_support/json/encoders/time.rb
@@ -11,7 +11,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 to_json(options = nil)
if ActiveSupport.use_standard_json_time_format
xmlschema.inspect
else
View
2 activesupport/lib/active_support/json/encoders/true_class.rb
@@ -1,5 +1,5 @@
class TrueClass
- def rails_to_json(options = nil) #:nodoc:
+ def to_json(options = nil) #:nodoc:
'true'
end
end
View
10 activesupport/lib/active_support/json/encoding.rb
@@ -1,15 +1,21 @@
+# Hack to load json gem first so we can overwrite its to_json.
+begin
+ require 'json'
+rescue LoadError
+end
+
module ActiveSupport
module JSON
class CircularReferenceError < StandardError
end
# Converts a Ruby object into a JSON string.
def self.encode(value, options = nil)
- options ||= {}
+ options = {} unless Hash === options
seen = (options[:seen] ||= [])
raise CircularReferenceError, 'object references itself' if seen.include?(value)
seen << value
- value.rails_to_json(options)
+ value.to_json(options)
ensure
seen.pop
end
View
4 activesupport/lib/active_support/json/variable.rb
@@ -2,11 +2,9 @@ module ActiveSupport
module JSON
# A string that returns itself as its JSON-encoded form.
class Variable < String
- def rails_to_json(options=nil)
+ def to_json(options=nil)
self
end
-
- alias to_json rails_to_json
end
end
end
View
4 activesupport/lib/active_support/time_with_zone.rb
@@ -120,16 +120,14 @@ def xmlschema(fraction_digits = 0)
# # 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 to_json(options = nil)
if ActiveSupport.use_standard_json_time_format
xmlschema.inspect
else
%("#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}")
end
end
- alias to_json rails_to_json
-
def to_yaml(options = {})
if options.kind_of?(YAML::Emitter)
utc.to_yaml(options)
View
19 activesupport/test/json/encoding_test.rb
@@ -8,6 +8,12 @@ def initialize(a, b)
end
end
+ class Custom
+ def to_json(options)
+ '"custom"'
+ end
+ end
+
TrueTests = [[ true, %(true) ]]
FalseTests = [[ false, %(false) ]]
NilTests = [[ nil, %(null) ]]
@@ -26,6 +32,7 @@ def initialize(a, b)
[ :"a b", %("a b") ]]
ObjectTests = [[ Foo.new(1, 2), %({\"a\":1,\"b\":2}) ]]
+ CustomTests = [[ Custom.new, '"custom"' ]]
VariableTests = [[ ActiveSupport::JSON::Variable.new('foo'), 'foo'],
[ ActiveSupport::JSON::Variable.new('alert("foo")'), 'alert("foo")']]
@@ -127,12 +134,22 @@ def with_env_tz(new_tz = 'US/Eastern')
end
class JsonOptionsTests < Test::Unit::TestCase
+ # The json extension passes internal state to to_json
+ def test_non_hash_options_should_be_tolerated
+ faux_internal_state_object = Object.new
+
+ value = Object.new
+ def value.to_json(options) options end
+
+ assert_kind_of Hash, ActiveSupport::JSON.encode(value, faux_internal_state_object)
+ end
+
def test_enumerable_should_passthrough_options_to_elements
json_options = { :include => :posts }
ActiveSupport::JSON.expects(:encode).with(1, json_options)
ActiveSupport::JSON.expects(:encode).with(2, json_options)
ActiveSupport::JSON.expects(:encode).with('foo', json_options)
- [1, 2, 'foo'].rails_to_json(json_options)
+ [1, 2, 'foo'].to_json(json_options)
end
end

0 comments on commit 4a78dae

Please sign in to comment.
Something went wrong with that request. Please try again.