GithubHelp home page GithubHelp logo

samuron / njsonschema Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ricosuter/njsonschema

0.0 1.0 0.0 2.59 MB

JSON Schema draft v4 reader, generator and validator for .NET

Home Page: http://njsonschema.org

License: Microsoft Public License

Batchfile 0.20% C# 99.80%

njsonschema's Introduction

NJsonSchema for .NET

Build status NuGet Version

JSON Schema draft v4 reader, generator and validator for .NET

NuGet packages:

The library uses Json.NET to read and write JSON data.

Features:

  • Read existing JSON Schemas and validate JSON data
  • Generate JSON Schema from .NET type via reflection (with support for many attributes/annotations)
  • Support for schema references ($ref) (relative, URL and file)
  • Generate C# and TypeScript code from JSON Schema

NJsonSchema is heavily used in NSwag, a Swagger API toolchain for .NET which generates client code for Web API services.

NJsonSchema usage

The JsonSchema4 class can be used as follows:

var schema = JsonSchema4.FromType<Person>();
var schemaData = schema.ToJson();
var errors = schema.Validate("{...}");

foreach (var error in errors)
    Console.WriteLine(error.Path + ": " + error.Kind);

schema = JsonSchema4.FromJson(schemaData);

The Person class:

public class Person
{
    [Required]
    public string FirstName { get; set; }

    [Required]
    public string LastName { get; set; }

    public Gender Gender { get; set; }

    [Range(2, 5)]
    public int NumberWithRange { get; set; }

    public DateTime Birthday { get; set; }

    public Company Company { get; set; }

    public Collection<Car> Cars { get; set; }
}

public enum Gender
{
    Male,
    Female
}

public class Car
{
    public string Name { get; set; }

    public Company Manufacturer { get; set; }
}

public class Company
{
    public string Name { get; set; }
}

The generated JSON schema data stored in the schemaData variable:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "typeName": "Person",
  "type": "object",
  "required": [
    "FirstName",
    "LastName",
    "Gender",
    "NumberWithRange",
    "Birthday"
  ],
  "properties": {
    "FirstName": {
      "type": "string"
    },
    "LastName": {
      "type": "string"
    },
    "Gender": {
      "type": "string",
      "enum": [
        "Male",
        "Female"
      ]
    },
    "NumberWithRange": {
      "type": "integer",
      "maximum": 5.0,
      "minimum": 2.0
    },
    "Birthday": {
      "type": "string",
      "format": "date-time"
    },
    "Company": {
      "typeName": "Company",
      "type": "object",
      "properties": {
        "Name": {
          "type": "string"
        }
      }
    },
    "Cars": {
      "items": {
        "typeName": "Car",
        "type": "object",
        "properties": {
          "Name": {
            "type": "string"
          },
          "Manufacturer": {
            "typeName": "Company",
            "type": "object",
            "$ref": "#/properties/Company"
          }
        }
      },
      "type": "array"
    }
  }
}

NJsonSchema.CodeGeneration usage

The NJsonSchema.CodeGeneration can be used to generate C# or TypeScript code from a JSON schema:

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

The file variable now contains the C# code for all the classes defined in the JSON schema.

Final notes

Applications which use the library:

  • VisualJsonEditor, a JSON schema based file editor for Windows.
  • NSwag: The Swagger API toolchain for .NET

njsonschema's People

Contributors

ricosuter avatar samuron avatar jonanders avatar prateekguru avatar acelot avatar yovio avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.