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

Is there an easy way to serialize / deserialize toots? #380

Open
haraldg opened this issue Sep 5, 2024 · 1 comment
Open

Is there an easy way to serialize / deserialize toots? #380

haraldg opened this issue Sep 5, 2024 · 1 comment

Comments

@haraldg
Copy link

haraldg commented Sep 5, 2024

I have been thinking, since mastodon already uses json and mastodonpy objects close to hashmaps, it should be quite easy, but alas with my naive attempt I get:

>>> json.dump(status[1], file)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.11/json/__init__.py", line 179, in dump
    for chunk in iterable:
  File "/usr/lib/python3.11/json/encoder.py", line 432, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "/usr/lib/python3.11/json/encoder.py", line 406, in _iterencode_dict
    yield from chunks
  File "/usr/lib/python3.11/json/encoder.py", line 439, in _iterencode
    o = _default(o)
        ^^^^^^^^^^^
  File "/usr/lib/python3.11/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type datetime is not JSON serializable

And when reading back I probably will run into issues, because, mastodonpy objects are like hashmaps but not pure hashmaps.

I know I can just store the ids and retrieve the toots from the server again. This would actually have the benefit of getting updated toots, eg when more replies have been added.

OTOH caching toots would be better for privacy. (Nobody can track when and how often a toot is displayed.) So I'd like to know, whether there is some way.

@halcy
Copy link
Owner

halcy commented Dec 1, 2024

In my own code, I use some custom hooks. I will try to add a mechanism for json <-> internal object conversion that doesn't lose data (which is more interesting now that there is a whole type system)

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