MCPcopy Create free account
hub / github.com/danielaparker/jsoncons / defaults_example

Function defaults_example

examples/src/jsonschema_examples.cpp:180–219  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

178}
179
180void defaults_example()
181{
182 json schema = json::parse(R"(
183{
184 "$schema": "https://json-schema.org/draft/2020-12/schema",
185 "properties": {
186 "bar": {
187 "type": "string",
188 "minLength": 4,
189 "default": "bad"
190 }
191 }
192}
193)");
194
195 try
196 {
197 // Data
198 json data = json::parse("{}");
199
200 // will throw schema_error if JSON Schema compilation fails
201 jsonschema::json_schema<json> compiled = jsonschema::make_json_schema(schema);
202
203 // will throw a validation_error when a schema violation happens
204 json patch;
205 compiled.validate(data, patch);
206
207 std::cout << "Patch: " << patch << "\n";
208
209 std::cout << "Original data: " << data << "\n";
210
211 jsonpatch::apply_patch(data, patch);
212
213 std::cout << "Patched data: " << data << "\n\n";
214 }
215 catch (const std::exception& e)
216 {
217 std::cout << e.what() << "\n";
218 }
219}
220
221#if defined(JSONCONS_HAS_STD_VARIANT)
222

Callers 1

mainFunction · 0.85

Calls 5

parseFunction · 0.85
make_json_schemaFunction · 0.85
apply_patchFunction · 0.85
validateMethod · 0.45
whatMethod · 0.45

Tested by

no test coverage detected