aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorGodfrey Chan <godfreykfc@gmail.com>2014-07-15 08:43:18 -0700
committerGodfrey Chan <godfreykfc@gmail.com>2014-07-15 08:43:18 -0700
commita66aeb84e215c8a6e365a7fd0197c5703a14f241 (patch)
treee7fc06a0499ba62d089fc36a4d5ea4a1d17e9e48 /activerecord/lib/active_record
parentdffc6a43f4df05039693380c5cd2bd8762fb0382 (diff)
downloadrails-a66aeb84e215c8a6e365a7fd0197c5703a14f241.tar.gz
rails-a66aeb84e215c8a6e365a7fd0197c5703a14f241.tar.bz2
rails-a66aeb84e215c8a6e365a7fd0197c5703a14f241.zip
Revert "Revert "Merge pull request #16059 from jenncoop/json-serialized-attr""
This reverts commit 6f3c64eeb1dc8288dae49f114aaf619adc7dcb7f. Conflicts: activerecord/CHANGELOG.md
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/attribute_methods/serialization.rb7
-rw-r--r--activerecord/lib/active_record/coders/json.rb13
2 files changed, 19 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/serialization.rb b/activerecord/lib/active_record/attribute_methods/serialization.rb
index 734d94865a..264ce2bdfa 100644
--- a/activerecord/lib/active_record/attribute_methods/serialization.rb
+++ b/activerecord/lib/active_record/attribute_methods/serialization.rb
@@ -37,7 +37,12 @@ module ActiveRecord
# serialize :preferences, Hash
# end
def serialize(attr_name, class_name_or_coder = Object)
- coder = if [:load, :dump].all? { |x| class_name_or_coder.respond_to?(x) }
+ # When ::JSON is used, force it to go through the Active Support JSON encoder
+ # to ensure special objects (e.g. Active Record models) are dumped correctly
+ # using the #as_json hook.
+ coder = if class_name_or_coder == ::JSON
+ Coders::JSON
+ elsif [:load, :dump].all? { |x| class_name_or_coder.respond_to?(x) }
class_name_or_coder
else
Coders::YAMLColumn.new(class_name_or_coder)
diff --git a/activerecord/lib/active_record/coders/json.rb b/activerecord/lib/active_record/coders/json.rb
new file mode 100644
index 0000000000..0f60b6dab2
--- /dev/null
+++ b/activerecord/lib/active_record/coders/json.rb
@@ -0,0 +1,13 @@
+module ActiveRecord
+ module Coders # :nodoc:
+ class JSON # :nodoc:
+ def self.dump(obj)
+ ActiveSupport::JSON.encode(obj)
+ end
+
+ def self.load(json)
+ ActiveSupport::JSON.decode(json)
+ end
+ end
+ end
+end