當前位置: 首頁>>代碼示例>>C#>>正文


C# Schema.JSchemaValidatingReader類代碼示例

本文整理匯總了C#中Newtonsoft.Json.Schema.JSchemaValidatingReader的典型用法代碼示例。如果您正苦於以下問題:C# JSchemaValidatingReader類的具體用法?C# JSchemaValidatingReader怎麽用?C# JSchemaValidatingReader使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


JSchemaValidatingReader類屬於Newtonsoft.Json.Schema命名空間,在下文中一共展示了JSchemaValidatingReader類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: AdheresToSchema

        public JsonBrowserResponse AdheresToSchema([NotNull] JSchema schema)
        {
            var validatingReader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(ResponseBody)));
            validatingReader.Schema = schema;
            var validationErrors = new List<SchemaValidationEventArgs>();
            validatingReader.ValidationEventHandler += (sender, args) => validationErrors.Add(args);
            
            var serializer = new JsonSerializer();
            try
            {
                serializer.Deserialize(validatingReader);
            }
            catch (JsonReaderException)
            {
                Assert.True(false, "The response body did not contain JSON.");
            }

            if (!validationErrors.Any())
            {
                return this;
            }

            var messages = validationErrors.Select(e => e.Message).ToList();
            var message = string.Join(Environment.NewLine, messages);
                
            Assert.True(
                false,
                $"Encountered validation errors against schema: {message}.{Environment.NewLine}Response body: {ResponseBody}");

            return this;
        }
開發者ID:ThePadawan,項目名稱:the_diary,代碼行數:31,代碼來源:JsonBrowserResponse.cs

示例2: ValidateStream

        public void ValidateStream()
        {
            #region ValidateStream
            string schemaJson = @"{
              'description': 'A person',
              'type': 'object',
              'properties': {
                'name': {'type': 'string'},
                'hobbies': {
                  'type': 'array',
                  'items': {'type': 'string'}
                }
              }
            }";

            JSchema schema = JSchema.Parse(schemaJson);

            using (StreamReader s = File.OpenText(@"c:\bigdata.json"))
            using (JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(s)))
            {
                // assign schema and setup event handler
                reader.Schema = schema;
                reader.ValidationEventHandler += (sender, args) =>
                {
                    Console.WriteLine(args.Message);
                };

                // bigdata.json will be validated without loading the entire document into memory
                while (reader.Read())
                {
                }
            }
            #endregion
        }
開發者ID:bmperdue,項目名稱:Newtonsoft.Json.Schema,代碼行數:34,代碼來源:PerformanceTests.cs

示例3: ReadSpecTest

        public void ReadSpecTest(SchemaSpecTest schemaSpecTest)
        {
            Console.WriteLine("Running reader JSON Schema {0} test {1}: {2}", schemaSpecTest.Version, schemaSpecTest.TestNumber, schemaSpecTest);

            IList<string> errorMessages = new List<string>();

            JSchemaPreloadedResolver resolver = GetResolver();

            JSchema s = JSchema.Load(schemaSpecTest.Schema.CreateReader(), resolver);

            JsonReader jsonReader = schemaSpecTest.Data.CreateReader();

            using (JSchemaValidatingReader reader = new JSchemaValidatingReader(jsonReader))
            {
                reader.Schema = s;
                reader.ValidationEventHandler += (sender, args) => errorMessages.Add(args.Message);

                while (reader.Read())
                {
                }
            }

            bool isValid = (errorMessages.Count == 0);

            Assert.AreEqual(schemaSpecTest.IsValid, isValid, schemaSpecTest.TestCaseDescription + " - " + schemaSpecTest.TestDescription + " - errors: " + StringHelpers.Join(", ", errorMessages));
        }
開發者ID:Pondidum,項目名稱:Newtonsoft.Json.Schema,代碼行數:26,代碼來源:JSchemaSpecTests.cs

示例4: GetByName

        public MapInfo GetByName(string name)
        {
            var path = Path.Combine(_directory, name);
            Map map;
            string comments;

            using (var file = File.OpenRead(path + ".json"))
            {
                var reader = new JSchemaValidatingReader(new JsonTextReader(new StreamReader(file)))
                {
                    Schema = _schema
                };

                map = _json.Deserialize<Map>(reader);
            }

            try
            {
                using (var file = File.OpenRead(path + ".txt"))
                {
                    comments = new StreamReader(file).ReadToEnd().TrimEnd();
                }
            }
            catch (FileNotFoundException)
            {
                comments = null;
            }

            return new MapInfo(name, name + ".pie", comments, map);
        }
開發者ID:deftflux,項目名稱:descent-glovepie,代碼行數:30,代碼來源:MapStore.cs

示例5: LoadConfiguration

        public static void LoadConfiguration(string configFile)
        {
            JsonTextReader reader = new JsonTextReader(File.OpenText(configFile));
            JSchemaValidatingReader validatingReader = new JSchemaValidatingReader(reader);
            JSchemaGenerator generator = new JSchemaGenerator();
            validatingReader.Schema = generator.Generate(typeof(ConfigurationJson));

            JsonSerializer serializer = new JsonSerializer();
            configJson = serializer.Deserialize<ConfigurationJson>(validatingReader);

            ConfigFileName = configFile;
        }
開發者ID:yuriibodarev,項目名稱:Nancy.RAML.Mock,代碼行數:12,代碼來源:Configuration.cs

示例6: CreateReader

        private JSchemaValidatingReader CreateReader(string json, JSchema schema, out IList<SchemaValidationEventArgs> errors)
        {
            JsonReader reader = new JsonTextReader(new StringReader(json));

            List<SchemaValidationEventArgs> localErrors = new List<SchemaValidationEventArgs>();

            JSchemaValidatingReader validatingReader = new JSchemaValidatingReader(reader);
            validatingReader.ValidationEventHandler += (sender, args) => { localErrors.Add(args); };
            validatingReader.Schema = schema;

            errors = localErrors;
            return validatingReader;
        }
開發者ID:Nangal,項目名稱:Newtonsoft.Json.Schema,代碼行數:13,代碼來源:JSchemaValidatingReaderTests.cs

示例7: Sample

        public void Sample()
        {
            JSchema schema = JSchema.Parse(@"{
  'type': 'array',
  'items': {'type':'string'}
}");

            JsonTextReader reader = new JsonTextReader(new StringReader(@"[
  'Developer',
  'Administrator'
]"));
            JSchemaValidatingReader validatingReader = new JSchemaValidatingReader(reader);
            validatingReader.Schema = schema;
            validatingReader.ValidationEventHandler += (sender, args) => { throw new Exception(args.Message); };

            JsonSerializer serializer = new JsonSerializer();
            List<string> roles = serializer.Deserialize<List<string>>(validatingReader);
        }
開發者ID:Nangal,項目名稱:Newtonsoft.Json.Schema,代碼行數:18,代碼來源:JSchemaValidatingReaderTests.cs

示例8: IntegerLessThanMinimumValue

        public void IntegerLessThanMinimumValue()
        {
            string schemaJson = @"{
  ""type"":""integer"",
  ""minimum"":5
}";

            string json = "1";

            SchemaValidationEventArgs validationEventArgs = null;

            JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(json)));
            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JSchema.Parse(schemaJson);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.Integer, reader.TokenType);
            Assert.AreEqual("Integer 1 is less than minimum value of 5. Path '', line 1, position 1.", validationEventArgs.Message);
            Assert.AreEqual(1, validationEventArgs.ValidationError.Value);
            Assert.AreEqual("#", validationEventArgs.ValidationError.SchemaId.ToString());
            Assert.AreEqual(ErrorType.Minimum, validationEventArgs.ValidationError.ErrorType);

            Assert.IsNotNull(validationEventArgs);
        }
開發者ID:Nangal,項目名稱:Newtonsoft.Json.Schema,代碼行數:24,代碼來源:JSchemaValidatingReaderTests.cs

示例9: FloatLessThanMinimumValue

        public void FloatLessThanMinimumValue()
        {
            string schemaJson = @"{
  ""type"":""number"",
  ""minimum"":5
}";

            string json = "1.1";

            SchemaValidationEventArgs validationEventArgs = null;

            JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(json)));
            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JSchema.Parse(schemaJson);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.Float, reader.TokenType);
            Assert.AreEqual("Float 1.1 is less than minimum value of 5. Path '', line 1, position 3.", validationEventArgs.Message);
            Assert.AreEqual(1.1d, validationEventArgs.ValidationError.Value);

            Assert.IsNotNull(validationEventArgs);
        }
開發者ID:Nangal,項目名稱:Newtonsoft.Json.Schema,代碼行數:22,代碼來源:JSchemaValidatingReaderTests.cs

示例10: ThrowExceptionWhenNoValidationEventHandler

        public void ThrowExceptionWhenNoValidationEventHandler()
        {
            ExceptionAssert.Throws<JSchemaException>(() =>
            {
                string schemaJson = @"{
  ""type"":""integer"",
  ""maximum"":5
}";

                JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader("10")));
                reader.Schema = JSchema.Parse(schemaJson);

                Assert.IsTrue(reader.Read());
            }, "Integer 10 exceeds maximum value of 5. Path '', line 1, position 2.");
        }
開發者ID:Nangal,項目名稱:Newtonsoft.Json.Schema,代碼行數:15,代碼來源:JSchemaValidatingReaderTests.cs

示例11: ReadAsDecimalFailure

        public void ReadAsDecimalFailure()
        {
            ExceptionAssert.Throws<JSchemaException>(() =>
            {
                JSchema s = new JSchemaGenerator().Generate(typeof(decimal));
                s.MultipleOf = 1;

                JsonReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(@"5.5")))
                {
                    Schema = s
                };
                reader.ReadAsDecimal();
            }, "Float 5.5 is not a multiple of 1. Path '', line 1, position 3.");
        }
開發者ID:Nangal,項目名稱:Newtonsoft.Json.Schema,代碼行數:14,代碼來源:JSchemaValidatingReaderTests.cs

示例12: ReadAsInt32InArrayIncomplete

        public void ReadAsInt32InArrayIncomplete()
        {
            string schemaJson = @"{
  ""type"":""array"",
  ""items"":{
    ""type"":""integer""
  },
  ""maxItems"":1
}";

            string json = "[1,2";

            SchemaValidationEventArgs validationEventArgs = null;

            JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(json)));
            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JSchema.Parse(schemaJson);

            reader.Read();
            Assert.AreEqual(JsonToken.StartArray, reader.TokenType);

            reader.ReadAsInt32();
            Assert.AreEqual(JsonToken.Integer, reader.TokenType);
            Assert.AreEqual(null, validationEventArgs);

            reader.ReadAsInt32();
            Assert.AreEqual(JsonToken.Integer, reader.TokenType);
            Assert.AreEqual(null, validationEventArgs);

            reader.ReadAsInt32();
            Assert.AreEqual(JsonToken.None, reader.TokenType);
            Assert.AreEqual(null, validationEventArgs);
        }
開發者ID:Nangal,項目名稱:Newtonsoft.Json.Schema,代碼行數:33,代碼來源:JSchemaValidatingReaderTests.cs

示例13: IntegerGreaterThanMaximumValue_BigInteger

        public void IntegerGreaterThanMaximumValue_BigInteger()
        {
            string schemaJson = @"{
  ""type"":""integer"",
  ""maximum"":5
}";

            string json = "99999999999999999999999999999999999999999999999999999999999999999999";

            SchemaValidationEventArgs validationEventArgs = null;

            JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(json)));
            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JSchema.Parse(schemaJson);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.Integer, reader.TokenType);
            Assert.AreEqual("Integer 99999999999999999999999999999999999999999999999999999999999999999999 exceeds maximum value of 5. Path '', line 1, position 68.", validationEventArgs.Message);
            Assert.AreEqual(BigInteger.Parse("99999999999999999999999999999999999999999999999999999999999999999999"), validationEventArgs.ValidationError.Value);
            Assert.AreEqual("", validationEventArgs.Path);

            Assert.IsNotNull(validationEventArgs);
        }
開發者ID:Nangal,項目名稱:Newtonsoft.Json.Schema,代碼行數:23,代碼來源:JSchemaValidatingReaderTests.cs

示例14: ReadAsInt32InArray

        public void ReadAsInt32InArray()
        {
            string schemaJson = @"{
  ""type"":""array"",
  ""items"":{
    ""type"":""integer""
  },
  ""maxItems"":1
}";

            string json = "[1,2]";

            SchemaValidationEventArgs validationEventArgs = null;

            JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(json)));
            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JSchema.Parse(schemaJson);

            reader.Read();
            Assert.AreEqual(JsonToken.StartArray, reader.TokenType);

            reader.ReadAsInt32();
            Assert.AreEqual(JsonToken.Integer, reader.TokenType);
            Assert.AreEqual(null, validationEventArgs);

            reader.ReadAsInt32();
            Assert.AreEqual(JsonToken.Integer, reader.TokenType);
            Assert.AreEqual(null, validationEventArgs);

            reader.ReadAsInt32();
            Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
            Assert.AreEqual("Array item count 2 exceeds maximum count of 1. Path '', line 1, position 5.", validationEventArgs.Message);
            Assert.AreEqual(2, validationEventArgs.ValidationError.Value);
            Assert.AreEqual("", validationEventArgs.Path);
        }
開發者ID:Nangal,項目名稱:Newtonsoft.Json.Schema,代碼行數:35,代碼來源:JSchemaValidatingReaderTests.cs

示例15: BigIntegerDivisibleBy_Success

        public void BigIntegerDivisibleBy_Success()
        {
            string schemaJson = @"{
  ""type"":""array"",
  ""items"":{
    ""type"":""number"",
    ""divisibleBy"":2
  }
}";

            string json = "[999999999999999999999999999999999999999999999999999999998]";

            SchemaValidationEventArgs validationEventArgs = null;

            JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(json)));
            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JSchema.Parse(schemaJson);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.StartArray, reader.TokenType);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.Integer, reader.TokenType);
            Assert.IsNull(validationEventArgs);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
        }
開發者ID:Nangal,項目名稱:Newtonsoft.Json.Schema,代碼行數:28,代碼來源:JSchemaValidatingReaderTests.cs


注:本文中的Newtonsoft.Json.Schema.JSchemaValidatingReader類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。