Skip to Content Skip to Search

Base class for serializing and deserializing custom objects.

Example:

class MoneySerializer < ActiveJob::Serializers::ObjectSerializer
  def serialize(money)
    super("amount" => money.amount, "currency" => money.currency)
  end

  def deserialize(hash)
    Money.new(hash["amount"], hash["currency"])
  end

  def klass
    Money
  end
end
Methods
D
K
N
S
Included Modules

Class Public methods

new()

# File activejob/lib/active_job/serializers/object_serializer.rb, line 31
def initialize
  super
  @template = { Arguments::OBJECT_SERIALIZER_KEY => self.class.name }.freeze
end

Instance Public methods

deserialize(hash)

Deserializes an argument from a JSON primitive type.

# File activejob/lib/active_job/serializers/object_serializer.rb, line 47
def deserialize(hash)
  raise NotImplementedError, "#{self.class.name} should implement a public #deserialize(hash) method"
end

klass()

The class of the object that will be serialized.

# File activejob/lib/active_job/serializers/object_serializer.rb, line 52
def klass
  raise NotImplementedError, "#{self.class.name} should implement a public #klass method"
end

serialize(hash)

Serializes an argument to a JSON primitive type.

# File activejob/lib/active_job/serializers/object_serializer.rb, line 42
def serialize(hash)
  @template.merge(hash)
end

serialize?(argument)

Determines if an argument should be serialized by a serializer.

# File activejob/lib/active_job/serializers/object_serializer.rb, line 37
def serialize?(argument)
  argument.is_a?(klass)
end