8

I followed the quickstart guide. Now I'm trying to return some super simple JSON and the documentation is wrong and there's no way to submit a ticket without getting on IRC.

Error

error[E0432]: unresolved import `rocket::serde::json`
 --> src/main.rs:2:20
  |
2 | use rocket::serde::json::Json;
  |                    ^^^^ could not find `json` in `serde`

For more information about this error, try `rustc --explain E0432`.
error: could not compile `my-api` due to previous error

Files

Cargo.toml

[package]
name = "my-api"
version = "0.1.0"
edition = "2021"
publish = false

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

[dependencies]
rocket = "0.5.0-rc.1"
serde = "1.0.130"

main.rs

#[macro_use] extern crate rocket;
use rocket::serde::{Serialize, json::Json};

#[derive(Serialize)]
struct Location {
    lat: String,
    lng: String,
}

#[get("/?<lat>&<lng>")]
fn location(lat: &str, lng: &str) -> Json<Location> {
    Json(Location {
        lat: 111.1111.to_string(),
        lng: 222.2222.to_string(),
    })
}

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/", routes![location])
}

If you go here you'll see this is almost a direct copy/paste from the documentation. I don't know enough of Rust to troubleshoot dependency errors.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
kalm42
  • 784
  • 9
  • 19

2 Answers2

14

The json feature for rocket needs to be explicitly turned on in your Cargo.toml.

[package]
name = "my-api"
version = "0.1.0"
edition = "2018"  // cut back for testing with nixpkgs-provided rust
publish = false

[dependencies]
serde = "1.0.130"

[dependencies.rocket]
version = "0.5.0-rc.1"
features = ["json"]

This is documented in a comment in the Rocket source which generates the document here.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 2
    I think it's "iffy" to call it "documented" if it is exclusively in a comment in a source file. – Kevin Anderson Nov 15 '21 at 02:17
  • 3
    @KevinAnderson that comment is included in the generated documentation: [docs.rs](https://docs.rs/rocket/0.5.0-rc.1/rocket/serde/json/index.html) – kmdreko Nov 15 '21 at 04:18
  • OK, that's fair. I'd still say it deserves to be in something like your explicit readme.md on your GitHub, but acknowledge that it's on a higher level than I originally thought. – Kevin Anderson Nov 15 '21 at 13:20
  • @KevinAnderson Thank you so much! How can I get the documentation to note the prerequisite change in the Cargo.toml file at the location I found? – kalm42 Nov 15 '21 at 13:23
  • 1
    @kalm42, https://github.com/stackblitz/template-rust-rocket/blob/master/site/guide/2-getting-started.md is the file to submit a PR against if you want to do so. I would suggest that said PR describe the feature as something that can be optionally enabled -- to just show it being enabled with no commentary on how this is an _option_ is contrary to the design decision to make it off-by-default, so I wouldn't expect upstream to accept a change that does so. – Charles Duffy Nov 15 '21 at 14:07
2

On top of Charles' answer, I'd change the serde import to:

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

As documented here

matiu
  • 7,469
  • 4
  • 44
  • 48