module ActiveRecord::AttributeMethods::Serialization::ClassMethods

def serialize(attr_name, class_name_or_coder = Object)

end
serialize :preferences, Hash
class User < ActiveRecord::Base
# Serialize preferences as Hash using YAML coder.

end
serialize :preferences, JSON
class User < ActiveRecord::Base
# Serialize preferences using JSON as coder.

end
serialize :preferences
class User < ActiveRecord::Base
# Serialize a preferences attribute.

==== Example

or a class name that the object type should be equal to.
* +class_name_or_coder+ - Optional, a coder object, which responds to +.load+ and +.dump+
* +attr_name+ - The field name that should be serialized.

==== Parameters

domain objects, consider using the ActiveRecord::Attributes API.
For more complex cases, such as conversion to or from your application

case.
objects transparently. There is no need to use #serialize in this
converted between JSON object/array syntax and Ruby +Hash+ or +Array+
for you. For instance: +json+ and +jsonb+ types in PostgreSQL will be
Keep in mind that database adapters handle certain serialization tasks

+Array+, will always be persisted as null.
Empty objects as {}, in the case of +Hash+, or [], in the case of

Otherwise SerializationTypeMismatch will be raised.
serialized object must be of that class on assignment and retrieval.
serialization is done through YAML. If +class_name+ is specified, the
attribute using this method and it will be handled automatically. The
object, and retrieved as the same object, then specify the name of that
If you have an attribute that needs to be saved to the database as an
def serialize(attr_name, class_name_or_coder = Object)
  # 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(attr_name, class_name_or_coder)
  end
  decorate_attribute_type(attr_name, :serialize) do |type|
    if type_incompatible_with_serialize?(type, class_name_or_coder)
      raise ColumnNotSerializableError.new(attr_name, type)
    end
    Type::Serialized.new(type, coder)
  end
end

def type_incompatible_with_serialize?(type, class_name)

def type_incompatible_with_serialize?(type, class_name)
  type.is_a?(ActiveRecord::Type::Json) && class_name == ::JSON ||
    type.respond_to?(:type_cast_array, true) && class_name == ::Array
end