OXIESEC PANEL
- Current Dir:
/
/
opt
/
alt
/
python37
/
lib
/
python3.7
/
site-packages
/
jsons
Server IP: 2a02:4780:11:1084:0:327f:3464:10
Upload:
Create Dir:
Name
Size
Modified
Perms
📁
..
-
09/05/2025 09:36:16 AM
rwxr-xr-x
📄
__init__.py
11.57 KB
03/16/2023 12:57:19 PM
rw-r--r--
📁
__pycache__
-
03/16/2023 12:57:19 PM
rwxr-xr-x
📄
_cache.py
1.23 KB
03/16/2023 12:57:19 PM
rw-r--r--
📄
_common_impl.py
5.89 KB
03/16/2023 12:57:19 PM
rw-r--r--
📄
_compatibility_impl.py
3.06 KB
03/16/2023 12:57:19 PM
rw-r--r--
📄
_datetime_impl.py
4.98 KB
03/16/2023 12:57:19 PM
rw-r--r--
📄
_dump_impl.py
4.01 KB
03/16/2023 12:57:19 PM
rw-r--r--
📄
_extra_impl.py
1.7 KB
03/16/2023 12:57:19 PM
rw-r--r--
📄
_fork_impl.py
1.39 KB
03/16/2023 12:57:19 PM
rw-r--r--
📄
_key_transformers.py
1.32 KB
03/16/2023 12:57:19 PM
rw-r--r--
📄
_lizers_impl.py
5.73 KB
03/16/2023 12:57:19 PM
rw-r--r--
📄
_load_impl.py
8.16 KB
03/16/2023 12:57:19 PM
rw-r--r--
📄
_multitasking.py
2.34 KB
03/16/2023 12:57:19 PM
rw-r--r--
📄
_package_info.py
376 bytes
03/16/2023 12:57:19 PM
rw-r--r--
📄
_transform_impl.py
1.48 KB
03/16/2023 12:57:19 PM
rw-r--r--
📄
_validation.py
2.64 KB
03/16/2023 12:57:19 PM
rw-r--r--
📁
classes
-
03/16/2023 12:57:19 PM
rwxr-xr-x
📄
decorators.py
6.62 KB
03/16/2023 12:57:19 PM
rw-r--r--
📁
deserializers
-
03/16/2023 12:57:19 PM
rwxr-xr-x
📄
exceptions.py
6.02 KB
03/16/2023 12:57:19 PM
rw-r--r--
📁
serializers
-
03/16/2023 12:57:19 PM
rwxr-xr-x
Editing: _lizers_impl.py
Close
""" PRIVATE MODULE: do not import (from) it directly. This module contains functionality for setting and getting serializers and deserializers. """ from typing import Optional, Dict, Sequence, Union from jsons._cache import cached from jsons._common_impl import StateHolder, get_class_name from jsons._compatibility_impl import get_naked_class def set_serializer( func: callable, cls: Union[type, Sequence[type]], high_prio: bool = True, fork_inst: type = StateHolder) -> None: """ Set a serializer function for the given type. You may override the default behavior of ``jsons.load`` by setting a custom serializer. The ``func`` argument must take one argument (i.e. the object that is to be serialized) and also a ``kwargs`` parameter. For example: >>> def func(obj, **kwargs): ... return dict() You may ask additional arguments between ``cls`` and ``kwargs``. :param func: the serializer function. :param cls: the type or sequence of types this serializer can handle. :param high_prio: determines the order in which is looked for the callable. :param fork_inst: if given, it uses this fork of ``JsonSerializable``. :return: None. """ if isinstance(cls, Sequence): for cls_ in cls: set_serializer(func, cls_, high_prio, fork_inst) elif cls: index = 0 if high_prio else len(fork_inst._classes_serializers) fork_inst._classes_serializers.insert(index, cls) cls_name = get_class_name(cls, fully_qualified=True) fork_inst._serializers[cls_name.lower()] = func else: fork_inst._serializers['nonetype'] = func def set_deserializer( func: callable, cls: Union[type, Sequence[type]], high_prio: bool = True, fork_inst: type = StateHolder) -> None: """ Set a deserializer function for the given type. You may override the default behavior of ``jsons.dump`` by setting a custom deserializer. The ``func`` argument must take two arguments (i.e. the dict containing the serialized values and the type that the values should be deserialized into) and also a ``kwargs`` parameter. For example: >>> def func(dict_, cls, **kwargs): ... return cls() You may ask additional arguments between ``cls`` and ``kwargs``. :param func: the deserializer function. :param cls: the type or sequence of types this serializer can handle. :param high_prio: determines the order in which is looked for the callable. :param fork_inst: if given, it uses this fork of ``JsonSerializable``. :return: None. """ if isinstance(cls, Sequence): for cls_ in cls: set_deserializer(func, cls_, high_prio, fork_inst) elif cls: index = 0 if high_prio else len(fork_inst._classes_deserializers) fork_inst._classes_deserializers.insert(index, cls) cls_name = get_class_name(cls, fully_qualified=True) fork_inst._deserializers[cls_name.lower()] = func else: fork_inst._deserializers['nonetype'] = func @cached def get_serializer( cls: type, fork_inst: Optional[type] = StateHolder) -> callable: """ Return the serializer function that would be used for the given ``cls``. :param cls: the type for which a serializer is to be returned. :param fork_inst: if given, it uses this fork of ``JsonSerializable``. :return: a serializer function. """ serializer = _get_lizer(cls, fork_inst._serializers, fork_inst._classes_serializers, fork_inst) return serializer @cached def get_deserializer( cls: type, fork_inst: Optional[type] = StateHolder) -> callable: """ Return the deserializer function that would be used for the given ``cls``. :param cls: the type for which a deserializer is to be returned. :param fork_inst: if given, it uses this fork of ``JsonSerializable``. :return: a deserializer function. """ deserializer = _get_lizer(cls, fork_inst._deserializers, fork_inst._classes_deserializers, fork_inst) return deserializer def _get_lizer( cls: type, lizers: Dict[str, callable], classes_lizers: list, fork_inst: type, recursive: bool = False) -> callable: cls_name = get_class_name(cls, str.lower, fully_qualified=True) lizer = (lizers.get(cls_name, None) or _get_lizer_by_parents(cls, lizers, classes_lizers, fork_inst)) if not lizer and not recursive and hasattr(cls, '__supertype__'): return _get_lizer(cls.__supertype__, lizers, classes_lizers, fork_inst, True) return lizer def _get_lizer_by_parents( cls: type, lizers: Dict[str, callable], classes_lizers: list, fork_inst: type) -> callable: result = None parents = _get_parents(cls, classes_lizers) if parents: pname = get_class_name(parents[0], str.lower, fully_qualified=True) result = lizers[pname] return result def _get_parents(cls: type, lizers: list) -> list: """ Return a list of serializers or deserializers that can handle a parent of ``cls``. :param cls: the type that :param lizers: a list of serializers or deserializers. :return: a list of serializers or deserializers. """ parents = [] naked_cls = get_naked_class(cls) for cls_ in lizers: try: if issubclass(naked_cls, cls_): parents.append(cls_) except (TypeError, AttributeError): pass # Some types do not support `issubclass` (e.g. Union). return parents