From 4962e1f731447a75602e3d3f7253511e03feaa50 Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Sun, 15 Sep 2019 16:07:23 +0100 Subject: [PATCH] Add example to readme --- README.md | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/README.md b/README.md index 4bfff58..a18b37a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,67 @@ # schemars Generate JSON Schema documents from Rust code + +Work in progress! + +## Basic Usage + +```rust +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, + #[serde(default)] + my_string: String, + #[serde(rename = "myArray")] + my_float_vec: Vec, +} + +fn main() -> Result<(), Box> { + let schema = schema_for!(MyStruct)?; + println!("{}", serde_json::to_string_pretty(&schema)?); + Ok(()) +} +``` + +This outputs the following: + +```json +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "MyStruct", + "type": "object", + "required": [ + "myArray", + "myInt", + "myNullable" + ], + "properties": { + "myArray": { + "type": "array", + "items": { + "type": "number", + "format": "float" + } + }, + "myInt": { + "type": "integer", + "format": "int32" + }, + "myNullable": { + "type": [ + "boolean", + "null" + ] + }, + "myString": { + "type": "string" + } + } +} +``` + +Note that the `#[serde(...)]` attributes are respected.