Serde Either
serde_either is a simple library that provides a few enums that enables you to deserialize and serialize values that can have string, struct or vec forms.
Let's say you want to deserialize a json for a Book, and that json has an "authors" field and this files can be either a string or a object like:
Or like
All you need to do is create a data structure like this:
use ;
use StringOrStruct;
use serde_json;
// And StringOrStruct is just a normal enum
let books = r#"[
{
"authors": {
"first_name": "John",
"last_name": "Smith"
}
},
{
"authors": "Michael J. Smith"
}
]"#;
let res: = from_str.unwrap;
assert_eq!;
assert_eq!;
And if you need to also capture a possible array of authors like this:
Then all you need to change is:
use StringOrStructOrVec;
// ...
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.