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
I'm using dataclasses-json to load search configuration with a regular expression pattern from JSON. To speed up my application I compile regexp in __post_init__ and store it in a separate private field.
The problem is that I cannot use .to_json() after that because of compiled pattern, despite I don't need it in the output JSON.
Marking field as ClassVar/InitVar or omitting an annotation produces desired result, but I'd like to avoid misleading annotations.
Some tricks that excludes certain fields from dataclasses control (e.g. base class that is not a dataclass) should work, but I think they should be part of a dataclass and ignored only for serialization purposes, also it separates part of class definition into somewhere else.
Found a :paramref:dataclasses_json.cfg.config.exclude, which solves exactly this problem. It is not clear why it was not mentioned in the documentation and readme file.
Description
I'm using dataclasses-json to load search configuration with a regular expression pattern from JSON. To speed up my application I compile regexp in
__post_init__
and store it in a separate private field.The problem is that I cannot use
.to_json()
after that because of compiled pattern, despite I don't need it in the output JSON.My code:
Desired output:
{"pattern": "\\d+"}
Actual output:
TypeError: Object of type Pattern is not JSON serializable
Possible solution
Possible solution is a flag that causes dataclasses_json to skip private fields:
or a config parameter that removes field from output:
Alternatives
Marking field as ClassVar/InitVar or omitting an annotation produces desired result, but I'd like to avoid misleading annotations.
Some tricks that excludes certain fields from
dataclasses
control (e.g. base class that is not a dataclass) should work, but I think they should be part of a dataclass and ignored only for serialization purposes, also it separates part of class definition into somewhere else.Context
versions:
The text was updated successfully, but these errors were encountered: