Permalink
Browse files

AS json refactor, move to_json implementation to core_ext and a clean…

…up a bit the code
  • Loading branch information...
1 parent d7c1057 commit 4a0c514eb48b8e5d4ceffb4817661c182c2368a3 @spastorino spastorino committed with jeremy Jun 30, 2010
@@ -1,4 +1,3 @@
require 'active_support'
require 'active_support/time'
require 'active_support/core_ext'
-require 'active_support/json'
@@ -9,6 +9,7 @@
require 'active_support/core_ext/object/extending'
require 'active_support/core_ext/object/returning'
+require 'active_support/core_ext/object/to_json'
require 'active_support/core_ext/object/to_param'
require 'active_support/core_ext/object/to_query'
require 'active_support/core_ext/object/with_options'
@@ -0,0 +1,19 @@
+# Hack to load json gem first so we can overwrite its to_json.
+begin
+ require 'json'
+rescue LoadError
+end
+
+# The JSON gem adds a few modules to Ruby core classes containing :to_json definition, overwriting
+# their default behavior. That said, we need to define the basic to_json method in all of them,
+# otherwise they will always use to_json gem implementation, which is backwards incompatible in
+# several cases (for instance, the JSON implementation for Hash does not work) with inheritance
+# and consequently classes as ActiveSupport::OrderedHash cannot be serialized to json.
+[Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass].each do |klass|
+ klass.class_eval <<-RUBY, __FILE__, __LINE__
+ # Dumps object in JSON (JavaScript Object Notation). See www.json.org for more info.
+ def to_json(options = nil)
+ ActiveSupport::JSON.encode(self, options)
+ end
+ RUBY
+end
@@ -1,21 +1,16 @@
-# encoding: utf-8
+require 'active_support/core_ext/object/to_json'
+require 'active_support/core_ext/module/delegation'
+require 'active_support/deprecation'
+require 'active_support/json/variable'
+
require 'bigdecimal'
-require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/big_decimal/conversions' # for #to_s
+require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/hash/except'
require 'active_support/core_ext/hash/slice'
-require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/object/instance_variables'
-require 'active_support/deprecation'
-
require 'active_support/time'
-# Hack to load json gem first so we can overwrite its to_json.
-begin
- require 'json'
-rescue LoadError
-end
-
module ActiveSupport
class << self
delegate :use_standard_json_time_format, :use_standard_json_time_format=,
@@ -128,20 +123,6 @@ def escape(string)
end
end
-# The JSON gem adds a few modules to Ruby core classes containing :to_json definition, overwriting
-# their default behavior. That said, we need to define the basic to_json method in all of them,
-# otherwise they will always use to_json gem implementation, which is backwards incompatible in
-# several cases (for instance, the JSON implementation for Hash does not work) with inheritance
-# and consequently classes as ActiveSupport::OrderedHash cannot be serialized to json.
-[Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass].each do |klass|
- klass.class_eval <<-RUBY, __FILE__, __LINE__
- # Dumps object in JSON (JavaScript Object Notation). See www.json.org for more info.
- def to_json(options = nil)
- ActiveSupport::JSON.encode(self, options)
- end
- RUBY
-end
-
class Object
def as_json(options = nil) #:nodoc:
if respond_to?(:to_hash)
@@ -152,12 +133,6 @@ def as_json(options = nil) #:nodoc:
end
end
-# A string that returns itself as its JSON-encoded form.
-class ActiveSupport::JSON::Variable < String
- def as_json(options = nil) self end #:nodoc:
- def encode_json(encoder) self end #:nodoc:
-end
-
class TrueClass
AS_JSON = ActiveSupport::JSON::Variable.new('true').freeze
def as_json(options = nil) AS_JSON end #:nodoc:
@@ -2,10 +2,8 @@ module ActiveSupport
module JSON
# A string that returns itself as its JSON-encoded form.
class Variable < String
- private
- def rails_to_json(*)
- self
- end
+ def as_json(options = nil) self end #:nodoc:
+ def encode_json(encoder) self end #:nodoc:
end
end
end
@@ -1,5 +1,6 @@
require 'abstract_unit'
require 'active_support/json'
+require 'active_support/core_ext/object/to_json'
class OrderedHashTest < Test::Unit::TestCase
def setup

0 comments on commit 4a0c514

Please sign in to comment.