Closed AKApumkin closed 4 years ago
Id like to take a stab at this one.
In rust the single quote and the double quote are NOT interchangeable. Simplistically speaking a double quote is for creating the str
type and the single quote is for creating a char
type , Single quote's can only accept at most one (possibly escaped) character for example 'n' or '\n' are both char
types. This is what this error is about
error: character literal may only contain one codepoint
Basically it means the character literal may only contain one character, you can find more information by looking up the char type in rust docs/book
So to fix the above code you need to pass content::json a static string that is valid JSON by escaping the double quotes within the string like this:
#[get("/")]
fn json() -> content::Json<&'static str> {
content::json("{\"hi\":\"world\"}")
}
As for libraries to handle JSON in rocket, i just use rocket_contrib, there's no need for an external library
This is a duplicate of (one part of) #1356, which was fixed but is not yet published on the main site. It looks like @bytebuddha covered everything pretty well, so I'm closing this issue. Please feel free to comment or re-open if we missed something!
Using version 0.4
From the guide page https://rocket.rs/v0.4/guide/responses/
Valid JSON expects that strings are defined by double quotation marks, however the example gives single. So outputting this gives an error when trying to validate the JSON
Tried to change them around, however the compiler presents me with the error,
error: character literal may only contain one codepoint
This is important for developing a RESTFULL API where there are additional statuses that require simple JSON objects that are not stored in a DB.
From what i can see there are 3 different additional libraries that have this type of support, the main use-case would be from an easy getting started point of view, to have one simple method that works out of the box, spent 4 hours trying to get this working and looking at other solutions, which in my opinion is madness for a simple JSON response.
Please also note i am a rust and rocket newbie, so may have missed a fundamental way of structuring things that would achieve the same result, apologies in advance if such is the case.