def default_object_serializer( obj: object, key_transformer: Optional[Callable[[str], str]] = None, strip_nulls: bool = False, strip_privates: bool = False, strip_properties: bool = False, strip_class_variables: bool = False, verbose: Union[Verbosity, bool] = False, **kwargs) -> dict: """ Serialize the given ``obj`` to a dict. All values within ``obj`` are also serialized. If ``key_transformer`` is given, it will be used to transform the casing (e.g. snake_case) to a different format (e.g. camelCase). :param obj: the object that is to be serialized. :param key_transformer: a function that will be applied to all keys in the resulting dict. :param strip_nulls: if ``True`` the resulting dict will not contain null values. :param strip_privates: if ``True`` the resulting dict will not contain private attributes (i.e. attributes that start with an underscore). :param strip_properties: if ``True`` the resulting dict will not contain values from @properties. :param strip_class_variables: if ``True`` the resulting dict will not contain values from class variables. :param verbose: if ``True`` the resulting dict will contain meta information (e.g. on how to deserialize). :param kwargs: any keyword arguments that are to be passed to the serializer functions. :return: a Python dict holding the values of ``obj``. """ if obj is None: return obj cls = kwargs['cls'] or obj.__class__ obj_dict = _get_dict_from_obj(obj, strip_privates, strip_properties, strip_class_variables, **kwargs) kwargs_ = {**kwargs, 'verbose': verbose} verbose = Verbosity.from_value(verbose) if Verbosity.WITH_CLASS_INFO in verbose: kwargs_['_store_cls'] = True result = default_dict_serializer( obj_dict, key_transformer=key_transformer, strip_nulls=strip_nulls, strip_privates=strip_privates, strip_properties=strip_properties, strip_class_variables=strip_class_variables, **kwargs_) cls_name = get_class_name(cls, fully_qualified=True, fork_inst=kwargs['fork_inst']) if kwargs.get('_store_cls'): result['-cls'] = cls_name else: result = _get_dict_with_meta(result, cls_name, verbose, kwargs['fork_inst']) return result
def default_object_serializer(obj: object, cls: Optional[type] = None, *, key_transformer: Optional[Callable[[str], str]] = None, strip_nulls: bool = False, strip_privates: bool = False, strip_properties: bool = False, strip_class_variables: bool = False, strip_attr: Union[str, MutableSequence[str], Tuple[str]] = None, verbose: Union[Verbosity, bool] = False, strict: bool = False, fork_inst: Optional[type] = StateHolder, **kwargs) -> Optional[dict]: """ Serialize the given ``obj`` to a dict. All values within ``obj`` are also serialized. If ``key_transformer`` is given, it will be used to transform the casing (e.g. snake_case) to a different format (e.g. camelCase). :param obj: the object that is to be serialized. :param cls: the type of the object that is to be dumped. :param key_transformer: a function that will be applied to all keys in the resulting dict. :param strip_nulls: if ``True`` the resulting dict will not contain null values. :param strip_privates: if ``True`` the resulting dict will not contain private attributes (i.e. attributes that start with an underscore). :param strip_properties: if ``True`` the resulting dict will not contain values from @properties. :param strip_class_variables: if ``True`` the resulting dict will not contain values from class variables. :param strip_attr: can be a name or a collection of names of attributes that are not to be included in the dump. :param verbose: if ``True`` the resulting dict will contain meta information (e.g. on how to deserialize). :param strict: a bool to determine if the serializer should be strict (i.e. only dumping stuff that is known to ``cls``). :param fork_inst: if given, it uses this fork of ``JsonSerializable``. :param kwargs: any keyword arguments that are to be passed to the serializer functions. :return: a Python dict holding the values of ``obj``. """ strip_attr = _normalize_strip_attr(strip_attr) if cls and strict: attributes = _get_attributes_from_class(cls, strip_privates, strip_properties, strip_class_variables, strip_attr, strict) else: attributes = _get_attributes_from_object(obj, strip_privates, strip_properties, strip_class_variables, strip_attr, strict) cls = obj.__class__ verbose = Verbosity.from_value(verbose) kwargs_ = { **kwargs, 'fork_inst': fork_inst, 'verbose': verbose, 'strict': strict, # Set a flag in kwargs to temporarily store -cls: '_store_cls': Verbosity.WITH_CLASS_INFO in verbose } result = _do_serialize(obj=obj, cls=cls, attributes=attributes, kwargs=kwargs_, key_transformer=key_transformer, strip_nulls=strip_nulls, strip_privates=strip_privates, strip_properties=strip_properties, strip_class_variables=strip_class_variables, strip_attr=strip_attr, strict=strict, fork_inst=fork_inst) cls_name = get_class_name(cls, fully_qualified=True) if not kwargs.get('_store_cls'): result = _get_dict_with_meta(result, cls_name, verbose, fork_inst) return result
def default_object_serializer(obj: object, cls: Optional[type] = None, *, key_transformer: Optional[Callable[[str], str]] = None, strip_nulls: bool = False, strip_privates: bool = False, strip_properties: bool = False, strip_class_variables: bool = False, strip_attr: Union[str, MutableSequence[str], Tuple[str]] = None, verbose: Union[Verbosity, bool] = False, **kwargs) -> dict: """ Serialize the given ``obj`` to a dict. All values within ``obj`` are also serialized. If ``key_transformer`` is given, it will be used to transform the casing (e.g. snake_case) to a different format (e.g. camelCase). :param obj: the object that is to be serialized. :param cls: the type of the object that is to be dumped. :param key_transformer: a function that will be applied to all keys in the resulting dict. :param strip_nulls: if ``True`` the resulting dict will not contain null values. :param strip_privates: if ``True`` the resulting dict will not contain private attributes (i.e. attributes that start with an underscore). :param strip_properties: if ``True`` the resulting dict will not contain values from @properties. :param strip_class_variables: if ``True`` the resulting dict will not contain values from class variables. :param strip_attr: can be a name or a collection of names of attributes that are not to be included in the dump. dict will not contain attributes with :param verbose: if ``True`` the resulting dict will contain meta information (e.g. on how to deserialize). :param kwargs: any keyword arguments that are to be passed to the serializer functions. :return: a Python dict holding the values of ``obj``. """ if obj is None: return obj strip_attr = strip_attr or [] if (not isinstance(strip_attr, MutableSequence) and not isinstance(strip_attr, tuple)): strip_attr = (strip_attr, ) cls = cls or obj.__class__ obj_dict = _get_dict_from_obj(obj, cls, strip_privates, strip_properties, strip_class_variables, strip_attr, **kwargs) kwargs_ = {**kwargs, 'verbose': verbose} verbose = Verbosity.from_value(verbose) if Verbosity.WITH_CLASS_INFO in verbose: # Set a flag in kwargs to temporarily store -cls. kwargs_['_store_cls'] = True result = default_dict_serializer( obj_dict, cls, key_transformer=key_transformer, strip_nulls=strip_nulls, strip_privates=strip_privates, strip_properties=strip_properties, strip_class_variables=strip_class_variables, strip_attr=strip_attr, **kwargs_) cls_name = get_class_name(cls, fully_qualified=True, fork_inst=kwargs['fork_inst']) if not kwargs.get('_store_cls'): result = _get_dict_with_meta(result, cls_name, verbose, kwargs['fork_inst']) return result