Skip to content

Unable to create struct from json string #682

@asnimansari

Description

@asnimansari

I am trying to Deserialize a string to struct in rust. The below code is from json_serde documentation only.

use serde::{Deserialize, Serialize};
use serde_json::Result;

#[derive(Serialize, Deserialize)]
struct Person {
    name: String,
    age: u8,
    phones: Vec<String>,
}

fn main() {
    let data = r#"
        {
            "name": "John Doe",
            "age": 43,
            "phones": [
                "+44 1234567",
                "+44 2345678"
            ]
        }"#;

    // Parse the string of data into a Person object. This is exactly the
    // same function as the one that produced serde_json::Value above, but
    // now we are asking it for a Person as output.
    let p: Person = serde_json::from_str(data).expect("bal");

    // Do things just like with any other Rust data structure.
    println!("Please call {} at the number {}", p.name, p.phones[0]);


}

The above code snippet gives me error:

   Compiling test-json-rs v0.1.0 (/Users/asnimpansari/Desktop/Personal/test-json-rs)
error: cannot find derive macro `Serialize` in this scope
 --> src/main.rs:4:10
  |
4 | #[derive(Serialize, Deserialize)]
  |          ^^^^^^^^^

error: cannot find derive macro `Deserialize` in this scope
 --> src/main.rs:4:21
  |
4 | #[derive(Serialize, Deserialize)]
  |                     ^^^^^^^^^^^

warning: unused imports: `Deserialize`, `Serialize`
 --> src/main.rs:1:13
  |
1 | use serde::{Deserialize, Serialize};
  |             ^^^^^^^^^^^  ^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `serde_json::Result`
 --> src/main.rs:2:5
  |
2 | use serde_json::Result;
  |     ^^^^^^^^^^^^^^^^^^

error[E0277]: the trait bound `Person: serde::de::Deserialize<'_>` is not satisfied
    --> src/main.rs:25:21
     |
25   |     let p: Person = serde_json::from_str(data).expect("bal");
     |                     ^^^^^^^^^^^^^^^^^^^^ the trait `serde::de::Deserialize<'_>` is not implemented for `Person`
     | 
    ::: /Users/asnimpansari/.cargo/registry/src/git.colasdn.top-1ecc6299db9ec823/serde_json-1.0.55/src/de.rs:2584:8
     |
2584 |     T: de::Deserialize<'a>,
     |        ------------------- required by this bound in `serde_json::de::from_str`

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0277`.
error: could not compile `test-json-rs`.

To learn more, run the command again with --verbose.

My Cargo.toml file is

[package]
name = "test-json-rs"
version = "0.1.0"
authors = ["asnimansari <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde_json = "1.0.55"
serde = "1.0.100"

Metadata

Metadata

Assignees

No one assigned

    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