public void ReadAsDecimalFailure()
        {
            AssertException.Throws<JsonSchemaException>(() =>
            {
                JsonSchema s = new JsonSchemaGenerator().Generate(typeof(decimal));
                s.DivisibleBy = 1;

                JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"5.5")))
                {
                    Schema = s
                };
                reader.ReadAsDecimal();
            }, "Float 5.5 is not evenly divisible by 1. Line 1, position 3.");
        }
        public void ReadAsDecimal()
        {
            JsonSchema s = new JsonSchemaGenerator().Generate(typeof(decimal));

            JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"1.5")))
            {
                Schema = s
            };
            decimal? d = reader.ReadAsDecimal();

            Assert.Equal(1.5m, d);
        }
        public void ReadAsInt32Failure()
        {
            AssertException.Throws<JsonSchemaException>(() =>
            {
                JsonSchema s = new JsonSchemaGenerator().Generate(typeof(int));
                s.Maximum = 2;

                JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"5")))
                {
                    Schema = s
                };
                reader.ReadAsInt32();
            }, "Integer 5 exceeds maximum value of 2. Line 1, position 1.");
        }
        public void ReadAsInt32()
        {
            JsonSchema s = new JsonSchemaGenerator().Generate(typeof(int));

            JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"1")))
            {
                Schema = s
            };
            int? i = reader.ReadAsInt32();

            Assert.Equal(1, i);
        }
        public void ReadAsBytes()
        {
            JsonSchema s = new JsonSchemaGenerator().Generate(typeof(byte[]));

            byte[] data = Encoding.UTF8.GetBytes("Hello world");

            JsonReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(@"""" + Convert.ToBase64String(data) + @"""")))
            {
                Schema = s
            };
            byte[] bytes = reader.ReadAsBytes();

            Assert.Equal(data, bytes);
        }
Example #6
0
        public void GenerateSchema()
        {
            JsonSchemaGenerator generator = new JsonSchemaGenerator();

            JsonSchema schema = generator.Generate(typeof(Session));

            // {
            //   "type": "object",
            //   "properties": {
            //     "Name": {
            //       "required": true,
            //       "type": [
            //         "string",
            //         "null"
            //       ]
            //     },
            //     "Date": {
            //       "required": true,
            //       "type": "string"
            //     }
            //   }
            // }
        }