Skip to content

preserve_order feature of serde_json crate enabled globally when using mongodb crate #1493

@dvn8weil

Description

@dvn8weil

Versions/Environment

  1. What version of Rust are you using?
  2. What operating system are you using?
  3. What versions of the driver and its dependencies are you using? (Run
    cargo pkgid mongodb & cargo pkgid bson)
  4. What version of MongoDB are you using? (Check with the MongoDB shell using db.version())
  5. What is your MongoDB topology (standalone, replica set, sharded cluster, serverless)?
rustup 1.28.2 (e4f3ad6f8 2025-04-28)

-> sw_vers
ProductName:            macOS
ProductVersion:         15.4.1
BuildVersion:           24E263

▶ cargo pkgid bson   
registry+https://github.com/rust-lang/crates.io-index#bson@2.15.0

▶ cargo pkgid mongodb
registry+https://github.com/rust-lang/crates.io-index#mongodb@3.3.0

mongodb version: 8.0.15
Standalone

Describe the bug

A clear and concise description of what the bug is.

When i am using mongodb crate in my rust project, it enables serde_json crate's feature preserve_order which then leads to my serialized strings becoming a certain way which then creates conflicts in other parts of my project.

BE SPECIFIC:

  • What is the expected behavior and what is actually happening?
  • Do you have any particular output that demonstrates this problem?
  • Do you have any ideas on why this may be happening that could give us a
    clue in the right direction?
  • Did this issue arise out of nowhere, or after an update (of the driver,
    server, and/or Rust)?
  • Are there multiple ways of triggering this bug (perhaps more than one
    function produce a crash)?
  • If you know how to reproduce this bug, please include a code snippet here:

I will provide a simple code snippet here to reproduce this issue :

main.rs :

use serde_json::{Map, Value, json};

fn main() {
    let mut map = Map::new();
    map.insert("ca".to_string(), json!("hello"));
    map.insert("ba".to_string(), json!(323));
    map.insert("x3".to_string(), json!("nice"));

    let v = Value::Object(map);
    println!("Default JSON: {}", serde_json::to_string(&v).unwrap());
}

with Cargo.toml as :

[package]
name = "serialize_versions"
version = "0.1.0"
edition = "2024"

[dependencies]
serde_json = { version = "1.0.140", default-features = false, features = ["std"] }
serde = { version = "1.0.219", features = ["derive", "rc"] }
mongodb = "3.3.0"

with this setup , the output i get on cargo run is :

Default JSON: {"ca":"hello","ba":323,"x3":"nice"}

Now, when i comment out the mongodb dependency inside Cargo.tom, the output i get is :

Default JSON: {"ba":323,"ca":"hello","x3":"nice"}

The issue is that serde_json's dep tree looks like :

│   │   ├── serde_json v1.0.145
│   │   │   ├── indexmap v2.11.4 (*)
│   │   │   ├── itoa v1.0.15
│   │   │   ├── memchr v2.7.5
│   │   │   ├── ryu v1.0.20
│   │   │   └── serde_core v1.0.226

an extra indexmap dependency that is enabled everywhere from what i have understood.

Thank you. Can someone please help me with this issue where the serialization of objects are affected because of the mongodb dependency.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions