Pass-Through Classes and Containers

In Example 2, the DeterministicJsonSerializer passed through instances of some classes without altering them or adding class tags to them. This is because DeterministicJsonSerializer has the following default set of classes preconfigured as pass-through classes:

  • bool

  • int

  • float

  • str

  • bytes

  • None

Classes that should be treated this way can be added to the serializer with the pass_through_classes parameter or the method register_pass_through_class. Pass-through classes can also be removed with the method deregister_pass_through_class. Make sure however that objects of a removed class are handled appropriately using class hooks, predicate hooks or the configured fallback strategy.

Similarly, serialized data in form of lists is also returned without a class tag, while other containers are serialized as either list or dict together with a class tag (e.g. set, frozenset, dict, MappingProxy). This is because DeterministicJsonSerializer is configured to serialize a list by returning a list of the serialized elements, i.e. the type of the serialized object (here a list) doesn’t change, only its content. Any object whose type doesn’t change during serialization is returned without a class tag, as the target class for deserialization is already given by the type of the serialized data itself. This automatically also applies to pass-through classes.