JsonSchema4
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NJsonSchema.Collections;
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, JsonSchema4> _definitions;
private ICollection<JsonSchema4> _allOf;
private ICollection<JsonSchema4> _anyOf;
private ICollection<JsonSchema4> _oneOf;
private JsonSchema4 _items;
private JsonSchema4 _not;
[JsonProperty("$schema", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public string Schema { get; set; }
[JsonProperty("id", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
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 virtual JsonSchema4 Parent { 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<string> RequiredProperties { get; set; }
[JsonIgnore]
public IDictionary<string, JsonProperty> Properties {
get {
return _properties;
}
internal set {
if (_properties != value) {
RegisterProperties(_properties, value);
_properties = value;
}
}
}
[JsonProperty("items", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public JsonSchema4 Items {
get {
return _items;
}
set {
_items = value;
if (_items != null)
_items.Parent = this;
}
}
[JsonProperty("not", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public JsonSchema4 Not {
get {
return _not;
}
internal set {
_not = value;
if (_not != null)
_not.Parent = 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;
}
}
}
[JsonProperty("type", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
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("definitions", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal IDictionary<string, JsonSchema4> DefinitionsRaw {
get {
if (Definitions == null || Definitions.Count <= 0)
return null;
return Definitions;
}
set {
Definitions = (value ?? new ObservableDictionary<string, JsonSchema4>());
}
}
[JsonProperty("allOf", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal ICollection<JsonSchema4> AllOfRaw {
get {
if (AllOf == null || AllOf.Count <= 0)
return null;
return AllOf;
}
set {
AllOf = (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 ?? 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 ?? new ObservableCollection<JsonSchema4>());
}
}
public JsonSchema4()
{
Initialize();
}
public static JsonSchema4 FromType<TType>()
{
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
return jsonSchemaGenerator.Generate<JsonSchema4>(typeof(TType));
}
public static JsonSchema4 FromJson(string data)
{
return JsonConvert.DeserializeObject<JsonSchema4>(data, new JsonSerializerSettings {
ConstructorHandling = ConstructorHandling.Default
});
}
public string ToJson()
{
string schema = Schema;
Schema = "http://json-schema.org/draft-04/schema#";
string result = JsonConvert.SerializeObject(this, Formatting.Indented);
Schema = schema;
return result;
}
public ICollection<ValidationError> Validate(JToken token)
{
JsonSchemaValidator jsonSchemaValidator = new JsonSchemaValidator(this);
return jsonSchemaValidator.Validate(token, null, null);
}
public JsonSchema4 FindRootParent()
{
JsonSchema4 parent = Parent;
if (parent == null)
return this;
while (parent.Parent != null) {
parent = parent.Parent;
}
return parent;
}
[OnDeserialized]
internal void OnDeserialized(StreamingContext ctx)
{
Initialize();
}
private static JsonObjectType ConvertSimpleTypeFromString(string value)
{
return JsonConvert.DeserializeObject<JsonObjectType>("\"" + value + "\"");
}
private void Initialize()
{
if (Properties == null)
Properties = 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>();
}
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>) {
ObservableDictionary<string, JsonProperty> observableDictionary = (ObservableDictionary<string, JsonProperty>)sender;
foreach (KeyValuePair<string, JsonProperty> item in observableDictionary) {
item.Value.Key = item.Key;
item.Value.Parent = this;
}
} else if (sender is ObservableCollection<JsonSchema4>) {
ObservableCollection<JsonSchema4> observableCollection = (ObservableCollection<JsonSchema4>)sender;
foreach (JsonSchema4 item2 in observableCollection) {
item2.Parent = this;
}
} else if (sender is ObservableDictionary<string, JsonSchema4>) {
ObservableDictionary<string, JsonSchema4> observableDictionary2 = (ObservableDictionary<string, JsonSchema4>)sender;
foreach (JsonSchema4 value in observableDictionary2.Values) {
value.Parent = this;
}
}
}
}
}