NJsonSchema Documentation

repository·master·Indexed 23 days ago

https://github.com/ricosuter/njsonschema

A .NET library for reading, generating, and validating JSON Schema (draft v4+). It supports generating schemas from .NET types or sample JSON, validating data against schemas, and generating C# or TypeScript code. The library is distributed via NuGet with specialized packages for annotations, YAML support, and code generation.

Tokens
1K
Snippets
6
Records
9
Agent score
32%

What's inside NJsonSchema

  1. Generate TypeScript code from a JSON schema

    master

    You can generate TypeScript code using NJsonSchema.CodeGeneration. You can control the output style using TypeScriptGeneratorSettings.

    Interface Style

    Set TypeStyle = TypeScriptTypeStyle.Interface to generate TypeScript interfaces.

    Class Style

    Set TypeStyle = TypeScriptTypeStyle.Class to generate TypeScript classes that implement corresponding interfaces, including methods like init(), fromJS(), and toJSON().

  2. Install NJsonSchema NuGet packages

    master

    NJsonSchema is distributed via NuGet. Depending on your requirements, you can install the core library or specialized packages for annotations, YAML support, or code generation:

    • NJsonSchema: Core JSON Schema parsing, validation, and generation.
    • NJsonSchema.Annotations: JSON Schema annotations for controlling serialization.
    • NJsonSchema.Yaml: Support for reading and writing JSON Schemas in YAML format.
    • NJsonSchema.CodeGeneration: Base classes for generating code from a JSON Schema.
    • NJsonSchema.CodeGeneration.CSharp: Generates C# classes from a schema.
    • NJsonSchema.CodeGeneration.TypeScript: Generates TypeScript interfaces or classes from a schema.
  3. Generate JSON Schema from .NET types

    master

    You can generate a JSON Schema from an existing .NET class using the JsonSchema.FromType<T>() method. This uses reflection and supports various attributes and annotations to define schema constraints like [Required] or [Range].

    var schema = JsonSchema.FromType<Person>();
    var schemaData = schema.ToJson();
  4. Generate OpenAPI 3 JSON Schema from sample JSON

    master

    To generate a JSON Schema compatible with the OpenAPI specification instead of the standard JSON Schema spec, configure the SchemaType property in SampleJsonSchemaGeneratorSettings to SchemaType.OpenApi3.

    var generator = new SampleJsonSchemaGenerator(new SampleJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 });
    
    var schema = generator.Generate("{...}");
  5. Generate JSON Schema from sample JSON data

    master

    The NJsonSchema.SampleJsonSchemaGenerator allows you to create a JSON Schema by providing a sample JSON object.

    By default, it generates schemas following the standard JSON Schema specification. Use SampleJsonSchemaGeneratorSettings to configure the generator.

    var generator = new SampleJsonSchemaGenerator(new SampleJsonSchemaGeneratorSettings());
    
    var schema = generator.Generate("{...}");
  6. Validate JSON data against a schema

    master

    To validate JSON data, use the Validate method on a JsonSchema instance. The method returns a collection of errors that you can iterate through to inspect the Path and Kind of each validation failure.

    var schema = JsonSchema.FromType<Person>();
    var errors = schema.Validate("{...}");
    
    foreach (var error in errors)
        Console.WriteLine(error.Path + ": " + error.Kind);
  7. Generate C# code from a JSON schema

    master

    Use the CSharpGenerator class from NJsonSchema.CodeGeneration to transform a JSON schema into C# classes. The GenerateFile() method returns a string containing the C# code for all classes defined in the schema.

    var generator = new CSharpGenerator(schema);
    var file = generator.GenerateFile();