aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/serializers/json_serializer.rb
diff options
context:
space:
mode:
authorrick <technoweenie@gmail.com>2009-04-23 00:08:40 -0700
committerrick <technoweenie@gmail.com>2009-04-23 00:08:40 -0700
commit3c4c6bd0df598f865f49a983b4c65c415af4bcfc (patch)
tree0984e610d94f22dfa70e95259f1a81e57e7f9db7 /activerecord/lib/active_record/serializers/json_serializer.rb
parentbab2bfa69220ca1b6c7b56dccc79cf8e41245306 (diff)
downloadrails-3c4c6bd0df598f865f49a983b4c65c415af4bcfc.tar.gz
rails-3c4c6bd0df598f865f49a983b4c65c415af4bcfc.tar.bz2
rails-3c4c6bd0df598f865f49a983b4c65c415af4bcfc.zip
* Add pluggable JSON backends with support for the JSON gem. [rick]
Example: ActiveSupport::JSON.backend = "JSONGem" All internal Rails JSON encoding is now handled by ActiveSupport::JSON.encode(). Use of #to_json is not recommended, as it may clash with other libraries that overwrite it. However, you can recover Rails specific functionality if you really want to use #to_json. gem 'json' ActiveSupport::JSON.backend = "JSONGem" class ActiveRecord::Base alias to_json rails_to_json end
Diffstat (limited to 'activerecord/lib/active_record/serializers/json_serializer.rb')
-rw-r--r--activerecord/lib/active_record/serializers/json_serializer.rb10
1 files changed, 7 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/serializers/json_serializer.rb b/activerecord/lib/active_record/serializers/json_serializer.rb
index c709c3f1eb..e9cb8bfca9 100644
--- a/activerecord/lib/active_record/serializers/json_serializer.rb
+++ b/activerecord/lib/active_record/serializers/json_serializer.rb
@@ -74,13 +74,17 @@ module ActiveRecord #:nodoc:
# {"comments": [{"body": "Don't think too hard"}],
# "title": "So I was thinking"}]}
def to_json(options = {})
+ json = JsonSerializer.new(self, options).to_s
if include_root_in_json
- "{#{self.class.json_class_name}: #{JsonSerializer.new(self, options).to_s}}"
+ "{#{self.class.json_class_name}:#{json}}"
else
- JsonSerializer.new(self, options).to_s
+ json
end
end
+ # For compatibility with ActiveSupport::JSON.encode
+ alias rails_to_json to_json
+
def from_json(json)
self.attributes = ActiveSupport::JSON.decode(json)
self
@@ -88,7 +92,7 @@ module ActiveRecord #:nodoc:
class JsonSerializer < ActiveRecord::Serialization::Serializer #:nodoc:
def serialize
- serializable_record.to_json
+ ActiveSupport::JSON.encode(serializable_record)
end
end