diff options
Diffstat (limited to 'activerecord/lib/active_record/serializers/json_serializer.rb')
-rw-r--r-- | activerecord/lib/active_record/serializers/json_serializer.rb | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/serializers/json_serializer.rb b/activerecord/lib/active_record/serializers/json_serializer.rb index 1fd65ed006..67e2b2abb3 100644 --- a/activerecord/lib/active_record/serializers/json_serializer.rb +++ b/activerecord/lib/active_record/serializers/json_serializer.rb @@ -1,8 +1,11 @@ +require 'active_support/json' + module ActiveRecord #:nodoc: module Serialization - def self.included(base) - base.cattr_accessor :include_root_in_json, :instance_writer => false - base.extend ClassMethods + extend ActiveSupport::Concern + + included do + cattr_accessor :include_root_in_json, :instance_writer => false end # Returns a JSON string representing the model. Some configuration is @@ -72,10 +75,11 @@ 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 @@ -84,9 +88,15 @@ module ActiveRecord #:nodoc: self end + private + # For compatibility with ActiveSupport::JSON.encode + def rails_to_json(options, *args) + to_json(options) + end + class JsonSerializer < ActiveRecord::Serialization::Serializer #:nodoc: def serialize - serializable_record.to_json + ActiveSupport::JSON.encode(serializable_record) end end |