9

I am getting the following error when I use serde to read a json from a file:

Failed to resolve: use of undeclared crate or module serde_json

here's the code:

use serde::Deserialize;


fn main() {
  let file = fs::File::open("./feed.json")
      .expect("file should open read only");
  let reader = BufReader::new(file);
  let json = serde_json::from_reader(reader)
      .expect("file should have FirstName key");
  let feed_url = json.get("2.0")
      .expect("file should have FirstName key");
  println!("{}", reedFeed(feed_url));
}

Here's the doc on this function. I am on ubuntu and using intellij as my ide. What am I missing here?

3
  • 1
    What's in your Cargo.toml? Commented Apr 27, 2022 at 12:43
  • 2
    Have you added serde_json to your Cargo.toml (along with serde)?
    – Dogbert
    Commented Apr 27, 2022 at 13:24
  • You need to edit your question to include all the elements of a minimal reproducible example. (Including your Cargo.toml)
    – Herohtar
    Commented Apr 27, 2022 at 14:31

1 Answer 1

5

As @Dogbert said, add serde = "1.0.136" and serde_json = "1.0.79" to the end of your Cargo.toml (under "[dependencies]"). This will tell cargo to download the dependencies on the next cargo run and use it from that point on.

Update two years later: I posed this question while starting to learn Rust and notice that there are multiple errors within the code, primarily the missing dependencies, so I am very thankful to the patient answerers and want to highlight cargo add serde and cargo add serde_json as recommended by @Kai.

Because the initial example is in such poor quality in hindsight here a working one for anyone landing here with a similar problem through the search function:

Working example with step by step guide

$ mkdir serdee
$ cd serdee
$ cargo init
$ cargo add serde serde_json
  • Manually edit Cargo.toml and enable the derive feature:
[package]
name = "serdee"
version = "0.1.0"
edition = "2021"

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

src/main.rs

use serde::Deserialize;
use std::fs::File;
use std::io::BufReader;

#[derive(Deserialize, Debug)]
struct Feed { 
    feed_url: String,
}

fn main() {
    let file = File::open("./feed.json").expect("error opening feed file");
    let reader = BufReader::new(file);
    let feed: Feed = serde_json::from_reader(reader)
        .expect("file contents should conform to the feed structure");
    println!("{}", feed.feed_url);
}

feed.json

{ "feed_url": "http://testfeed.com/"}
6
  • 1
    >Also, you need to add use serde_json; at the top of your code.< this part is not true
    – Ivan C
    Commented Apr 27, 2022 at 14:38
  • @IvanC: Thanks for the correction! I removed that line. Commented Apr 28, 2022 at 6:53
  • under "[dependencies]", not "[dev-dependencies]"
    – Russo
    Commented Sep 21, 2022 at 10:41
  • @Russo: on my browser it already says "dependencies", does it show as "dev-dependencies" in your browser? Commented Sep 21, 2022 at 11:36
  • ahhh... no I was just emphasizing because I did not notice that
    – Russo
    Commented Sep 22, 2022 at 10:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.