Hook Resolution Order

Class hooks are evaluated before Predicate Hooks. Class hooks work by dictionary lookup using functools.singledispatch’s resolution algorithm. Predicate-hook pairs are registered and stored in an ordered sequence and are evaluated in reverse order of registration, i.e. most recently registered predicates are evaluated first. This enables a hierarchical organization of predicate hook pairs from fine grained to increasing coarseness.

Serialization

Objects are serialized in the following order of preference:

  1. If the object is an instance of one of the registered pass_through_classes, the object is returned unaltered.

  2. If the object’s class or any of its base classes has a class serialization hook registered for it, the corresponding hook is used.

  3. If any of the registered serialization predicates evaluate to True on the object, its corresponding serialization hook is used.

  4. If none of the above apply, the fallback strategy is applied

Deserialization

Likewise, objects are deserialized in the following order of preference, where cls is the target class corresponding to the class tag contained in the serialized data:

  1. If the serialized data is an instance of one of the registered pass_through_classes, the data is returned unaltered.

  2. If cls or any of its base classes has a class deserialization hook registered for it, the corresponding hook is used.

  3. If any of the registered deserialization predicates evaluate to True on cls or the serialized data, the corresponding deserialization hook is used.

  4. If none of the above apply, the fallback strategy is applied