serde icon indicating copy to clipboard operation
serde copied to clipboard

Implement `visit_i128` and `visit_u128` for ContentVisitor

Open appcypher opened this issue 3 years ago • 1 comments

I'm working with a library that only supports 128-bit integers for its data format. It is a self describing format which means it implements deserialize_any. It mostly works fine with serde out of the box because there is a custom visitor impl that implements the visit_*128 methods.

The issue I have only rears its head when I derive Deserialize on an untagged enum. The debugger drops me in a Content::deserialize, which calls deserializer.__deserialize_content(actually_private::T, visitor) that takes a ContentVisitor.

https://github.com/serde-rs/serde/blob/56bd3694227fa9d02e85ee1493f9709f241995e8/serde/src/private/de.rs#L297-L298

ContentVisitor does not implement visit_i128 and visit_u128. Which I assume is because there is no Content::I128 and Content::U128 variants.

https://github.com/serde-rs/serde/blob/56bd3694227fa9d02e85ee1493f9709f241995e8/serde/src/private/de.rs#L220-L249

So the default visit_*128 methods get called instead causing the error I end up getting.

https://github.com/serde-rs/serde/blob/e1c4517335cc5ce440b7d21bd76887c9fd31f5fa/serde/src/de/mod.rs#L1362-L1376

I implemented a quick solution that ought to be less intrusive but it ends up breaking multiple libraries, including serde_json, that use the Unexpected::* enum. In fact, in my case it broke so many libraries that I have decided to take a different approach to the problem.

https://github.com/serde-rs/serde/compare/master...appcypher:appcypher/content-visitor-i128

128-bit integers have been part of stable Rust for years now and with more people working with 128-bit integers, I think it is worth finding a permanent solution to this problem.

appcypher avatar Jun 20 '22 17:06 appcypher

Any obvious blockers on this? I am running into the same issue with untagged enums, caused by the same underlying missing variants.

In addition (and likely out of scope for this issue, but related) - is there a public API for hand-writing Deserialize implementations for my own untagged enums (mainly because I am trying to deserialize them with a type that implements DeserializeSeed)?

sunny-g avatar Sep 14 '22 02:09 sunny-g

+1. Just spent a few hours debugging the problem, thanks to untagged enums gobbling up error messages.

Would be happy to provide a fix, if need be.

appetrosyan avatar Mar 29 '23 20:03 appetrosyan