Serde nested JSON
Summary and basic use
This is a small utility crate to help deal with nested JSON in structs.
Here's a basic example:
use ;
use ;
use serde_nested_json;
let as_json = json!;
# println!;
Optional values
Optional values deserialize just fine from "null"
, but if the value may be undefined please add default
field attribute:
use ;
use ;
use serde_nested_json;
let as_json = json!;
# println!;
Vecs
There's also a helper module for vecs containing nested items.
To use it, Just add ::vec
to the with
field attribute:
use ;
use ;
use serde_nested_json;
let as_json = json!;
# println!;
NestedJson<T>
The main helper type of this crate is NestedJson<T>
which
can be used without field annotation. You will however need
to extract and insert the inner value on your own:
use ;
use ;
use NestedJson;
let as_json = json!;
// NestedJson<T> implements From<T>, so you can use t.into()
// as well as NestedJson::from(t)
// There's also an associated `into` fn which returns T
// NestedJson::from is also available, but I havent found
// a way to associate it with with the std::conversion
// traits using stable features
// NestedJson<T> also implements AsRef<T>:
let a = SomeData ;
let b: MyStruct = a.into;
let c: SomeData = b.into;
let d: = c.into;
let e: SomeData = d.into;
let a = SomeData ;
let b = from;
let c = from;
let d = from;
// Sadly not possible without manual implementation:
// let e = SomeData::from(d);
# println!;
Forwarded trait implementations
NestedJson<T>
implements the following traits for any type
T
that also implements them, but does not require any of them:
- Debug
- Clone
- PartialEq (and Eq)
- PartialOrd
- Ord
These should be enough at least for basic tests etc., but just open an issue if you need anything else.
# use NestedJson;
let stuff = from;
let mut clone = stuff.clone;
assert_eq!;
println!; // 'NestedJson(["hello"])'
clone.into.sort;