JsonSchema4
A base class for describing a JSON schema.
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NJsonSchema.Collections;
using NJsonSchema.Generation;
using NJsonSchema.Infrastructure;
using NJsonSchema.Validation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
namespace NJsonSchema
{
public class JsonSchema4 : IDocumentPathProvider
{
private Lazy<object> _typeRaw;
private IDictionary<string, JsonProperty> _properties;
private IDictionary<string, JsonSchema4> _patternProperties;
private IDictionary<string, JsonSchema4> _definitions;
private ICollection<JsonSchema4> _allOf;
private ICollection<JsonSchema4> _anyOf;
private ICollection<JsonSchema4> _oneOf;
private JsonSchema4 _not;
private JsonSchema4 _item;
private ICollection<JsonSchema4> _items;
private bool _allowAdditionalItems = true;
private JsonSchema4 _additionalItemsSchema;
private bool _allowAdditionalProperties = true;
private JsonSchema4 _additionalPropertiesSchema;
private JsonSchema4 _schemaReference;
[JsonProperty("x-typeName", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate, Order = -96)]
public string TypeNameRaw { get; set; }
[JsonProperty("discriminator", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate, Order = -95)]
public string Discriminator { get; set; }
[JsonIgnore]
public Collection<string> EnumerationNames { get; set; }
[JsonProperty("additionalItems", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal object AdditionalItemsRaw {
get {
if (AdditionalItemsSchema != null)
return AdditionalItemsSchema;
if (!AllowAdditionalItems)
return false;
return null;
}
set {
if (value is bool)
AllowAdditionalItems = (bool)value;
else if (value != null) {
AdditionalItemsSchema = FromJsonWithoutReferenceHandling(value.ToString());
}
}
}
[JsonProperty("additionalProperties", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal object AdditionalPropertiesRaw {
get {
if (AdditionalPropertiesSchema != null)
return AdditionalPropertiesSchema;
if (!AllowAdditionalProperties)
return false;
return null;
}
set {
if (value is bool)
AllowAdditionalProperties = (bool)value;
else if (value != null) {
AdditionalPropertiesSchema = FromJsonWithoutReferenceHandling(value.ToString());
}
}
}
[JsonProperty("items", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal object ItemsRaw {
get {
if (Item != null)
return Item;
if (Items.Count > 0)
return Items;
return null;
}
set {
if (value is JArray)
Items = new ObservableCollection<JsonSchema4>(from t in (JArray)value
select FromJsonWithoutReferenceHandling(t.ToString()));
else if (value != null) {
Item = FromJsonWithoutReferenceHandling(value.ToString());
}
}
}
[JsonProperty("type", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate, Order = -97)]
internal object TypeRaw {
get {
if (_typeRaw == null)
ResetTypeRaw();
return _typeRaw.Value;
}
set {
if (value is JArray)
Type = ((JArray)value).Aggregate(JsonObjectType.None, (JsonObjectType type, JToken token) => type | ConvertStringToJsonObjectType(token.ToString()));
else
Type = ConvertStringToJsonObjectType(value as string);
ResetTypeRaw();
}
}
[JsonProperty("required", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal ICollection<string> RequiredPropertiesRaw {
get {
if (RequiredProperties == null || RequiredProperties.Count <= 0)
return null;
return RequiredProperties;
}
set {
RequiredProperties = value;
}
}
[JsonProperty("properties", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal IDictionary<string, JsonSchema4> PropertiesRaw {
get {
if (Properties == null || Properties.Count <= 0)
return null;
return ((IEnumerable<KeyValuePair<string, JsonProperty>>)Properties).ToDictionary((Func<KeyValuePair<string, JsonProperty>, string>)((KeyValuePair<string, JsonProperty> p) => p.Key), (Func<KeyValuePair<string, JsonProperty>, JsonSchema4>)((KeyValuePair<string, JsonProperty> p) => p.Value));
}
set {
Properties = ((value != null) ? new ObservableDictionary<string, JsonProperty>(value.ToDictionary((KeyValuePair<string, JsonSchema4> p) => p.Key, (KeyValuePair<string, JsonSchema4> p) => JsonProperty.FromJsonSchema(p.Key, p.Value))) : new ObservableDictionary<string, JsonProperty>());
}
}
[JsonProperty("patternProperties", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal IDictionary<string, JsonSchema4> PatternPropertiesRaw {
get {
if (PatternProperties == null || PatternProperties.Count <= 0)
return null;
return PatternProperties.ToDictionary((KeyValuePair<string, JsonSchema4> p) => p.Key, (KeyValuePair<string, JsonSchema4> p) => p.Value);
}
set {
PatternProperties = ((value != null) ? new ObservableDictionary<string, JsonSchema4>(value.ToDictionary((KeyValuePair<string, JsonSchema4> p) => p.Key, (KeyValuePair<string, JsonSchema4> p) => p.Value)) : new ObservableDictionary<string, JsonSchema4>());
}
}
[JsonProperty("definitions", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal IDictionary<string, JsonSchema4> DefinitionsRaw {
get {
if (Definitions == null || Definitions.Count <= 0)
return null;
return Definitions;
}
set {
Definitions = ((value != null) ? new ObservableDictionary<string, JsonSchema4>(value) : new ObservableDictionary<string, JsonSchema4>());
}
}
[JsonProperty("x-enumNames", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public Collection<string> EnumerationNamesRaw {
get {
if (EnumerationNames == null || EnumerationNames.Count <= 0)
return null;
return EnumerationNames;
}
set {
EnumerationNames = ((value != null) ? new ObservableCollection<string>(value) : new ObservableCollection<string>());
}
}
[JsonProperty("enum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal ICollection<object> EnumerationRaw {
get {
if (Enumeration == null || Enumeration.Count <= 0)
return null;
return Enumeration;
}
set {
Enumeration = ((value != null) ? new ObservableCollection<object>(value) : new ObservableCollection<object>());
}
}
[JsonProperty("allOf", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal ICollection<JsonSchema4> AllOfRaw {
get {
if (AllOf == null || AllOf.Count <= 0)
return null;
return AllOf;
}
set {
AllOf = ((value != null) ? new ObservableCollection<JsonSchema4>(value) : new ObservableCollection<JsonSchema4>());
}
}
[JsonProperty("anyOf", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal ICollection<JsonSchema4> AnyOfRaw {
get {
if (AnyOf == null || AnyOf.Count <= 0)
return null;
return AnyOf;
}
set {
AnyOf = ((value != null) ? new ObservableCollection<JsonSchema4>(value) : new ObservableCollection<JsonSchema4>());
}
}
[JsonProperty("oneOf", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal ICollection<JsonSchema4> OneOfRaw {
get {
if (OneOf == null || OneOf.Count <= 0)
return null;
return OneOf;
}
set {
OneOf = ((value != null) ? new ObservableCollection<JsonSchema4>(value) : new ObservableCollection<JsonSchema4>());
}
}
[JsonExtensionData]
public IDictionary<string, object> ExtensionData { get; set; }
public static string ToolchainVersion => typeof(JsonSchema4).GetTypeInfo().get_Assembly().GetName()
.Version.ToString();
[JsonIgnore]
public IReadOnlyCollection<JsonSchema4> InheritedSchemas {
get {
return new ReadOnlyCollection<JsonSchema4>((from s in AllOf
where s.ActualSchema.Type == JsonObjectType.Object
select s).ToList());
}
}
[JsonIgnore]
public IReadOnlyCollection<JsonSchema4> AllInheritedSchemas {
get {
IReadOnlyCollection<JsonSchema4> inheritedSchemas = InheritedSchemas;
return inheritedSchemas.Concat(inheritedSchemas.SelectMany((JsonSchema4 s) => s.AllInheritedSchemas)).ToList();
}
}
[JsonIgnore]
public string BaseDiscriminator {
get {
if (!string.IsNullOrEmpty(Discriminator))
return Discriminator;
foreach (JsonSchema4 inheritedSchema in InheritedSchemas) {
string baseDiscriminator = inheritedSchema.ActualSchema.BaseDiscriminator;
if (!string.IsNullOrEmpty(baseDiscriminator))
return baseDiscriminator;
}
return null;
}
}
[JsonIgnore]
public IReadOnlyDictionary<string, JsonProperty> ActualProperties {
get {
return new ReadOnlyDictionary<string, JsonProperty>(Properties.Union((from s in AllOf
where s.ActualSchema.Type == JsonObjectType.None
select s).SelectMany((JsonSchema4 s) => s.ActualSchema.ActualProperties)).ToDictionary((KeyValuePair<string, JsonProperty> p) => p.Key, (KeyValuePair<string, JsonProperty> p) => p.Value));
}
}
[JsonProperty("$schema", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate, Order = -99)]
public string SchemaVersion { get; set; }
[JsonProperty("id", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate, Order = -98)]
public string Id { get; set; }
[JsonProperty("title", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public string Title { get; set; }
[JsonProperty("description", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public string Description { get; set; }
[JsonIgnore]
public JsonObjectType Type { get; set; }
[JsonIgnore]
public string DocumentPath { get; set; }
[JsonProperty("schemaReferencePath", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal string SchemaReferencePath { get; set; }
[JsonIgnore]
public JsonSchema4 SchemaReference {
get {
return _schemaReference;
}
set {
if (_schemaReference != value) {
_schemaReference = value;
SchemaReferencePath = null;
if (value != null) {
Type = JsonObjectType.None;
TypeNameRaw = null;
}
}
}
}
[JsonIgnore]
public bool HasSchemaReference {
get {
return SchemaReference != null;
}
}
[JsonIgnore]
public virtual JsonSchema4 ActualSchema {
get {
if (SchemaReferencePath != null && SchemaReference == null)
throw new InvalidOperationException("The schema reference path '" + SchemaReferencePath + "' has not been resolved.");
if (HasSchemaReference)
return SchemaReference.ActualSchema;
return this;
}
}
[JsonIgnore]
public virtual JsonSchema4 ParentSchema { get; set; }
[JsonProperty("format", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public string Format { get; set; }
[JsonProperty("default", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public object Default { get; set; }
[JsonProperty("multipleOf", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public decimal? MultipleOf { get; set; }
[JsonProperty("maximum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public decimal? Maximum { get; set; }
[JsonProperty("exclusiveMaximum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public bool IsExclusiveMaximum { get; set; }
[JsonProperty("minimum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public decimal? Minimum { get; set; }
[JsonProperty("exclusiveMinimum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public bool IsExclusiveMinimum { get; set; }
[JsonProperty("maxLength", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public int? MaxLength { get; set; }
[JsonProperty("minLength", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public int? MinLength { get; set; }
[JsonProperty("pattern", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public string Pattern { get; set; }
[JsonProperty("maxItems", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public int MaxItems { get; set; }
[JsonProperty("minItems", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public int MinItems { get; set; }
[JsonProperty("uniqueItems", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public bool UniqueItems { get; set; }
[JsonProperty("maxProperties", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public int MaxProperties { get; set; }
[JsonProperty("minProperties", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public int MinProperties { get; set; }
[JsonIgnore]
public ICollection<object> Enumeration { get; set; }
[JsonIgnore]
public bool IsEnumeration {
get {
return Enumeration.Count > 0;
}
}
[JsonIgnore]
public ICollection<string> RequiredProperties { get; set; }
[JsonIgnore]
public IDictionary<string, JsonProperty> Properties {
get {
return _properties;
}
internal set {
if (_properties != value) {
RegisterProperties(_properties, value);
_properties = value;
}
}
}
[JsonIgnore]
public IDictionary<string, JsonSchema4> PatternProperties {
get {
return _patternProperties;
}
internal set {
if (_patternProperties != value) {
RegisterSchemaDictionary(_patternProperties, value);
_patternProperties = value;
}
}
}
[JsonIgnore]
public JsonSchema4 Item {
get {
return _item;
}
set {
if (_item != value) {
_item = value;
if (_item != null) {
_item.ParentSchema = this;
Items.Clear();
}
}
}
}
[JsonIgnore]
public ICollection<JsonSchema4> Items {
get {
return _items;
}
internal set {
if (_items != value) {
RegisterSchemaCollection(_items, value);
_items = value;
if (_items != null)
Item = null;
}
}
}
[JsonProperty("not", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public JsonSchema4 Not {
get {
return _not;
}
set {
_not = value;
if (_not != null)
_not.ParentSchema = this;
}
}
[JsonIgnore]
public IDictionary<string, JsonSchema4> Definitions {
get {
return _definitions;
}
internal set {
if (_definitions != value) {
RegisterSchemaDictionary(_definitions, value);
_definitions = value;
}
}
}
[JsonProperty("resourceDefinitions", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public IDictionary<string, JsonSchema4> ResourceDefinitions { get; set; }
[JsonIgnore]
public ICollection<JsonSchema4> AllOf {
get {
return _allOf;
}
internal set {
if (_allOf != value) {
RegisterSchemaCollection(_allOf, value);
_allOf = value;
}
}
}
[JsonIgnore]
public ICollection<JsonSchema4> AnyOf {
get {
return _anyOf;
}
internal set {
if (_anyOf != value) {
RegisterSchemaCollection(_anyOf, value);
_anyOf = value;
}
}
}
[JsonIgnore]
public ICollection<JsonSchema4> OneOf {
get {
return _oneOf;
}
internal set {
if (_oneOf != value) {
RegisterSchemaCollection(_oneOf, value);
_oneOf = value;
}
}
}
[JsonIgnore]
public bool AllowAdditionalItems {
get {
return _allowAdditionalItems;
}
set {
if (_allowAdditionalItems != value) {
_allowAdditionalItems = value;
if (!_allowAdditionalItems)
AdditionalItemsSchema = null;
}
}
}
[JsonIgnore]
public JsonSchema4 AdditionalItemsSchema {
get {
return _additionalItemsSchema;
}
set {
if (_additionalItemsSchema != value) {
_additionalItemsSchema = value;
if (_additionalItemsSchema != null)
AllowAdditionalItems = true;
}
}
}
[JsonIgnore]
public bool AllowAdditionalProperties {
get {
return _allowAdditionalProperties;
}
set {
if (_allowAdditionalProperties != value) {
_allowAdditionalProperties = value;
if (!_allowAdditionalProperties)
AdditionalPropertiesSchema = null;
}
}
}
[JsonIgnore]
public JsonSchema4 AdditionalPropertiesSchema {
get {
return _additionalPropertiesSchema;
}
set {
if (_additionalPropertiesSchema != value) {
_additionalPropertiesSchema = value;
if (_additionalPropertiesSchema != null)
AllowAdditionalProperties = true;
}
}
}
[JsonIgnore]
public bool IsDictionary {
get {
if (Type.HasFlag(JsonObjectType.Object) && Properties.Count == 0) {
if (!AllowAdditionalProperties)
return PatternProperties.Any();
return true;
}
return false;
}
}
[JsonIgnore]
public bool IsAnyType {
get {
if (string.IsNullOrEmpty(TypeNameRaw) && (Type.HasFlag(JsonObjectType.Object) || Type == JsonObjectType.None) && Properties.Count == 0 && PatternProperties.Count == 0 && AnyOf.Count == 0 && AllOf.Count == 0 && OneOf.Count == 0 && AllowAdditionalProperties && AdditionalPropertiesSchema == null)
return !MultipleOf.HasValue;
return false;
}
}
[OnDeserialized]
internal void OnDeserialized(StreamingContext ctx)
{
Initialize();
}
public string GetTypeName(ITypeNameGenerator typeNameGenerator, string typeNameHint)
{
if (typeNameGenerator != null) {
string text = typeNameGenerator.Generate(this, typeNameHint);
if (text == null)
return null;
return text.Split(new char[1] {
'.'
}).Last();
}
string typeNameRaw = TypeNameRaw;
if (typeNameRaw == null)
return null;
return typeNameRaw.Split(new char[1] {
'.'
}).Last();
}
private void ResetTypeRaw()
{
_typeRaw = new Lazy<object>(delegate {
JsonObjectType[] array = (from v in (from Enum v in Enum.GetValues(Type.GetType())
where Type.HasFlag(v)
select v).OfType<JsonObjectType>()
where v != JsonObjectType.None
select v).ToArray();
if (array.Length > 1)
return new JArray(from f in array
select new JValue(f.ToString().ToLower()));
if (array.Length == 1)
return new JValue(array[0].ToString().ToLower());
return null;
});
}
private void RegisterProperties(IDictionary<string, JsonProperty> oldCollection, IDictionary<string, JsonProperty> newCollection)
{
if (oldCollection != null)
((ObservableDictionary<string, JsonProperty>)oldCollection).CollectionChanged -= InitializeSchemaCollection;
if (newCollection != null) {
((ObservableDictionary<string, JsonProperty>)newCollection).CollectionChanged += InitializeSchemaCollection;
InitializeSchemaCollection(newCollection, null);
}
}
private void RegisterSchemaDictionary(IDictionary<string, JsonSchema4> oldCollection, IDictionary<string, JsonSchema4> newCollection)
{
if (oldCollection != null)
((ObservableDictionary<string, JsonSchema4>)oldCollection).CollectionChanged -= InitializeSchemaCollection;
if (newCollection != null) {
((ObservableDictionary<string, JsonSchema4>)newCollection).CollectionChanged += InitializeSchemaCollection;
InitializeSchemaCollection(newCollection, null);
}
}
private void RegisterSchemaCollection(ICollection<JsonSchema4> oldCollection, ICollection<JsonSchema4> newCollection)
{
if (oldCollection != null)
((ObservableCollection<JsonSchema4>)oldCollection).CollectionChanged -= InitializeSchemaCollection;
if (newCollection != null) {
((ObservableCollection<JsonSchema4>)newCollection).CollectionChanged += InitializeSchemaCollection;
InitializeSchemaCollection(newCollection, null);
}
}
private void InitializeSchemaCollection(object sender, NotifyCollectionChangedEventArgs e)
{
if (sender is ObservableDictionary<string, JsonProperty>) {
foreach (KeyValuePair<string, JsonProperty> item in (ObservableDictionary<string, JsonProperty>)sender) {
item.Value.Name = item.Key;
item.Value.ParentSchema = this;
}
} else if (sender is ObservableCollection<JsonSchema4>) {
foreach (JsonSchema4 item2 in (ObservableCollection<JsonSchema4>)sender) {
item2.ParentSchema = this;
}
} else if (sender is ObservableDictionary<string, JsonSchema4>) {
foreach (JsonSchema4 value in ((ObservableDictionary<string, JsonSchema4>)sender).Values) {
value.ParentSchema = this;
}
}
}
public JsonSchema4()
{
Initialize();
}
public static JsonSchema4 CreateAnySchema()
{
return new JsonSchema4();
}
public static TSchemaType CreateAnySchema<TSchemaType>() where TSchemaType : JsonSchema4, new
{
return new TSchemaType();
}
public static JsonSchema4 FromType<TType>()
{
return FromType<TType>(new JsonSchemaGeneratorSettings());
}
public static JsonSchema4 FromType(Type type)
{
return FromType(type, new JsonSchemaGeneratorSettings());
}
public static JsonSchema4 FromType<TType>(JsonSchemaGeneratorSettings settings)
{
return new JsonSchemaGenerator(settings).Generate(typeof(TType));
}
public static JsonSchema4 FromType(Type type, JsonSchemaGeneratorSettings settings)
{
return new JsonSchemaGenerator(settings).Generate(type);
}
public static JsonSchema4 FromFile(string filePath)
{
return FromFile(filePath, new JsonReferenceResolver());
}
public static JsonSchema4 FromFile(string filePath, JsonReferenceResolver jsonReferenceResolver)
{
return FromJson(DynamicApis.FileReadAllText(filePath), filePath, jsonReferenceResolver);
}
public static JsonSchema4 FromUrl(string url)
{
return FromUrl(url, new JsonReferenceResolver());
}
public static JsonSchema4 FromUrl(string url, JsonReferenceResolver jsonReferenceResolver)
{
return FromJson(DynamicApis.HttpGet(url), url, jsonReferenceResolver);
}
public static JsonSchema4 FromJson(string data)
{
return FromJson(data, null, new JsonReferenceResolver());
}
public static JsonSchema4 FromJson(string data, string documentPath)
{
return FromJson(data, documentPath, new JsonReferenceResolver());
}
public static JsonSchema4 FromJson(string data, string documentPath, JsonReferenceResolver jsonReferenceResolver)
{
data = JsonSchemaReferenceUtilities.ConvertJsonReferences(data);
JsonSchema4 jsonSchema = JsonConvert.DeserializeObject<JsonSchema4>(data, new JsonSerializerSettings {
ConstructorHandling = ConstructorHandling.Default,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
PreserveReferencesHandling = PreserveReferencesHandling.Objects
});
jsonSchema.DocumentPath = documentPath;
if (jsonReferenceResolver != null && !string.IsNullOrEmpty(documentPath))
jsonReferenceResolver.AddDocumentReference(documentPath, jsonSchema);
JsonSchemaReferenceUtilities.UpdateSchemaReferences(jsonSchema, jsonReferenceResolver);
return jsonSchema;
}
internal static JsonSchema4 FromJsonWithoutReferenceHandling(string data)
{
return JsonConvert.DeserializeObject<JsonSchema4>(data, new JsonSerializerSettings {
ConstructorHandling = ConstructorHandling.Default,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
PreserveReferencesHandling = PreserveReferencesHandling.Objects
});
}
public static JsonSchema4 CreateTypeReference(JsonSchema4 schema)
{
return new JsonSchema4 {
Type = JsonObjectType.Object,
TypeNameRaw = schema.TypeNameRaw,
SchemaReference = schema
};
}
public bool Inherits(JsonSchema4 schema)
{
schema = schema.ActualSchema;
return InheritedSchemas.Any(delegate(JsonSchema4 s) {
if (s.ActualSchema != schema)
return s.Inherits(schema);
return true;
});
}
public virtual bool IsNullable(NullHandling nullHandling)
{
if (Type.HasFlag(JsonObjectType.Null) && OneOf.Count == 0)
return true;
if (Type == JsonObjectType.None || Type.HasFlag(JsonObjectType.Null))
return OneOf.Any((JsonSchema4 o) => o.IsNullable(nullHandling));
return false;
}
public string ToJson()
{
return ToJson(null);
}
public string ToJson(ITypeNameGenerator typeNameGenerator)
{
string schemaVersion = SchemaVersion;
SchemaVersion = "http://json-schema.org/draft-04/schema#";
JsonSchemaReferenceUtilities.UpdateSchemaReferencePaths(this, new JsonSchemaDefinitionAppender(this, typeNameGenerator));
string data = JsonConvert.SerializeObject(this, Formatting.Indented);
JsonSchemaReferenceUtilities.UpdateSchemaReferences(this, new JsonReferenceResolver());
SchemaVersion = schemaVersion;
return JsonSchemaReferenceUtilities.ConvertPropertyReferences(data);
}
public ICollection<ValidationError> Validate(string jsonData)
{
JsonSerializerSettings settings = new JsonSerializerSettings {
DateParseHandling = DateParseHandling.None
};
JToken token = JsonConvert.DeserializeObject<JToken>(jsonData, settings);
return Validate(token);
}
public ICollection<ValidationError> Validate(JToken token)
{
return new JsonSchemaValidator(ActualSchema).Validate(token, null, token.Path);
}
public JsonSchema4 FindRootParent()
{
JsonSchema4 parentSchema = ParentSchema;
if (parentSchema == null)
return this;
while (parentSchema.ParentSchema != null) {
parentSchema = parentSchema.ParentSchema;
}
return parentSchema;
}
private static JsonObjectType ConvertStringToJsonObjectType(string value)
{
switch (value) {
case "array":
return JsonObjectType.Array;
case "boolean":
return JsonObjectType.Boolean;
case "integer":
return JsonObjectType.Integer;
case "number":
return JsonObjectType.Number;
case "null":
return JsonObjectType.Null;
case "object":
return JsonObjectType.Object;
case "string":
return JsonObjectType.String;
case "file":
return JsonObjectType.File;
default:
return JsonObjectType.None;
}
}
private void Initialize()
{
if (Items == null)
Items = new ObservableCollection<JsonSchema4>();
if (Properties == null)
Properties = new ObservableDictionary<string, JsonProperty>();
if (PatternProperties == null)
PatternProperties = new ObservableDictionary<string, JsonSchema4>();
if (Definitions == null)
Definitions = new ObservableDictionary<string, JsonSchema4>();
if (RequiredProperties == null)
RequiredProperties = new ObservableCollection<string>();
if (AllOf == null)
AllOf = new ObservableCollection<JsonSchema4>();
if (AnyOf == null)
AnyOf = new ObservableCollection<JsonSchema4>();
if (OneOf == null)
OneOf = new ObservableCollection<JsonSchema4>();
if (Enumeration == null)
Enumeration = new Collection<object>();
if (EnumerationNames == null)
EnumerationNames = new Collection<string>();
}
}
}