Add example
This commit is contained in:
parent
4962e1f731
commit
8a507f38a1
2 changed files with 68 additions and 11 deletions
26
schemars/examples/structs.rs
Normal file
26
schemars/examples/structs.rs
Normal file
|
@ -0,0 +1,26 @@
|
|||
use schemars::{JsonSchema, schema_for};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Deserialize, Serialize, JsonSchema)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct MyStruct {
|
||||
my_int: i32,
|
||||
my_nullable: Option<bool>,
|
||||
my_nested_struct: Nested,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, JsonSchema)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct Nested {
|
||||
#[serde(default)]
|
||||
my_string: String,
|
||||
#[serde(rename = "myArray")]
|
||||
my_float_vec: Vec<f32>,
|
||||
my_recursive_struct: Option<Box<Nested>>,
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let schema = schema_for!(MyStruct)?;
|
||||
println!("{}", serde_json::to_string_pretty(&schema)?);
|
||||
Ok(())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue