Skip to content
Rico Suter edited this page Sep 17, 2016 · 15 revisions

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

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

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

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

var schema = JsonSchema4.FromType<Person>(new JsonSchemaGeneratorSettings
{
	TypeMappers =
	{
		new ObjectTypeMapper(typeof(MyType), new JsonSchema4
			{
				Type = JsonObjectType.Object,
				Properties =
				{
					{
						"Prop",
						new JsonProperty
						{
							IsRequired = true,
							Type = JsonObjectType.String
						}
					}
				}
			}
		)
	}
});
Clone this wiki locally