|
| 1 | +# JSON schema |
| 2 | + |
| 3 | +JSON schemata are a powerful tool for expressing the expected structure of your input. You can use it to validate your input before you even send it to your C++ backend, which will result in better UX. |
| 4 | + |
| 5 | +It can also be used for code generation. For instance, tools such as https://app.quicktype.io/ allow you to generate code in multiple programming languages from the JSON schema (even though the validations are usually omitted). |
| 6 | + |
| 7 | +If you are interacting with Python, we warmly recommend https://docs.pydantic.dev/latest/integrations/datamodel_code_generator/. This allows you to generate Pydantic dataclasses, including the validation, from the JSON schema. |
| 8 | + |
| 9 | +Note that this is only supported for JSON, not for other formats. |
| 10 | + |
| 11 | +## Basic idea |
| 12 | + |
| 13 | +Suppose you have a struct like this: |
| 14 | + |
| 15 | +```cpp |
| 16 | +struct Person { |
| 17 | + std::string first_name; |
| 18 | + std::string last_name; |
| 19 | + rfl::Email email; |
| 20 | + std::vector<Person> children; |
| 21 | + float salary; |
| 22 | +}; |
| 23 | +``` |
| 24 | +
|
| 25 | +You can generate a JSON schema like this: |
| 26 | +
|
| 27 | +```cpp |
| 28 | +const std::string json_schema = rfl::json::to_schema<Person>(rfl::json::pretty); |
| 29 | +``` |
| 30 | + |
| 31 | +You do not have to pass `rfl::json::pretty`, but for the purposes of this documentation it is better to do so. |
| 32 | + |
| 33 | +This will result in the following JSON schema: |
| 34 | + |
| 35 | +```json |
| 36 | +{ |
| 37 | + "$schema": "https://json-schema.org/draft/2020-12/schema", |
| 38 | + "$ref": "#/definitions/Person", |
| 39 | + "definitions": { |
| 40 | + "Person": { |
| 41 | + "type": "object", |
| 42 | + "properties": { |
| 43 | + "children": { |
| 44 | + "type": "array", |
| 45 | + "items": { |
| 46 | + "$ref": "#/definitions/Person" |
| 47 | + } |
| 48 | + }, |
| 49 | + "email": { |
| 50 | + "type": "string", |
| 51 | + "pattern": "^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$" |
| 52 | + }, |
| 53 | + "first_name": { |
| 54 | + "type": "string" |
| 55 | + }, |
| 56 | + "last_name": { |
| 57 | + "type": "string" |
| 58 | + }, |
| 59 | + "salary": { |
| 60 | + "type": "number" |
| 61 | + } |
| 62 | + }, |
| 63 | + "required": [ |
| 64 | + "children", |
| 65 | + "email", |
| 66 | + "first_name", |
| 67 | + "last_name", |
| 68 | + "salary" |
| 69 | + ] |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +You can insert this into the tools mentioned above and see the generated code. |
| 76 | + |
| 77 | +## Adding descriptions |
| 78 | + |
| 79 | +JSON schemata also often contain descriptions. reflect-cpp supports this as well. |
| 80 | + |
| 81 | +```cpp |
| 82 | +struct Person { |
| 83 | + std::string first_name; |
| 84 | + std::string last_name; |
| 85 | + rfl::Description<"Must be a proper email in the form [email protected].", |
| 86 | + rfl::Email> |
| 87 | + email; |
| 88 | + rfl::Description< |
| 89 | + "The person's children. Pass an empty array for no children.", |
| 90 | + std::vector<Person>> |
| 91 | + children; |
| 92 | + float salary; |
| 93 | +}; |
| 94 | +``` |
| 95 | +
|
| 96 | +```cpp |
| 97 | +const std::string json_schema = rfl::json::to_schema<Person>(rfl::json::pretty); |
| 98 | +``` |
| 99 | + |
| 100 | +```json |
| 101 | +{ |
| 102 | + "$schema": "https://json-schema.org/draft/2020-12/schema", |
| 103 | + "$ref": "#/definitions/Person", |
| 104 | + "definitions": { |
| 105 | + "Person": { |
| 106 | + "type": "object", |
| 107 | + "properties": { |
| 108 | + "children": { |
| 109 | + "type": "array", |
| 110 | + "description": "The person's children. Pass an empty array for no children.", |
| 111 | + "items": { |
| 112 | + "$ref": "#/definitions/Person" |
| 113 | + } |
| 114 | + }, |
| 115 | + "email": { |
| 116 | + "type": "string", |
| 117 | + "description": "Must be a proper email in the form [email protected].", |
| 118 | + "pattern": "^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$" |
| 119 | + }, |
| 120 | + "first_name": { |
| 121 | + "type": "string" |
| 122 | + }, |
| 123 | + "last_name": { |
| 124 | + "type": "string" |
| 125 | + }, |
| 126 | + "salary": { |
| 127 | + "type": "number" |
| 128 | + } |
| 129 | + }, |
| 130 | + "required": [ |
| 131 | + "children", |
| 132 | + "email", |
| 133 | + "first_name", |
| 134 | + "last_name", |
| 135 | + "salary" |
| 136 | + ] |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +You also add a description to the entire JSON schema: |
| 143 | + |
| 144 | +```cpp |
| 145 | +const std::string json_schema = rfl::json::to_schema< |
| 146 | + rfl::Description<"JSON schema that describes the required " |
| 147 | + "attributes for the person class.", |
| 148 | + Person>>(rfl::json::pretty); |
| 149 | +``` |
| 150 | + |
| 151 | +```json |
| 152 | +{ |
| 153 | + "$schema": "https://json-schema.org/draft/2020-12/schema", |
| 154 | + "$ref": "#/definitions/Person", |
| 155 | + "description": "JSON schema that describes the required attributes for the person class.", |
| 156 | + "definitions": { |
| 157 | + "Person": { |
| 158 | + "type": "object", |
| 159 | + "properties": { |
| 160 | + "children": { |
| 161 | + "type": "array", |
| 162 | + "description": "The person's children. Pass an empty array for no children.", |
| 163 | + "items": { |
| 164 | + "$ref": "#/definitions/Person" |
| 165 | + } |
| 166 | + }, |
| 167 | + "email": { |
| 168 | + "type": "string", |
| 169 | + "description": "Must be a proper email in the form [email protected].", |
| 170 | + "pattern": "^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$" |
| 171 | + }, |
| 172 | + "first_name": { |
| 173 | + "type": "string" |
| 174 | + }, |
| 175 | + "last_name": { |
| 176 | + "type": "string" |
| 177 | + }, |
| 178 | + "salary": { |
| 179 | + "type": "number" |
| 180 | + } |
| 181 | + }, |
| 182 | + "required": [ |
| 183 | + "children", |
| 184 | + "email", |
| 185 | + "first_name", |
| 186 | + "last_name", |
| 187 | + "salary" |
| 188 | + ] |
| 189 | + } |
| 190 | + } |
| 191 | +} |
| 192 | +``` |
0 commit comments