添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I've read the docs , but I haven't seen a way to solve my problem. I'm parsing an API response which on success would return only a 2xx status code, while on bad requests it sends JSON.

extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
fn main() {
    let _: X = serde_json::from_str(r#"{ "ok": true }"#).expect("with string");
    let _: X = serde_json::from_str("").expect("empty string");
#[derive(Deserialize)]
struct X {
    ok: Option<bool>,

Playground

This is the struct I would like to deserialize into. I've tried adding #[serde(skip_serializing_if = "some_func")] to the field, but it doesn't change anything.

The error is

Error("EOF while parsing a value", line: 1, column: 0)

Is there a way to handle this properly, or do I always have to check the status code if it's 2xx before attempting to deserialize the response?

Can you provide your full attempt with #[serde(skip_serializing_if...)], including some_func? – E_net4 is on strike Aug 5, 2018 at 21:14 I've only attempted it with a simple func that just returned `true˙ to see if it works. Since it didn't (got the same error) I didn't really bother with it anymore. – Andrew Aug 5, 2018 at 21:16 Oh! Well, for one, you probably meant to use #[serde(skip_deserializing)], not #[serde(skip_serializing)]. The latter only works when encoding the value. :) – E_net4 is on strike Aug 5, 2018 at 21:18 Moreover, consider making a full minimal reproducible example, by including a small main function deserializing an example of a response reproducing the issue. This makes the question much easier to address and much easier for us to help. – E_net4 is on strike Aug 5, 2018 at 21:21 Ahh, right, I've totally missed that. :) I thought I can't use external crates on play, my bad, I'll edit my post. – Andrew Aug 5, 2018 at 21:57

The main problem here is that the empty string is not valid JSON according to most parsers including the one built into ES6 and apparently Serde. There are three possible solutions that I see to this problem.

  • Return an empty dict from the endpoint
  • Do not blindly attempt deserialization of the empty string checking first for the content length of the response body.
  • Implement a custom Deserialize method
  • the one built into ES6 — what does that mean? How does a specification have something "built-in"? Do you mean that some specific implementation of ES6 has this behavior (if so, which) or do you mean that the ES6 specification mandates this behavior? – Shepmaster Aug 6, 2018 at 13:26 I am referring to the parser built into my browser. I did manage to find the spec as defined by ECMA. It seems a bit ambiguous about whether zero tokens is valid, they neglected to say "one or more tokens". "A JSON text is a sequence of tokens formed from Unicode code points that conforms to the JSON value grammar" The spec does not mention if there can be zero tokens or not but the implementation indicates that there cannot be zero. – user25064 Aug 6, 2018 at 13:42 I am referring to the parser built into my browser. — in that case, your answer would be better suited to say "the one built into $mybrowser (version $browser_version)". – Shepmaster Aug 6, 2018 at 13:43

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.