dataclasses-json
dataclasses-json copied to clipboard
Ignoring `None`/`null` when encoding
Is it possible to ignore NoneType (null) when encoding? As in, if I have a dataclass like:
@dataclass_json()
@dataclass
class Group:
account: str
thing: str
and I initialize it like: g = Group(account=None, thing='123'), I'd like to use g.to_json() to output as {"thing": 123} instead of {"account": null, "thing": 123}
Right now, I'm using this:
def IgnoreNull():
return field(
metadata=config(
exclude=lambda x : x is None
)
)
Then initializing fields like:
thing: int = IgnoreNull()
which works, however, I would like to apply this to an entire dataclass, instead of having to do it for every field.
Possible duplicate of #187.
@kevinhikaruevans If you use the global config option like here https://github.com/lidatong/dataclasses-json/issues/187#issuecomment-919992503 Will that be a solution?
Closing for now, feel free to reopen in the suggested approach doesn't work for some reason