Skip to content
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

Enable custom type serialization in JSONDisk #285

Open
randomir opened this issue Aug 9, 2023 · 2 comments
Open

Enable custom type serialization in JSONDisk #285

randomir opened this issue Aug 9, 2023 · 2 comments

Comments

@randomir
Copy link

randomir commented Aug 9, 2023

Currently in JSONDisk, json.loads and json.dumps are used for keys/values (de-)serialization (with the stdlib json module). Both the json module and arguments to load/dump are hard-coded, making it hard to (de-)serialize custom/user types, like NumPy types, Pydantic models, even Python builtins like set or datetime.

To implement custom JSON serialization, one needs to practically rewrite JSONDisk class. While, instead, providing json.JSONEncoder/json.JSONDecoder subclassed would be a lot easier.

I'm imagining new JSONDisk arguments, json_encoder and json_decoder that could be provided by the user on instantiation and then used during (de-)serialization.

@grantjenks
Copy link
Owner

I would rather not add more arguments. What about changing references like “json.loads” into instance attributes like “self.json_loads” and making “json_loads = json.loads” as a class attribute. Then you can inherit and assign those attributes as you like.

@randomir
Copy link
Author

randomir commented Aug 9, 2023

That sounds good as well. But if generalizing it this way, it would be nice to define:

def json_dumps(obj: Any, **kwargs) -> bytes:
    return json.dumps(obj, **kwargs).encode('utf-8')

i.e. dumps method to return bytes instead of str that needs to be encoded.
That's because some libraries, like orjson, return already byte-encoded JSON.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants