Add example for remote deriving JsonSchema
This commit is contained in:
parent
b11536e527
commit
8d40625f10
5 changed files with 165 additions and 0 deletions
38
docs/_includes/examples/remote_derive.rs
Normal file
38
docs/_includes/examples/remote_derive.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
// Pretend that this is somebody else's crate, not a module.
|
||||
mod other_crate {
|
||||
// Neither Schemars nor the other crate provides a JsonSchema impl
|
||||
// for this struct.
|
||||
pub struct Duration {
|
||||
pub secs: i64,
|
||||
pub nanos: i32,
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
use other_crate::Duration;
|
||||
use schemars::{schema_for, JsonSchema};
|
||||
|
||||
// This is just a copy of the remote data structure that Schemars can use to
|
||||
// create a suitable JsonSchema impl.
|
||||
#[derive(JsonSchema)]
|
||||
pub struct DurationDef {
|
||||
pub secs: i64,
|
||||
pub nanos: i32,
|
||||
}
|
||||
|
||||
// Now the remote type can be used almost like it had its own JsonSchema impl
|
||||
// all along. The `with` attribute gives the path to the definition for the
|
||||
// remote type. Note that the real type of the field is the remote type, not
|
||||
// the definition type.
|
||||
#[derive(JsonSchema)]
|
||||
pub struct Process {
|
||||
pub command_line: String,
|
||||
#[serde(with = "DurationDef")]
|
||||
pub wall_time: Duration,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let schema = schema_for!(Process);
|
||||
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue