You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
in my program most of calculations are done with numpy and thus values are e.g of type np.int32 or numpy.ndarrays.
Consider following class:
@dataclass_json
@dataclass
class Results:
value: int
values: List[int]
results = Results()
results.value = some_numpy_computation_returning_numpy_int()
results.values = some_numpy_computation_returning_numpy_array()
json_data = results.to_json()
When running this code I would get a conversion error.
It would be very convenient if dataclassjson would be able to handle those errors, by passing in an extended json encoder which handles numpy datatypes.
In the dataclassjson code some cases are already dealt with the _ExtendedEncoder.
class _ExtendedEncoder(json.JSONEncoder):
def default(self, o) -> JSON:
result: JSON
if _isinstance_safe(o, Collection):
if _isinstance_safe(o, Mapping):
result = dict(o)
else:
result = list(o)
elif _isinstance_safe(o, datetime):
result = o.timestamp()
elif _isinstance_safe(o, UUID):
result = str(o)
elif _isinstance_safe(o, Enum):
result = o.value
else:
result = json.JSONEncoder.default(self, o)
return result
However, I cannot extend this decoder to support my needs, since cls=_ExtendedEncoder is not in the argument list of to_json.
class DataClassJsonMixin(abc.ABC):
"""
DataClassJsonMixin is an ABC that functions as a Mixin.
As with other ABCs, it should not be instantiated directly.
"""
def to_json(self,
*,
skipkeys: bool = False,
ensure_ascii: bool = True,
check_circular: bool = True,
allow_nan: bool = True,
indent: Optional[Union[int, str]] = None,
separators: Tuple[str, str] = None,
default: Callable = None,
sort_keys: bool = False,
**kw) -> str:
return json.dumps(_asdict(self),
cls=_ExtendedEncoder,
skipkeys=skipkeys,
ensure_ascii=ensure_ascii,
check_circular=check_circular,
allow_nan=allow_nan,
indent=indent,
separators=separators,
default=default,
sort_keys=sort_keys,
**kw)
Would it be possible to change it to:
class DataClassJsonMixin(abc.ABC):
"""
DataClassJsonMixin is an ABC that functions as a Mixin.
As with other ABCs, it should not be instantiated directly.
"""
def to_json(self,
*,
skipkeys: bool = False,
ensure_ascii: bool = True,
check_circular: bool = True,
allow_nan: bool = True,
indent: Optional[Union[int, str]] = None,
separators: Tuple[str, str] = None,
default: Callable = None,
sort_keys: bool = False,
cls: json.JSONEncoder=_ExtendedEncoder,
**kw) -> str:
return json.dumps(_asdict(self),
cls=cls,
skipkeys=skipkeys,
ensure_ascii=ensure_ascii,
check_circular=check_circular,
allow_nan=allow_nan,
indent=indent,
separators=separators,
default=default,
sort_keys=sort_keys,
**kw)
I know about the possibility to define an encoder via metadata, but instead of adding this to all of my fields of the dataclasses, I'd rather prefer a global solution.
The text was updated successfully, but these errors were encountered:
I second this request. I'm looking for a way to use my custom data types which can provide their own to/from JSON serialization in the class. It doesn't look like there's any way to do this today without adding the encoder/decoder metadata to every field of these types.
#470 does not fix this issue. That PR only explains how to use field metadata to specify the encoding, something OP was already aware of:
I know about the possibility to define an encoder via metadata, but instead of adding this to all of my fields of the dataclasses, I'd rather prefer a global solution.
Hi,
in my program most of calculations are done with numpy and thus values are e.g of type np.int32 or numpy.ndarrays.
Consider following class:
When running this code I would get a conversion error.
It would be very convenient if dataclassjson would be able to handle those errors, by passing in an extended json encoder which handles numpy datatypes.
In the dataclassjson code some cases are already dealt with the _ExtendedEncoder.
However, I cannot extend this decoder to support my needs, since cls=_ExtendedEncoder is not in the argument list of to_json.
Would it be possible to change it to:
I know about the possibility to define an encoder via metadata, but instead of adding this to all of my fields of the dataclasses, I'd rather prefer a global solution.
The text was updated successfully, but these errors were encountered: