using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using DPumpHydr.WinFrmUI.WenSkin.Json.Linq;
using DPumpHydr.WinFrmUI.WenSkin.Json.Utilities;
namespace DPumpHydr.WinFrmUI.WenSkin.Json.Schema
{
///
///
/// An in-memory representation of a JSON Schema.
///
///
/// JSON Schema validation has been moved to its own package. See http://www.newtonsoft.com/jsonschema for more details.
///
///
[Obsolete("JSON Schema validation has been moved to its own package. See http://www.newtonsoft.com/jsonschema for more details.")]
public class JsonSchema
{
private readonly string _internalId = Guid.NewGuid().ToString("N");
///
/// Gets or sets the id.
///
public string Id { get; set; }
///
/// Gets or sets the title.
///
public string Title { get; set; }
///
/// Gets or sets whether the object is required.
///
public bool? Required { get; set; }
///
/// Gets or sets whether the object is read only.
///
public bool? ReadOnly { get; set; }
///
/// Gets or sets whether the object is visible to users.
///
public bool? Hidden { get; set; }
///
/// Gets or sets whether the object is transient.
///
public bool? Transient { get; set; }
///
/// Gets or sets the description of the object.
///
public string Description { get; set; }
///
/// Gets or sets the types of values allowed by the object.
///
/// The type.
public JsonSchemaType? Type { get; set; }
///
/// Gets or sets the pattern.
///
/// The pattern.
public string Pattern { get; set; }
///
/// Gets or sets the minimum length.
///
/// The minimum length.
public int? MinimumLength { get; set; }
///
/// Gets or sets the maximum length.
///
/// The maximum length.
public int? MaximumLength { get; set; }
///
/// Gets or sets a number that the value should be divisble by.
///
/// A number that the value should be divisble by.
public double? DivisibleBy { get; set; }
///
/// Gets or sets the minimum.
///
/// The minimum.
public double? Minimum { get; set; }
///
/// Gets or sets the maximum.
///
/// The maximum.
public double? Maximum { get; set; }
///
/// Gets or sets a flag indicating whether the value can not equal the number defined by the "minimum" attribute.
///
/// A flag indicating whether the value can not equal the number defined by the "minimum" attribute.
public bool? ExclusiveMinimum { get; set; }
///
/// Gets or sets a flag indicating whether the value can not equal the number defined by the "maximum" attribute.
///
/// A flag indicating whether the value can not equal the number defined by the "maximum" attribute.
public bool? ExclusiveMaximum { get; set; }
///
/// Gets or sets the minimum number of items.
///
/// The minimum number of items.
public int? MinimumItems { get; set; }
///
/// Gets or sets the maximum number of items.
///
/// The maximum number of items.
public int? MaximumItems { get; set; }
///
/// Gets or sets the of items.
///
/// The of items.
public IList Items { get; set; }
///
/// Gets or sets a value indicating whether items in an array are validated using the instance at their array position from .
///
///
/// true if items are validated using their array position; otherwise, false.
///
public bool PositionalItemsValidation { get; set; }
///
/// Gets or sets the of additional items.
///
/// The of additional items.
public JsonSchema AdditionalItems { get; set; }
///
/// Gets or sets a value indicating whether additional items are allowed.
///
///
/// true if additional items are allowed; otherwise, false.
///
public bool AllowAdditionalItems { get; set; }
///
/// Gets or sets whether the array items must be unique.
///
public bool UniqueItems { get; set; }
///
/// Gets or sets the of properties.
///
/// The of properties.
public IDictionary Properties { get; set; }
///
/// Gets or sets the of additional properties.
///
/// The of additional properties.
public JsonSchema AdditionalProperties { get; set; }
///
/// Gets or sets the pattern properties.
///
/// The pattern properties.
public IDictionary PatternProperties { get; set; }
///
/// Gets or sets a value indicating whether additional properties are allowed.
///
///
/// true if additional properties are allowed; otherwise, false.
///
public bool AllowAdditionalProperties { get; set; }
///
/// Gets or sets the required property if this property is present.
///
/// The required property if this property is present.
public string Requires { get; set; }
///
/// Gets or sets the a collection of valid enum values allowed.
///
/// A collection of valid enum values allowed.
public IList Enum { get; set; }
///
/// Gets or sets disallowed types.
///
/// The disallow types.
public JsonSchemaType? Disallow { get; set; }
///
/// Gets or sets the default value.
///
/// The default value.
public JToken Default { get; set; }
///
/// Gets or sets the collection of that this schema extends.
///
/// The collection of that this schema extends.
public IList Extends { get; set; }
///
/// Gets or sets the format.
///
/// The format.
public string Format { get; set; }
internal string Location { get; set; }
internal string InternalId => _internalId;
internal string DeferredReference { get; set; }
internal bool ReferencesResolved { get; set; }
///
/// Initializes a new instance of the class.
///
public JsonSchema()
{
AllowAdditionalProperties = true;
AllowAdditionalItems = true;
}
///
/// Reads a from the specified .
///
/// The containing the JSON Schema to read.
/// The object representing the JSON Schema.
public static JsonSchema Read(JsonReader reader)
{
return Read(reader, new JsonSchemaResolver());
}
///
/// Reads a from the specified .
///
/// The containing the JSON Schema to read.
/// The to use when resolving schema references.
/// The object representing the JSON Schema.
public static JsonSchema Read(JsonReader reader, JsonSchemaResolver resolver)
{
ValidationUtils.ArgumentNotNull(reader, "reader");
ValidationUtils.ArgumentNotNull(resolver, "resolver");
return new JsonSchemaBuilder(resolver).Read(reader);
}
///
/// Load a from a string that contains schema JSON.
///
/// A that contains JSON.
/// A populated from the string that contains JSON.
public static JsonSchema Parse(string json)
{
return Parse(json, new JsonSchemaResolver());
}
///
/// Parses the specified json.
///
/// The json.
/// The resolver.
/// A populated from the string that contains JSON.
public static JsonSchema Parse(string json, JsonSchemaResolver resolver)
{
ValidationUtils.ArgumentNotNull(json, "json");
using JsonReader reader = new JsonTextReader(new StringReader(json));
return Read(reader, resolver);
}
///
/// Writes this schema to a .
///
/// A into which this method will write.
public void WriteTo(JsonWriter writer)
{
WriteTo(writer, new JsonSchemaResolver());
}
///
/// Writes this schema to a using the specified .
///
/// A into which this method will write.
/// The resolver used.
public void WriteTo(JsonWriter writer, JsonSchemaResolver resolver)
{
ValidationUtils.ArgumentNotNull(writer, "writer");
ValidationUtils.ArgumentNotNull(resolver, "resolver");
new JsonSchemaWriter(writer, resolver).WriteSchema(this);
}
///
/// Returns a that represents the current .
///
///
/// A that represents the current .
///
public override string ToString()
{
StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture);
WriteTo(new JsonTextWriter(stringWriter)
{
Formatting = Formatting.Indented
});
return stringWriter.ToString();
}
}
}