this post was submitted on 08 Sep 2023
0 points (50.0% liked)

Rust

5974 readers
124 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 1 year ago
MODERATORS
 

The first post had a couple errors that made things confusing. I have an accurately formatted JSON response, but I still can't seem to deserialize it.

I keep getting an error where the response isn’t matching up as expected: Error("data did not match any variant of untagged enum NationResponse"

I have a JSON response that looks like this:

 {
      {
            "id": 1,
            "country": "usa"
        },
        [
            {
                "id": 1,
                "age": 37,
                "name": "John"
            },
            {
                "id": 2,
                "age": 21,
                "name": "Nick"
            },
        ]
}

And I’m trying to deserialize it, but I can’t seem to get it right. Isn’t this accurate?

#[derive(Deserialize, Debug)]
#[serde(untagged)]
enum NationResponse {
    Nation(Nation),
    People(Vec),
}

struct Person {
    id : i32,
    age : i32,
    name : String
}

struct Nation {
    id : i32,
    country : String
}

Edit:

The problem I was actually experiencing was trying to use an enum as the response. Yes the formatting for my example was wrong (it should have been an array). But the structure besides that was accurate.

What I needed was Vec>

top 9 comments
sorted by: hot top controversial new old
[–] orhtej2@eviltoast.org 18 points 1 year ago (1 children)

I don't get it, this is not valid JSON.

[–] nerdblood@programming.dev 2 points 1 year ago

It should be wrapped in an array, not an object. Then it's valid. The problem was that I was trying to use an enum.

[–] envis10n@lemm.ee 12 points 1 year ago (1 children)

So, no. With the way you have it setup right now you would need to adjust your JSON structure to have the nation info be under a key, as well as the people array.

{
    "Nation": {...},
    "People": [...],
}

Every value has to have a key, unless it is the only value being serialized.

[1,2]

Is valid JSON, but

{ {"Id":1} }

Is not

[–] TehPers@beehaw.org 1 points 1 year ago* (last edited 1 year ago) (1 children)

An untagged enum doesn't need keys for the variants. It just tries to deserialize into each variant in the order defined.

That being said, you're right that the JSON is invalid.

[–] envis10n@lemm.ee 1 points 1 year ago

Yeah, I wasn't trying to imply that it was a problem on the rust side or that they needed to name the keys that way, just that the JSON does need to have keys because that is how JSON works

[–] Anders429@lemmy.world 8 points 1 year ago

This is not valid JSON.

[–] Turun@feddit.de 1 points 1 year ago* (last edited 1 year ago)

What are you deserializing into? Minimum reproducible Code would be best. That means a main.rs file with a main method and no dependencies, which prints your error when run. Then you can simply share it on the rust playground or ideone.com.

I believe the JSON you have given us is invalid. Either the outer most parentheses must be [] instead of {}, or the nation and the list must get a name, i.e. "nation" = {"id"= 1, "country"="USA"}, "people"= []

[–] ActuallyRuben@actuallyruben.nl 1 points 1 year ago (1 children)

Does the People(Vec) even work if you don't specify the type inside the Vec?

[–] TehPers@beehaw.org 3 points 1 year ago

Lemmy loves to remove things that look like HTML tags :/