-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question about JsonSerializable.json method #173
Comments
Hi @nightan42643, The reason for class MyCollection(list, JsonSerializable):
...
c = MyCollection((1, 2, 3))
c.json # <-- this results in a list, not a dict.
In your case, you could override as follows: @dataclass
class Person(JsonSerializable
.with_dump(key_transformer=KEY_TRANSFORMER_CAMELCASE)
.with_load(key_transformer=KEY_TRANSFORMER_SNAKECASE)):
first_name: str
last_name: str
@property
def json(self) -> dict[str, str]:
return JsonSerializable.json.fget(self) Or maybe even like this: @dataclass
class Person(JsonSerializable
.with_dump(key_transformer=KEY_TRANSFORMER_CAMELCASE)
.with_load(key_transformer=KEY_TRANSFORMER_SNAKECASE)):
first_name: str
last_name: str
json: dict[str, str] = field(init=False, repr=False, compare=False) Alternatively, you could use arno_json = typing.cast(dict[str, str], arno.json) I hope this helps. |
@ramonhagenaars Very clear, thanks! |
Hey
When I defined a
dataclass
as a subclass ofJsonSerializable
, I can access itsjson
method. Butjson
method returns an objectarno_json
is an object that lint thinks. If I want let lint think it is adict
, I need to ...I am not sure if my understanding was correct: If I use
JsonSerializable
like above, I mean create a subclass of it. In this case, the objectjson
returned always is adict
? But I must do anisInstance
judgment, it looks a little weird.Is it possible to let the
json
method return adict
not anobject
?The text was updated successfully, but these errors were encountered: