#region License // Copyright (c) 2007 James Newton-King // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. #endregion using System; using System.Collections.Generic; using System.IO; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Utilities; using System.Globalization; namespace Newtonsoft.Json.Schema { /// /// An in-memory representation of a JSON Schema. /// public class JsonSchema { /// /// 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 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 identity. /// /// The identity. public IList Identity { 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 a collection of options. /// /// A collection of options. public IDictionary Options { 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 extend . /// /// The extended . public JsonSchema Extends { get; set; } /// /// Gets or sets the format. /// /// The format. public string Format { get; set; } private readonly string _internalId = Guid.NewGuid().ToString("N"); internal string InternalId { get { return _internalId; } } /// /// Initializes a new instance of the class. /// public JsonSchema() { AllowAdditionalProperties = 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"); JsonSchemaBuilder builder = new JsonSchemaBuilder(resolver); return builder.Parse(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"); 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"); JsonSchemaWriter schemaWriter = new JsonSchemaWriter(writer, resolver); schemaWriter.WriteSchema(this); } /// /// Returns a that represents the current . /// /// /// A that represents the current . /// public override string ToString() { StringWriter writer = new StringWriter(CultureInfo.InvariantCulture); JsonTextWriter jsonWriter = new JsonTextWriter(writer); jsonWriter.Formatting = Formatting.Indented; WriteTo(jsonWriter); return writer.ToString(); } } }