aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2009-06-08 19:25:56 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2009-06-08 19:37:51 -0700
commit6e73cf6bdf0ab3961f212735b4ac46f7e924fcd5 (patch)
treeaa9b07d8dbfeb56121cf7ff660c3ec8cff72f542 /activerecord/lib
parent99cf77be270e71408a14f0c00472517adb5981b2 (diff)
downloadrails-6e73cf6bdf0ab3961f212735b4ac46f7e924fcd5.tar.gz
rails-6e73cf6bdf0ab3961f212735b4ac46f7e924fcd5.tar.bz2
rails-6e73cf6bdf0ab3961f212735b4ac46f7e924fcd5.zip
Fix AR json encoding
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/serialization.rb5
-rw-r--r--activerecord/lib/active_record/serializers/json_serializer.rb32
2 files changed, 10 insertions, 27 deletions
diff --git a/activerecord/lib/active_record/serialization.rb b/activerecord/lib/active_record/serialization.rb
index 7959f2b510..23d085bea9 100644
--- a/activerecord/lib/active_record/serialization.rb
+++ b/activerecord/lib/active_record/serialization.rb
@@ -3,8 +3,9 @@ module ActiveRecord #:nodoc:
class Serializer #:nodoc:
attr_reader :options
- def initialize(record, options = {})
- @record, @options = record, options.dup
+ def initialize(record, options = nil)
+ @record = record
+ @options = options ? options.dup : {}
end
# To replicate the behavior in ActiveRecord#attributes,
diff --git a/activerecord/lib/active_record/serializers/json_serializer.rb b/activerecord/lib/active_record/serializers/json_serializer.rb
index 67e2b2abb3..e49cf59494 100644
--- a/activerecord/lib/active_record/serializers/json_serializer.rb
+++ b/activerecord/lib/active_record/serializers/json_serializer.rb
@@ -1,4 +1,5 @@
require 'active_support/json'
+require 'active_support/core_ext/module/model_naming'
module ActiveRecord #:nodoc:
module Serialization
@@ -74,36 +75,17 @@ module ActiveRecord #:nodoc:
# "title": "Welcome to the weblog"},
# {"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}:#{json}}"
- else
- json
- end
+ def encode_json(encoder)
+ hash = Serializer.new(self, encoder.options).serializable_record
+ hash = { self.class.model_name.element => hash } if include_root_in_json
+ ActiveSupport::JSON.encode(hash)
end
+ def as_json(options = nil) self end #:nodoc:
+
def from_json(json)
self.attributes = ActiveSupport::JSON.decode(json)
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
- ActiveSupport::JSON.encode(serializable_record)
- end
- end
-
- module ClassMethods
- def json_class_name
- @json_class_name ||= name.demodulize.underscore.inspect
- end
- end
end
end