Debian Codegraph @

examples/serialize_contract.rs

raw symbols SCIP
1 use schemars::{generate::SchemaSettings, JsonSchema};
2 use serde::{Deserialize, Serialize};
3
4 #[derive(JsonSchema, Deserialize, Serialize)]
5 // The schema effectively ignores this `rename_all`, since it doesn't apply to serialization
6 #[serde(rename_all(deserialize = "PascalCase"))]
7 pub struct MyStruct {
8 pub my_int: i32,
9 #[serde(skip_deserializing)]
10 pub my_read_only_bool: bool,
11 // This property is excluded from the schema
12 #[serde(skip_serializing)]
13 pub my_write_only_bool: bool,
14 // This property is excluded from the "required" properties of the schema, because it may be
15 // be skipped during serialization
16 #[serde(skip_serializing_if = "str::is_empty")]
17 pub maybe_string: String,
18 pub definitely_string: String,
19 }
20
21 fn main() {
22 // By default, generated schemas describe how types are deserialized.
23 // So we modify the settings here to instead generate schemas describing how it's serialized:
24 let settings = SchemaSettings::default().for_serialize();
25
26 let generator = settings.into_generator();
27 let schema = generator.into_root_schema_for::<MyStruct>();
28 println!("{}", serde_json::to_string_pretty(&schema).unwrap());
29 }