Comments (3)
Hi @annmuor, thanks for opening this issue! The problem you're experiencing is due to the fact that a HashMap
will serialize to a Document
when converting to BSON, which only supports string keys. I added the following lines to your test case:
let doc: Document = bson::from_slice(&buf).unwrap();
dbg!("{}", doc);
and the output printed was:
doc = Document({
"field": Document({
"127.0.0.1": Int64(
10,
),
}),
})
which differs from how a Vec<IpAddr>
will be serialized because our serializer needs to serialize the IpAddr
keys as strings.
When you call bson::from_slice
with the buffer you've created, our raw BSON deserializer will try to deserialize the key string into the specified type, which is IpAddr
in your case. I looked into the Deserialize
implementation for IpAddr
, and it looks like deserializing from a string is only supported when the deserializer is human readable. Our deserializer for raw BSON is not human readable, so the deserializer tries to deserialize an enum and fails when it encounters a string. That said, our non-raw BSON deserializer is human readable, so changing the last two lines in your test to the following will work just fine:
let doc = bson::to_document(&test).unwrap();
let _: Test = bson::from_document(doc).unwrap();
I recommend using this approach instead to fix the error you're encountering. If that's not feasible, you'll likely need to write a custom deserialization method for your HashMap<IpAddr, usize>
field and use it in a deserialize_with
attribute. I can help out with writing that method if needed. Please let me know if you have any further questions!
from bson-rust.
There has not been any recent activity on this ticket, so we are marking it as stale. If we do not hear anything further from you, this issue will be automatically closed in one week.
from bson-rust.
Thank you very much.
from bson-rust.
Related Issues (20)
- serde_with version 3 is out, but bson library forces it still to 1 HOT 2
- Deserialize ObjectId from hex string? HOT 5
- Deserialization implementation for Document doesn't handle documents with extjson keys HOT 1
- 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
- Add Option<ObjectId> serialize_object_id_as_hex_string support HOT 3
- 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
- `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
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.