示例#1
0
        public void ComplexArrayProperty()
        {
            //// Arrange
            var data = @"{
                persons: [
                    {
                        foo: ""bar"", 
                        bar: ""foo""
                    },
                    {
                        foo: ""bar"", 
                        puk: ""fii""
                    }
                ]
            }";

            //// Act
            var schema   = JsonSchema4.FromSampleJson(data);
            var json     = schema.ToJson();
            var property = schema.Properties["persons"].ActualTypeSchema;

            //// Assert
            Assert.Equal(JsonObjectType.Array, property.Type);
            Assert.Equal(3, property.Item.ActualSchema.Properties.Count);
            Assert.True(schema.Definitions.ContainsKey("Person"));
        }
示例#2
0
        public void ComplexArrayProperty()
        {
            //// Arrange
            var data = @"{
                array: [
                    {
                        foo: ""bar"", 
                        bar: ""foo""
                    },
                    {
                        foo: ""bar"", 
                        puk: ""fii""
                    }
                ]
            }";

            //// Act
            var schema   = JsonSchema4.FromSampleJson(data);
            var json     = schema.ToJson();
            var property = schema.Properties["array"].ActualTypeSchema;

            //// Assert
            Assert.AreEqual(JsonObjectType.Array, property.Type);
            Assert.AreEqual(3, property.Item.ActualSchema.Properties.Count);
        }
示例#3
0
        public static async Task RestTest(string url)
        {
            HttpClient client = new HttpClient();

            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));

            client.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter");
            Task <string> stringTask = client.GetStringAsync(url);
            String        json       = await stringTask;

            // https://www.newtonsoft.com/json
            // https://github.com/RSuter/NJsonSchema

            JToken parsedJson = JToken.Parse(json);
            string beautified = parsedJson.ToString(Formatting.Indented);

            Debug.WriteLine("--------------------------------------------------");
            Debug.WriteLine(beautified);

            JsonSchema4 jsonSchema       = JsonSchema4.FromSampleJson(beautified);
            string      beautifiedSchema = jsonSchema.ToJson();

            Debug.WriteLine("--------------------------------------------------");
            Debug.WriteLine(beautifiedSchema);

            CSharpGenerator generator = new CSharpGenerator(jsonSchema);
            string          file      = generator.GenerateFile();

            Debug.WriteLine("--------------------------------------------------");
            Debug.WriteLine(file);
        }
示例#4
0
        public static void WriteSchema(string sampleJson, FileInfo outputSchema)
        {
            Directory.CreateDirectory(outputSchema.DirectoryName);

            try
            {
                JsonSchema4 schema = JsonSchema4.FromSampleJson(sampleJson);
                File.WriteAllText(outputSchema.FullName, schema.ToJson());
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
                throw;
            }
        }
示例#5
0
        private static void BuildSchema(string name, bool overwrite)
        {
            Directory.CreateDirectory(outputSampleDirectory);
            Directory.CreateDirectory(outputSchemaDirectory);

            string samplePath = Path.Combine(outputSampleDirectory, name + ".json");
            string schemaPath = Path.Combine(outputSchemaDirectory, name + ".schema.json");

            if (!File.Exists(schemaPath) || overwrite)
            {
                try
                {
                    JsonSchema4 schema = JsonSchema4.FromSampleJson(File.ReadAllText(samplePath));
                    File.WriteAllText(schemaPath, schema.ToJson());
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                    throw;
                }
            }
        }
示例#6
0
        public void MergedSchemas()
        {
            //// Arrange
            var data = @"{
    ""Address"": {
        ""Street"": [
            {
                ""Street"": ""Straße 1"",
                ""House"": {
                    ""Floor"": ""1"",
                    ""Number"": ""35""
                }
},
            {
                ""Street"": ""Straße 2"",
                ""House"": {
                    ""Floor"": ""2"",
                    ""Number"": ""54""
                }
            }
        ],
        ""@first_name"": ""Albert"",
        ""@last_name"": ""Einstein""
    }
}";

            //// Act
            var schema = JsonSchema4.FromSampleJson(data);
            var json   = schema.ToJson();

            //// Assert
            Assert.Equal(3, schema.Definitions.Count);
            Assert.True(schema.Definitions.ContainsKey("Street"));
            Assert.True(schema.Definitions.ContainsKey("House"));
            Assert.True(schema.Definitions.ContainsKey("Address"));
        }
示例#7
0
        public static void WriteCSharp(string sampleJson, FileInfo outputCSharp, CSharpGeneratorSettings settings)
        {
            JsonSchema4 schema = JsonSchema4.FromSampleJson(sampleJson);

            WriteCSharp(schema, outputCSharp, settings);
        }