Code Coverage Statistics for Source File
Newtonsoft.Json\Schema\JsonSchema.cs
Symbol Coverage: 93.75% (30 of 32)
Branch Coverage: 96.92% (63 of 65)
Cyclomatic Complexity Avg: 1.00 Max:1
Code Lines: 31
Symbol Coverage Trend
View:
L | V | Source |
---|---|---|
1 |
#region License |
|
2 |
// Copyright (c) 2007 James Newton-King |
|
3 |
// |
|
4 |
// Permission is hereby granted, free of charge, to any person |
|
5 |
// obtaining a copy of this software and associated documentation |
|
6 |
// files (the "Software"), to deal in the Software without |
|
7 |
// restriction, including without limitation the rights to use, |
|
8 |
// copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
9 |
// copies of the Software, and to permit persons to whom the |
|
10 |
// Software is furnished to do so, subject to the following |
|
11 |
// conditions: |
|
12 |
// |
|
13 |
// The above copyright notice and this permission notice shall be |
|
14 |
// included in all copies or substantial portions of the Software. |
|
15 |
// |
|
16 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
17 |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
18 |
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
19 |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
20 |
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
21 |
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
22 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
23 |
// OTHER DEALINGS IN THE SOFTWARE. |
|
24 |
#endregion |
|
25 |
||
26 |
using System; |
|
27 |
using System.Collections; |
|
28 |
using System.Collections.Generic; |
|
29 |
using System.Linq; |
|
30 |
using System.Text; |
|
31 |
using System.IO; |
|
32 |
using Newtonsoft.Json.Linq; |
|
33 |
using Newtonsoft.Json.Utilities; |
|
34 |
using System.Globalization; |
|
35 |
||
36 |
namespace Newtonsoft.Json.Schema |
|
37 |
{ |
|
38 |
/// <summary> |
|
39 |
/// An in-memory representation of a JSON Schema. |
|
40 |
/// </summary> |
|
41 |
public class JsonSchema |
|
42 |
{ |
|
43 |
/// <summary> |
|
44 |
/// Gets or sets the id. |
|
45 |
/// </summary> |
|
46 |
public string Id { get; set; } |
|
47 |
/// <summary> |
|
48 |
/// Gets or sets the title. |
|
49 |
/// </summary> |
|
50 |
public string Title { get; set; } |
|
51 |
/// <summary> |
|
52 |
/// Gets or sets whether the object is optional. |
|
53 |
/// </summary> |
|
54 |
public bool? Optional { get; set; } |
|
55 |
/// <summary> |
|
56 |
/// Gets or sets whether the object is read only. |
|
57 |
/// </summary> |
|
58 |
public bool? ReadOnly { get; set; } |
|
59 |
/// <summary> |
|
60 |
/// Gets or sets whether the object is visible to users. |
|
61 |
/// </summary> |
|
62 |
public bool? Hidden { get; set; } |
|
63 |
/// <summary> |
|
64 |
/// Gets or sets whether the object is transient. |
|
65 |
/// </summary> |
|
66 |
public bool? Transient { get; set; } |
|
67 |
/// <summary> |
|
68 |
/// Gets or sets the description of the object. |
|
69 |
/// </summary> |
|
70 |
public string Description { get; set; } |
|
71 |
/// <summary> |
|
72 |
/// Gets or sets the types of values allowed by the object. |
|
73 |
/// </summary> |
|
74 |
/// <value>The type.</value> |
|
75 |
public JsonSchemaType? Type { get; set; } |
|
76 |
/// <summary> |
|
77 |
/// Gets or sets the pattern. |
|
78 |
/// </summary> |
|
79 |
/// <value>The pattern.</value> |
|
80 |
public string Pattern { get; set; } |
|
81 |
/// <summary> |
|
82 |
/// Gets or sets the minimum length. |
|
83 |
/// </summary> |
|
84 |
/// <value>The minimum length.</value> |
|
85 |
public int? MinimumLength { get; set; } |
|
86 |
/// <summary> |
|
87 |
/// Gets or sets the maximum length. |
|
88 |
/// </summary> |
|
89 |
/// <value>The maximum length.</value> |
|
90 |
public int? MaximumLength { get; set; } |
|
91 |
/// <summary> |
|
92 |
/// Gets or sets the maximum decimals. |
|
93 |
/// </summary> |
|
94 |
/// <value>The maximum decimals.</value> |
|
95 |
public int? MaximumDecimals { get; set; } |
|
96 |
/// <summary> |
|
97 |
/// Gets or sets the minimum. |
|
98 |
/// </summary> |
|
99 |
/// <value>The minimum.</value> |
|
100 |
public double? Minimum { get; set; } |
|
101 |
/// <summary> |
|
102 |
/// Gets or sets the maximum. |
|
103 |
/// </summary> |
|
104 |
/// <value>The maximum.</value> |
|
105 |
public double? Maximum { get; set; } |
|
106 |
/// <summary> |
|
107 |
/// Gets or sets the minimum number of items. |
|
108 |
/// </summary> |
|
109 |
/// <value>The minimum number of items.</value> |
|
110 |
public int? MinimumItems { get; set; } |
|
111 |
/// <summary> |
|
112 |
/// Gets or sets the maximum number of items. |
|
113 |
/// </summary> |
|
114 |
/// <value>The maximum number of items.</value> |
|
115 |
public int? MaximumItems { get; set; } |
|
116 |
/// <summary> |
|
117 |
/// Gets or sets the <see cref="JsonSchema"/> of items. |
|
118 |
/// </summary> |
|
119 |
/// <value>The <see cref="JsonSchema"/> of items.</value> |
|
120 |
public IList<JsonSchema> Items { get; set; } |
|
121 |
/// <summary> |
|
122 |
/// Gets or sets the <see cref="JsonSchema"/> of properties. |
|
123 |
/// </summary> |
|
124 |
/// <value>The <see cref="JsonSchema"/> of properties.</value> |
|
125 |
public IDictionary<string, JsonSchema> Properties { get; set; } |
|
126 |
/// <summary> |
|
127 |
/// Gets or sets the <see cref="JsonSchema"/> of additional properties. |
|
128 |
/// </summary> |
|
129 |
/// <value>The <see cref="JsonSchema"/> of additional properties.</value> |
|
130 |
public JsonSchema AdditionalProperties { get; set; } |
|
131 |
/// <summary> |
|
132 |
/// Gets or sets a value indicating whether additional properties are allowed. |
|
133 |
/// </summary> |
|
134 |
/// <value> |
|
135 |
/// <c>true</c> if additional properties are allowed; otherwise, <c>false</c>. |
|
136 |
/// </value> |
|
137 |
public bool AllowAdditionalProperties { get; set; } |
|
138 |
/// <summary> |
|
139 |
/// Gets or sets the required property if this property is present. |
|
140 |
/// </summary> |
|
141 |
/// <value>The required property if this property is present.</value> |
|
142 |
public string Requires { get; set; } |
|
143 |
/// <summary> |
|
144 |
/// Gets or sets the identity. |
|
145 |
/// </summary> |
|
146 |
/// <value>The identity.</value> |
|
147 |
public IList<string> Identity { get; set; } |
|
148 |
/// <summary> |
|
149 |
/// Gets or sets the a collection of valid enum values allowed. |
|
150 |
/// </summary> |
|
151 |
/// <value>A collection of valid enum values allowed.</value> |
|
152 |
public IList<JToken> Enum { get; set; } |
|
153 |
/// <summary> |
|
154 |
/// Gets or sets a collection of options. |
|
155 |
/// </summary> |
|
156 |
/// <value>A collection of options.</value> |
|
157 |
public IDictionary<JToken, string> Options { get; set; } |
|
158 |
/// <summary> |
|
159 |
/// Gets or sets disallowed types. |
|
160 |
/// </summary> |
|
161 |
/// <value>The disallow types.</value> |
|
162 |
public JsonSchemaType? Disallow { get; set; } |
|
163 |
/// <summary> |
|
164 |
/// Gets or sets the default value. |
|
165 |
/// </summary> |
|
166 |
/// <value>The default value.</value> |
|
167 |
public JToken Default { get; set; } |
|
168 |
/// <summary> |
|
169 |
/// Gets or sets the extend <see cref="JsonSchema"/>. |
|
170 |
/// </summary> |
|
171 |
/// <value>The extended <see cref="JsonSchema"/>.</value> |
|
172 |
public JsonSchema Extends { get; set; } |
|
173 |
/// <summary> |
|
174 |
/// Gets or sets the format. |
|
175 |
/// </summary> |
|
176 |
/// <value>The format.</value> |
|
177 |
public string Format { get; set; } |
|
178 |
||
179 |
425 |
private readonly string _internalId = Guid.NewGuid().ToString("N");
|
180 |
||
181 |
internal string InternalId |
|
182 |
{ |
|
183 |
466 |
get { return _internalId; }
|
184 |
} |
|
185 |
||
186 |
/// <summary> |
|
187 |
/// Initializes a new instance of the <see cref="JsonSchema"/> class. |
|
188 |
/// </summary> |
|
189 |
425 |
public JsonSchema()
|
190 |
{ |
|
191 |
425 |
AllowAdditionalProperties = true;
|
192 |
425 |
}
|
193 |
||
194 |
/// <summary> |
|
195 |
/// Reads a <see cref="JsonSchema"/> from the specified <see cref="JsonReader"/>. |
|
196 |
/// </summary> |
|
197 |
/// <param name="reader">The <see cref="JsonReader"/> containing the JSON Schema to read.</param> |
|
198 |
/// <returns>The <see cref="JsonSchema"/> object representing the JSON Schema.</returns> |
|
199 |
public static JsonSchema Read(JsonReader reader) |
|
200 |
{ |
|
201 |
0 |
return Read(reader, new JsonSchemaResolver());
|
202 |
0 |
}
|
203 |
||
204 |
/// <summary> |
|
205 |
/// Reads a <see cref="JsonSchema"/> from the specified <see cref="JsonReader"/>. |
|
206 |
/// </summary> |
|
207 |
/// <param name="reader">The <see cref="JsonReader"/> containing the JSON Schema to read.</param> |
|
208 |
/// <param name="resolver">The <see cref="JsonSchemaResolver"/> to use when resolving schema references.</param> |
|
209 |
/// <returns>The <see cref="JsonSchema"/> object representing the JSON Schema.</returns> |
|
210 |
public static JsonSchema Read(JsonReader reader, JsonSchemaResolver resolver) |
|
211 |
{ |
|
212 |
55 |
ValidationUtils.ArgumentNotNull(reader, "reader");
|
213 |
55 |
ValidationUtils.ArgumentNotNull(resolver, "resolver");
|
214 |
||
215 |
55 |
JsonSchemaBuilder builder = new JsonSchemaBuilder(resolver);
|
216 |
55 |
return builder.Parse(reader);
|
217 |
55 |
}
|
218 |
||
219 |
/// <summary> |
|
220 |
/// Load a <see cref="JsonSchema"/> from a string that contains schema JSON. |
|
221 |
/// </summary> |
|
222 |
/// <param name="json">A <see cref="String"/> that contains JSON.</param> |
|
223 |
/// <returns>A <see cref="JsonSchema"/> populated from the string that contains JSON.</returns> |
|
224 |
public static JsonSchema Parse(string json) |
|
225 |
{ |
|
226 |
42 |
return Parse(json, new JsonSchemaResolver());
|
227 |
42 |
}
|
228 |
||
229 |
/// <summary> |
|
230 |
/// Parses the specified json. |
|
231 |
/// </summary> |
|
232 |
/// <param name="json">The json.</param> |
|
233 |
/// <param name="resolver">The resolver.</param> |
|
234 |
/// <returns>A <see cref="JsonSchema"/> populated from the string that contains JSON.</returns> |
|
235 |
public static JsonSchema Parse(string json, JsonSchemaResolver resolver) |
|
236 |
{ |
|
237 |
55 |
ValidationUtils.ArgumentNotNull(json, "json");
|
238 |
||
239 |
55 |
JsonReader reader = new JsonTextReader(new StringReader(json));
|
240 |
||
241 |
55 |
return Read(reader, resolver);
|
242 |
55 |
}
|
243 |
||
244 |
/// <summary> |
|
245 |
/// Writes this schema to a <see cref="JsonWriter"/>. |
|
246 |
/// </summary> |
|
247 |
/// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param> |
|
248 |
public void WriteTo(JsonWriter writer) |
|
249 |
{ |
|
250 |
37 |
WriteTo(writer, new JsonSchemaResolver());
|
251 |
37 |
}
|
252 |
||
253 |
/// <summary> |
|
254 |
/// Writes this schema to a <see cref="JsonWriter"/> using the specified <see cref="JsonSchemaResolver"/>. |
|
255 |
/// </summary> |
|
256 |
/// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param> |
|
257 |
/// <param name="resolver">The resolver used.</param> |
|
258 |
public void WriteTo(JsonWriter writer, JsonSchemaResolver resolver) |
|
259 |
{ |
|
260 |
38 |
ValidationUtils.ArgumentNotNull(writer, "writer");
|
261 |
38 |
ValidationUtils.ArgumentNotNull(resolver, "resolver");
|
262 |
||
263 |
38 |
JsonSchemaWriter schemaWriter = new JsonSchemaWriter(writer, resolver);
|
264 |
38 |
schemaWriter.WriteSchema(this);
|
265 |
38 |
}
|
266 |
||
267 |
/// <summary> |
|
268 |
/// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>. |
|
269 |
/// </summary> |
|
270 |
/// <returns> |
|
271 |
/// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>. |
|
272 |
/// </returns> |
|
273 |
public override string ToString() |
|
274 |
{ |
|
275 |
29 |
StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
|
276 |
29 |
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
277 |
29 |
jsonWriter.Formatting = Formatting.Indented;
|
278 |
||
279 |
29 |
WriteTo(jsonWriter);
|
280 |
||
281 |
29 |
return writer.ToString();
|
282 |
29 |
}
|
283 |
} |
|
284 |
} |