HEX
Server: LiteSpeed
System: Linux us-phx-web1284.main-hosting.eu 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: u300739242 (300739242)
PHP: 8.2.30
Disabled: system, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail
Upload Files
File: //opt/alt/python37/lib/python3.7/site-packages/jsons/serializers/default_primitive.py
from typing import Optional, Any, NewType

from jsons.exceptions import SerializationError


def default_primitive_serializer(obj: object,
                                 cls: Optional[type] = None,
                                 **kwargs) -> object:
    """
    Serialize a primitive; simply return the given ``obj``.
    :param obj: the primitive.
    :param cls: the type of ``obj``.
    :return: ``obj``.
    """
    result = obj

    cls_ = cls
    if _is_newtype(cls):
        cls_ = cls.__supertype__

    if cls_ and obj is not None and not isinstance(obj, cls_):
        try:
            result = cls_(obj)
        except ValueError as err:
            raise SerializationError('Could not cast "{}" into "{}"'
                                     .format(obj, cls_.__name__)) from err
    return result


def _is_newtype(cls: Any) -> bool:
    try:
        # isinstance(cls, NewType) only works as of Python3.10.
        result = isinstance(cls, NewType)
    except TypeError:
        result = hasattr(cls, '__supertype__')
    return result