~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/Newtonsoft.Json/Src/Newtonsoft.Json/Schema/JsonSchemaBuilder.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.Generic;
28
 
using System.Linq;
29
 
using System.Text;
30
 
using System.Globalization;
31
 
using Newtonsoft.Json.Utilities;
32
 
using Newtonsoft.Json.Linq;
33
 
 
34
 
namespace Newtonsoft.Json.Schema
35
 
{
36
 
  internal class JsonSchemaBuilder
37
 
  {
38
 
    private JsonReader _reader;
39
 
    private readonly IList<JsonSchema> _stack;
40
 
    private readonly JsonSchemaResolver _resolver;
41
 
    private JsonSchema _currentSchema;
42
 
 
43
 
    private void Push(JsonSchema value)
44
 
    {
45
 
      _currentSchema = value;
46
 
      _stack.Add(value);
47
 
      _resolver.LoadedSchemas.Add(value);
48
 
    }
49
 
 
50
 
    private JsonSchema Pop()
51
 
    {
52
 
      JsonSchema poppedSchema = _currentSchema;
53
 
      _stack.RemoveAt(_stack.Count - 1);
54
 
      _currentSchema = _stack.LastOrDefault();
55
 
 
56
 
      return poppedSchema;
57
 
    }
58
 
 
59
 
    private JsonSchema CurrentSchema
60
 
    {
61
 
      get { return _currentSchema; }
62
 
    }
63
 
 
64
 
    public JsonSchemaBuilder(JsonSchemaResolver resolver)
65
 
    {
66
 
      _stack = new List<JsonSchema>();
67
 
      _resolver = resolver;
68
 
    }
69
 
 
70
 
    internal JsonSchema Parse(JsonReader reader)
71
 
    {
72
 
      _reader = reader;
73
 
 
74
 
      if (reader.TokenType == JsonToken.None)
75
 
        _reader.Read();
76
 
 
77
 
      return BuildSchema();
78
 
    }
79
 
 
80
 
    private JsonSchema BuildSchema()
81
 
    {
82
 
      if (_reader.TokenType != JsonToken.StartObject)
83
 
        throw new Exception("Expected StartObject while parsing schema object, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
84
 
 
85
 
      _reader.Read();
86
 
      // empty schema object
87
 
      if (_reader.TokenType == JsonToken.EndObject)
88
 
      {
89
 
        Push(new JsonSchema());
90
 
        return Pop();
91
 
      }
92
 
 
93
 
      string propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture);
94
 
      _reader.Read();
95
 
      
96
 
      // schema reference
97
 
      if (propertyName == JsonSchemaConstants.ReferencePropertyName)
98
 
      {
99
 
        string id = (string)_reader.Value;
100
 
        _reader.Read();
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));
104
 
 
105
 
        return referencedSchema;
106
 
      }
107
 
 
108
 
      // regular ol' schema object
109
 
      Push(new JsonSchema());
110
 
 
111
 
      ProcessSchemaProperty(propertyName);
112
 
 
113
 
      while (_reader.Read() && _reader.TokenType != JsonToken.EndObject)
114
 
      {
115
 
        propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture);
116
 
        _reader.Read();
117
 
 
118
 
        ProcessSchemaProperty(propertyName);
119
 
      }
120
 
 
121
 
      return Pop();
122
 
    }
123
 
 
124
 
    private void ProcessSchemaProperty(string propertyName)
125
 
    {
126
 
      switch (propertyName)
127
 
      {
128
 
        case JsonSchemaConstants.TypePropertyName:
129
 
          CurrentSchema.Type = ProcessType();
130
 
          break;
131
 
        case JsonSchemaConstants.IdPropertyName:
132
 
          CurrentSchema.Id = (string) _reader.Value;
133
 
          break;
134
 
        case JsonSchemaConstants.TitlePropertyName:
135
 
          CurrentSchema.Title = (string) _reader.Value;
136
 
          break;
137
 
        case JsonSchemaConstants.DescriptionPropertyName:
138
 
          CurrentSchema.Description = (string)_reader.Value;
139
 
          break;
140
 
        case JsonSchemaConstants.PropertiesPropertyName:
141
 
          ProcessProperties();
142
 
          break;
143
 
        case JsonSchemaConstants.ItemsPropertyName:
144
 
          ProcessItems();
145
 
          break;
146
 
        case JsonSchemaConstants.AdditionalPropertiesPropertyName:
147
 
          ProcessAdditionalProperties();
148
 
          break;
149
 
        case JsonSchemaConstants.OptionalPropertyName:
150
 
          CurrentSchema.Optional = (bool)_reader.Value;
151
 
          break;
152
 
        case JsonSchemaConstants.RequiresPropertyName:
153
 
          CurrentSchema.Requires = (string) _reader.Value;
154
 
          break;
155
 
        case JsonSchemaConstants.IdentityPropertyName:
156
 
          ProcessIdentity();
157
 
          break;
158
 
        case JsonSchemaConstants.MinimumPropertyName:
159
 
          CurrentSchema.Minimum = Convert.ToDouble(_reader.Value, CultureInfo.InvariantCulture);
160
 
          break;
161
 
        case JsonSchemaConstants.MaximumPropertyName:
162
 
          CurrentSchema.Maximum = Convert.ToDouble(_reader.Value, CultureInfo.InvariantCulture);
163
 
          break;
164
 
        case JsonSchemaConstants.MaximumLengthPropertyName:
165
 
          CurrentSchema.MaximumLength = Convert.ToInt32(_reader.Value, CultureInfo.InvariantCulture);
166
 
          break;
167
 
        case JsonSchemaConstants.MinimumLengthPropertyName:
168
 
          CurrentSchema.MinimumLength = Convert.ToInt32(_reader.Value, CultureInfo.InvariantCulture);
169
 
          break;
170
 
        case JsonSchemaConstants.MaximumItemsPropertyName:
171
 
          CurrentSchema.MaximumItems = Convert.ToInt32(_reader.Value, CultureInfo.InvariantCulture);
172
 
          break;
173
 
        case JsonSchemaConstants.MinimumItemsPropertyName:
174
 
          CurrentSchema.MinimumItems = Convert.ToInt32(_reader.Value, CultureInfo.InvariantCulture);
175
 
          break;
176
 
        case JsonSchemaConstants.MaximumDecimalsPropertyName:
177
 
          CurrentSchema.MaximumDecimals = Convert.ToInt32(_reader.Value, CultureInfo.InvariantCulture);
178
 
          break;
179
 
        case JsonSchemaConstants.DisallowPropertyName:
180
 
          CurrentSchema.Disallow = ProcessType();
181
 
          break;
182
 
        case JsonSchemaConstants.DefaultPropertyName:
183
 
          ProcessDefault();
184
 
          break;
185
 
        case JsonSchemaConstants.HiddenPropertyName:
186
 
          CurrentSchema.Hidden = (bool) _reader.Value;
187
 
          break;
188
 
        case JsonSchemaConstants.ReadOnlyPropertyName:
189
 
          CurrentSchema.ReadOnly = (bool) _reader.Value;
190
 
          break;
191
 
        case JsonSchemaConstants.FormatPropertyName:
192
 
          CurrentSchema.Format = (string) _reader.Value;
193
 
          break;
194
 
        case JsonSchemaConstants.PatternPropertyName:
195
 
          CurrentSchema.Pattern = (string) _reader.Value;
196
 
          break;
197
 
        case JsonSchemaConstants.OptionsPropertyName:
198
 
          ProcessOptions();
199
 
          break;
200
 
        case JsonSchemaConstants.EnumPropertyName:
201
 
          ProcessEnum();
202
 
          break;
203
 
        case JsonSchemaConstants.ExtendsPropertyName:
204
 
          ProcessExtends();
205
 
          break;
206
 
        default:
207
 
          _reader.Skip();
208
 
          break;
209
 
      }
210
 
    }
211
 
 
212
 
    private void ProcessExtends()
213
 
    {
214
 
      CurrentSchema.Extends = BuildSchema();
215
 
    }
216
 
 
217
 
    private void ProcessEnum()
218
 
    {
219
 
      if (_reader.TokenType != JsonToken.StartArray)
220
 
        throw new Exception("Expected StartArray token while parsing enum values, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
221
 
 
222
 
      CurrentSchema.Enum = new List<JToken>();
223
 
 
224
 
      while (_reader.Read() && _reader.TokenType != JsonToken.EndArray)
225
 
      {
226
 
        JToken value = JToken.ReadFrom(_reader);
227
 
        CurrentSchema.Enum.Add(value);
228
 
      }
229
 
    }
230
 
 
231
 
    private void ProcessOptions()
232
 
    {
233
 
      CurrentSchema.Options = new Dictionary<JToken, string>(new JTokenEqualityComparer());
234
 
 
235
 
      switch (_reader.TokenType)
236
 
      {
237
 
        case JsonToken.StartArray:
238
 
          while (_reader.Read() && _reader.TokenType != JsonToken.EndArray)
239
 
          {
240
 
            if (_reader.TokenType != JsonToken.StartObject)
241
 
              throw new Exception("Expect object token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
242
 
 
243
 
            string label = null;
244
 
            JToken value = null;
245
 
 
246
 
            while (_reader.Read() && _reader.TokenType != JsonToken.EndObject)
247
 
            {
248
 
              string propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture);
249
 
              _reader.Read();
250
 
 
251
 
              switch (propertyName)
252
 
              {
253
 
                case JsonSchemaConstants.OptionValuePropertyName:
254
 
                  value = JToken.ReadFrom(_reader);
255
 
                  break;
256
 
                case JsonSchemaConstants.OptionLabelPropertyName:
257
 
                  label = (string) _reader.Value;
258
 
                  break;
259
 
                default:
260
 
                  throw new Exception("Unexpected property in JSON schema option: {0}.".FormatWith(CultureInfo.InvariantCulture, propertyName));
261
 
              }
262
 
            }
263
 
 
264
 
            if (value == null)
265
 
              throw new Exception("No value specified for JSON schema option.");
266
 
 
267
 
            if (CurrentSchema.Options.ContainsKey(value))
268
 
              throw new Exception("Duplicate value in JSON schema option collection: {0}".FormatWith(CultureInfo.InvariantCulture, value));
269
 
 
270
 
            CurrentSchema.Options.Add(value, label);
271
 
          }
272
 
          break;
273
 
        default:
274
 
          throw new Exception("Expected array token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
275
 
      }
276
 
    }
277
 
 
278
 
    private void ProcessDefault()
279
 
    {
280
 
      CurrentSchema.Default = JToken.ReadFrom(_reader);
281
 
    }
282
 
 
283
 
    private void ProcessIdentity()
284
 
    {
285
 
      CurrentSchema.Identity = new List<string>();
286
 
 
287
 
      switch (_reader.TokenType)
288
 
      {
289
 
        case JsonToken.String:
290
 
          CurrentSchema.Identity.Add(_reader.Value.ToString());
291
 
          break;
292
 
        case JsonToken.StartArray:
293
 
          while (_reader.Read() && _reader.TokenType != JsonToken.EndArray)
294
 
          {
295
 
            if (_reader.TokenType != JsonToken.String)
296
 
              throw new Exception("Exception JSON property name string token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
297
 
 
298
 
            CurrentSchema.Identity.Add(_reader.Value.ToString());
299
 
          }
300
 
          break;
301
 
        default:
302
 
          throw new Exception("Expected array or JSON property name string token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
303
 
      }
304
 
    }
305
 
 
306
 
    private void ProcessAdditionalProperties()
307
 
    {
308
 
      if (_reader.TokenType == JsonToken.Boolean)
309
 
        CurrentSchema.AllowAdditionalProperties = (bool)_reader.Value;
310
 
      else
311
 
        CurrentSchema.AdditionalProperties = BuildSchema();
312
 
    }
313
 
 
314
 
    private void ProcessItems()
315
 
    {
316
 
      CurrentSchema.Items = new List<JsonSchema>();
317
 
 
318
 
      switch (_reader.TokenType)
319
 
      {
320
 
        case JsonToken.StartObject:
321
 
          CurrentSchema.Items.Add(BuildSchema());
322
 
          break;
323
 
        case JsonToken.StartArray:
324
 
          while (_reader.Read() && _reader.TokenType != JsonToken.EndArray)
325
 
          {
326
 
            CurrentSchema.Items.Add(BuildSchema());
327
 
          }
328
 
          break;
329
 
        default:
330
 
          throw new Exception("Expected array or JSON schema object token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
331
 
      }
332
 
    }
333
 
 
334
 
    private void ProcessProperties()
335
 
    {
336
 
      IDictionary<string, JsonSchema> properties = new Dictionary<string, JsonSchema>();
337
 
 
338
 
      if (_reader.TokenType != JsonToken.StartObject)
339
 
        throw new Exception("Expected StartObject token while parsing schema properties, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
340
 
 
341
 
      while (_reader.Read() && _reader.TokenType != JsonToken.EndObject)
342
 
      {
343
 
        string propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture);
344
 
        _reader.Read();
345
 
 
346
 
        if (properties.ContainsKey(propertyName))
347
 
          throw new Exception("Property {0} has already been defined in schema.".FormatWith(CultureInfo.InvariantCulture, propertyName));
348
 
 
349
 
        properties.Add(propertyName, BuildSchema());
350
 
      }
351
 
 
352
 
      CurrentSchema.Properties = properties;
353
 
    }
354
 
 
355
 
    private JsonSchemaType? ProcessType()
356
 
    {
357
 
      switch (_reader.TokenType)
358
 
      {
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;
364
 
 
365
 
          while (_reader.Read() && _reader.TokenType != JsonToken.EndArray)
366
 
          {
367
 
            if (_reader.TokenType != JsonToken.String)
368
 
              throw new Exception("Exception JSON schema type string token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
369
 
 
370
 
            type = type | MapType(_reader.Value.ToString());
371
 
          }
372
 
 
373
 
          return type;
374
 
        default:
375
 
          throw new Exception("Expected array or JSON schema type string token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
376
 
      }
377
 
    }
378
 
 
379
 
    internal static JsonSchemaType MapType(string type)
380
 
    {
381
 
      JsonSchemaType mappedType;
382
 
      if (!JsonSchemaConstants.JsonSchemaTypeMapping.TryGetValue(type, out mappedType))
383
 
        throw new Exception("Invalid JSON schema type: {0}".FormatWith(CultureInfo.InvariantCulture, type));
384
 
 
385
 
      return mappedType;
386
 
    }
387
 
 
388
 
    internal static string MapType(JsonSchemaType type)
389
 
    {
390
 
      return JsonSchemaConstants.JsonSchemaTypeMapping.Single(kv => kv.Value == type).Key;
391
 
    }
392
 
  }
393
 
}
 
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.Generic;
 
28
#if NET20
 
29
using Newtonsoft.Json.Utilities.LinqBridge;
 
30
#else
 
31
using System.Linq;
 
32
#endif
 
33
using System.Globalization;
 
34
using Newtonsoft.Json.Utilities;
 
35
using Newtonsoft.Json.Linq;
 
36
 
 
37
namespace Newtonsoft.Json.Schema
 
38
{
 
39
  internal class JsonSchemaBuilder
 
40
  {
 
41
    private JsonReader _reader;
 
42
    private readonly IList<JsonSchema> _stack;
 
43
    private readonly JsonSchemaResolver _resolver;
 
44
    private JsonSchema _currentSchema;
 
45
 
 
46
    private void Push(JsonSchema value)
 
47
    {
 
48
      _currentSchema = value;
 
49
      _stack.Add(value);
 
50
      _resolver.LoadedSchemas.Add(value);
 
51
    }
 
52
 
 
53
    private JsonSchema Pop()
 
54
    {
 
55
      JsonSchema poppedSchema = _currentSchema;
 
56
      _stack.RemoveAt(_stack.Count - 1);
 
57
      _currentSchema = _stack.LastOrDefault();
 
58
 
 
59
      return poppedSchema;
 
60
    }
 
61
 
 
62
    private JsonSchema CurrentSchema
 
63
    {
 
64
      get { return _currentSchema; }
 
65
    }
 
66
 
 
67
    public JsonSchemaBuilder(JsonSchemaResolver resolver)
 
68
    {
 
69
      _stack = new List<JsonSchema>();
 
70
      _resolver = resolver;
 
71
    }
 
72
 
 
73
    internal JsonSchema Parse(JsonReader reader)
 
74
    {
 
75
      _reader = reader;
 
76
 
 
77
      if (reader.TokenType == JsonToken.None)
 
78
        _reader.Read();
 
79
 
 
80
      return BuildSchema();
 
81
    }
 
82
 
 
83
    private JsonSchema BuildSchema()
 
84
    {
 
85
      if (_reader.TokenType != JsonToken.StartObject)
 
86
        throw JsonReaderException.Create(_reader, "Expected StartObject while parsing schema object, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
 
87
 
 
88
      _reader.Read();
 
89
      // empty schema object
 
90
      if (_reader.TokenType == JsonToken.EndObject)
 
91
      {
 
92
        Push(new JsonSchema());
 
93
        return Pop();
 
94
      }
 
95
 
 
96
      string propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture);
 
97
      _reader.Read();
 
98
      
 
99
      // schema reference
 
100
      if (propertyName == JsonSchemaConstants.ReferencePropertyName)
 
101
      {
 
102
        string id = (string)_reader.Value;
 
103
 
 
104
        // skip to the end of the current object
 
105
        while (_reader.Read() && _reader.TokenType != JsonToken.EndObject)
 
106
        {
 
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));
 
109
        }
 
110
 
 
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));
 
114
 
 
115
        return referencedSchema;
 
116
      }
 
117
 
 
118
      // regular ol' schema object
 
119
      Push(new JsonSchema());
 
120
 
 
121
      ProcessSchemaProperty(propertyName);
 
122
 
 
123
      while (_reader.Read() && _reader.TokenType != JsonToken.EndObject)
 
124
      {
 
125
        propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture);
 
126
        _reader.Read();
 
127
 
 
128
        ProcessSchemaProperty(propertyName);
 
129
      }
 
130
 
 
131
      return Pop();
 
132
    }
 
133
 
 
134
    private void ProcessSchemaProperty(string propertyName)
 
135
    {
 
136
      switch (propertyName)
 
137
      {
 
138
        case JsonSchemaConstants.TypePropertyName:
 
139
          CurrentSchema.Type = ProcessType();
 
140
          break;
 
141
        case JsonSchemaConstants.IdPropertyName:
 
142
          CurrentSchema.Id = (string) _reader.Value;
 
143
          break;
 
144
        case JsonSchemaConstants.TitlePropertyName:
 
145
          CurrentSchema.Title = (string) _reader.Value;
 
146
          break;
 
147
        case JsonSchemaConstants.DescriptionPropertyName:
 
148
          CurrentSchema.Description = (string)_reader.Value;
 
149
          break;
 
150
        case JsonSchemaConstants.PropertiesPropertyName:
 
151
          ProcessProperties();
 
152
          break;
 
153
        case JsonSchemaConstants.ItemsPropertyName:
 
154
          ProcessItems();
 
155
          break;
 
156
        case JsonSchemaConstants.AdditionalPropertiesPropertyName:
 
157
          ProcessAdditionalProperties();
 
158
          break;
 
159
        case JsonSchemaConstants.PatternPropertiesPropertyName:
 
160
          ProcessPatternProperties();
 
161
          break;
 
162
        case JsonSchemaConstants.RequiredPropertyName:
 
163
          CurrentSchema.Required = (bool)_reader.Value;
 
164
          break;
 
165
        case JsonSchemaConstants.RequiresPropertyName:
 
166
          CurrentSchema.Requires = (string) _reader.Value;
 
167
          break;
 
168
        case JsonSchemaConstants.IdentityPropertyName:
 
169
          ProcessIdentity();
 
170
          break;
 
171
        case JsonSchemaConstants.MinimumPropertyName:
 
172
          CurrentSchema.Minimum = Convert.ToDouble(_reader.Value, CultureInfo.InvariantCulture);
 
173
          break;
 
174
        case JsonSchemaConstants.MaximumPropertyName:
 
175
          CurrentSchema.Maximum = Convert.ToDouble(_reader.Value, CultureInfo.InvariantCulture);
 
176
          break;
 
177
        case JsonSchemaConstants.ExclusiveMinimumPropertyName:
 
178
          CurrentSchema.ExclusiveMinimum = (bool)_reader.Value;
 
179
          break;
 
180
        case JsonSchemaConstants.ExclusiveMaximumPropertyName:
 
181
          CurrentSchema.ExclusiveMaximum = (bool)_reader.Value;
 
182
          break;
 
183
        case JsonSchemaConstants.MaximumLengthPropertyName:
 
184
          CurrentSchema.MaximumLength = Convert.ToInt32(_reader.Value, CultureInfo.InvariantCulture);
 
185
          break;
 
186
        case JsonSchemaConstants.MinimumLengthPropertyName:
 
187
          CurrentSchema.MinimumLength = Convert.ToInt32(_reader.Value, CultureInfo.InvariantCulture);
 
188
          break;
 
189
        case JsonSchemaConstants.MaximumItemsPropertyName:
 
190
          CurrentSchema.MaximumItems = Convert.ToInt32(_reader.Value, CultureInfo.InvariantCulture);
 
191
          break;
 
192
        case JsonSchemaConstants.MinimumItemsPropertyName:
 
193
          CurrentSchema.MinimumItems = Convert.ToInt32(_reader.Value, CultureInfo.InvariantCulture);
 
194
          break;
 
195
        case JsonSchemaConstants.DivisibleByPropertyName:
 
196
          CurrentSchema.DivisibleBy = Convert.ToDouble(_reader.Value, CultureInfo.InvariantCulture);
 
197
          break;
 
198
        case JsonSchemaConstants.DisallowPropertyName:
 
199
          CurrentSchema.Disallow = ProcessType();
 
200
          break;
 
201
        case JsonSchemaConstants.DefaultPropertyName:
 
202
          ProcessDefault();
 
203
          break;
 
204
        case JsonSchemaConstants.HiddenPropertyName:
 
205
          CurrentSchema.Hidden = (bool) _reader.Value;
 
206
          break;
 
207
        case JsonSchemaConstants.ReadOnlyPropertyName:
 
208
          CurrentSchema.ReadOnly = (bool) _reader.Value;
 
209
          break;
 
210
        case JsonSchemaConstants.FormatPropertyName:
 
211
          CurrentSchema.Format = (string) _reader.Value;
 
212
          break;
 
213
        case JsonSchemaConstants.PatternPropertyName:
 
214
          CurrentSchema.Pattern = (string) _reader.Value;
 
215
          break;
 
216
        case JsonSchemaConstants.OptionsPropertyName:
 
217
          ProcessOptions();
 
218
          break;
 
219
        case JsonSchemaConstants.EnumPropertyName:
 
220
          ProcessEnum();
 
221
          break;
 
222
        case JsonSchemaConstants.ExtendsPropertyName:
 
223
          ProcessExtends();
 
224
          break;
 
225
        default:
 
226
          _reader.Skip();
 
227
          break;
 
228
      }
 
229
    }
 
230
 
 
231
    private void ProcessExtends()
 
232
    {
 
233
      CurrentSchema.Extends = BuildSchema();
 
234
    }
 
235
 
 
236
    private void ProcessEnum()
 
237
    {
 
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));
 
240
 
 
241
      CurrentSchema.Enum = new List<JToken>();
 
242
 
 
243
      while (_reader.Read() && _reader.TokenType != JsonToken.EndArray)
 
244
      {
 
245
        JToken value = JToken.ReadFrom(_reader);
 
246
        CurrentSchema.Enum.Add(value);
 
247
      }
 
248
    }
 
249
 
 
250
    private void ProcessOptions()
 
251
    {
 
252
      CurrentSchema.Options = new Dictionary<JToken, string>(new JTokenEqualityComparer());
 
253
 
 
254
      switch (_reader.TokenType)
 
255
      {
 
256
        case JsonToken.StartArray:
 
257
          while (_reader.Read() && _reader.TokenType != JsonToken.EndArray)
 
258
          {
 
259
            if (_reader.TokenType != JsonToken.StartObject)
 
260
              throw JsonReaderException.Create(_reader, "Expect object token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
 
261
 
 
262
            string label = null;
 
263
            JToken value = null;
 
264
 
 
265
            while (_reader.Read() && _reader.TokenType != JsonToken.EndObject)
 
266
            {
 
267
              string propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture);
 
268
              _reader.Read();
 
269
 
 
270
              switch (propertyName)
 
271
              {
 
272
                case JsonSchemaConstants.OptionValuePropertyName:
 
273
                  value = JToken.ReadFrom(_reader);
 
274
                  break;
 
275
                case JsonSchemaConstants.OptionLabelPropertyName:
 
276
                  label = (string) _reader.Value;
 
277
                  break;
 
278
                default:
 
279
                  throw JsonReaderException.Create(_reader, "Unexpected property in JSON schema option: {0}.".FormatWith(CultureInfo.InvariantCulture, propertyName));
 
280
              }
 
281
            }
 
282
 
 
283
            if (value == null)
 
284
              throw new JsonException("No value specified for JSON schema option.");
 
285
 
 
286
            if (CurrentSchema.Options.ContainsKey(value))
 
287
              throw new JsonException("Duplicate value in JSON schema option collection: {0}".FormatWith(CultureInfo.InvariantCulture, value));
 
288
 
 
289
            CurrentSchema.Options.Add(value, label);
 
290
          }
 
291
          break;
 
292
        default:
 
293
          throw JsonReaderException.Create(_reader, "Expected array token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
 
294
      }
 
295
    }
 
296
 
 
297
    private void ProcessDefault()
 
298
    {
 
299
      CurrentSchema.Default = JToken.ReadFrom(_reader);
 
300
    }
 
301
 
 
302
    private void ProcessIdentity()
 
303
    {
 
304
      CurrentSchema.Identity = new List<string>();
 
305
 
 
306
      switch (_reader.TokenType)
 
307
      {
 
308
        case JsonToken.String:
 
309
          CurrentSchema.Identity.Add(_reader.Value.ToString());
 
310
          break;
 
311
        case JsonToken.StartArray:
 
312
          while (_reader.Read() && _reader.TokenType != JsonToken.EndArray)
 
313
          {
 
314
            if (_reader.TokenType != JsonToken.String)
 
315
              throw JsonReaderException.Create(_reader, "Exception JSON property name string token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
 
316
 
 
317
            CurrentSchema.Identity.Add(_reader.Value.ToString());
 
318
          }
 
319
          break;
 
320
        default:
 
321
          throw JsonReaderException.Create(_reader, "Expected array or JSON property name string token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
 
322
      }
 
323
    }
 
324
 
 
325
    private void ProcessAdditionalProperties()
 
326
    {
 
327
      if (_reader.TokenType == JsonToken.Boolean)
 
328
        CurrentSchema.AllowAdditionalProperties = (bool)_reader.Value;
 
329
      else
 
330
        CurrentSchema.AdditionalProperties = BuildSchema();
 
331
    }
 
332
 
 
333
    private void ProcessPatternProperties()
 
334
    {
 
335
      Dictionary<string, JsonSchema> patternProperties = new Dictionary<string, JsonSchema>();
 
336
 
 
337
      if (_reader.TokenType != JsonToken.StartObject)
 
338
        throw JsonReaderException.Create(_reader, "Expected StartObject token.");
 
339
 
 
340
      while (_reader.Read() && _reader.TokenType != JsonToken.EndObject)
 
341
      {
 
342
        string propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture);
 
343
        _reader.Read();
 
344
 
 
345
        if (patternProperties.ContainsKey(propertyName))
 
346
          throw new JsonException("Property {0} has already been defined in schema.".FormatWith(CultureInfo.InvariantCulture, propertyName));
 
347
 
 
348
        patternProperties.Add(propertyName, BuildSchema());
 
349
      }
 
350
 
 
351
      CurrentSchema.PatternProperties = patternProperties;
 
352
    }
 
353
 
 
354
    private void ProcessItems()
 
355
    {
 
356
      CurrentSchema.Items = new List<JsonSchema>();
 
357
 
 
358
      switch (_reader.TokenType)
 
359
      {
 
360
        case JsonToken.StartObject:
 
361
          CurrentSchema.Items.Add(BuildSchema());
 
362
          break;
 
363
        case JsonToken.StartArray:
 
364
          while (_reader.Read() && _reader.TokenType != JsonToken.EndArray)
 
365
          {
 
366
            CurrentSchema.Items.Add(BuildSchema());
 
367
          }
 
368
          break;
 
369
        default:
 
370
          throw JsonReaderException.Create(_reader, "Expected array or JSON schema object token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
 
371
      }
 
372
    }
 
373
 
 
374
    private void ProcessProperties()
 
375
    {
 
376
      IDictionary<string, JsonSchema> properties = new Dictionary<string, JsonSchema>();
 
377
 
 
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));
 
380
 
 
381
      while (_reader.Read() && _reader.TokenType != JsonToken.EndObject)
 
382
      {
 
383
        string propertyName = Convert.ToString(_reader.Value, CultureInfo.InvariantCulture);
 
384
        _reader.Read();
 
385
 
 
386
        if (properties.ContainsKey(propertyName))
 
387
          throw new JsonException("Property {0} has already been defined in schema.".FormatWith(CultureInfo.InvariantCulture, propertyName));
 
388
 
 
389
        properties.Add(propertyName, BuildSchema());
 
390
      }
 
391
 
 
392
      CurrentSchema.Properties = properties;
 
393
    }
 
394
 
 
395
    private JsonSchemaType? ProcessType()
 
396
    {
 
397
      switch (_reader.TokenType)
 
398
      {
 
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;
 
404
 
 
405
          while (_reader.Read() && _reader.TokenType != JsonToken.EndArray)
 
406
          {
 
407
            if (_reader.TokenType != JsonToken.String)
 
408
              throw JsonReaderException.Create(_reader, "Exception JSON schema type string token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
 
409
 
 
410
            type = type | MapType(_reader.Value.ToString());
 
411
          }
 
412
 
 
413
          return type;
 
414
        default:
 
415
          throw JsonReaderException.Create(_reader, "Expected array or JSON schema type string token, got {0}.".FormatWith(CultureInfo.InvariantCulture, _reader.TokenType));
 
416
      }
 
417
    }
 
418
 
 
419
    internal static JsonSchemaType MapType(string type)
 
420
    {
 
421
      JsonSchemaType mappedType;
 
422
      if (!JsonSchemaConstants.JsonSchemaTypeMapping.TryGetValue(type, out mappedType))
 
423
        throw new JsonException("Invalid JSON schema type: {0}".FormatWith(CultureInfo.InvariantCulture, type));
 
424
 
 
425
      return mappedType;
 
426
    }
 
427
 
 
428
    internal static string MapType(JsonSchemaType type)
 
429
    {
 
430
      return JsonSchemaConstants.JsonSchemaTypeMapping.Single(kv => kv.Value == type).Key;
 
431
    }
 
432
  }
 
433
}
 
 
b'\\ No newline at end of file'