Serde_json issues on Linux - json! macro

Running on Linux Mint, serde_json seems unable to use it's own macros. The json! macro fails to work on Linux Mint, but works just fine on Win11. I was trying to serialize some data and couldn't get the example from the docs to work, and had an LLM generate a code snippet to test. After checking my dependencies for a while, I was unable to resolve the issue. I booted into Windows to test if it was an OS issue, and lo and behold it appears to be.

Code snippet if you would like to test:

use serde::{Serialize, Deserialize};
use serde_json::{json, Value};

#[derive(Serialize, Deserialize)]
struct Person {
    name: String,
    age: u32,
}

fn main() -> Result<(), serde_json::Error> {
    // Create a vector of Person structs
    let people = vec![
        Person { name: "Alice".to_string(), age: 30 },
        Person { name: "Bob".to_string(), age: 25 },
        Person { name: "Charlie".to_string(), age: 35 },
    ];

    // Create a vector to hold the JSON Values
    let mut json_objects: Vec<Value> = Vec::new();

    // Iterate over the people vector and add each serialized Person to json_objects
    for person in people {
        let person_value = serde_json::to_value(&person)?;
        json_objects.push(person_value);
    }

    // Create the final JSON object with the "people" key containing the array of objects
    let final_json = json!({
        "people": json_objects
    });

    println!("Serialized JSON: {}", final_json.to_string());

    Ok(())
}

The actual error is this:

error: cannot find macro `json_internal` in this scope
  --> src/main.rs:28:22
   |
28 |       let final_json = json!({
   |  ______________________^
29 | |         "people": json_objects
30 | |     });
   | |______^
   |
   = note: this error originates in the macro `json` (in Nightly builds, run with -Z macro-backtrace for more info)

Any ideas how to fix this? I've been seeing massive performance gains on my original script (200ms execution time vs 50ms) since switching from Windows to Linux, and would hate to have to switch back simply to make data parsing easier.

Any suggestions?

Your code snippet works fine for me on Ubuntu and in the playground FWIW (I don't know what OS it is running). I suggest posting your Rust version and your Cargo.toml.

I'm guessing it will end up being a problem with your Rust installation/version or your installed packages. I'm not good at diagnosing those types of problems, but you could try Cargo clean first.

Weird. I'm a little confused then.

Rust version:
rustc 1.81.0 (eeb90cda1 2024-09-04)

cargo.toml :

[package]
name = "tests"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

Cargo clean and then build yields the same results.

This issue says it was fixed in serde 1.0.25 so perhaps your Cargo.lock has an earlier version. My Cargo.lock has much later 1.0.x versions for serde and serde_json (below).

Try cargo update.

[[package]]
name = "serde"
version = "1.0.203"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094"
dependencies = [
 "serde_derive",
]

[[package]]
name = "serde_derive"
version = "1.0.203"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba"
dependencies = [
 "proc-macro2",
 "quote",
 "syn",
]

[[package]]
name = "serde_json"
version = "1.0.127"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8043c06d9f82bd7271361ed64f415fe5e12a77fdb52e573e7f06a516dea329ad"
dependencies = [
 "itoa",
 "memchr",
 "ryu",
 "serde",
1 Like

Thank you, it was in fact an outdated serde-json version in the cargo.lock. I erased the file and regenerated it, code runs fine now.

Thank you!

Great, you're welcome.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.