<PackageReference Include="NJsonSchema" Version="2.5.5941.14558" />

JsonSchema4

public class 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.Runtime.Serialization; namespace NJsonSchema { public class JsonSchema4 { private IDictionary<string, JsonProperty> _properties; private IDictionary<string, JsonProperty> _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("typeName", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate, Order = -96)] public string TypeName { 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 { 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; } set { if (value is JArray) Type = ((JArray)value).Aggregate(JsonObjectType.None, (JsonObjectType type, JToken token) => type | ConvertSimpleTypeFromString(token.ToString())); else if (value != null) { Type = ConvertSimpleTypeFromString(value.ToString()); } else { Type = JsonObjectType.None; } } } [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 ((IEnumerable<KeyValuePair<string, JsonProperty>>)PatternProperties).ToDictionary((Func<KeyValuePair<string, JsonProperty>, string>)((KeyValuePair<string, JsonProperty> p) => p.Key), (Func<KeyValuePair<string, JsonProperty>, JsonSchema4>)((KeyValuePair<string, JsonProperty> p) => p.Value)); } set { PatternProperties = ((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("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("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; } [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; } [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; } } } [JsonIgnore] internal string RootDirectory { get; set; } [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 this; return SchemaReference.ActualSchema; } } [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 double? MultipleOf { get; set; } [JsonProperty("maximum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] public double? Maximum { get; set; } [JsonProperty("exclusiveMaximum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] public bool IsExclusiveMaximum { get; set; } [JsonProperty("minimum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] public double? 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, JsonProperty> PatternProperties { get { return _patternProperties; } internal set { if (_patternProperties != value) { RegisterProperties(_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; } } } [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 (Properties.Count == 0 && AllowAdditionalProperties) return AdditionalPropertiesSchema != null; return false; } } [JsonIgnore] public bool IsAnyType { get { if (string.IsNullOrEmpty(TypeName) && (Type == JsonObjectType.Object || Type == (JsonObjectType.Null | JsonObjectType.Object)) && Properties.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(); } 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 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), null, null, new JsonSchemaDefinitionAppender(), new SchemaResolver()); } public static JsonSchema4 FromType(Type type, JsonSchemaGeneratorSettings settings) { return new JsonSchemaGenerator(settings).Generate(type, null, null, new JsonSchemaDefinitionAppender(), new SchemaResolver()); } public static JsonSchema4 FromFile(string filePath) { string data = FullDotNetMethods.FileReadAllText(filePath); string rootDirectory = FullDotNetMethods.PathGetDirectoryName(filePath); return FromJson(data, rootDirectory); } public static JsonSchema4 FromJson(string data, string rootDirectory = null) { data = JsonSchemaReferenceUtilities.ConvertJsonReferences(data); JsonSchema4 jsonSchema = JsonConvert.DeserializeObject<JsonSchema4>(data, new JsonSerializerSettings { ConstructorHandling = ConstructorHandling.Default, ReferenceLoopHandling = ReferenceLoopHandling.Serialize, PreserveReferencesHandling = PreserveReferencesHandling.Objects }); jsonSchema.RootDirectory = rootDirectory; JsonSchemaReferenceUtilities.UpdateSchemaReferences(jsonSchema); 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, TypeName = schema.TypeName, SchemaReference = schema }; } public string ToJson() { string schemaVersion = SchemaVersion; SchemaVersion = "http://json-schema.org/draft-04/schema#"; JsonSchemaReferenceUtilities.UpdateSchemaReferencePaths(this, new JsonSchemaDefinitionAppender()); string data = JsonConvert.SerializeObject(this, Formatting.Indented); JsonSchemaReferenceUtilities.UpdateSchemaReferences(this); SchemaVersion = schemaVersion; return JsonSchemaReferenceUtilities.ConvertPropertyReferences(data); } public ICollection<ValidationError> Validate(string jsonData) { JsonSerializerSettings settings = new JsonSerializerSettings { DateParseHandling = DateParseHandling.None }; JObject token = JsonConvert.DeserializeObject<JObject>(jsonData, settings); return Validate(token); } public ICollection<ValidationError> Validate(JToken token) { return new JsonSchemaValidator(ActualSchema).Validate(token, null, null); } public JsonSchema4 FindRootParent() { JsonSchema4 parentSchema = ParentSchema; if (parentSchema == null) return this; while (parentSchema.ParentSchema != null) { parentSchema = parentSchema.ParentSchema; } return parentSchema; } private static JsonObjectType ConvertSimpleTypeFromString(string value) { return JsonConvert.DeserializeObject<JsonObjectType>("\"" + value + "\""); } 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, JsonProperty>(); 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>(); } } }