How to deserialize reqwest to JSON or other struct?

Hi! I has been using serde_json for some time, everything is nice, deserialize to json to structures, everything easy.

Until I found a server which does a weird thing.... it has two options.. would return a pure string with "No Data", or a JSON (which we know the structure it has).

Most of the time I has been using:

let data: SomeType = request.json();

But do this, and serde Enums only works fine when all the requested data is JSON, so here the question, how can we use Enums, to deserialize types that are not necessarily JSON? like:

enum Data {
    JSONType(SomeType),
    NoData(String), //expect to be "No Data"
    WeirdValue(u32), //Other cases that do not match the previous ones
}

Thx!

This can be done with an untagged enum.

Would you mind providing some examples of what the serialised data looks like? I'm having trouble understanding your requirements. If you want to deserialise JSON, your data has to be JSON. Are you referring to fields that are either JSON Objects or some other JSON type? Like the example you provided in the first part of your problem description, where you either get a JSON Object, or a JSON String?

is not JSON, literally the string is "No Data", and when is JSON, is a right one.

Can you use the response's content type to distinguish between a text body and a json body?

I think, the most useful case, would be think of this when the response content type is wrong, not all servers do it right, so have a way to handle it would be nice.

I would not dig that much when the server returns it correctly, becasue we can always check the type and deserialize in the right case, I think is trivial.