Comments (3)
Hey @Chu-4hun, thanks for filing this issue! Unfortunately the serde helpers we provide are fairly brittle when it comes to working with optional/nested types. I discussed this issue with the team and we'd prefer not to add duplicates of the helpers for these types; however, we did discuss converting the helpers to serde_with::(De)SerializeAs
implementations to allow for more flexibility in use cases like yours. We don't have the bandwidth for this work right now (and it would be a breaking change), but I filed RUST-1748 for consideration in the next major version.
In the meantime, I'd recommend using your own custom serialization method. The following should do the trick:
fn serialize_object_id_option_as_hex_string<S: Serializer>(
val: &Option<ObjectId>,
serializer: S
) -> Result<S::Ok, S::Error> {
match val {
Some(oid) => oid.to_hex().serialize(serializer),
None => serializer.serialize_none(),
}
}
from bson-rust.
I'm trying to rewrite a backend in rust and this is one of the first thing I stumbled across.
We have a MongoDB collection where some documents have optional references to other documents.
These references can be null, so like the example above I'm using Option<ObjectId>
to store the value.
isabelatkinson's workaround does the job, but shouldn't this be included by default? Feels awkward to keep a helper snippet of code around for a basic task like this.
Please reconsider to support this somehow. Or make it at least clearer in the documentation and provide some guidance for people who just stumbled across this.
I mean, the main idea is to have a simple hex representation of an ObjectID (in my case, for a REST API). I imagine a lot of other developers are facing the same issue. If so, are there better workarounds for this?
Anyway, I'd like to +1 this.
from bson-rust.
Thanks for the reply. Glad I could help
from bson-rust.
Related Issues (20)
- Feature Request: Consider CompactString for Document keys HOT 1
- Number type (u16) field of struct throwing "invalid type: floating point `21010`, expected u16" HOT 2
- Use RawDocument/Buf in doc! HOT 3
- serde_with `SerializeAs` & `DeserializeAs` support for time crate HOT 2
- Serialising and deserialising a BSON document with Regex can change the document HOT 3
- Better Details for BSON Deserialization Errors HOT 5
- use simd to optimize uft8 validation. HOT 3
- Bson fails to serialize AND deserialize IPAddr keys in hashmaps HOT 3
- `js-sys` should not be used when targeting `wasm32-wasi` HOT 3
- add ObjectId deserialization from sequence to support messagepack HOT 8
- Inconsistency between documentation and code implementation HOT 3
- RUST-1892 incorrect serialization/deserializer on big endian machines ( s390x ) HOT 5
- Serialization issue with Uuid HOT 3
- RUST-1899 Deserialization with UUID HOT 5
- How to deserialize ISO formatted json into Bson::DateTime HOT 1
- Time's "large-dates" feature should be optional HOT 1
- document has bytes remaining that were not visited HOT 7
- Operating on BSON Decimal128 type HOT 2
- Add chrono::TimeDelta to mongodb::bson::DateTime HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bson-rust.