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

Serialize for Tweet? #90

Closed
PSeitz opened this issue May 27, 2020 · 5 comments · Fixed by #99
Closed

Serialize for Tweet? #90

PSeitz opened this issue May 27, 2020 · 5 comments · Fixed by #99

Comments

@PSeitz
Copy link

PSeitz commented May 27, 2020

It seems for Tweet only Deserialize is implemented and not Serialize
https://github.com/egg-mode-rs/egg-mode/blob/master/src/tweet/mod.rs#L236

@adwhit
Copy link
Collaborator

adwhit commented May 28, 2020

Could you outline your use-case? It would not be possible to serialize the tweet in the exact form that it is received from the twitter servers as it is a lossy deserialization.

If you just want to dump the tweet e.g. to JSON for storage, I agree Serialize would be useful. Though, it might be better to create your own type that only plucks out the values you need to store.

@PSeitz
Copy link
Author

PSeitz commented May 28, 2020

Yes, storing them in a json format is the use case. Copying the values in a struct is what I'm doing currently, but it would be nice to have a default out of the box, since I guess this use case is quite common.

@QuietMisdreavus
Copy link
Collaborator

I'm hesitant about just deriving Serialize and calling it a day, for the thing @adwhit mentioned - the Serialize and Deserialize implementations wouldn't be symmetric. If you wanted to load the JSON back up into a Tweet instance, there would need to be another function to just load the fields up. The biggest thing that would cause a problem is the fact that egg-mode converts the text ranges in things like entities to be byte offsets instead of codepoint offsets like what Twitter returns. There would be no way to determine that this conversion had taken place without having some out-of-band indication, like a dummy field we put into Tweet that signals that it came from serializing a Tweet instead of being a raw JSON response.

...although now that i type that, that does seem like a solution. It would make the deserializer for Tweets much more complicated, since we'd need to make sure that RawTweet could handle both the response from Twitter and a round-trip serialization by writing an egg-mode Tweet out. (That, or read the whole thing to a serde_json::Value first, check for the dummy field, then either go through RawTweet or rip everything out manually depending on whether it's there. It would add the overhead of reading out the JSON first, but it would also be much easier to understand from a code-reading perspective.)

@hdevalence
Copy link
Contributor

I'd also like to have a Serialize implementation (use-case: caching tweets to avoid hitting API limits too much). I'd be happy to contribute a PR if there was an acceptable design for the implementation.

@QuietMisdreavus
Copy link
Collaborator

I had an idea on how to do this somewhat efficiently, and posted PR #99 with the implementation. I asked around and people pointed me to #[serde(untagged)], which enables you to load/save an enum solely by the contents of its variants, instead of a tag value. This seemed to work perfectly, with the drawback that error messages become opaque. Does this work for y'all?

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

Successfully merging a pull request may close this issue.

4 participants