Skip to content
Charles Wagner edited this page Mar 12, 2021 · 15 revisions

Type mappers allow you to generate a custom JSON Schema for a given .NET type.

Primitive type mapper

Example: Set the JSON Schema type to string for the .NET type NodaTime:

var schema = JsonSchema.FromType<Person>(new JsonSchemaGeneratorSettings
{
  TypeMappers =
  {
    new PrimitiveTypeMapper(typeof(NodaTime), s => s.Type = JsonObjectType.String)
  }
});

NJsonSchema.Generation.TypeMappers.PrimitiveTypeMapper

Object type mapper

Example: Generate a custom object JSON Schema for the .NET type MyType:

var schema = JsonSchema.FromType<Person>(new JsonSchemaGeneratorSettings
{
  TypeMappers =
  {
    new ObjectTypeMapper(typeof(MyType), new JsonSchema
      {
        Type = JsonObjectType.Object,
        Properties =
        {
          {
            "Prop",
            new JsonSchemaProperty
            {
              IsRequired = true,
              Type = JsonObjectType.String
            }
          }
        }
      }
    )
  }
});

NJsonSchema.Generation.TypeMappers.ObjectTypeMapper