Git Product home page Git Product logo

json-schema-to-poco's Introduction

JSON Schema to POCO

The purpose of this tool is to convert JSON schemas based on the official JSON schema standard into C# POCOs. This tool uses JSON.net as a JSON deserializer, and currently supports up to the v3 draft.

Turn this JSON schema:

{
  "$schema": "http://json-schema.org/draft-03/schema#",
  "title": "Country",
  "description": "A nation with its own government, occupying a particular territory",
  "type": "object",
  "properties": {
    "flag": {
      "$ref": "flag.json"
    },
    "cities": {
      "type": "array",
      "description": "A large town",
      "items": {
        "$ref": "city.json"
      },
      "uniqueItems": true
    },
    "population": {
      "type": "integer",
      "description": "The number of people inhabiting this country",
      "minimum": 1000,
      "required": true
    }
  }
}

Into this! (with all references generated in separate files)

namespace generated
{
    using System;
    using com.cvent.country.entities;
    using generated;
    using System.Collections.Generic;
    using Cvent.SchemaToPoco.Core.ValidationAttributes;
    using System.ComponentModel.DataAnnotations;
    
    // A nation with its own government, occupying a particular territory
    public class Country
    {
        // Used as the symbol or emblem of a country
        private Flag _flag;
        
        // A large town
        private HashSet<City> _cities;
        
        // The number of people inhabiting this country
        private int _population;
        
        // Used as the symbol or emblem of a country
        public virtual Flag Flag
        {
            get
            {
                return _flag;
            }
            set
            {
                _flag = value;
            }
        }
        
        // A large town
        public virtual HashSet<City> Cities
        {
            get
            {
                return _cities;
            }
            set
            {
                _cities = value;
            }
        }
        
        // The number of people inhabiting this country
        [Required()]
        [MinValue(1000)]
        public virtual int Population
        {
            get
            {
                return _population;
            }
            set
            {
                _population = value;
            }
        }
    }
}

Usage (CLI)

Requirements

Instructions

  1. Download the latest executable, build the solution in Visual Studio (Ctrl+Shift+B), or run msbuild \Path\to\.sln.
  2. Run the following command:
Cvent.SchemaToPoco.Console.exe -s \Location\To\Json\Schema

Optional Flags

-n name.for.namespace

Default: generated

-o \Location\To\Generate\Files

Default: <exe location>\generated

-v

Prints out generated code without generating files

Usage (Library)

Download the latest DLL, and add it to your project as a reference.

Basic Usage

// To generate files:
// The location can be a web address, an absolute path, or a relative path.
var controller = new JsonSchemaToPoco("/location/to/schema");
int status = controller.Execute();

// To get the C# code as a string:
string code = JsonSchemaToPoco.Generate("/location/to/schema");

Current version: 1.2 (Alpha)

View changelog

json-schema-to-poco's People

Contributors

sbl03 avatar codedemonuk avatar lroling8350 avatar

Watchers

James Cloos avatar Fernando Nogueira avatar  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.