-
-
Notifications
You must be signed in to change notification settings - Fork 47
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
support for complex attributes like pandas dataframes or onnx models #279
Comments
Hi @zkurtz Thank you for your interest in this library! I look through your code and found this and this modules that do all the work for dataclasses with mashumaro mixins. If I am not mistaken, two solutions come to my mind:
from mashumaro.codecs.json import JSONEncoder
from mashumaro.dialect import Dialect
...
class DummioSerializationDialect(Dialect):
serialization_strategy = {
... # pandas and other type handlers here
}
def save(
data: DataClassJSONMixin,
*,
filepath: PathType,
) -> None:
"""Save a mashumaro dataclass instance to a json text file."""
encoder = JSONEncoder(
type(data), default_dialect= DummioSerializationDialect
)
json_str = encoder.encode(data)
assert isinstance(json_str, str), "expected a string from to_json()"
UPath(filepath).write_text(json_str) However, in terms of performance it is not wise to create disposable encoders and decoders every time you enter the function because it involves code generation. But if you are not in high load environment or you are in no hurry it could be a perfect solution for you that is simple and available right now.
mashumaro/mashumaro/core/meta/types/pack.py Lines 81 to 82 in 0e97dd7
mashumaro/mashumaro/core/meta/types/unpack.py Lines 108 to 109 in 0e97dd7
You can register handlers for third-party types in the image and likeness (but it's not a public API, so it can be changed): from typing import Optional
from dataclasses import dataclass
from mashumaro.core.meta.types.common import Expression, ValueSpec
from mashumaro.core.meta.types.pack import register as register_pack
from mashumaro.core.meta.types.unpack import register as register_unpack
from mashumaro.mixins.json import DataClassJSONMixin
class MyType:
def __init__(self, x: int):
self.x = x
def __repr__(self):
return f"MyType(x={self.x})"
@register_pack
def pack_my_type(spec: ValueSpec,
) -> Optional[Expression]:
if spec.type is MyType:
return f"{{'x': {spec.expression}.x }}"
@register_unpack
def unpack_my_type(spec: ValueSpec,
) -> Optional[Expression]:
if spec.type is MyType:
spec.builder.ensure_object_imported(MyType)
return f"MyType({spec.expression}['x'])"
@dataclass
class MyDataClass(DataClassJSONMixin):
my_type: MyType
x = MyDataClass.from_json('{"my_type": {"x": 1}}')
print(x) # MyDataClass(my_type=MyType(x=1))
print(x.to_json()) # {"my_type": {"x": 1}} You could register your handlers this way before importing any dataclasses with mashumaro mixins you rely on. This So, in order to be able to increase the priority of your handlers, we need to make the registries more flexible to external changes. The easiest way would be to add an optional argument |
Just trying to digest this, a few notes:
|
I've been developing classio, an experimental package for adding IO methods to relatively complex data classes that may have attributes like pandas data frames, onnx models, or other complex objects. I recently added support for attributes that are mashumaro yaml & json based classes. What I'm wondering is how hard it would be to add support for, say, a pandas.DataFrame-type attribute directly inside of mashumaro.
The text was updated successfully, but these errors were encountered: