Class: ActiveJob::Serializers::ObjectSerializer

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/active_job/serializers/object_serializer.rb

Overview

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

  private

    def klass
      Money
    end
end

Instance Method Summary collapse

Instance Method Details

#deserialize(json) ⇒ Object

Deserializes an argument from a JSON primitive type.

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/active_job/serializers/object_serializer.rb', line 44

def deserialize(json)
  raise NotImplementedError
end

#serialize(hash) ⇒ Object

Serializes an argument to a JSON primitive type.



39
40
41
# File 'lib/active_job/serializers/object_serializer.rb', line 39

def serialize(hash)
  { Arguments::OBJECT_SERIALIZER_KEY => self.class.name }.merge!(hash)
end

#serialize?(argument) ⇒ Boolean

Determines if an argument should be serialized by a serializer.

Returns:

  • (Boolean)


34
35
36
# File 'lib/active_job/serializers/object_serializer.rb', line 34

def serialize?(argument)
  argument.is_a?(klass)
end