2017-10-17 08:05:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-09 15:23:05 -05:00
|
|
|
require "set"
|
|
|
|
|
2017-10-17 08:05:05 -04:00
|
|
|
module ActiveJob
|
|
|
|
# The <tt>ActiveJob::Serializers</tt> module is used to store a list of known serializers
|
2018-02-16 17:41:54 -05:00
|
|
|
# and to add new ones. It also has helpers to serialize/deserialize objects.
|
|
|
|
module Serializers # :nodoc:
|
2017-10-17 08:05:05 -04:00
|
|
|
extend ActiveSupport::Autoload
|
|
|
|
|
|
|
|
autoload :ObjectSerializer
|
2018-02-09 15:45:11 -05:00
|
|
|
autoload :SymbolSerializer
|
|
|
|
autoload :DurationSerializer
|
2018-02-16 17:15:11 -05:00
|
|
|
autoload :DateTimeSerializer
|
2018-02-09 16:50:57 -05:00
|
|
|
autoload :DateSerializer
|
2018-02-16 17:15:11 -05:00
|
|
|
autoload :TimeWithZoneSerializer
|
2018-02-09 16:50:57 -05:00
|
|
|
autoload :TimeSerializer
|
2017-10-17 08:05:05 -04:00
|
|
|
|
2018-02-09 14:24:55 -05:00
|
|
|
mattr_accessor :_additional_serializers
|
2018-02-09 15:23:05 -05:00
|
|
|
self._additional_serializers = Set.new
|
2018-02-09 14:24:55 -05:00
|
|
|
|
|
|
|
class << self
|
|
|
|
# Returns serialized representative of the passed object.
|
|
|
|
# Will look up through all known serializers.
|
2018-02-16 17:41:54 -05:00
|
|
|
# Raises <tt>ActiveJob::SerializationError</tt> if it can't find a proper serializer.
|
2018-02-09 14:24:55 -05:00
|
|
|
def serialize(argument)
|
|
|
|
serializer = serializers.detect { |s| s.serialize?(argument) }
|
|
|
|
raise SerializationError.new("Unsupported argument type: #{argument.class.name}") unless serializer
|
|
|
|
serializer.serialize(argument)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns deserialized object.
|
|
|
|
# Will look up through all known serializers.
|
2018-02-16 17:41:54 -05:00
|
|
|
# If no serializer found will raise <tt>ArgumentError</tt>.
|
2018-02-09 14:24:55 -05:00
|
|
|
def deserialize(argument)
|
2018-02-12 14:16:41 -05:00
|
|
|
serializer_name = argument[Arguments::OBJECT_SERIALIZER_KEY]
|
|
|
|
raise ArgumentError, "Serializer name is not present in the argument: #{argument.inspect}" unless serializer_name
|
|
|
|
|
|
|
|
serializer = serializer_name.safe_constantize
|
2018-02-16 17:45:17 -05:00
|
|
|
raise ArgumentError, "Serializer #{serializer_name} is not known" unless serializer
|
2018-02-12 14:16:41 -05:00
|
|
|
|
2018-02-09 14:24:55 -05:00
|
|
|
serializer.deserialize(argument)
|
|
|
|
end
|
2017-10-17 08:05:05 -04:00
|
|
|
|
2018-02-16 17:41:54 -05:00
|
|
|
# Returns list of known serializers.
|
2017-10-17 08:05:05 -04:00
|
|
|
def serializers
|
2018-02-09 14:24:55 -05:00
|
|
|
self._additional_serializers
|
2017-10-17 08:05:05 -04:00
|
|
|
end
|
|
|
|
|
2018-02-16 17:41:54 -05:00
|
|
|
# Adds new serializers to a list of known serializers.
|
2017-10-23 09:29:28 -04:00
|
|
|
def add_serializers(*new_serializers)
|
2018-02-15 01:46:42 -05:00
|
|
|
self._additional_serializers += new_serializers.flatten
|
2017-10-17 08:05:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-12 14:16:41 -05:00
|
|
|
add_serializers SymbolSerializer,
|
2018-02-09 16:50:57 -05:00
|
|
|
DurationSerializer,
|
|
|
|
DateTimeSerializer,
|
|
|
|
DateSerializer,
|
2018-02-16 17:15:11 -05:00
|
|
|
TimeWithZoneSerializer,
|
2018-02-09 16:50:57 -05:00
|
|
|
TimeSerializer
|
2017-10-17 08:05:05 -04:00
|
|
|
end
|
|
|
|
end
|