using Newtonsoft.Json.Linq; using NJsonSchema; using System; class Program { static void Main(string[] args) { string schemaJson = @"{ ""type"": ""object"", ""properties"": { ""name"": { ""type"": ""string"" }, ""age"": { ""type"": ""integer"" } }, ""required"": [ ""name"" ] }"; // Parse JSON schema JsonSchema schema = JsonSchema.FromJsonAsync(schemaJson).Result; // JSON object to validate string json = @"{ ""name"": ""John"", ""age"": 30 }"; JObject jsonObject = JObject.Parse(json); // Validate JSON object bool isValid = jsonObject.IsValid(schema); Console.WriteLine(isValid); // true } }
using Newtonsoft.Json; using NJsonSchema.Generation; using System; class Person { [JsonProperty("firstName")] public string FirstName { get; set; } [JsonProperty("lastName")] public string LastName { get; set; } [JsonIgnore] public int Age { get; set; } } class Program { static void Main(string[] args) { // Generate JSON schema from C# class var schema = JsonSchema.FromTypeIn this example, we define a C# class "Person" with three properties: "firstName", "lastName", and "Age". We use JsonPropertyAttribute to specify the JSON property names and JsonIgnoreAttribute to exclude the "Age" property from the generated schema. We then generate the JSON schema from the class using JsonSchemaGenerator and output the schema as a JSON string. Overall, JsonSchema4 is a useful C# package library for working with JSON schema. It provides a simple API for validating and generating JSON schema in accordance with different specifications.(new JsonSchemaGeneratorSettings { DefaultPropertyNameHandling = PropertyNameHandling.UseCamelCase, FlattenInheritanceHierarchy = true }); // Output generated schema Console.WriteLine(schema.ToJson()); } }