Пример #1
0
        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
        }
        public void JSchemaUrlResolverRelativeTest()
        {
            #region JSchemaUrlResolver_Relative
            // person.json, has a relative external schema reference 'address.json'
            // --------
            // {
            //   'type': 'object',
            //   'properties': {
            //     'name': {'type':'string'},
            //     'addresses': {
            //       'type': 'array',
            //       'items': {'$ref': 'address.json'}
            //     }
            //   }
            // }
            // --------

            using (StreamReader file = File.OpenText(@"c:\person.json"))
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    JSchemaUrlResolver resolver = new JSchemaUrlResolver();

                    JSchema schema = JSchema.Load(reader, new JSchemaReaderSettings
                    {
                        Resolver = resolver,
                        // where the schema is being loaded from
                        // referenced 'address.json' schema will be loaded from disk at 'c:\address.json'
                        BaseUri = new Uri(@"c:\person.json")
                    });

                    // validate JSON
                }
            #endregion
        }
        public void LoadFile()
        {
            #region LoadFile
            using (StreamReader file = File.OpenText(@"c:\person.json"))
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    JSchema schema = JSchema.Load(reader);

                    // validate JSON
                }
            #endregion
        }