Exportador de esquema JSON
A JsonSchemaExporter classe, introduzida no .NET 9, permite extrair documentos de esquema JSON de tipos .NET usando uma JsonSerializerOptions instância ou JsonTypeInfo . O esquema resultante fornece uma especificação do contrato de serialização JSON para o tipo .NET. O esquema descreve a forma do que seria serializado e do que pode ser desserializado.
O trecho de código a seguir mostra um exemplo.
public static void SimpleExtraction()
{
JsonSerializerOptions options = JsonSerializerOptions.Default;
JsonNode schema = options.GetJsonSchemaAsNode(typeof(Person));
Console.WriteLine(schema.ToString());
//{
// "type": ["object", "null"],
// "properties": {
// "Name": { "type": "string" },
// "Age": { "type": "integer" },
// "Address": { "type": ["string", "null"], "default": null }
// },
// "required": ["Name", "Age"]
//}
}
record Person(string Name, int Age, string? Address = null);
Como pode ser visto neste exemplo, o exportador distingue entre propriedades anuláveis e não anuláveis, e preenche a required
palavra-chave em virtude de um parâmetro do construtor ser opcional ou não.
Configurar a saída do esquema
Você pode influenciar a saída do esquema pela configuração especificada na JsonSerializerOptions instância ou JsonTypeInfo na qual você chama o GetJsonSchemaAsNode método. O exemplo a seguir define a política de nomenclatura como KebabCaseUpper, grava números como cadeias de caracteres e não permite propriedades não mapeadas.
public static void CustomExtraction()
{
JsonSerializerOptions options = new(JsonSerializerOptions.Default)
{
PropertyNamingPolicy = JsonNamingPolicy.KebabCaseUpper,
NumberHandling = JsonNumberHandling.WriteAsString,
UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow,
};
JsonNode schema = options.GetJsonSchemaAsNode(typeof(MyPoco));
Console.WriteLine(schema.ToString());
//{
// "type": ["object", "null"],
// "properties": {
// "NUMERIC-VALUE": {
// "type": ["string", "integer"],
// "pattern": "^-?(?:0|[1-9]\\d*)$"
// }
// },
// "additionalProperties": false
//}
}
class MyPoco
{
public int NumericValue { get; init; }
}
Você pode controlar ainda mais o esquema gerado usando o JsonSchemaExporterOptions tipo de configuração. O exemplo a seguir define a TreatNullObliviousAsNonNullable propriedade para true
marcar tipos de nível raiz como não anuláveis.
public static void CustomExtraction()
{
JsonSerializerOptions options = JsonSerializerOptions.Default;
JsonSchemaExporterOptions exporterOptions = new()
{
TreatNullObliviousAsNonNullable = true,
};
JsonNode schema = options.GetJsonSchemaAsNode(typeof(Person), exporterOptions);
Console.WriteLine(schema.ToString());
//{
// "type": "object",
// "properties": {
// "Name": { "type": "string" }
// },
// "required": ["Name"]
//}
}
record Person(string Name);
Transformar o esquema gerado
Você pode aplicar suas próprias transformações aos nós de esquema gerados especificando um TransformSchemaNode delegado. O exemplo a seguir incorpora texto de DescriptionAttribute anotações no esquema gerado.
JsonSchemaExporterOptions exporterOptions = new()
{
TransformSchemaNode = (context, schema) =>
{
// Determine if a type or property and extract the relevant attribute provider.
ICustomAttributeProvider? attributeProvider = context.PropertyInfo is not null
? context.PropertyInfo.AttributeProvider
: context.TypeInfo.Type;
// Look up any description attributes.
DescriptionAttribute? descriptionAttr = attributeProvider?
.GetCustomAttributes(inherit: true)
.Select(attr => attr as DescriptionAttribute)
.FirstOrDefault(attr => attr is not null);
// Apply description attribute to the generated schema.
if (descriptionAttr != null)
{
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);
}
}
jObj.Insert(0, "description", descriptionAttr.Description);
}
return schema;
}
};
O exemplo de código a seguir gera um esquema que incorpora description
a origem da palavra-chave a partir de DescriptionAttribute anotações:
JsonSerializerOptions options = JsonSerializerOptions.Default;
JsonNode schema = options.GetJsonSchemaAsNode(typeof(Person), exporterOptions);
Console.WriteLine(schema.ToString());
//{
// "description": "A person",
// "type": ["object", "null"],
// "properties": {
// "Name": { "description": "The name of the person", "type": "string" }
// },
// "required": ["Name"]
//}
[Description("A person")]
record Person([property: Description("The name of the person")] string Name);