Suppose I have a dataclass that looks something like this:
from dataclasses_json import DataClassJsonMixin
from dataclasses import dataclass
class ToyDataclass(DataClassJsonMixin):
name: str
value: float
The value field can be np.nan, which is meaningful in my setting. Sometimes, I need to read these from serialized format, where the np.nan cases are being stored as such:
'{"name": "testname", "value": null}'. The problem is that when I deserialize, null is converted to None instead of np.nan.
My goal is to write a custom deserializer that simply treats every null as a np.nan. I have tried and failed to do this a few times. For example, this does not work (it simply deserializes as None, like before):
from dataclasses_json import DataClassJsonMixin, config
from dataclasses import dataclass, field
class ToyDataclass(DataClassJsonMixin):
name: str
value: float = field(
metadata=config(
decoder=(lambda val: np.nan if val is None else val),
)
)
Also, I can't control that the serialized dataclass have "null" in them instead of "NaN" for the nans. This is a constraint I have to work around in my codebase. Any pointers would be much appreciated!