I'm writing a (non-general-purpose) GraphQL client, so I often end up having to deserialize JSON maps like {"repository": DATA}
that contain only one field where I want to end up with only that field's value. So far, I've just been doing this by defining a bunch of types like:
#[derive(Deserialize)]
struct Response {
repository: InterestingData,
}
and then just returning the repository
field of the deserialized Response
, but this is repetitive. I tried to at least get rid of all the singleton struct definitions by instead deserializing to (InterestingData,)
and returning the .0
field (thinking that, if a JSON list can be deserialized to a struct, surely deserializing the other way would work), but serde-json didn't like that. serde_with
didn't seem to have anything helpful for this either.
At the moment, it looks like my best option is to define a struct Singleton<T>(pub T)
type and give it a custom Deserialize
impl that expects a one-field map & extracts the field's value, but I'm wondering if there's another way I've overlooked.