def msgpackext_encode(obj: Any) -> Any: """ Encodes an object using pydantic and NumPy array serialization techniques suitable for msgpack. Parameters ---------- obj : Any Any object that can be serialized with pydantic and NumPy encoding techniques. Returns ------- Any A msgpack compatible form of the object. """ # First try pydantic base objects try: return pydantic_encoder(obj) except TypeError: pass if isinstance(obj, np.ndarray): if obj.shape: data = {b"_nd_": True, b"dtype": obj.dtype.str, b"data": np.ascontiguousarray(obj).tobytes()} if len(obj.shape) > 1: data[b"shape"] = obj.shape return data else: # Converts np.array(5) -> 5 return obj.tolist() return obj
def cast2dump(cls, result: Any) -> Any: if result is None: return None if isinstance(result, FieldInfo): field_info = result result = (smart_deepcopy(field_info.default) if field_info.default_factory is None else field_info.default_factory()) if result == Ellipsis: raise _InvalidArguments return result if isinstance(result, BaseModel): return cls.cast2dump(result.dict()) if isinstance(result, (int, float, str, bool, type(None))): return result if isinstance(result, Mapping): res_dict = {} for key, value in result.items(): try: res_dict[key] = cls.cast2dump(value) except _InvalidArguments: raise _InvalidArguments( Exception(f'Missing required argument: {key}')) return res_dict if isinstance(result, Iterable): res_list = [] for item in result: res_list.append(cls.cast2dump(item)) return res_list for enc_type, enc_func in ENCODERS_BY_TYPE.items(): if isinstance(result, enc_type): return enc_func(result) return pydantic_encoder(result)
def msgpack_encode(obj: Any) -> Any: r""" Encodes an object using pydantic. Converts numpy arrays to plain python lists Parameters ---------- obj : Any Any object that can be serialized with pydantic and NumPy encoding techniques. Returns ------- Any A msgpack compatible form of the object. """ try: return pydantic_encoder(obj) except TypeError: pass if isinstance(obj, np.ndarray): if obj.shape: return obj.ravel().tolist() else: return obj.tolist() return obj
def custom_encoder(obj: Any) -> Any: """ Custom encoder function to be passed to the default argument of json.dumps() :param obj: A pydantic object :return: An encoded pydantic object """ if isinstance(obj, SecretStr): return obj.get_secret_value() return pydantic_encoder(obj)
def default(self, obj: Any) -> Any: try: return pydantic_encoder(obj) except TypeError: pass if isinstance(obj, np.ndarray): if obj.shape: return obj.ravel().tolist() else: return obj.tolist() return json.JSONEncoder.default(self, obj)
def default(self, obj: Any) -> Any: try: return pydantic_encoder(obj) except TypeError: pass if isinstance(obj, np.ndarray): if obj.shape: data = {"_nd_": True, "dtype": obj.dtype.str, "data": np.ascontiguousarray(obj).tobytes().hex()} if len(obj.shape) > 1: data["shape"] = obj.shape return data else: # Converts np.array(5) -> 5 return obj.tolist() return json.JSONEncoder.default(self, obj)
def _encode_pydantic(obj): from pydantic.json import pydantic_encoder # json atoms if isinstance(obj, (str, int, float, bool)) or obj is None: return obj # json containers if isinstance(obj, dict): return { _encode_pydantic(k): _encode_pydantic(v) for k, v in obj.items() } if isinstance(obj, list): return [_encode_pydantic(i) for i in obj] # pydantic types return _encode_pydantic(pydantic_encoder(obj))
def jsonable_encoder( obj: Any, include: Set[str] = None, exclude: Set[str] = set(), by_alias: bool = False, include_none: bool = True, ) -> Any: if isinstance(obj, BaseModel): return jsonable_encoder( obj.dict(include=include, exclude=exclude, by_alias=by_alias), include_none=include_none, ) if isinstance(obj, Enum): return obj.value if isinstance(obj, (str, int, float, type(None))): return obj if isinstance(obj, dict): return { jsonable_encoder( key, by_alias=by_alias, include_none=include_none ): jsonable_encoder(value, by_alias=by_alias, include_none=include_none) for key, value in obj.items() if value is not None or include_none } if isinstance(obj, (list, set, frozenset, GeneratorType, tuple)): return [ jsonable_encoder( item, include=include, exclude=exclude, by_alias=by_alias, include_none=include_none, ) for item in obj ] return pydantic_encoder(obj)
def show_secrets_encoder(obj): if type(obj) == SecretStr: return obj.get_secret_value() else: return pydantic_encoder(obj)
def default(self, obj): try: return pydantic_encoder(obj) except TypeError: pass return super().default(obj)
def default(self, obj): if isinstance(obj, BaseModel): return pydantic_encoder(obj) if isinstance(obj, set): return list(obj) return json.JSONEncoder.default(self, obj)
def default(self, data): return pydantic_encoder(data)
def default(self, obj): try: encoder = self.ENCODER_BY_TYPE[type(obj)] except KeyError: return pydantic_encoder(obj) return encoder(obj)
def strategy_config_schema_encoder(o): if callable(o): return None else: return pydantic_encoder(o)