Skip to main content
Version: 0.3.x (0.3.0)

Class ValueObjectJsonSchema

Namespace: AdCodicem.ValueObjects.Json
Assembly: AdCodicem.ValueObjects.Json.dll

Describes a value object in a JSON Schema exported by System.Text.Json as its underlying value, carrying the rules declared on it.

public static class ValueObjectJsonSchema

Inheritance​

object ← ValueObjectJsonSchema

Inherited Members​

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Remarks​

builds a schema from , and a type serialized by a converter of its own has none to read: it describes every value object as true, the schema that accepts anything, leaves items out of a collection of them, and loses the null a nullable one accepts. Plugged into , this fills each of them in from the of the value object, the rules its Validate enforces:

var schema = JsonSchemaExporter.GetJsonSchemaAsNode(
AppJsonContext.Default.Options,
typeof(Order),
new JsonSchemaExporterOptions { TransformSchemaNode = ValueObjectJsonSchema.TransformSchemaNode });

The underlying type gives type, and "null" joins it, and the enum, for a nullable value object. MinLength and MaxLength give minLength and maxLength, one character both for a , a pattern pattern, and a bound minimum or maximum on a number; on a value written as a string, a date, a time or a character, which those keywords cannot bound, a sentence of the description states it. A duration, a and a are held to the pattern of the form they are written in, which no format of JSON Schema describes. A closed set lists its values in enum, and the example goes to examples, each written by the value object's own converter. The description and the format follow, the format as the profile says ().

A collection or a dictionary of value objects gets its items or additionalProperties, which the exporter leaves out when the element's schema is true, and a dictionary keyed by a value object gets the key's rules in propertyNames, as the key is written: always text, the text of a number for a value object over a number. A value object is described in place, never as a reference.

The schema a host hands over is completed rather than replaced: a description it already wrote comes first, the value object's following it, and a keyword the value object does not declare, such as a default, stays. Every host built on the exporter exposes the takes, whatever its own context is called: Microsoft.Extensions.AI, the Model Context Protocol SDK, Agent Framework.

A value object is looked up in , without reflection. One nothing registered, a value object written by hand or a construction of a generic one, is described by reflection where the runtime supports dynamic code, and left as the exporter described it under native AOT, where it has to be registered, as serializing it through asks already: a value object written by hand through , and a construction of a generic one through .

Methods​

Apply(JsonTypeInfo, JsonNode, ValueObjectJsonSchemaProfile)​

Describes the value object a schema node stands for, or the value objects a collection or a dictionary holds.

public static JsonNode Apply(JsonTypeInfo typeInfo, JsonNode schema, ValueObjectJsonSchemaProfile profile = ValueObjectJsonSchemaProfile.OpenApi)

Parameters​

typeInfo JsonTypeInfo

The contract of the node's type, under the options the schema describes the wire with.

schema JsonNode

The schema built so far for the node, true for a value object.

profile ValueObjectJsonSchemaProfile

Whom the schema describes a value object for.

Returns​

JsonNode

The schema: a new object in place of true, the same object completed otherwise, or the schema unchanged when the node is neither a value object nor a container of them, or the schema false.

Remarks​

This is the piece every host shares: each one that builds on hands a transform of its own the of the node, whatever its context is called. With Microsoft.Extensions.AI:

var options = new AIJsonSchemaCreateOptions
{
TransformSchemaNode = (context, schema) => ValueObjectJsonSchema.Apply(
context.TypeInfo, schema, ValueObjectJsonSchemaProfile.LanguageModel),
};

The node of a value object is , its converter being its own. A value object System.Text.Json serializes as an object with properties, one written by hand without a converter, has a contract of another kind and is left as it is.

Exceptions​

ArgumentNullException

typeInfo or schema is null.

ArgumentOutOfRangeException

profile is not a defined profile.

CreateTransform(ValueObjectJsonSchemaProfile)​

Creates the transform takes for a profile.

public static Func<JsonSchemaExporterContext, JsonNode, JsonNode> CreateTransform(ValueObjectJsonSchemaProfile profile)

Parameters​

profile ValueObjectJsonSchemaProfile

Whom the schema describes a value object for.

Returns​

Func<JsonSchemaExporterContext, JsonNode, JsonNode>

The transform, the same instance for a given profile.

Remarks​

var options = new JsonSchemaExporterOptions
{
TransformSchemaNode = ValueObjectJsonSchema.CreateTransform(ValueObjectJsonSchemaProfile.LanguageModel),
};

Exceptions​

ArgumentOutOfRangeException

profile is not a defined profile.

TransformSchemaNode(JsonSchemaExporterContext, JsonNode)​

Describes the value objects of a schema the exporter builds, under the profile: the delegate takes.

public static JsonNode TransformSchemaNode(JsonSchemaExporterContext context, JsonNode schema)

Parameters​

context JsonSchemaExporterContext

The context of the node, which names its type.

schema JsonNode

The schema the exporter built for the node.

Returns​

JsonNode

The schema, completed when it describes a value object or a container of value objects.

Exceptions​

ArgumentNullException

schema is null.