Serde_json doesn't see the Serialize implementation

I cloned serde.
In the other crate, I specified the path to the local serde:
serde = {path = "../../serde/serde", features = ["derive"]}
Also specified:
serde_json = "^1.0.127"

In the code:

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
struct Tmp {
    a: i64,
    b: i32,
}
fn main() {
    let tmp = Tmp { a: 2, b: 3 };
    println!("Hello, world! {tmp:?}", );

    serde_json::to_string(&tmp);
}

An error occurs:

cargo build
   Compiling tmp v0.1.0 (C:\Users\letif\VS Code Projects\rust\mertex\tmp)
error[E0277]: the trait bound `Tmp: serde::ser::Serialize` is not satisfied
    --> tmp\src/main.rs:12:27
     |
12   |     serde_json::to_string(&tmp);
     |     --------------------- ^^^^ the trait `serde::ser::Serialize` is not implemented for `Tmp`     
     |     |
     |     required by a bound introduced by this call
     |
     = note: for local types consider adding `#[derive(serde::Serialize)]` to your `Tmp` type
     = note: for types from other crates check whether the crate offers a `serde` feature flag
     = help: the following other types implement trait `serde::ser::Serialize`:
               &'a T
               &'a mut T
               ()
               (T,)
               (T0, T1)
               (T0, T1, T2)
               (T0, T1, T2, T3)
               (T0, T1, T2, T3, T4)
             and 131 others
note: required by a bound in `serde_json::to_string`
    --> C:\Users\letif\.cargo\registry\src\index.crates.io-6f17d22bba15001f\serde_json-1.0.127\src\ser.rs:2209:17
     |
2207 | pub fn to_string<T>(value: &T) -> Result<String>
     |        --------- required by a bound in this function
2208 | where
2209 |     T: ?Sized + Serialize,
     |                 ^^^^^^^^^ required by this bound in `to_string`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `tmp` (bin "tmp") due to 1 previous error

The same thing happens with:
serde = {git = “https://github.com/serde-rs/serde.git”, features = [“derive”]}.

While with serde = {version = “1.0.209”, features = [“derive”]} the code compiles.

You should patch serde_json to use your serde.

https://doc.rust-lang.org/cargo/reference/overriding-dependencies.html

[patch.crates-io]
serde = { path = "../../serde/serde" }

[dependencies]
serde = { path = "../../serde/serde", features = ["derive"] } 
serde_json = "^1.0.127"

serde_json doesn't depend on your local copy, so it still uses mainstream serde.

I cloned serde_json and referenced it:

[dependencies].
serde = {path = “../serde/serde”, features = [“derive”]}
serde_json = {path = “../json”}

In serde_json 1.0.127:
serde = { version = “1.0.194”, default-features = false }

So I specify the versions:

[dependencies].
serde = { version = “1.0.194”, path = “../serde/serde”, features = [“derive”]}
serde_json = {version = “1.0.127”, path = “../json”}

But this still causes an error

I added:

[patch.crates-io].
serde = { path = “../serde/serde” }

to serde_json, now serde_json uses the local serde 1.0.209, instead of the published one (serde_json):

[dependencies].
serde = { version = “1.0.194”, default-features = false }

Next, in my crate, I reference the local serde_json:

[dependencies].
serde = {path = “../serde/serde”, features = [“derive”]}
serde_json = {path = “../json”}

The error is still being called.

Even doing this: not patching serde_json, but rolling serde back to 1.0.194:

[dependencies].
serde = {version = “1.0.194”, path = “../serde/serde”, features = [“derive”]}
serde_json = {path = “../json”}

does not solve the problem

This should added in your own crate's manifest, you don't have to clone and modify serde_json itself.

2 Likes

This is the problem, your clone of serde_json is using the serde dependency from crates.io. You need to change this to a path dependency too.

that still uses the upstream version.