aboutsummaryrefslogtreecommitdiffstats
path: root/activejob/lib/active_job/serializers.rb
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2018-02-12 14:16:41 -0500
committerRafael Mendonça França <rafaelmfranca@gmail.com>2018-02-14 13:10:08 -0500
commit69645cba727dfa1c18c666d2a2f1c0dedffde938 (patch)
treedc5fda6749d63a795d12459a8dc2d8994c5bfea6 /activejob/lib/active_job/serializers.rb
parent71721dc1c9b769d3c06317122dc88cad4a346580 (diff)
downloadrails-69645cba727dfa1c18c666d2a2f1c0dedffde938.tar.gz
rails-69645cba727dfa1c18c666d2a2f1c0dedffde938.tar.bz2
rails-69645cba727dfa1c18c666d2a2f1c0dedffde938.zip
Simplify the implementation of custom argument serializers
We can speed up things for the supported types by keeping the code in the way it was. We can also avoid to loop trough all serializers in the deserialization by trying to access the class already in the Hash. We could also speed up the custom serialization if we define the class that is going to be serialized when registering the serializers, but that will remove the possibility of defining a serialzer for a superclass and have the subclass serialized using it.
Diffstat (limited to 'activejob/lib/active_job/serializers.rb')
-rw-r--r--activejob/lib/active_job/serializers.rb61
1 files changed, 7 insertions, 54 deletions
diff --git a/activejob/lib/active_job/serializers.rb b/activejob/lib/active_job/serializers.rb
index dfd654175d..d9a130fa73 100644
--- a/activejob/lib/active_job/serializers.rb
+++ b/activejob/lib/active_job/serializers.rb
@@ -3,37 +3,13 @@
require "set"
module ActiveJob
- # Raised when an exception is raised during job arguments deserialization.
- #
- # Wraps the original exception raised as +cause+.
- class DeserializationError < StandardError
- def initialize #:nodoc:
- super("Error while trying to deserialize arguments: #{$!.message}")
- set_backtrace $!.backtrace
- end
- end
-
- # Raised when an unsupported argument type is set as a job argument. We
- # currently support NilClass, Integer, Fixnum, Float, String, TrueClass, FalseClass,
- # Bignum, BigDecimal, and objects that can be represented as GlobalIDs (ex: Active Record).
- # Raised if you set the key for a Hash something else than a string or
- # a symbol. Also raised when trying to serialize an object which can't be
- # identified with a Global ID - such as an unpersisted Active Record model.
- class SerializationError < ArgumentError; end
-
# The <tt>ActiveJob::Serializers</tt> module is used to store a list of known serializers
# and to add new ones. It also has helpers to serialize/deserialize objects
module Serializers
extend ActiveSupport::Autoload
extend ActiveSupport::Concern
- autoload :ArraySerializer
- autoload :BaseSerializer
- autoload :GlobalIDSerializer
- autoload :HashWithIndifferentAccessSerializer
- autoload :HashSerializer
autoload :ObjectSerializer
- autoload :StandardTypeSerializer
autoload :SymbolSerializer
autoload :DurationSerializer
autoload :DateSerializer
@@ -57,8 +33,12 @@ module ActiveJob
# Will look up through all known serializers.
# If no serializers found will raise `ArgumentError`
def deserialize(argument)
- serializer = serializers.detect { |s| s.deserialize?(argument) }
- raise ArgumentError, "Can only deserialize primitive arguments: #{argument.inspect}" unless serializer
+ 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
+ raise ArgumentError, "Serializer #{serializer_name} is not know" unless serializer
+
serializer.deserialize(argument)
end
@@ -71,36 +51,9 @@ module ActiveJob
def add_serializers(*new_serializers)
self._additional_serializers += new_serializers
end
-
- # Returns a list of reserved keys, which cannot be used as keys for a hash
- def reserved_serializers_keys
- RESERVED_KEYS
- end
end
- # :nodoc:
- GLOBALID_KEY = "_aj_globalid".freeze
- # :nodoc:
- SYMBOL_KEYS_KEY = "_aj_symbol_keys".freeze
- # :nodoc:
- WITH_INDIFFERENT_ACCESS_KEY = "_aj_hash_with_indifferent_access".freeze
- # :nodoc:
- OBJECT_SERIALIZER_KEY = "_aj_serialized"
-
- # :nodoc:
- RESERVED_KEYS = [
- GLOBALID_KEY, GLOBALID_KEY.to_sym,
- SYMBOL_KEYS_KEY, SYMBOL_KEYS_KEY.to_sym,
- WITH_INDIFFERENT_ACCESS_KEY, WITH_INDIFFERENT_ACCESS_KEY.to_sym,
- ]
- private_constant :RESERVED_KEYS
-
- add_serializers GlobalIDSerializer,
- StandardTypeSerializer,
- HashWithIndifferentAccessSerializer,
- HashSerializer,
- ArraySerializer,
- SymbolSerializer,
+ add_serializers SymbolSerializer,
DurationSerializer,
DateTimeSerializer,
DateSerializer,