-
-
Notifications
You must be signed in to change notification settings - Fork 39
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
Skip default value members on serialization #11
Comments
This work-around does not work on nested member/classes though: import json
from typing import Optional, Mapping
from types import MappingProxyType
from mashumaro import DataClassJSONMixin
from mashumaro.serializer.json import Encoder,EncodedData,DEFAULT_DICT_PARAMS,T
@dataclass
class MyClass(DataClassJSONMixin):
name: str
soul: str = None
def to_json(
self: T,
encoder: Optional[Encoder] = json.dumps,
dict_params: Optional[Mapping] = MappingProxyType({}),
**encoder_kwargs) -> EncodedData:
dct = self.to_dict(**dict(DEFAULT_DICT_PARAMS, **dict_params))
for key,f in self.__class__.__dataclass_fields__.items():
if dct[key] == f.default:
dct.pop(key)
return encoder(
dct,
**encoder_kwargs
)
c=MyClass('itseme')
print(to_json()) |
Hi @atteneder Your solution seems to work with nested classes as well. @dataclass
class MyClassChild(MyClass, DataClassJSONMixin):
another: str = ''
c = MyClassChild('itseme')
print(c.to_json()) # {"name": "itseme"}
@dataclass
class Parent(DataClassJSONMixin):
a: str
b: str = None
def to_json(
self: T,
encoder: Optional[Encoder] = json.dumps,
dict_params: Optional[Mapping] = MappingProxyType({}),
**encoder_kwargs) -> EncodedData:
dct = self.to_dict(**dict(DEFAULT_DICT_PARAMS, **dict_params))
for key,f in self.__class__.__dataclass_fields__.items():
if dct[key] == f.default:
dct.pop(key)
return encoder(
dct,
**encoder_kwargs
)
@dataclass
class Child(Parent, DataClassJSONMixin):
c: str = None
c = Child('a')
print(c.to_json()) # {"a": "a"} Could you write an example of what doesn't work? |
Sure! What I meant was nested dataclasses: import json
from dataclasses import dataclass
from typing import Optional, Mapping
from types import MappingProxyType
from mashumaro import DataClassJSONMixin
from mashumaro.serializer.json import Encoder,EncodedData,DEFAULT_DICT_PARAMS,T
@dataclass
class SkipperBase(DataClassJSONMixin):
def to_json(
self: T,
encoder: Optional[Encoder] = json.dumps,
dict_params: Optional[Mapping] = MappingProxyType({}),
**encoder_kwargs) -> EncodedData:
dct = self.to_dict(**dict(DEFAULT_DICT_PARAMS, **dict_params))
for key,f in self.__class__.__dataclass_fields__.items():
if dct[key] == f.default:
dct.pop(key)
return encoder(
dct,
**encoder_kwargs
)
@dataclass
class MyClass(SkipperBase):
name: str
soul: str = None
@dataclass
class MyParentClass(SkipperBase):
child: MyClass = None
p=MyParentClass()
p.child = MyClass('itseme')
print(p.to_json()) output: That's because the child is converted to a dict (with all default params as well) and serialized to JSON within the parent's I guess I could override the to_dict (so that it skips default members), but that's an odd default behaviour. Ideally What do you think? |
I think the additional parameter in |
Any updates on this? I need this too. |
Sorry, seems I won't have time to tinker with this. |
It's possible to skip from dataclasses import dataclass
from mashumaro import DataClassJSONMixin
from mashumaro.config import TO_DICT_ADD_OMIT_NONE_FLAG
@dataclass
class MyClass(DataClassJSONMixin):
name: str = None
soul: str = None
class Config:
code_generation_options = [TO_DICT_ADD_OMIT_NONE_FLAG]
c = MyClass()
c.name = 'itseme'
print(c.to_json(dict_params=dict(omit_none=True))) # {"name": "itseme"} At the moment this flag exists in the master branch but it will be released in the next version. |
Thank you very much for offering a solution! To me this is satisfactory. It seems to not work for non- @Fatal1ty Feel free to close the issue or keep it open for said shortcoming. Again, thanks for this library! |
Hi!
Is there a way to skip variables that are default values from serialization/dict conversion?
exampe:
output:
{"name": "itseme", "soul": null}
desired output :
{"name": "itseme"}
thanks!
The text was updated successfully, but these errors were encountered: