Code Monkey home page Code Monkey logo

Comments (3)

isabelatkinson avatar isabelatkinson commented on August 17, 2024

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.

github-actions avatar github-actions commented on August 17, 2024

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.

annmuor avatar annmuor commented on August 17, 2024

Thank you very much.

from bson-rust.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.