JsonSchemaGeneratorSettings
The JSON Schema generator settings.
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using NJsonSchema.Generation.TypeMappers;
using NJsonSchema.Infrastructure;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace NJsonSchema.Generation
{
public class JsonSchemaGeneratorSettings
{
public EnumHandling DefaultEnumHandling { get; set; }
public ReferenceTypeNullHandling DefaultReferenceTypeNullHandling { get; set; }
public PropertyNameHandling DefaultPropertyNameHandling { get; set; }
public bool GenerateAbstractProperties { get; set; }
public bool FlattenInheritanceHierarchy { get; set; }
public bool GenerateKnownTypes { get; set; } = true;
public bool GenerateXmlObjects { get; set; }
public bool IgnoreObsoleteProperties { get; set; }
public bool AllowReferencesWithProperties { get; set; }
public SchemaType SchemaType { get; set; }
public IContractResolver ContractResolver { get; set; }
public string[] ExcludedTypeNames { get; set; }
[JsonIgnore]
public ITypeNameGenerator TypeNameGenerator { get; set; }
[JsonIgnore]
public ISchemaNameGenerator SchemaNameGenerator { get; set; }
[JsonIgnore]
public IReflectionService ReflectionService { get; set; }
[JsonIgnore]
public ICollection<ITypeMapper> TypeMappers { get; set; } = new Collection<ITypeMapper>();
[JsonIgnore]
public ICollection<ISchemaProcessor> SchemaProcessors { get; } = new Collection<ISchemaProcessor>();
public IContractResolver ActualContractResolver {
get {
if (DefaultPropertyNameHandling != 0 && ContractResolver != null)
throw new InvalidOperationException("The settingsDefaultPropertyNameHandling and ContractResolver cannot be used at the same time.");
if (ContractResolver != null)
return ContractResolver;
if (DefaultPropertyNameHandling == PropertyNameHandling.CamelCase)
return new CamelCasePropertyNamesContractResolver();
if (DefaultPropertyNameHandling == PropertyNameHandling.SnakeCase)
return new DefaultContractResolver {
NamingStrategy = new SnakeCaseNamingStrategy()
};
return new DefaultContractResolver();
}
}
public JsonSchemaGeneratorSettings()
{
DefaultEnumHandling = EnumHandling.Integer;
DefaultReferenceTypeNullHandling = ReferenceTypeNullHandling.Null;
DefaultPropertyNameHandling = PropertyNameHandling.Default;
SchemaType = SchemaType.JsonSchema;
TypeNameGenerator = new DefaultTypeNameGenerator();
SchemaNameGenerator = new DefaultSchemaNameGenerator();
ReflectionService = new DefaultReflectionService();
ExcludedTypeNames = new string[0];
}
public JsonContract ResolveContract(Type type)
{
if (type.GetTypeInfo().IsGenericTypeDefinition)
return null;
return ActualContractResolver.ResolveContract(type);
}
}
}