-
Notifications
You must be signed in to change notification settings - Fork 21.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed issue with ActiveRecord serialize object as JSON #16059
Conversation
👍 from me on this. |
@@ -52,7 +52,9 @@ module ClassMethods | |||
def serialize(attr_name, class_name_or_coder = Object) | |||
include Behavior | |||
|
|||
coder = if [:load, :dump].all? { |x| class_name_or_coder.respond_to?(x) } | |||
coder = if class_name_or_coder == JSON |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though it's not needed ::JSON
may be nice here, to be very clear?
I think this probably also deserves a comment noting that we're consciously actually breaking our contract (by ignoring the #dump/#load present on the supplied object) to maintain better compatibility with 4.0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would anyone conceivably want to actually use the native ::JSON
serializer which this implementation will suppress?
An alternate (not sure if good/even possible idea but just throwing out there) is to alias within ActiveRecord::Base
namespace (or ActiveRecord
in general, or Rails even more general) JSON
to ActiveSupport::JSON
, making users always use ::JSON
if they want the native serializer.
👍 |
Fixed issue with ActiveRecord serialize object as JSON
Fixed issue with ActiveRecord serialize object as JSON Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/attribute_methods/serialization.rb
This reverts commit a030977. This needs more work before it would work correctly on master.
…ain) This reverts commit 6f3c64e. Conflicts: activerecord/CHANGELOG.md
…ialized-attr"" This reverts commit 6f3c64e. Conflicts: activerecord/CHANGELOG.md
With 4.1.x
serialize
started returning a string whenJSON
was passed as the second attribute. It will now return a hash as per previous versions (prior to 4.1.x)Example: An ActiveRecord model named
Post
with acomment
column that uses the serialize method to convert aComment
object toJSON
.When
JSON
is used withserialize
, ActiveRecord will use the newActiveRecord::Coders::JSON
coder which delegatesActiveRecord::Coders::JSON.dump/load
toActiveSupport::JSON.encode/decode
.Fixes #15594