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 Newtonsoft.Json.Utilities.LinqBridge;
34
using NUnit.Framework;
36
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
37
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
38
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
41
using System.Xml.Schema;
42
using Newtonsoft.Json.Schema;
43
using Newtonsoft.Json.Utilities;
44
using ValidationEventArgs = Newtonsoft.Json.Schema.ValidationEventArgs;
46
namespace Newtonsoft.Json.Tests
49
public class JsonValidatingReaderTests : TestFixtureBase
52
public void CheckInnerReader()
54
string json = "{'name':'James','hobbies':['pie','cake']}";
55
JsonReader reader = new JsonTextReader(new StringReader(json));
57
JsonValidatingReader validatingReader = new JsonValidatingReader(reader);
58
Assert.AreEqual(reader, validatingReader.Reader);
62
public void ValidateTypes()
64
string schemaJson = @"{
65
""description"":""A person"",
69
""name"":{""type"":""string""},
73
""items"": {""type"":""string""}
78
string json = @"{'name':""James"",'hobbies':[""pie"",'cake']}";
80
Json.Schema.ValidationEventArgs validationEventArgs = null;
82
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
83
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
84
JsonSchema schema = JsonSchema.Parse(schemaJson);
85
reader.Schema = schema;
86
Assert.AreEqual(schema, reader.Schema);
87
Assert.AreEqual(0, reader.Depth);
88
Assert.AreEqual("", reader.Path);
90
Assert.IsTrue(reader.Read());
91
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
92
Assert.AreEqual("", reader.Path);
94
Assert.IsTrue(reader.Read());
95
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
96
Assert.AreEqual("name", reader.Value.ToString());
97
Assert.AreEqual("name", reader.Path);
98
Assert.AreEqual(1, reader.Depth);
100
Assert.IsTrue(reader.Read());
101
Assert.AreEqual(JsonToken.String, reader.TokenType);
102
Assert.AreEqual("James", reader.Value.ToString());
103
Assert.AreEqual(typeof (string), reader.ValueType);
104
Assert.AreEqual('"', reader.QuoteChar);
105
Assert.AreEqual("name", reader.Path);
107
Assert.IsTrue(reader.Read());
108
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
109
Assert.AreEqual("hobbies", reader.Value.ToString());
110
Assert.AreEqual('\'', reader.QuoteChar);
111
Assert.AreEqual("hobbies", reader.Path);
113
Assert.IsTrue(reader.Read());
114
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
115
Assert.AreEqual("hobbies", reader.Path);
117
Assert.IsTrue(reader.Read());
118
Assert.AreEqual(JsonToken.String, reader.TokenType);
119
Assert.AreEqual("pie", reader.Value.ToString());
120
Assert.AreEqual('"', reader.QuoteChar);
121
Assert.AreEqual("hobbies[0]", reader.Path);
123
Assert.IsTrue(reader.Read());
124
Assert.AreEqual(JsonToken.String, reader.TokenType);
125
Assert.AreEqual("cake", reader.Value.ToString());
126
Assert.AreEqual("hobbies[1]", reader.Path);
128
Assert.IsTrue(reader.Read());
129
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
130
Assert.AreEqual("hobbies", reader.Path);
132
Assert.IsTrue(reader.Read());
133
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
134
Assert.AreEqual("", reader.Path);
136
Assert.IsFalse(reader.Read());
138
Assert.IsNull(validationEventArgs);
142
public void ValidateUnrestrictedArray()
144
string schemaJson = @"{
148
string json = "['pie','cake',['nested1','nested2'],{'nestedproperty1':1.1,'nestedproperty2':[null]}]";
150
Json.Schema.ValidationEventArgs validationEventArgs = null;
152
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
153
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
154
reader.Schema = JsonSchema.Parse(schemaJson);
156
Assert.IsTrue(reader.Read());
157
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
159
Assert.IsTrue(reader.Read());
160
Assert.AreEqual(JsonToken.String, reader.TokenType);
161
Assert.AreEqual("pie", reader.Value.ToString());
163
Assert.IsTrue(reader.Read());
164
Assert.AreEqual(JsonToken.String, reader.TokenType);
165
Assert.AreEqual("cake", reader.Value.ToString());
167
Assert.IsTrue(reader.Read());
168
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
170
Assert.IsTrue(reader.Read());
171
Assert.AreEqual(JsonToken.String, reader.TokenType);
172
Assert.AreEqual("nested1", reader.Value.ToString());
174
Assert.IsTrue(reader.Read());
175
Assert.AreEqual(JsonToken.String, reader.TokenType);
176
Assert.AreEqual("nested2", reader.Value.ToString());
178
Assert.IsTrue(reader.Read());
179
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
181
Assert.IsTrue(reader.Read());
182
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
184
Assert.IsTrue(reader.Read());
185
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
186
Assert.AreEqual("nestedproperty1", reader.Value.ToString());
188
Assert.IsTrue(reader.Read());
189
Assert.AreEqual(JsonToken.Float, reader.TokenType);
190
Assert.AreEqual(1.1, reader.Value);
192
Assert.IsTrue(reader.Read());
193
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
194
Assert.AreEqual("nestedproperty2", reader.Value.ToString());
196
Assert.IsTrue(reader.Read());
197
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
199
Assert.IsTrue(reader.Read());
200
Assert.AreEqual(JsonToken.Null, reader.TokenType);
202
Assert.IsTrue(reader.Read());
203
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
205
Assert.IsTrue(reader.Read());
206
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
208
Assert.IsTrue(reader.Read());
209
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
211
Assert.IsNull(validationEventArgs);
215
public void StringLessThanMinimumLength()
217
string schemaJson = @"{
223
string json = "'pie'";
225
Json.Schema.ValidationEventArgs validationEventArgs = null;
227
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
228
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
229
reader.Schema = JsonSchema.Parse(schemaJson);
231
Assert.IsTrue(reader.Read());
232
Assert.AreEqual(JsonToken.String, reader.TokenType);
233
Assert.AreEqual("String 'pie' is less than minimum length of 5. Line 1, position 5.", validationEventArgs.Message);
235
Assert.IsNotNull(validationEventArgs);
239
public void StringGreaterThanMaximumLength()
241
string schemaJson = @"{
247
string json = "'The quick brown fox jumps over the lazy dog.'";
249
Json.Schema.ValidationEventArgs validationEventArgs = null;
251
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
252
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
253
reader.Schema = JsonSchema.Parse(schemaJson);
255
Assert.IsTrue(reader.Read());
256
Assert.AreEqual(JsonToken.String, reader.TokenType);
257
Assert.AreEqual("String 'The quick brown fox jumps over the lazy dog.' exceeds maximum length of 10. Line 1, position 46.", validationEventArgs.Message);
259
Assert.IsNotNull(validationEventArgs);
263
public void StringIsNotInEnum()
265
string schemaJson = @"{
269
""enum"":[""one"",""two""]
274
string json = "['one','two','THREE']";
276
Json.Schema.ValidationEventArgs validationEventArgs = null;
278
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
279
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
280
reader.Schema = JsonSchema.Parse(schemaJson);
282
Assert.IsTrue(reader.Read());
283
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
285
Assert.IsTrue(reader.Read());
286
Assert.AreEqual(JsonToken.String, reader.TokenType);
288
Assert.IsTrue(reader.Read());
289
Assert.AreEqual(JsonToken.String, reader.TokenType);
290
Assert.AreEqual(null, validationEventArgs);
292
Assert.IsTrue(reader.Read());
293
Assert.AreEqual(JsonToken.String, reader.TokenType);
294
Assert.AreEqual(@"Value ""THREE"" is not defined in enum. Line 1, position 20.", validationEventArgs.Message);
295
Assert.AreEqual("[2]", validationEventArgs.Path);
297
Assert.IsTrue(reader.Read());
298
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
300
Assert.IsNotNull(validationEventArgs);
304
public void StringDoesNotMatchPattern()
306
string schemaJson = @"{
311
string json = "'The quick brown fox jumps over the lazy dog.'";
313
Json.Schema.ValidationEventArgs validationEventArgs = null;
315
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
316
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
317
reader.Schema = JsonSchema.Parse(schemaJson);
319
Assert.IsTrue(reader.Read());
320
Assert.AreEqual(JsonToken.String, reader.TokenType);
321
Assert.AreEqual("String 'The quick brown fox jumps over the lazy dog.' does not match regex pattern 'foo'. Line 1, position 46.", validationEventArgs.Message);
322
Assert.AreEqual("", validationEventArgs.Path);
324
Assert.IsNotNull(validationEventArgs);
328
public void IntegerGreaterThanMaximumValue()
330
string schemaJson = @"{
331
""type"":""integer"",
337
Json.Schema.ValidationEventArgs validationEventArgs = null;
339
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
340
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
341
reader.Schema = JsonSchema.Parse(schemaJson);
343
Assert.IsTrue(reader.Read());
344
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
345
Assert.AreEqual("Integer 10 exceeds maximum value of 5. Line 1, position 2.", validationEventArgs.Message);
346
Assert.AreEqual("", validationEventArgs.Path);
348
Assert.IsNotNull(validationEventArgs);
352
public void ThrowExceptionWhenNoValidationEventHandler()
354
ExceptionAssert.Throws<JsonSchemaException>("Integer 10 exceeds maximum value of 5. Line 1, position 2.",
357
string schemaJson = @"{
358
""type"":""integer"",
362
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader("10")));
363
reader.Schema = JsonSchema.Parse(schemaJson);
365
Assert.IsTrue(reader.Read());
370
public void IntegerLessThanMinimumValue()
372
string schemaJson = @"{
373
""type"":""integer"",
379
Json.Schema.ValidationEventArgs validationEventArgs = null;
381
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
382
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
383
reader.Schema = JsonSchema.Parse(schemaJson);
385
Assert.IsTrue(reader.Read());
386
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
387
Assert.AreEqual("Integer 1 is less than minimum value of 5. Line 1, position 1.", validationEventArgs.Message);
389
Assert.IsNotNull(validationEventArgs);
393
public void IntegerIsNotInEnum()
395
string schemaJson = @"{
398
""type"":""integer"",
404
string json = "[1,2,3]";
406
Json.Schema.ValidationEventArgs validationEventArgs = null;
408
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
409
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
410
reader.Schema = JsonSchema.Parse(schemaJson);
412
Assert.IsTrue(reader.Read());
413
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
415
Assert.IsTrue(reader.Read());
416
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
418
Assert.IsTrue(reader.Read());
419
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
420
Assert.AreEqual(null, validationEventArgs);
422
Assert.IsTrue(reader.Read());
423
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
424
Assert.AreEqual(@"Value 3 is not defined in enum. Line 1, position 6.", validationEventArgs.Message);
425
Assert.AreEqual("[2]", validationEventArgs.Path);
427
Assert.IsTrue(reader.Read());
428
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
430
Assert.IsNotNull(validationEventArgs);
434
public void FloatGreaterThanMaximumValue()
436
string schemaJson = @"{
441
string json = "10.0";
443
Json.Schema.ValidationEventArgs validationEventArgs = null;
445
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
446
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
447
reader.Schema = JsonSchema.Parse(schemaJson);
449
Assert.IsTrue(reader.Read());
450
Assert.AreEqual(JsonToken.Float, reader.TokenType);
451
Assert.AreEqual("Float 10.0 exceeds maximum value of 5. Line 1, position 4.", validationEventArgs.Message);
453
Assert.IsNotNull(validationEventArgs);
457
public void FloatLessThanMinimumValue()
459
string schemaJson = @"{
466
Json.Schema.ValidationEventArgs validationEventArgs = null;
468
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
469
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
470
reader.Schema = JsonSchema.Parse(schemaJson);
472
Assert.IsTrue(reader.Read());
473
Assert.AreEqual(JsonToken.Float, reader.TokenType);
474
Assert.AreEqual("Float 1.1 is less than minimum value of 5. Line 1, position 3.", validationEventArgs.Message);
476
Assert.IsNotNull(validationEventArgs);
480
public void FloatIsNotInEnum()
482
string schemaJson = @"{
491
string json = "[1.1,2.2,3.0]";
493
Json.Schema.ValidationEventArgs validationEventArgs = null;
495
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
496
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
497
reader.Schema = JsonSchema.Parse(schemaJson);
499
Assert.IsTrue(reader.Read());
500
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
502
Assert.IsTrue(reader.Read());
503
Assert.AreEqual(JsonToken.Float, reader.TokenType);
505
Assert.IsTrue(reader.Read());
506
Assert.AreEqual(JsonToken.Float, reader.TokenType);
507
Assert.AreEqual(null, validationEventArgs);
509
Assert.IsTrue(reader.Read());
510
Assert.AreEqual(JsonToken.Float, reader.TokenType);
511
Assert.AreEqual(@"Value 3.0 is not defined in enum. Line 1, position 12.", validationEventArgs.Message);
512
Assert.AreEqual("[2]", validationEventArgs.Path);
514
Assert.IsTrue(reader.Read());
515
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
517
Assert.IsNotNull(validationEventArgs);
521
public void FloatExceedsMaxDecimalPlaces()
523
string schemaJson = @"{
531
string json = "[1.1,2.2,4.001]";
533
Json.Schema.ValidationEventArgs validationEventArgs = null;
535
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
536
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
537
reader.Schema = JsonSchema.Parse(schemaJson);
539
Assert.IsTrue(reader.Read());
540
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
542
Assert.IsTrue(reader.Read());
543
Assert.AreEqual(JsonToken.Float, reader.TokenType);
545
Assert.IsTrue(reader.Read());
546
Assert.AreEqual(JsonToken.Float, reader.TokenType);
547
Assert.AreEqual(null, validationEventArgs);
549
Assert.IsTrue(reader.Read());
550
Assert.AreEqual(JsonToken.Float, reader.TokenType);
551
Assert.AreEqual(@"Float 4.001 is not evenly divisible by 0.1. Line 1, position 14.", validationEventArgs.Message);
552
Assert.AreEqual("[2]", validationEventArgs.Path);
554
Assert.IsTrue(reader.Read());
555
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
557
Assert.IsNotNull(validationEventArgs);
561
public void IntValidForNumber()
563
string schemaJson = @"{
572
Json.Schema.ValidationEventArgs validationEventArgs = null;
574
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
575
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
576
reader.Schema = JsonSchema.Parse(schemaJson);
578
Assert.IsTrue(reader.Read());
579
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
581
Assert.IsTrue(reader.Read());
582
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
584
Assert.IsTrue(reader.Read());
585
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
587
Assert.IsNull(validationEventArgs);
591
public void NullNotInEnum()
593
string schemaJson = @"{
602
string json = "[null]";
604
Json.Schema.ValidationEventArgs validationEventArgs = null;
606
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
607
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
608
reader.Schema = JsonSchema.Parse(schemaJson);
610
Assert.IsTrue(reader.Read());
611
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
613
Assert.IsTrue(reader.Read());
614
Assert.AreEqual(JsonToken.Null, reader.TokenType);
615
Assert.AreEqual(@"Value null is not defined in enum. Line 1, position 5.", validationEventArgs.Message);
616
Assert.AreEqual("[0]", validationEventArgs.Path);
618
Assert.IsTrue(reader.Read());
619
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
621
Assert.IsNotNull(validationEventArgs);
625
public void BooleanNotInEnum()
627
string schemaJson = @"{
630
""type"":""boolean"",
636
string json = "[true,false]";
638
Json.Schema.ValidationEventArgs validationEventArgs = null;
640
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
641
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
642
reader.Schema = JsonSchema.Parse(schemaJson);
644
Assert.IsTrue(reader.Read());
645
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
647
Assert.IsTrue(reader.Read());
648
Assert.AreEqual(JsonToken.Boolean, reader.TokenType);
649
Assert.AreEqual(null, validationEventArgs);
651
Assert.IsTrue(reader.Read());
652
Assert.AreEqual(JsonToken.Boolean, reader.TokenType);
653
Assert.AreEqual(@"Value false is not defined in enum. Line 1, position 11.", validationEventArgs.Message);
654
Assert.AreEqual("[1]", validationEventArgs.Path);
656
Assert.IsTrue(reader.Read());
657
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
659
Assert.IsNotNull(validationEventArgs);
663
public void ArrayCountGreaterThanMaximumItems()
665
string schemaJson = @"{
671
string json = "[null,null,null,null]";
673
Json.Schema.ValidationEventArgs validationEventArgs = null;
675
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
676
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
677
reader.Schema = JsonSchema.Parse(schemaJson);
679
Assert.IsTrue(reader.Read());
680
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
682
Assert.IsTrue(reader.Read());
683
Assert.AreEqual(JsonToken.Null, reader.TokenType);
684
Assert.IsTrue(reader.Read());
685
Assert.AreEqual(JsonToken.Null, reader.TokenType);
686
Assert.IsTrue(reader.Read());
687
Assert.AreEqual(JsonToken.Null, reader.TokenType);
688
Assert.IsTrue(reader.Read());
689
Assert.AreEqual(JsonToken.Null, reader.TokenType);
691
Assert.IsTrue(reader.Read());
692
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
693
Assert.AreEqual("Array item count 4 exceeds maximum count of 3. Line 1, position 21.", validationEventArgs.Message);
695
Assert.IsNotNull(validationEventArgs);
699
public void ArrayCountLessThanMinimumItems()
701
string schemaJson = @"{
707
string json = "[null]";
709
Json.Schema.ValidationEventArgs validationEventArgs = null;
711
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
712
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
713
reader.Schema = JsonSchema.Parse(schemaJson);
715
Assert.IsTrue(reader.Read());
716
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
718
Assert.IsTrue(reader.Read());
719
Assert.AreEqual(JsonToken.Null, reader.TokenType);
721
Assert.IsTrue(reader.Read());
722
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
723
Assert.AreEqual("Array item count 1 is less than minimum count of 2. Line 1, position 6.", validationEventArgs.Message);
725
Assert.IsNotNull(validationEventArgs);
729
public void InvalidDataType()
731
string schemaJson = @"{
738
string json = "[null,null,null,null]";
740
Json.Schema.ValidationEventArgs validationEventArgs = null;
742
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
743
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
744
reader.Schema = JsonSchema.Parse(schemaJson);
746
Assert.IsTrue(reader.Read());
747
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
748
Assert.AreEqual(@"Invalid type. Expected String but got Array. Line 1, position 1.", validationEventArgs.Message);
750
Assert.IsNotNull(validationEventArgs);
754
public void StringDisallowed()
756
string schemaJson = @"{
759
""disallow"":[""number""]
764
string json = "['pie',1.1]";
766
Json.Schema.ValidationEventArgs validationEventArgs = null;
768
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
769
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
770
reader.Schema = JsonSchema.Parse(schemaJson);
772
Assert.IsTrue(reader.Read());
773
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
775
Assert.IsTrue(reader.Read());
776
Assert.AreEqual(JsonToken.String, reader.TokenType);
777
Assert.AreEqual(null, validationEventArgs);
779
Assert.IsTrue(reader.Read());
780
Assert.AreEqual(JsonToken.Float, reader.TokenType);
781
Assert.AreEqual(@"Type Float is disallowed. Line 1, position 10.", validationEventArgs.Message);
783
Assert.IsTrue(reader.Read());
784
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
786
Assert.IsNotNull(validationEventArgs);
790
public void MissingRequiredProperties()
792
string schemaJson = @"{
793
""description"":""A person"",
797
""name"":{""type"":""string""},
798
""hobbies"":{""type"":""string"",""required"":true},
799
""age"":{""type"":""integer"",""required"":true}
803
string json = "{'name':'James'}";
805
Json.Schema.ValidationEventArgs validationEventArgs = null;
807
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
808
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
809
reader.Schema = JsonSchema.Parse(schemaJson);
811
Assert.IsTrue(reader.Read());
812
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
814
Assert.IsTrue(reader.Read());
815
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
816
Assert.AreEqual("name", reader.Value.ToString());
818
Assert.IsTrue(reader.Read());
819
Assert.AreEqual(JsonToken.String, reader.TokenType);
820
Assert.AreEqual("James", reader.Value.ToString());
821
Assert.AreEqual(null, validationEventArgs);
823
Assert.IsTrue(reader.Read());
824
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
825
Assert.AreEqual("Required properties are missing from object: hobbies, age. Line 1, position 16.", validationEventArgs.Message);
826
Assert.AreEqual("", validationEventArgs.Path);
828
Assert.IsNotNull(validationEventArgs);
832
public void MissingNonRequiredProperties()
834
string schemaJson = @"{
835
""description"":""A person"",
839
""name"":{""type"":""string"",""required"":true},
840
""hobbies"":{""type"":""string"",""required"":false},
841
""age"":{""type"":""integer""}
845
string json = "{'name':'James'}";
847
Json.Schema.ValidationEventArgs validationEventArgs = null;
849
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
850
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
851
reader.Schema = JsonSchema.Parse(schemaJson);
853
Assert.IsTrue(reader.Read());
854
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
856
Assert.IsTrue(reader.Read());
857
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
858
Assert.AreEqual("name", reader.Value.ToString());
860
Assert.IsTrue(reader.Read());
861
Assert.AreEqual(JsonToken.String, reader.TokenType);
862
Assert.AreEqual("James", reader.Value.ToString());
863
Assert.IsNull(validationEventArgs);
865
Assert.IsTrue(reader.Read());
866
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
868
Assert.IsNull(validationEventArgs);
872
public void DisableAdditionalProperties()
874
string schemaJson = @"{
875
""description"":""A person"",
879
""name"":{""type"":""string""}
881
""additionalProperties"":false
884
string json = "{'name':'James','additionalProperty1':null,'additionalProperty2':null}";
886
Json.Schema.ValidationEventArgs validationEventArgs = null;
888
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
889
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
890
reader.Schema = JsonSchema.Parse(schemaJson);
892
Assert.IsTrue(reader.Read());
893
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
895
Assert.IsTrue(reader.Read());
896
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
897
Assert.AreEqual("name", reader.Value.ToString());
899
Assert.IsTrue(reader.Read());
900
Assert.AreEqual(JsonToken.String, reader.TokenType);
901
Assert.AreEqual("James", reader.Value.ToString());
902
Assert.AreEqual(null, validationEventArgs);
904
Assert.IsTrue(reader.Read());
905
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
906
Assert.AreEqual("additionalProperty1", reader.Value.ToString());
908
Assert.IsTrue(reader.Read());
909
Assert.AreEqual(JsonToken.Null, reader.TokenType);
910
Assert.AreEqual(null, reader.Value);
911
Assert.AreEqual("Property 'additionalProperty1' has not been defined and the schema does not allow additional properties. Line 1, position 38.", validationEventArgs.Message);
913
Assert.IsTrue(reader.Read());
914
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
915
Assert.AreEqual("additionalProperty2", reader.Value.ToString());
917
Assert.IsTrue(reader.Read());
918
Assert.AreEqual(JsonToken.Null, reader.TokenType);
919
Assert.AreEqual(null, reader.Value);
920
Assert.AreEqual("Property 'additionalProperty2' has not been defined and the schema does not allow additional properties. Line 1, position 65.", validationEventArgs.Message);
922
Assert.IsTrue(reader.Read());
923
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
925
Assert.IsNotNull(validationEventArgs);
929
public void ExtendsStringGreaterThanMaximumLength()
931
string schemaJson = @"{
940
List<string> errors = new List<string>();
941
string json = "'The quick brown fox jumps over the lazy dog.'";
943
Json.Schema.ValidationEventArgs validationEventArgs = null;
945
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
946
reader.ValidationEventHandler += (sender, args) =>
948
validationEventArgs = args;
949
errors.Add(validationEventArgs.Message);
951
reader.Schema = JsonSchema.Parse(schemaJson);
953
Assert.IsTrue(reader.Read());
954
Assert.AreEqual(JsonToken.String, reader.TokenType);
955
Assert.AreEqual(1, errors.Count);
956
Assert.AreEqual("String 'The quick brown fox jumps over the lazy dog.' exceeds maximum length of 9. Line 1, position 46.", errors[0]);
958
Assert.IsNotNull(validationEventArgs);
961
private JsonSchema GetExtendedSchema()
968
""firstproperty"":{""type"":""string"",""required"":true}
970
""additionalProperties"":{}
976
""extends"":{""$ref"":""first""},
979
""secondproperty"":{""type"":""string"",""required"":true}
981
""additionalProperties"":false
984
JsonSchemaResolver resolver = new JsonSchemaResolver();
985
JsonSchema firstSchema = JsonSchema.Parse(first, resolver);
986
JsonSchema secondSchema = JsonSchema.Parse(second, resolver);
992
public void ExtendsDisallowAdditionProperties()
994
string json = "{'firstproperty':'blah','secondproperty':'blah2','additional':'blah3','additional2':'blah4'}";
996
Json.Schema.ValidationEventArgs validationEventArgs = null;
998
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
999
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
1000
reader.Schema = GetExtendedSchema();
1002
Assert.IsTrue(reader.Read());
1003
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
1005
Assert.IsTrue(reader.Read());
1006
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
1007
Assert.AreEqual("firstproperty", reader.Value.ToString());
1008
Assert.AreEqual(null, validationEventArgs);
1010
Assert.IsTrue(reader.Read());
1011
Assert.AreEqual(JsonToken.String, reader.TokenType);
1012
Assert.AreEqual("blah", reader.Value.ToString());
1013
Assert.AreEqual(null, validationEventArgs);
1015
Assert.IsTrue(reader.Read());
1016
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
1017
Assert.AreEqual("secondproperty", reader.Value.ToString());
1018
Assert.AreEqual(null, validationEventArgs);
1020
Assert.IsTrue(reader.Read());
1021
Assert.AreEqual(JsonToken.String, reader.TokenType);
1022
Assert.AreEqual("blah2", reader.Value.ToString());
1023
Assert.AreEqual(null, validationEventArgs);
1025
Assert.IsTrue(reader.Read());
1026
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
1027
Assert.AreEqual("additional", reader.Value.ToString());
1028
Assert.AreEqual("Property 'additional' has not been defined and the schema does not allow additional properties. Line 1, position 62.", validationEventArgs.Message);
1030
Assert.IsTrue(reader.Read());
1031
Assert.AreEqual(JsonToken.String, reader.TokenType);
1032
Assert.AreEqual("blah3", reader.Value.ToString());
1034
Assert.IsTrue(reader.Read());
1035
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
1036
Assert.AreEqual("additional2", reader.Value.ToString());
1037
Assert.AreEqual("Property 'additional2' has not been defined and the schema does not allow additional properties. Line 1, position 84.", validationEventArgs.Message);
1039
Assert.IsTrue(reader.Read());
1040
Assert.AreEqual(JsonToken.String, reader.TokenType);
1041
Assert.AreEqual("blah4", reader.Value.ToString());
1043
Assert.IsTrue(reader.Read());
1044
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
1046
Assert.IsFalse(reader.Read());
1050
public void ExtendsMissingRequiredProperties()
1054
List<string> errors = new List<string>();
1056
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
1057
reader.ValidationEventHandler += (sender, args) => { errors.Add(args.Message); };
1058
reader.Schema = GetExtendedSchema();
1060
Assert.IsTrue(reader.Read());
1061
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
1063
Assert.IsTrue(reader.Read());
1064
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
1066
Assert.AreEqual(1, errors.Count);
1067
Assert.AreEqual("Required properties are missing from object: secondproperty, firstproperty. Line 1, position 2.", errors[0]);
1071
public void NoAdditionalProperties()
1073
string schemaJson = @"{
1075
""items"": [{""type"":""string""},{""type"":""integer""}],
1076
""additionalProperties"": false
1079
string json = @"[1, 'a', null]";
1081
Json.Schema.ValidationEventArgs validationEventArgs = null;
1083
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
1084
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
1085
reader.Schema = JsonSchema.Parse(schemaJson);
1087
Assert.IsTrue(reader.Read());
1088
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
1090
Assert.IsTrue(reader.Read());
1091
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
1092
Assert.AreEqual("Invalid type. Expected String but got Integer. Line 1, position 2.", validationEventArgs.Message);
1094
Assert.IsTrue(reader.Read());
1095
Assert.AreEqual(JsonToken.String, reader.TokenType);
1096
Assert.AreEqual("Invalid type. Expected Integer but got String. Line 1, position 7.", validationEventArgs.Message);
1098
Assert.IsTrue(reader.Read());
1099
Assert.AreEqual(JsonToken.Null, reader.TokenType);
1100
Assert.AreEqual("Index 3 has not been defined and the schema does not allow additional items. Line 1, position 13.", validationEventArgs.Message);
1102
Assert.IsTrue(reader.Read());
1103
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
1105
Assert.IsFalse(reader.Read());
1109
public void PatternPropertiesNoAdditionalProperties()
1111
string schemaJson = @"{
1112
""type"":""object"",
1113
""patternProperties"": {
1114
""hi"": {""type"":""string""},
1115
""ho"": {""type"":""string""}
1117
""additionalProperties"": false
1121
""hi"": ""A string!"",
1122
""hide"": ""A string!"",
1124
""hey"": ""A string!""
1127
Json.Schema.ValidationEventArgs validationEventArgs = null;
1129
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
1130
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
1131
reader.Schema = JsonSchema.Parse(schemaJson);
1133
Assert.IsTrue(reader.Read());
1134
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
1136
Assert.IsTrue(reader.Read());
1137
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
1138
Assert.AreEqual(null, validationEventArgs);
1140
Assert.IsTrue(reader.Read());
1141
Assert.AreEqual(JsonToken.String, reader.TokenType);
1143
Assert.IsTrue(reader.Read());
1144
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
1145
Assert.AreEqual(null, validationEventArgs);
1147
Assert.IsTrue(reader.Read());
1148
Assert.AreEqual(JsonToken.String, reader.TokenType);
1150
Assert.IsTrue(reader.Read());
1151
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
1152
Assert.AreEqual(null, validationEventArgs);
1154
Assert.IsTrue(reader.Read());
1155
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
1156
Assert.AreEqual("Invalid type. Expected String but got Integer. Line 4, position 10.", validationEventArgs.Message);
1158
Assert.IsTrue(reader.Read());
1159
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
1160
Assert.AreEqual("Property 'hey' has not been defined and the schema does not allow additional properties. Line 5, position 9.", validationEventArgs.Message);
1162
Assert.IsTrue(reader.Read());
1163
Assert.AreEqual(JsonToken.String, reader.TokenType);
1165
Assert.IsTrue(reader.Read());
1166
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
1168
Assert.IsFalse(reader.Read());
1172
public void ExtendedComplex()
1176
""type"":""object"",
1179
""firstproperty"":{""type"":""string""},
1180
""secondproperty"":{""type"":""string"",""maxLength"":10},
1182
""type"":""object"",
1185
""thirdproperty_firstproperty"":{""type"":""string"",""maxLength"":10,""minLength"":7}
1189
""additionalProperties"":{}
1194
""type"":""object"",
1195
""extends"":{""$ref"":""first""},
1198
""secondproperty"":{""type"":""any""},
1203
""thirdproperty_firstproperty"":{""maxLength"":9,""minLength"":6,""pattern"":""hi2u""}
1205
""additionalProperties"":{""maxLength"":9,""minLength"":6,""enum"":[""one"",""two""]}
1207
""type"":""object"",
1210
""thirdproperty_firstproperty"":{""pattern"":""hi""}
1212
""additionalProperties"":{""type"":""string"",""enum"":[""two"",""three""]}
1214
""fourthproperty"":{""type"":""string""}
1216
""additionalProperties"":false
1219
JsonSchemaResolver resolver = new JsonSchemaResolver();
1220
JsonSchema firstSchema = JsonSchema.Parse(first, resolver);
1221
JsonSchema secondSchema = JsonSchema.Parse(second, resolver);
1223
JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
1226
'firstproperty':'blahblahblahblahblahblah',
1227
'secondproperty':'secasecasecasecaseca',
1229
'thirdproperty_firstproperty':'aaa',
1230
'additional':'three'
1234
Json.Schema.ValidationEventArgs validationEventArgs = null;
1235
List<string> errors = new List<string>();
1237
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
1238
reader.ValidationEventHandler += (sender, args) =>
1240
validationEventArgs = args;
1241
errors.Add(validationEventArgs.Path + " - " + validationEventArgs.Message);
1243
reader.Schema = secondSchema;
1245
Assert.IsTrue(reader.Read());
1246
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
1248
Assert.IsTrue(reader.Read());
1249
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
1250
Assert.AreEqual("firstproperty", reader.Value.ToString());
1251
Assert.AreEqual(null, validationEventArgs);
1253
Assert.IsTrue(reader.Read());
1254
Assert.AreEqual(JsonToken.String, reader.TokenType);
1255
Assert.AreEqual("blahblahblahblahblahblah", reader.Value.ToString());
1257
Assert.IsTrue(reader.Read());
1258
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
1259
Assert.AreEqual("secondproperty", reader.Value.ToString());
1261
Assert.IsTrue(reader.Read());
1262
Assert.AreEqual(JsonToken.String, reader.TokenType);
1263
Assert.AreEqual("secasecasecasecaseca", reader.Value.ToString());
1264
Assert.AreEqual(1, errors.Count);
1265
Assert.AreEqual("secondproperty - String 'secasecasecasecaseca' exceeds maximum length of 10. Line 3, position 42.", errors[0]);
1267
Assert.IsTrue(reader.Read());
1268
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
1269
Assert.AreEqual("thirdproperty", reader.Value.ToString());
1270
Assert.AreEqual(1, errors.Count);
1272
Assert.IsTrue(reader.Read());
1273
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
1274
Assert.AreEqual(1, errors.Count);
1276
Assert.IsTrue(reader.Read());
1277
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
1278
Assert.AreEqual("thirdproperty_firstproperty", reader.Value.ToString());
1279
Assert.AreEqual(1, errors.Count);
1281
Assert.IsTrue(reader.Read());
1282
Assert.AreEqual(JsonToken.String, reader.TokenType);
1283
Assert.AreEqual("aaa", reader.Value.ToString());
1284
Assert.AreEqual(4, errors.Count);
1285
Assert.AreEqual("thirdproperty.thirdproperty_firstproperty - String 'aaa' is less than minimum length of 7. Line 5, position 40.", errors[1]);
1286
Assert.AreEqual("thirdproperty.thirdproperty_firstproperty - String 'aaa' does not match regex pattern 'hi'. Line 5, position 40.", errors[2]);
1287
Assert.AreEqual("thirdproperty.thirdproperty_firstproperty - String 'aaa' does not match regex pattern 'hi2u'. Line 5, position 40.", errors[3]);
1289
Assert.IsTrue(reader.Read());
1290
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
1291
Assert.AreEqual("additional", reader.Value.ToString());
1292
Assert.AreEqual(4, errors.Count);
1294
Assert.IsTrue(reader.Read());
1295
Assert.AreEqual(JsonToken.String, reader.TokenType);
1296
Assert.AreEqual("three", reader.Value.ToString());
1297
Assert.AreEqual(5, errors.Count);
1298
Assert.AreEqual("thirdproperty.additional - String 'three' is less than minimum length of 6. Line 6, position 25.", errors[4]);
1300
Assert.IsTrue(reader.Read());
1301
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
1303
Assert.IsTrue(reader.Read());
1304
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
1306
Assert.IsFalse(reader.Read());
1310
public void DuplicateErrorsTest()
1313
""id"":""ErrorDemo.Database"",
1315
""ErrorDemoDatabase"":{
1316
""type"":""object"",
1320
""type"":""string"",
1324
""type"":""string"",
1328
""type"":""string"",
1329
""format"":""date-time"",
1333
""type"":""object"",
1344
""type"":""string"",
1348
""type"":""string"",
1352
""type"":""string"",
1356
""type"":""string"",
1371
""ErrorDemoDatabase"":{
1372
""URL"":""localhost:3164"",
1373
""Version"":""1.0"",
1374
""Date"":""6.23.2010, 9:35:18.121"",
1378
""@IDName"":""Developer"",
1380
""IDDesc"":""DeveloperDesc"",
1381
""IsActive"":""True""
1384
""IDName"":""Technician"",
1386
""IDDesc"":""TechnicianDesc"",
1387
""IsActive"":""True""
1390
""IDName"":""Administrator"",
1392
""IDDesc"":""AdministratorDesc"",
1393
""IsActive"":""True""
1396
""IDName"":""PowerUser"",
1398
""IDDesc"":""PowerUserDesc"",
1399
""IsActive"":""True""
1402
""IDName"":""Operator"",
1404
""IDDesc"":""OperatorDesc"",
1405
""IsActive"":""True""
1412
IList<ValidationEventArgs> validationEventArgs = new List<ValidationEventArgs>();
1414
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
1415
reader.ValidationEventHandler += (sender, args) =>
1417
validationEventArgs.Add(args);
1419
reader.Schema = JsonSchema.Parse(schema);
1421
while (reader.Read())
1425
Assert.AreEqual(1, validationEventArgs.Count);
1429
public void ReadAsBytes()
1431
JsonSchema s = new JsonSchemaGenerator().Generate(typeof (byte[]));
1433
byte[] data = Encoding.UTF8.GetBytes("Hello world");
1435
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"""" + Convert.ToBase64String(data) + @"""")))
1439
byte[] bytes = reader.ReadAsBytes();
1441
CollectionAssert.AreEquivalent(data, bytes);
1445
public void ReadAsInt32()
1447
JsonSchema s = new JsonSchemaGenerator().Generate(typeof (int));
1449
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"1")))
1453
int? i = reader.ReadAsInt32();
1455
Assert.AreEqual(1, i);
1459
public void ReadAsInt32Failure()
1461
ExceptionAssert.Throws<JsonSchemaException>("Integer 5 exceeds maximum value of 2. Line 1, position 1.",
1464
JsonSchema s = new JsonSchemaGenerator().Generate(typeof(int));
1467
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"5")))
1471
reader.ReadAsInt32();
1476
public void ReadAsDecimal()
1478
JsonSchema s = new JsonSchemaGenerator().Generate(typeof (decimal));
1480
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"1.5")))
1484
decimal? d = reader.ReadAsDecimal();
1486
Assert.AreEqual(1.5m, d);
1490
public void ReadAsDecimalFailure()
1492
ExceptionAssert.Throws<JsonSchemaException>("Float 5.5 is not evenly divisible by 1. Line 1, position 3.",
1495
JsonSchema s = new JsonSchemaGenerator().Generate(typeof(decimal));
1498
JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"5.5")))
1502
reader.ReadAsDecimal();
1507
public void ReadAsInt32FromSerializer()
1509
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader("[1,2,3]")));
1510
reader.Schema = new JsonSchemaGenerator().Generate(typeof(int[]));
1511
int[] values = new JsonSerializer().Deserialize<int[]>(reader);
1513
Assert.AreEqual(3, values.Length);
1514
Assert.AreEqual(1, values[0]);
1515
Assert.AreEqual(2, values[1]);
1516
Assert.AreEqual(3, values[2]);
1520
public void ReadAsInt32InArray()
1522
string schemaJson = @"{
1525
""type"":""integer""
1530
string json = "[1,2]";
1532
Json.Schema.ValidationEventArgs validationEventArgs = null;
1534
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
1535
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
1536
reader.Schema = JsonSchema.Parse(schemaJson);
1539
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
1541
reader.ReadAsInt32();
1542
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
1543
Assert.AreEqual(null, validationEventArgs);
1545
reader.ReadAsInt32();
1546
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
1547
Assert.AreEqual(null, validationEventArgs);
1549
reader.ReadAsInt32();
1550
Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
1551
Assert.AreEqual("Array item count 2 exceeds maximum count of 1. Line 1, position 5.", validationEventArgs.Message);
1552
Assert.AreEqual("", validationEventArgs.Path);
1556
public void ReadAsInt32InArrayIncomplete()
1558
string schemaJson = @"{
1561
""type"":""integer""
1566
string json = "[1,2";
1568
Json.Schema.ValidationEventArgs validationEventArgs = null;
1570
JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
1571
reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
1572
reader.Schema = JsonSchema.Parse(schemaJson);
1575
Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
1577
reader.ReadAsInt32();
1578
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
1579
Assert.AreEqual(null, validationEventArgs);
1581
reader.ReadAsInt32();
1582
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
1583
Assert.AreEqual(null, validationEventArgs);
1585
reader.ReadAsInt32();
1586
Assert.AreEqual(JsonToken.None, reader.TokenType);
1587
Assert.AreEqual(null, validationEventArgs);
b'\\ No newline at end of file'