Skip to content

FishHooks/json-schema-2-poco

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

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
        public Flag Flag { get; set; }

        // A large town
        public HashSet<City> Cities { get; set; }

        // The number of people inhabiting this country
        [Required()]
        [MinValue(1000)]
        public int Population { get; set; }
    }
}

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

About

Converts JSON schemas to C# POCOs

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%