-Lots of XML code comments

-Added type name handling to serializer
-Fixed various compilation warnings
This commit is contained in:
JamesNK
2009-05-30 06:13:51 +00:00
parent f0c5be266b
commit fec0ec5a1d
56 changed files with 1367 additions and 205 deletions
+22
View File
@@ -32,8 +32,19 @@ using Newtonsoft.Json.Utilities;
namespace Newtonsoft.Json.Schema
{
/// <summary>
/// Contains the JSON schema extension methods.
/// </summary>
public static class Extensions
{
/// <summary>
/// Determines whether the <see cref="JToken"/> is valid.
/// </summary>
/// <param name="source">The source <see cref="JToken"/> to test.</param>
/// <param name="schema">The schema to test with.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="JToken"/> is valid; otherwise, <c>false</c>.
/// </returns>
public static bool IsValid(this JToken source, JsonSchema schema)
{
bool valid = true;
@@ -41,11 +52,22 @@ namespace Newtonsoft.Json.Schema
return valid;
}
/// <summary>
/// Validates the specified <see cref="JToken"/>.
/// </summary>
/// <param name="source">The source <see cref="JToken"/> to test.</param>
/// <param name="schema">The schema to test with.</param>
public static void Validate(this JToken source, JsonSchema schema)
{
source.Validate(schema, null);
}
/// <summary>
/// Validates the specified <see cref="JToken"/>.
/// </summary>
/// <param name="source">The source <see cref="JToken"/> to test.</param>
/// <param name="schema">The schema to test with.</param>
/// <param name="validationEventHandler">The validation event handler.</param>
public static void Validate(this JToken source, JsonSchema schema, ValidationEventHandler validationEventHandler)
{
ValidationUtils.ArgumentNotNull(source, "source");
+150
View File
@@ -35,35 +35,145 @@ using System.Globalization;
namespace Newtonsoft.Json.Schema
{
/// <summary>
/// An in-memory representation of a JSON Schema.
/// </summary>
public class JsonSchema
{
/// <summary>
/// Gets or sets the id.
/// </summary>
public string Id { get; set; }
/// <summary>
/// Gets or sets the title.
/// </summary>
public string Title { get; set; }
/// <summary>
/// Gets or sets whether the object is optional.
/// </summary>
public bool? Optional { get; set; }
/// <summary>
/// Gets or sets whether the object is read only.
/// </summary>
public bool? ReadOnly { get; set; }
/// <summary>
/// Gets or sets whether the object is visible to users.
/// </summary>
public bool? Hidden { get; set; }
/// <summary>
/// Gets or sets whether the object is transient.
/// </summary>
public bool? Transient { get; set; }
/// <summary>
/// Gets or sets the description of the object.
/// </summary>
public string Description { get; set; }
/// <summary>
/// Gets or sets the types of values allowed by the object.
/// </summary>
/// <value>The type.</value>
public JsonSchemaType? Type { get; set; }
/// <summary>
/// Gets or sets the pattern.
/// </summary>
/// <value>The pattern.</value>
public string Pattern { get; set; }
/// <summary>
/// Gets or sets the minimum length.
/// </summary>
/// <value>The minimum length.</value>
public int? MinimumLength { get; set; }
/// <summary>
/// Gets or sets the maximum length.
/// </summary>
/// <value>The maximum length.</value>
public int? MaximumLength { get; set; }
/// <summary>
/// Gets or sets the maximum decimals.
/// </summary>
/// <value>The maximum decimals.</value>
public int? MaximumDecimals { get; set; }
/// <summary>
/// Gets or sets the minimum.
/// </summary>
/// <value>The minimum.</value>
public double? Minimum { get; set; }
/// <summary>
/// Gets or sets the maximum.
/// </summary>
/// <value>The maximum.</value>
public double? Maximum { get; set; }
/// <summary>
/// Gets or sets the minimum number of items.
/// </summary>
/// <value>The minimum number of items.</value>
public int? MinimumItems { get; set; }
/// <summary>
/// Gets or sets the maximum number of items.
/// </summary>
/// <value>The maximum number of items.</value>
public int? MaximumItems { get; set; }
/// <summary>
/// Gets or sets the <see cref="JsonSchema"/> of items.
/// </summary>
/// <value>The <see cref="JsonSchema"/> of items.</value>
public IList<JsonSchema> Items { get; set; }
/// <summary>
/// Gets or sets the <see cref="JsonSchema"/> of properties.
/// </summary>
/// <value>The <see cref="JsonSchema"/> of properties.</value>
public IDictionary<string, JsonSchema> Properties { get; set; }
/// <summary>
/// Gets or sets the <see cref="JsonSchema"/> of additional properties.
/// </summary>
/// <value>The <see cref="JsonSchema"/> of additional properties.</value>
public JsonSchema AdditionalProperties { get; set; }
/// <summary>
/// Gets or sets a value indicating whether additional properties are allowed.
/// </summary>
/// <value>
/// <c>true</c> if additional properties are allowed; otherwise, <c>false</c>.
/// </value>
public bool AllowAdditionalProperties { get; set; }
/// <summary>
/// Gets or sets the required property if this property is present.
/// </summary>
/// <value>The required property if this property is present.</value>
public string Requires { get; set; }
/// <summary>
/// Gets or sets the identity.
/// </summary>
/// <value>The identity.</value>
public IList<string> Identity { get; set; }
/// <summary>
/// Gets or sets the a collection of valid enum values allowed.
/// </summary>
/// <value>A collection of valid enum values allowed.</value>
public IList<JToken> Enum { get; set; }
/// <summary>
/// Gets or sets a collection of options.
/// </summary>
/// <value>A collection of options.</value>
public IDictionary<JToken, string> Options { get; set; }
/// <summary>
/// Gets or sets disallowed types.
/// </summary>
/// <value>The disallow types.</value>
public JsonSchemaType? Disallow { get; set; }
/// <summary>
/// Gets or sets the default value.
/// </summary>
/// <value>The default value.</value>
public JToken Default { get; set; }
/// <summary>
/// Gets or sets the extend <see cref="JsonSchema"/>.
/// </summary>
/// <value>The extended <see cref="JsonSchema"/>.</value>
public JsonSchema Extends { get; set; }
/// <summary>
/// Gets or sets the format.
/// </summary>
/// <value>The format.</value>
public string Format { get; set; }
private readonly string _internalId = Guid.NewGuid().ToString("N");
@@ -73,16 +183,30 @@ namespace Newtonsoft.Json.Schema
get { return _internalId; }
}
/// <summary>
/// Initializes a new instance of the <see cref="JsonSchema"/> class.
/// </summary>
public JsonSchema()
{
AllowAdditionalProperties = true;
}
/// <summary>
/// Reads a <see cref="JsonSchema"/> from the specified <see cref="JsonReader"/>.
/// </summary>
/// <param name="reader">The <see cref="JsonReader"/> containing the JSON Schema to read.</param>
/// <returns>The <see cref="JsonSchema"/> object representing the JSON Schema.</returns>
public static JsonSchema Read(JsonReader reader)
{
return Read(reader, new JsonSchemaResolver());
}
/// <summary>
/// Reads a <see cref="JsonSchema"/> from the specified <see cref="JsonReader"/>.
/// </summary>
/// <param name="reader">The <see cref="JsonReader"/> containing the JSON Schema to read.</param>
/// <param name="resolver">The <see cref="JsonSchemaResolver"/> to use when resolving schema references.</param>
/// <returns>The <see cref="JsonSchema"/> object representing the JSON Schema.</returns>
public static JsonSchema Read(JsonReader reader, JsonSchemaResolver resolver)
{
ValidationUtils.ArgumentNotNull(reader, "reader");
@@ -92,11 +216,22 @@ namespace Newtonsoft.Json.Schema
return builder.Parse(reader);
}
/// <summary>
/// Load a <see cref="JsonSchema"/> from a string that contains schema JSON.
/// </summary>
/// <param name="json">A <see cref="String"/> that contains JSON.</param>
/// <returns>A <see cref="JsonSchema"/> populated from the string that contains JSON.</returns>
public static JsonSchema Parse(string json)
{
return Parse(json, new JsonSchemaResolver());
}
/// <summary>
/// Parses the specified json.
/// </summary>
/// <param name="json">The json.</param>
/// <param name="resolver">The resolver.</param>
/// <returns></returns>
public static JsonSchema Parse(string json, JsonSchemaResolver resolver)
{
ValidationUtils.ArgumentNotNull(json, "json");
@@ -106,11 +241,20 @@ namespace Newtonsoft.Json.Schema
return Read(reader, resolver);
}
/// <summary>
/// Writes this schema to a <see cref="JsonWriter"/>.
/// </summary>
/// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param>
public void WriteTo(JsonWriter writer)
{
WriteTo(writer, new JsonSchemaResolver());
}
/// <summary>
/// Writes this schema to a <see cref="JsonWriter"/> using the specified <see cref="JsonSchemaResolver"/>.
/// </summary>
/// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param>
/// <param name="resolver">The resolver used.</param>
public void WriteTo(JsonWriter writer, JsonSchemaResolver resolver)
{
ValidationUtils.ArgumentNotNull(writer, "writer");
@@ -120,6 +264,12 @@ namespace Newtonsoft.Json.Schema
schemaWriter.WriteSchema(this);
}
/// <summary>
/// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
/// </summary>
/// <returns>
/// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
/// </returns>
public override string ToString()
{
StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
@@ -32,7 +32,17 @@ namespace Newtonsoft.Json.Schema
/// </summary>
public class JsonSchemaException : Exception
{
/// <summary>
/// Gets the line number indicating where the error occurred.
/// </summary>
/// <value>The line number indicating where the error occurred.</value>
public int LineNumber { get; private set; }
/// <summary>
/// Gets the line position indicating where the error occurred.
/// </summary>
/// <value>The line position indicating where the error occurred.</value>
public int LinePosition { get; private set; }
/// <summary>
@@ -34,9 +34,19 @@ using Newtonsoft.Json.Serialization;
namespace Newtonsoft.Json.Schema
{
/// <summary>
/// Generates a <see cref="JsonSchema"/> from a specified <see cref="Type"/>.
/// </summary>
public class JsonSchemaGenerator
{
/// <summary>
/// Gets or sets how undefined schemas are handled by the serializer.
/// </summary>
public UndefinedSchemaIdHandling UndefinedSchemaIdHandling { get; set; }
/// <summary>
/// Gets or sets the mapping resolver used by the generator when
/// mapping JSON properties to .NET objet members.
/// </summary>
public IMappingResolver MappingResolver { get; set; }
private class TypeSchema
@@ -96,21 +106,45 @@ namespace Newtonsoft.Json.Schema
return popped;
}
/// <summary>
/// Generate a <see cref="JsonSchema"/> from the specified type.
/// </summary>
/// <param name="type">The type to generate a <see cref="JsonSchema"/> from.</param>
/// <returns>A <see cref="JsonSchema"/> generated from the specified type.</returns>
public JsonSchema Generate(Type type)
{
return Generate(type, new JsonSchemaResolver(), false);
}
/// <summary>
/// Generate a <see cref="JsonSchema"/> from the specified type.
/// </summary>
/// <param name="type">The type to generate a <see cref="JsonSchema"/> from.</param>
/// <param name="resolver">The <see cref="JsonSchemaResolver"/> used to resolve schema references.</param>
/// <returns>A <see cref="JsonSchema"/> generated from the specified type.</returns>
public JsonSchema Generate(Type type, JsonSchemaResolver resolver)
{
return Generate(type, resolver, false);
}
/// <summary>
/// Generate a <see cref="JsonSchema"/> from the specified type.
/// </summary>
/// <param name="type">The type to generate a <see cref="JsonSchema"/> from.</param>
/// <param name="rootSchemaNullable">Specify whether the generated root <see cref="JsonSchema"/> will be nullable.</param>
/// <returns>A <see cref="JsonSchema"/> generated from the specified type.</returns>
public JsonSchema Generate(Type type, bool rootSchemaNullable)
{
return Generate(type, new JsonSchemaResolver(), rootSchemaNullable);
}
/// <summary>
/// Generate a <see cref="JsonSchema"/> from the specified type.
/// </summary>
/// <param name="type">The type to generate a <see cref="JsonSchema"/> from.</param>
/// <param name="resolver">The <see cref="JsonSchemaResolver"/> used to resolve schema references.</param>
/// <param name="rootSchemaNullable">Specify whether the generated root <see cref="JsonSchema"/> will be nullable.</param>
/// <returns>A <see cref="JsonSchema"/> generated from the specified type.</returns>
public JsonSchema Generate(Type type, JsonSchemaResolver resolver, bool rootSchemaNullable)
{
ValidationUtils.ArgumentNotNull(type, "type");
@@ -121,7 +155,7 @@ namespace Newtonsoft.Json.Schema
return GenerateInternal(type, !rootSchemaNullable);
}
public string GetTitle(Type type)
private string GetTitle(Type type)
{
JsonContainerAttribute containerAttribute = JsonTypeReflector.GetJsonContainerAttribute(type);
@@ -131,7 +165,7 @@ namespace Newtonsoft.Json.Schema
return null;
}
public string GetDescription(Type type)
private string GetDescription(Type type)
{
JsonContainerAttribute containerAttribute = JsonTypeReflector.GetJsonContainerAttribute(type);
@@ -255,7 +289,7 @@ namespace Newtonsoft.Json.Schema
if (mapping.DefaultValue != null)
propertySchema.Default = JToken.FromObject(mapping.DefaultValue);
CurrentSchema.Properties.Add(mapping.MappingName, propertySchema);
CurrentSchema.Properties.Add(mapping.PropertyName, propertySchema);
}
}
@@ -33,15 +33,30 @@ using Newtonsoft.Json.Utilities;
namespace Newtonsoft.Json.Schema
{
/// <summary>
/// Resolves <see cref="JsonSchema"/> from an id.
/// </summary>
public class JsonSchemaResolver
{
/// <summary>
/// Gets or sets the loaded schemas.
/// </summary>
/// <value>The loaded schemas.</value>
public IList<JsonSchema> LoadedSchemas { get; protected set; }
/// <summary>
/// Initializes a new instance of the <see cref="JsonSchemaResolver"/> class.
/// </summary>
public JsonSchemaResolver()
{
LoadedSchemas = new List<JsonSchema>();
}
/// <summary>
/// Gets a <see cref="JsonSchema"/> for the specified id.
/// </summary>
/// <param name="id">The id.</param>
/// <returns>A <see cref="JsonSchema"/> for the specified id.</returns>
public virtual JsonSchema GetSchema(string id)
{
JsonSchema schema = LoadedSchemas.SingleOrDefault(s => s.Id == id);
@@ -30,17 +30,47 @@ using System.Text;
namespace Newtonsoft.Json.Schema
{
/// <summary>
/// The value types allowed by the <see cref="JsonSchema"/>.
/// </summary>
[Flags]
public enum JsonSchemaType
{
/// <summary>
/// No type specified.
/// </summary>
None = 0,
/// <summary>
/// String type.
/// </summary>
String = 1,
/// <summary>
/// Float type.
/// </summary>
Float = 2,
/// <summary>
/// Integer type.
/// </summary>
Integer = 4,
/// <summary>
/// Boolean type.
/// </summary>
Boolean = 8,
/// <summary>
/// Object type.
/// </summary>
Object = 16,
/// <summary>
/// Array type.
/// </summary>
Array = 32,
/// <summary>
/// Null type.
/// </summary>
Null = 64,
/// <summary>
/// Any type.
/// </summary>
Any = String | Float | Integer | Boolean | Object | Array | Null
}
}
@@ -25,10 +25,22 @@
namespace Newtonsoft.Json.Schema
{
/// <summary>
/// Specifies undefined schema Id handling options for the <see cref="JsonSchemaGenerator"/>.
/// </summary>
public enum UndefinedSchemaIdHandling
{
/// <summary>
/// Do not infer a schema Id.
/// </summary>
None = 0,
/// <summary>
/// Use the .NET type name as the schema Id.
/// </summary>
UseTypeName = 1,
/// <summary>
/// Use the assembly qualified .NET type name as the schema Id.
/// </summary>
UseAssemblyQualifiedName = 2,
}
}
@@ -28,6 +28,9 @@ using Newtonsoft.Json.Utilities;
namespace Newtonsoft.Json.Schema
{
/// <summary>
/// Returns detailed information related to the <see cref="ValidationEventHandler"/>.
/// </summary>
public class ValidationEventArgs : EventArgs
{
private readonly JsonSchemaException _ex;
@@ -38,11 +41,19 @@ namespace Newtonsoft.Json.Schema
_ex = ex;
}
/// <summary>
/// Gets the <see cref="JsonSchemaException"/> associated with the validation event.
/// </summary>
/// <value>The JsonSchemaException associated with the validation event.</value>
public JsonSchemaException Exception
{
get { return _ex; }
}
/// <summary>
/// Gets the text description corresponding to the validation event.
/// </summary>
/// <value>The text description.</value>
public string Message
{
get { return _ex.Message; }
@@ -25,5 +25,8 @@
namespace Newtonsoft.Json.Schema
{
/// <summary>
/// Represents the callback method that will handle JSON schema validation events and the <see cref="ValidationEventArgs"/>.
/// </summary>
public delegate void ValidationEventHandler(object sender, ValidationEventArgs e);
}