0

I am calling an api which returns a Json. Its format can change depending upon a member value.

Json response is dependent on

enum Count {
Single,
Two,  
}

Response possible structure.

// When count is Single
{
"error": "false",
"count": "Single",
"fullname": "xyz_abc",      
}
    
// When count is Two
{
"error": "false",
"count": "Two",
"name": "abc",
"address": "Ind",      
}

How can i write a struct for this json format?

struct Response {
count: Count,
// Not sure what to add.
}
  • @Stargateur Sorry for the inconvenience. Formatted eg to valid json. – Siddharth Teli Jul 01 '23 at 12:39
  • @Stargateur, I assume tagging doesn't work well when response schema is dependent on non first value. I am getting missing filed `type`. Can this be resolved or need to manually impl `deserialize`? – Siddharth Teli Jul 02 '23 at 05:46
  • 1
    https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=7111e48ee5be61d2bb851c3f942b3786 – Stargateur Jul 02 '23 at 06:52
  • @Stargateur, Thanks alot for your response. This is what i was looking for. Sorry for the confusion again. Basically, the api's response was very nested. So, I tried a minimal, reproducible example but it was wrong. If you could convert above solution as answer i will accept it or else if you want i can close the question. Thanks again :) – Siddharth Teli Jul 02 '23 at 07:50
  • 1
    are you crazy ? I'm way too lazy that why we use duplicate :p you welcome have a nice day. – Stargateur Jul 02 '23 at 08:03

0 Answers0