Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,12 @@ The following code example generates a schema that incorporates `description` ke

:::code language="csharp" source="snippets/schema-exporter/TransformSchema.cs" id="2":::
:::code language="csharp" source="snippets/schema-exporter/TransformSchema.cs" id="Person":::

Similarly, you can set a custom title for the schema by modifying the schema node:

:::code language="csharp" source="snippets/schema-exporter/JsonObjectTitleExample.cs" id="1":::

The following code example generates a schema that includes a `title` keyword:

:::code language="csharp" source="snippets/schema-exporter/JsonObjectTitleExample.cs" id="2":::
:::code language="csharp" source="snippets/schema-exporter/JsonObjectTitleExample.cs" id="Person":::
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,24 @@ public class Person
}
```

Finally, consider the following example that uses `Newtonsoft.Json.JsonObjectAttribute` to specify a title for JSON schema generation:

```csharp
[JsonObject(Title = "PersonTitle")]
public class Person { ... }
```

The `Title` property is used for JSON schema metadata and doesn't have a direct equivalent in System.Text.Json. Starting in .NET 9, you can use the <xref:System.Text.Json.Schema.JsonSchemaExporter> to generate JSON schemas and customize the schema title using the <xref:System.Text.Json.Schema.JsonSchemaExporterOptions.TransformSchemaNode%2A> delegate:

:::code language="csharp" source="snippets/schema-exporter/JsonObjectTitleExample.cs" id="1":::

The following code example generates a schema that includes the `title` keyword:

:::code language="csharp" source="snippets/schema-exporter/JsonObjectTitleExample.cs" id="2":::
:::code language="csharp" source="snippets/schema-exporter/JsonObjectTitleExample.cs" id="Person":::

For OpenAPI scenarios, you can also set the schema title using document transformers or by directly manipulating the OpenAPI schema. For more information about JSON schema generation, see [JSON schema exporter](extract-schema.md).

### TraceWriter

`Newtonsoft.Json` lets you debug by using a `TraceWriter` to view logs that are generated by serialization or deserialization. <xref:System.Text.Json?displayProperty=fullName> doesn't do logging.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Schema;

class JsonObjectTitleExample
{
public static void RunIt()
{
// <Snippet1>
JsonSchemaExporterOptions exporterOptions = new()
{
TransformSchemaNode = (context, schema) =>
{
// Only set title for the root type (not properties)
if (context.PropertyInfo is null && context.TypeInfo.Type == typeof(Person))
{
if (schema is not JsonObject jObj)
{
// Handle the case where the schema is a Boolean.
JsonValueKind valueKind = schema.GetValueKind();
Debug.Assert(valueKind is JsonValueKind.True or JsonValueKind.False);
schema = jObj = new JsonObject();
if (valueKind is JsonValueKind.False)
{
jObj.Add("not", true);
}
}

// Set the title in the schema
jObj.Insert(0, "title", "PersonTitle");
}

return schema;
}
};
// </Snippet1>

// <Snippet2>
JsonSerializerOptions options = JsonSerializerOptions.Default;
JsonNode schema = options.GetJsonSchemaAsNode(typeof(Person), exporterOptions);
Console.WriteLine(schema.ToString());
//{
// "title": "PersonTitle",
// "type": ["object", "null"],
// "properties": {
// "Name": { "type": "string" },
// "Age": { "type": "integer" }
// },
// "required": ["Name", "Age"]
//}
// </Snippet2>
}

// <SnippetPerson>
public record Person(string Name, int Age);
// </SnippetPerson>
}
Loading