I want to make a Serde struct that is capable of being deserialized from either borrowed data (serde_json::from_str
) or owned data (serde_json::from_reader
). I have read Understanding deserializer lifetimes, and I understand the difference between the two trait bounds (<'de, T> where T: Deserialize<'de>
and <T> where T: DeserializeOwned
); what I want is a struct that implements both trait bounds, such that it can be used from either deserialization function.
The following code (playground) refuses to compile:
use serde_json; // 1.0.56
use serde; // 1.0.114
use std::fs;
use std::io;
use std::borrow::Cow;
#[derive(serde::Deserialize)]
struct Resource<'s> {
// The Cow should allow for either borrowed or owned data
#[serde(borrow)]
pub foo: Cow<'s, str>,
}
fn main() {
{
// Works as expected when referencing a string...
let s = "{\"foo\":\"bar\"}";
let resource: Resource = serde_json::from_str(s).unwrap();
}
{
// ...but refuses to compile when reading from a file
let file = fs::File::open("dummy.json").unwrap();
let reader = io::BufReader::new(file);
let resource: Resource = serde_json::from_reader(reader).unwrap();
}
}
How do I make this struct work with both owned and borrowed data? Is there a way to do something like #[serde(borrow_optional)]
?
(playground link)
It's not possible with the constraints on serde_json::from_reader
. However, it is possible if you construct Deserializer
yourself:
// ...but refuses to compile when reading from a file
let file = fs::File::open("dummy.json").unwrap();
let reader = io::BufReader::new(file);
let mut deser = serde_json::Deserializer::from_reader(reader);
let resource = Resource::deserialize(&mut deser).unwrap();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With