Skip to content

Commit

Permalink
Revert rails_to_json -> to_json so we don't break compatibility
Browse files Browse the repository at this point in the history
[#2753 state:resolved]
  • Loading branch information
jeremy committed Jun 8, 2009
1 parent 4b4164e commit 4a78dae
Show file tree
Hide file tree
Showing 17 changed files with 59 additions and 26 deletions.
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/json/encoders/date.rb
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion activesupport/lib/active_support/json/encoders/enumerable.rb
Expand Up @@ -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
@@ -1,5 +1,5 @@
class FalseClass
def rails_to_json(options = nil) #:nodoc:
def to_json(options = nil) #:nodoc:
'false'
end
end
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/json/encoders/hash.rb
Expand Up @@ -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
Expand Down
@@ -1,5 +1,5 @@
class NilClass
def rails_to_json(options = nil) #:nodoc:
def to_json(options = nil) #:nodoc:
'null'
end
end
14 changes: 13 additions & 1 deletion 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
6 changes: 1 addition & 5 deletions 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
2 changes: 1 addition & 1 deletion 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
2 changes: 1 addition & 1 deletion 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
2 changes: 1 addition & 1 deletion 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
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/json/encoders/time.rb
Expand Up @@ -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
Expand Down
@@ -1,5 +1,5 @@
class TrueClass
def rails_to_json(options = nil) #:nodoc:
def to_json(options = nil) #:nodoc:
'true'
end
end
10 changes: 8 additions & 2 deletions 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
Expand Down
4 changes: 1 addition & 3 deletions activesupport/lib/active_support/json/variable.rb
Expand Up @@ -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
4 changes: 1 addition & 3 deletions activesupport/lib/active_support/time_with_zone.rb
Expand Up @@ -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)
Expand Down
19 changes: 18 additions & 1 deletion activesupport/test/json/encoding_test.rb
Expand Up @@ -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) ]]
Expand All @@ -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")']]
Expand Down Expand Up @@ -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.