Skip to content

Commit 31ecc3b

Browse files
committed
add example for outputting legacy formats
1 parent da8bc05 commit 31ecc3b

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

_docs/schema/basics.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,14 @@ No errors would actually be reported here because the output format defaults to
263263

264264
## Evaluation results {#schema-results}
265265

266-
JSON Schema draft 2019-09 began the process to standardize the format for evaluation output in order to support cross-platform and cross-implementation compatibility. The format has been updated for the upcoming release to be more concise and clear. This includes support for both errors and annotation collection.
266+
JSON Schema draft 2019-09 began the process to standardize the format for evaluation output in order to support cross-platform and cross-implementation compatibility.
267+
268+
For the next release of JSON Schema, these formats have been extracted into their own specification. In addition, the formats themselves have been updated to increase clarity and readability.
269+
270+
> More information on the changes can be found in [this blog post](https://json-schema.org/blog/posts/fixing-json-schema-output).
271+
{: .prompt-tip}
272+
273+
_JsonSchema.Net_ supports the new, upcoming formats by default, however it can be configured for limited support of the specification requirements. See [this example](/schema/examples/legacy-output) for more information.
267274

268275
In summary, there are three levels of verbosity for output: Flag, List, and Hierarchy.
269276

@@ -273,6 +280,9 @@ A list format reduces all of the errors to a flat list, housed in a top-level ob
273280

274281
The hierarchical output format follows the structure of the schema.
275282

283+
> The output formats can contain a lot of information you're probably going to end up ignoring (but can nevertheless be useful in some cases). [This blog post](https://json-schema.org/blog/posts/interpreting-output) gives some examples and explanations of how to read JSON Schema output.
284+
{: .prompt-tip}
285+
276286
The default output format is Flag, but this can be configured via the `EvaluationOptions.OutputFormat` property.
277287

278288
> It's only possible to translate from a more detailed to a less detailed format.
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
layout: page
3+
title: Example - Serializing Output in the 2019-09 / 2020-12 Format
4+
bookmark: Legacy Output
5+
permalink: /schema/examples/:title/
6+
icon: fas fa-tag
7+
order: "01.4.5"
8+
---
9+
The 2019-09 and 2020-12 JSON Schema specifications define output formats that can be difficult to work with.
10+
11+
For future versions of the specification, the output is undergoing some [changes](https://json-schema.org/blog/posts/fixing-json-schema-output). This new format is the default for _JsonSchema.Net_, however the evaluation results can still be serialized to the 2019-09 / 2020-12 formats by using the `Pre202012EvaluationResultsJsonConverter`.
12+
13+
```c#
14+
var schema = JsonSchema.FromText(" ... ");
15+
var options = new EvaluationOptions
16+
{
17+
OutputFormat = OutputFormats.List
18+
};
19+
var instance = JsonNode.Parse(" ... ");
20+
var results = schema.Evaluate(instance, options);
21+
22+
var serializerOptions = new JsonSerializerOptions
23+
{
24+
Converters = { new Pre202012EvaluationResultsJsonConverter() }
25+
};
26+
var output = JsonSerializer.Serialize(results, serializationOptions);
27+
```
28+
29+
The `OutputFormat` option still controls which format you'll get:
30+
31+
| `OutputFormat` | "New" format (default) | "Legacy" format |
32+
|:-:|:-:|:-:|
33+
| `Flag` | flag | flag |
34+
| `List` | list | basic |
35+
| `Hierarchical` | hierarchical | verbose |
36+
37+
> The "detailed" format defined by the specification is not supported since it requires some advanced logic (which was never properly specified) to pare down the nodes.
38+
{: .prompt-warning}

0 commit comments

Comments
 (0)