2
// Copyright (c) 2007 James Newton-King
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
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
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.
27
using System.Collections.Generic;
30
using System.Globalization;
31
using Newtonsoft.Json.Utilities;
32
using Newtonsoft.Json.Linq;
34
namespace Newtonsoft.Json.Schema
36
internal class JsonSchemaBuilder
38
private JsonReader _reader;
39
private readonly IList<JsonSchema> _stack;
40
private readonly JsonSchemaResolver _resolver;
41
private JsonSchema _currentSchema;
43
private void Push(JsonSchema value)
45
_currentSchema = value;
47
_resolver.LoadedSchemas.Add(value);
50
private JsonSchema Pop()
52
JsonSchema poppedSchema = _currentSchema;
53
_stack.RemoveAt(_stack.Count - 1);
54
_currentSchema = _stack.LastOrDefault();
59
private JsonSchema CurrentSchema
61
get { return _currentSchema; }
64
public JsonSchemaBuilder(JsonSchemaResolver resolver)
66
_stack = new List<JsonSchema>();
70
internal JsonSchema Parse(JsonReader reader)
74
if (reader.TokenType == JsonToken.None)
80
private JsonSchema BuildSchema()
82
if (_reader.TokenType != JsonToken.StartObject)
83
throw new Exception("Expected StartObject while parsing schema object, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
86
// empty schema object
87
if (_reader.TokenType == JsonToken.EndObject)
89
Push(new JsonSchema());
93
string propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture);
97
if (propertyName == JsonSchemaConstants.ReferencePropertyName)
99
string id = (string)_reader.Value;
101
JsonSchema referencedSchema = _resolver.GetSchema(id);
102
if (referencedSchema == null)
103
throw new Exception("Could not resolve schema reference for Id '{0}'.".FormatWith(CultureInfo.InvariantCulture, id));
105
return referencedSchema;
108
// regular ol' schema object
109
Push(new JsonSchema());
111
ProcessSchemaProperty(propertyName);
113
while (_reader.Read() && _reader.TokenType != JsonToken.EndObject)
115
propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture);
118
ProcessSchemaProperty(propertyName);
124
private void ProcessSchemaProperty(string propertyName)
126
switch (propertyName)
128
case JsonSchemaConstants.TypePropertyName:
129
CurrentSchema.Type = ProcessType();
131
case JsonSchemaConstants.IdPropertyName:
132
CurrentSchema.Id = (string) _reader.Value;
134
case JsonSchemaConstants.TitlePropertyName:
135
CurrentSchema.Title = (string) _reader.Value;
137
case JsonSchemaConstants.DescriptionPropertyName:
138
CurrentSchema.Description = (string)_reader.Value;
140
case JsonSchemaConstants.PropertiesPropertyName:
143
case JsonSchemaConstants.ItemsPropertyName:
146
case JsonSchemaConstants.AdditionalPropertiesPropertyName:
147
ProcessAdditionalProperties();
149
case JsonSchemaConstants.OptionalPropertyName:
150
CurrentSchema.Optional = (bool)_reader.Value;
152
case JsonSchemaConstants.RequiresPropertyName:
153
CurrentSchema.Requires = (string) _reader.Value;
155
case JsonSchemaConstants.IdentityPropertyName:
158
case JsonSchemaConstants.MinimumPropertyName:
159
CurrentSchema.Minimum = Convert.ToDouble(_reader.Value, CultureInfo.InvariantCulture);
161
case JsonSchemaConstants.MaximumPropertyName:
162
CurrentSchema.Maximum = Convert.ToDouble(_reader.Value, CultureInfo.InvariantCulture);
164
case JsonSchemaConstants.MaximumLengthPropertyName:
165
CurrentSchema.MaximumLength = Convert.ToInt32(_reader.Value, CultureInfo.InvariantCulture);
167
case JsonSchemaConstants.MinimumLengthPropertyName:
168
CurrentSchema.MinimumLength = Convert.ToInt32(_reader.Value, CultureInfo.InvariantCulture);
170
case JsonSchemaConstants.MaximumItemsPropertyName:
171
CurrentSchema.MaximumItems = Convert.ToInt32(_reader.Value, CultureInfo.InvariantCulture);
173
case JsonSchemaConstants.MinimumItemsPropertyName:
174
CurrentSchema.MinimumItems = Convert.ToInt32(_reader.Value, CultureInfo.InvariantCulture);
176
case JsonSchemaConstants.MaximumDecimalsPropertyName:
177
CurrentSchema.MaximumDecimals = Convert.ToInt32(_reader.Value, CultureInfo.InvariantCulture);
179
case JsonSchemaConstants.DisallowPropertyName:
180
CurrentSchema.Disallow = ProcessType();
182
case JsonSchemaConstants.DefaultPropertyName:
185
case JsonSchemaConstants.HiddenPropertyName:
186
CurrentSchema.Hidden = (bool) _reader.Value;
188
case JsonSchemaConstants.ReadOnlyPropertyName:
189
CurrentSchema.ReadOnly = (bool) _reader.Value;
191
case JsonSchemaConstants.FormatPropertyName:
192
CurrentSchema.Format = (string) _reader.Value;
194
case JsonSchemaConstants.PatternPropertyName:
195
CurrentSchema.Pattern = (string) _reader.Value;
197
case JsonSchemaConstants.OptionsPropertyName:
200
case JsonSchemaConstants.EnumPropertyName:
203
case JsonSchemaConstants.ExtendsPropertyName:
212
private void ProcessExtends()
214
CurrentSchema.Extends = BuildSchema();
217
private void ProcessEnum()
219
if (_reader.TokenType != JsonToken.StartArray)
220
throw new Exception("Expected StartArray token while parsing enum values, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
222
CurrentSchema.Enum = new List<JToken>();
224
while (_reader.Read() && _reader.TokenType != JsonToken.EndArray)
226
JToken value = JToken.ReadFrom(_reader);
227
CurrentSchema.Enum.Add(value);
231
private void ProcessOptions()
233
CurrentSchema.Options = new Dictionary<JToken, string>(new JTokenEqualityComparer());
235
switch (_reader.TokenType)
237
case JsonToken.StartArray:
238
while (_reader.Read() && _reader.TokenType != JsonToken.EndArray)
240
if (_reader.TokenType != JsonToken.StartObject)
241
throw new Exception("Expect object token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
246
while (_reader.Read() && _reader.TokenType != JsonToken.EndObject)
248
string propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture);
251
switch (propertyName)
253
case JsonSchemaConstants.OptionValuePropertyName:
254
value = JToken.ReadFrom(_reader);
256
case JsonSchemaConstants.OptionLabelPropertyName:
257
label = (string) _reader.Value;
260
throw new Exception("Unexpected property in JSON schema option: {0}.".FormatWith(CultureInfo.InvariantCulture, propertyName));
265
throw new Exception("No value specified for JSON schema option.");
267
if (CurrentSchema.Options.ContainsKey(value))
268
throw new Exception("Duplicate value in JSON schema option collection: {0}".FormatWith(CultureInfo.InvariantCulture, value));
270
CurrentSchema.Options.Add(value, label);
274
throw new Exception("Expected array token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
278
private void ProcessDefault()
280
CurrentSchema.Default = JToken.ReadFrom(_reader);
283
private void ProcessIdentity()
285
CurrentSchema.Identity = new List<string>();
287
switch (_reader.TokenType)
289
case JsonToken.String:
290
CurrentSchema.Identity.Add(_reader.Value.ToString());
292
case JsonToken.StartArray:
293
while (_reader.Read() && _reader.TokenType != JsonToken.EndArray)
295
if (_reader.TokenType != JsonToken.String)
296
throw new Exception("Exception JSON property name string token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
298
CurrentSchema.Identity.Add(_reader.Value.ToString());
302
throw new Exception("Expected array or JSON property name string token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
306
private void ProcessAdditionalProperties()
308
if (_reader.TokenType == JsonToken.Boolean)
309
CurrentSchema.AllowAdditionalProperties = (bool)_reader.Value;
311
CurrentSchema.AdditionalProperties = BuildSchema();
314
private void ProcessItems()
316
CurrentSchema.Items = new List<JsonSchema>();
318
switch (_reader.TokenType)
320
case JsonToken.StartObject:
321
CurrentSchema.Items.Add(BuildSchema());
323
case JsonToken.StartArray:
324
while (_reader.Read() && _reader.TokenType != JsonToken.EndArray)
326
CurrentSchema.Items.Add(BuildSchema());
330
throw new Exception("Expected array or JSON schema object token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
334
private void ProcessProperties()
336
IDictionary<string, JsonSchema> properties = new Dictionary<string, JsonSchema>();
338
if (_reader.TokenType != JsonToken.StartObject)
339
throw new Exception("Expected StartObject token while parsing schema properties, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
341
while (_reader.Read() && _reader.TokenType != JsonToken.EndObject)
343
string propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture);
346
if (properties.ContainsKey(propertyName))
347
throw new Exception("Property {0} has already been defined in schema.".FormatWith(CultureInfo.InvariantCulture, propertyName));
349
properties.Add(propertyName, BuildSchema());
352
CurrentSchema.Properties = properties;
355
private JsonSchemaType? ProcessType()
357
switch (_reader.TokenType)
359
case JsonToken.String:
360
return MapType(_reader.Value.ToString());
361
case JsonToken.StartArray:
362
// ensure type is in blank state before ORing values
363
JsonSchemaType? type = JsonSchemaType.None;
365
while (_reader.Read() && _reader.TokenType != JsonToken.EndArray)
367
if (_reader.TokenType != JsonToken.String)
368
throw new Exception("Exception JSON schema type string token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
370
type = type | MapType(_reader.Value.ToString());
375
throw new Exception("Expected array or JSON schema type string token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
379
internal static JsonSchemaType MapType(string type)
381
JsonSchemaType mappedType;
382
if (!JsonSchemaConstants.JsonSchemaTypeMapping.TryGetValue(type, out mappedType))
383
throw new Exception("Invalid JSON schema type: {0}".FormatWith(CultureInfo.InvariantCulture, type));
388
internal static string MapType(JsonSchemaType type)
390
return JsonSchemaConstants.JsonSchemaTypeMapping.Single(kv => kv.Value == type).Key;
2
// Copyright (c) 2007 James Newton-King
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
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
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.
27
using System.Collections.Generic;
29
using Newtonsoft.Json.Utilities.LinqBridge;
33
using System.Globalization;
34
using Newtonsoft.Json.Utilities;
35
using Newtonsoft.Json.Linq;
37
namespace Newtonsoft.Json.Schema
39
internal class JsonSchemaBuilder
41
private JsonReader _reader;
42
private readonly IList<JsonSchema> _stack;
43
private readonly JsonSchemaResolver _resolver;
44
private JsonSchema _currentSchema;
46
private void Push(JsonSchema value)
48
_currentSchema = value;
50
_resolver.LoadedSchemas.Add(value);
53
private JsonSchema Pop()
55
JsonSchema poppedSchema = _currentSchema;
56
_stack.RemoveAt(_stack.Count - 1);
57
_currentSchema = _stack.LastOrDefault();
62
private JsonSchema CurrentSchema
64
get { return _currentSchema; }
67
public JsonSchemaBuilder(JsonSchemaResolver resolver)
69
_stack = new List<JsonSchema>();
73
internal JsonSchema Parse(JsonReader reader)
77
if (reader.TokenType == JsonToken.None)
83
private JsonSchema BuildSchema()
85
if (_reader.TokenType != JsonToken.StartObject)
86
throw JsonReaderException.Create(_reader, "Expected StartObject while parsing schema object, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
89
// empty schema object
90
if (_reader.TokenType == JsonToken.EndObject)
92
Push(new JsonSchema());
96
string propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture);
100
if (propertyName == JsonSchemaConstants.ReferencePropertyName)
102
string id = (string)_reader.Value;
104
// skip to the end of the current object
105
while (_reader.Read() && _reader.TokenType != JsonToken.EndObject)
107
if (_reader.TokenType == JsonToken.StartObject)
108
throw JsonReaderException.Create(_reader, "Found StartObject within the schema reference with the Id '{0}'".FormatWith(CultureInfo.InvariantCulture, id));
111
JsonSchema referencedSchema = _resolver.GetSchema(id);
112
if (referencedSchema == null)
113
throw new JsonException("Could not resolve schema reference for Id '{0}'.".FormatWith(CultureInfo.InvariantCulture, id));
115
return referencedSchema;
118
// regular ol' schema object
119
Push(new JsonSchema());
121
ProcessSchemaProperty(propertyName);
123
while (_reader.Read() && _reader.TokenType != JsonToken.EndObject)
125
propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture);
128
ProcessSchemaProperty(propertyName);
134
private void ProcessSchemaProperty(string propertyName)
136
switch (propertyName)
138
case JsonSchemaConstants.TypePropertyName:
139
CurrentSchema.Type = ProcessType();
141
case JsonSchemaConstants.IdPropertyName:
142
CurrentSchema.Id = (string) _reader.Value;
144
case JsonSchemaConstants.TitlePropertyName:
145
CurrentSchema.Title = (string) _reader.Value;
147
case JsonSchemaConstants.DescriptionPropertyName:
148
CurrentSchema.Description = (string)_reader.Value;
150
case JsonSchemaConstants.PropertiesPropertyName:
153
case JsonSchemaConstants.ItemsPropertyName:
156
case JsonSchemaConstants.AdditionalPropertiesPropertyName:
157
ProcessAdditionalProperties();
159
case JsonSchemaConstants.PatternPropertiesPropertyName:
160
ProcessPatternProperties();
162
case JsonSchemaConstants.RequiredPropertyName:
163
CurrentSchema.Required = (bool)_reader.Value;
165
case JsonSchemaConstants.RequiresPropertyName:
166
CurrentSchema.Requires = (string) _reader.Value;
168
case JsonSchemaConstants.IdentityPropertyName:
171
case JsonSchemaConstants.MinimumPropertyName:
172
CurrentSchema.Minimum = Convert.ToDouble(_reader.Value, CultureInfo.InvariantCulture);
174
case JsonSchemaConstants.MaximumPropertyName:
175
CurrentSchema.Maximum = Convert.ToDouble(_reader.Value, CultureInfo.InvariantCulture);
177
case JsonSchemaConstants.ExclusiveMinimumPropertyName:
178
CurrentSchema.ExclusiveMinimum = (bool)_reader.Value;
180
case JsonSchemaConstants.ExclusiveMaximumPropertyName:
181
CurrentSchema.ExclusiveMaximum = (bool)_reader.Value;
183
case JsonSchemaConstants.MaximumLengthPropertyName:
184
CurrentSchema.MaximumLength = Convert.ToInt32(_reader.Value, CultureInfo.InvariantCulture);
186
case JsonSchemaConstants.MinimumLengthPropertyName:
187
CurrentSchema.MinimumLength = Convert.ToInt32(_reader.Value, CultureInfo.InvariantCulture);
189
case JsonSchemaConstants.MaximumItemsPropertyName:
190
CurrentSchema.MaximumItems = Convert.ToInt32(_reader.Value, CultureInfo.InvariantCulture);
192
case JsonSchemaConstants.MinimumItemsPropertyName:
193
CurrentSchema.MinimumItems = Convert.ToInt32(_reader.Value, CultureInfo.InvariantCulture);
195
case JsonSchemaConstants.DivisibleByPropertyName:
196
CurrentSchema.DivisibleBy = Convert.ToDouble(_reader.Value, CultureInfo.InvariantCulture);
198
case JsonSchemaConstants.DisallowPropertyName:
199
CurrentSchema.Disallow = ProcessType();
201
case JsonSchemaConstants.DefaultPropertyName:
204
case JsonSchemaConstants.HiddenPropertyName:
205
CurrentSchema.Hidden = (bool) _reader.Value;
207
case JsonSchemaConstants.ReadOnlyPropertyName:
208
CurrentSchema.ReadOnly = (bool) _reader.Value;
210
case JsonSchemaConstants.FormatPropertyName:
211
CurrentSchema.Format = (string) _reader.Value;
213
case JsonSchemaConstants.PatternPropertyName:
214
CurrentSchema.Pattern = (string) _reader.Value;
216
case JsonSchemaConstants.OptionsPropertyName:
219
case JsonSchemaConstants.EnumPropertyName:
222
case JsonSchemaConstants.ExtendsPropertyName:
231
private void ProcessExtends()
233
CurrentSchema.Extends = BuildSchema();
236
private void ProcessEnum()
238
if (_reader.TokenType != JsonToken.StartArray)
239
throw JsonReaderException.Create(_reader, "Expected StartArray token while parsing enum values, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
241
CurrentSchema.Enum = new List<JToken>();
243
while (_reader.Read() && _reader.TokenType != JsonToken.EndArray)
245
JToken value = JToken.ReadFrom(_reader);
246
CurrentSchema.Enum.Add(value);
250
private void ProcessOptions()
252
CurrentSchema.Options = new Dictionary<JToken, string>(new JTokenEqualityComparer());
254
switch (_reader.TokenType)
256
case JsonToken.StartArray:
257
while (_reader.Read() && _reader.TokenType != JsonToken.EndArray)
259
if (_reader.TokenType != JsonToken.StartObject)
260
throw JsonReaderException.Create(_reader, "Expect object token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
265
while (_reader.Read() && _reader.TokenType != JsonToken.EndObject)
267
string propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture);
270
switch (propertyName)
272
case JsonSchemaConstants.OptionValuePropertyName:
273
value = JToken.ReadFrom(_reader);
275
case JsonSchemaConstants.OptionLabelPropertyName:
276
label = (string) _reader.Value;
279
throw JsonReaderException.Create(_reader, "Unexpected property in JSON schema option: {0}.".FormatWith(CultureInfo.InvariantCulture, propertyName));
284
throw new JsonException("No value specified for JSON schema option.");
286
if (CurrentSchema.Options.ContainsKey(value))
287
throw new JsonException("Duplicate value in JSON schema option collection: {0}".FormatWith(CultureInfo.InvariantCulture, value));
289
CurrentSchema.Options.Add(value, label);
293
throw JsonReaderException.Create(_reader, "Expected array token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
297
private void ProcessDefault()
299
CurrentSchema.Default = JToken.ReadFrom(_reader);
302
private void ProcessIdentity()
304
CurrentSchema.Identity = new List<string>();
306
switch (_reader.TokenType)
308
case JsonToken.String:
309
CurrentSchema.Identity.Add(_reader.Value.ToString());
311
case JsonToken.StartArray:
312
while (_reader.Read() && _reader.TokenType != JsonToken.EndArray)
314
if (_reader.TokenType != JsonToken.String)
315
throw JsonReaderException.Create(_reader, "Exception JSON property name string token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
317
CurrentSchema.Identity.Add(_reader.Value.ToString());
321
throw JsonReaderException.Create(_reader, "Expected array or JSON property name string token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
325
private void ProcessAdditionalProperties()
327
if (_reader.TokenType == JsonToken.Boolean)
328
CurrentSchema.AllowAdditionalProperties = (bool)_reader.Value;
330
CurrentSchema.AdditionalProperties = BuildSchema();
333
private void ProcessPatternProperties()
335
Dictionary<string, JsonSchema> patternProperties = new Dictionary<string, JsonSchema>();
337
if (_reader.TokenType != JsonToken.StartObject)
338
throw JsonReaderException.Create(_reader, "Expected StartObject token.");
340
while (_reader.Read() && _reader.TokenType != JsonToken.EndObject)
342
string propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture);
345
if (patternProperties.ContainsKey(propertyName))
346
throw new JsonException("Property {0} has already been defined in schema.".FormatWith(CultureInfo.InvariantCulture, propertyName));
348
patternProperties.Add(propertyName, BuildSchema());
351
CurrentSchema.PatternProperties = patternProperties;
354
private void ProcessItems()
356
CurrentSchema.Items = new List<JsonSchema>();
358
switch (_reader.TokenType)
360
case JsonToken.StartObject:
361
CurrentSchema.Items.Add(BuildSchema());
363
case JsonToken.StartArray:
364
while (_reader.Read() && _reader.TokenType != JsonToken.EndArray)
366
CurrentSchema.Items.Add(BuildSchema());
370
throw JsonReaderException.Create(_reader, "Expected array or JSON schema object token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
374
private void ProcessProperties()
376
IDictionary<string, JsonSchema> properties = new Dictionary<string, JsonSchema>();
378
if (_reader.TokenType != JsonToken.StartObject)
379
throw JsonReaderException.Create(_reader, "Expected StartObject token while parsing schema properties, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
381
while (_reader.Read() && _reader.TokenType != JsonToken.EndObject)
383
string propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture);
386
if (properties.ContainsKey(propertyName))
387
throw new JsonException("Property {0} has already been defined in schema.".FormatWith(CultureInfo.InvariantCulture, propertyName));
389
properties.Add(propertyName, BuildSchema());
392
CurrentSchema.Properties = properties;
395
private JsonSchemaType? ProcessType()
397
switch (_reader.TokenType)
399
case JsonToken.String:
400
return MapType(_reader.Value.ToString());
401
case JsonToken.StartArray:
402
// ensure type is in blank state before ORing values
403
JsonSchemaType? type = JsonSchemaType.None;
405
while (_reader.Read() && _reader.TokenType != JsonToken.EndArray)
407
if (_reader.TokenType != JsonToken.String)
408
throw JsonReaderException.Create(_reader, "Exception JSON schema type string token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
410
type = type | MapType(_reader.Value.ToString());
415
throw JsonReaderException.Create(_reader, "Expected array or JSON schema type string token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
419
internal static JsonSchemaType MapType(string type)
421
JsonSchemaType mappedType;
422
if (!JsonSchemaConstants.JsonSchemaTypeMapping.TryGetValue(type, out mappedType))
423
throw new JsonException("Invalid JSON schema type: {0}".FormatWith(CultureInfo.InvariantCulture, type));
428
internal static string MapType(JsonSchemaType type)
430
return JsonSchemaConstants.JsonSchemaTypeMapping.Single(kv => kv.Value == type).Key;
b'\\ No newline at end of file'