Skip to content
Shabai Liu edited this page Aug 13, 2014 · 29 revisions

Note: This wiki will follow the same general format as jsonschema2pojo

Schema Feature Support

Rules listed as not supported are generally ignored. You can include the unsupported rules in your JSON schema, they will simply have no effect on the generated C# types.

The table below covers support for JSON Schema rules, in addition to some custom keys. Because of the dependence on JSON.net, JSON Schema draft v4 is not officially supported. See the official v3 draft for documentation on the JSON Schema itself.

Currently, all the validation attributes are from the System.ComponentModel.DataAnnotations namespace, and they are not validated when deserializing with JSON.net.

JSON Schema Rule Support Since Last Update Note
type (Simple) Yes 1.0 1.1
type (Union) No
properties Yes 1.0 1.2
patternProperties No
additionalProperties No
items Yes 1.0
additionalItems No
required Yes 1.0
optional No Deprecated
dependencies No
minimum, maximum Yes 1.0
exclusiveMinimum, exclusiveMaximum Yes 1.0
minItems, maxItems Yes 1.0
uniqueItems Yes 1.0
pattern Yes 1.1
minLength, maxLength Yes 1.0
enum Yes* 1.0
default Yes* 1.0 1.2
title Yes 1.0
description Yes 1.0
format No
divisibleBy No
disallow No
extends Yes 1.0
id No
$ref Yes 1.0 1.1
$schema No Only v3 support
*Limited support

Custom Rules

JSON Schema Rule Data Type Since Last Update Note
csharpType String 1.0
csharpInterfaces String array 1.0

type

Schema Type C# Type
string string
number float
integer int
boolean bool
object object
array System.Collections.Generic.List<T>
array (with "uniqueitems": true) System.Collections.Generic.HashSet<T>
null System.Object

properties

For each property in the properties definition, a field and an associating property with getters and setters is generated (because there is no clean way of generating the shorthand code field { get; set; }).

example

{
    "type" : "object",
    "properties" : {
        "foo" : {
            "type" : "string"
        }
    }
}

Generated code

namespace generated
{
    using System;


    public class DefaultClassName
    {

        private string _foo;

        public virtual string Foo
        {
            get
            {
                return _foo;
            }
            set
            {
                _foo = value;
            }
        }
    }
}

items

The items rule defines the items inside an array. This rule is only used if type is array.

sub-rules

type - Defines the type of the array.

example

{
    "type" : "object",
    "properties" : {
        "foo" : {
            "type" : "array",
            "items" : {
                "type" : "string"
            }
        }
    }
}

Generated code

namespace generated
{
    using System;
    using System.Collections.Generic;

    public class DefaultClassName
    {

        private List<string> _foo;

        public virtual List<string> Foo
        {
            get
            {
                return _foo;
            }
            set
            {
                _foo = value;
            }
        }
    }
}

##required The required rule simply adds a RequiredAttribute attribute to a property.

###example

{
    "type" : "object",
    "properties" : {
        "foo" : {
            "type" : "string",
            "required" : true
        }
    }
}

Generated code

namespace generated
{
    using System;
    using System.ComponentModel.DataAnnotations;


    public class DefaultClassName
    {

        private string _foo;

        [Required()]
        public virtual string Foo
        {
            get
            {
                return _foo;
            }
            set
            {
                _foo = value;
            }
        }
    }
}

##minimum, maximum The minimum and maximum rules only apply when the type is integer or number. This defines the minimum or maximum value of a number, inclusive. A custom attribute of either MinValue or MaxValue will be added.

###example

{
    "type" : "object",
    "properties" : {
        "foo" : {
            "type" : "integer",
            "minimum" : 10,
            "maximum" : 15
        }
    }
}

Generated code

namespace generated
{
    using System;
    using Cvent.SchemaToPoco.Core.ValidationAttributes;


    public class DefaultClassName
    {

        private int _foo;

        [MinValue(10)]
        [MaxValue(15)]
        public virtual int Foo
        {
            get
            {
                return _foo;
            }
            set
            {
                _foo = value;
            }
        }
    }
}

##exclusiveMinimum, exclusiveMaximum The exclusiveMinimum and exclusiveMaximum rules only apply when the type is integer or number in conjunction with either the minimum or maximum. This will exclude that number.

###example

{
    "type" : "object",
    "properties" : {
        "foo" : {
            "type" : "integer",
            "minimum" : 10,
            "maximum" : 15,
            "exclusiveMinimum" : true,
            "exclusiveMaximum" : false
        }
    }
}

Generated code

namespace generated
{
    using System;
    using Cvent.SchemaToPoco.Core.ValidationAttributes;


    public class DefaultClassName
    {

        private int _foo;

        [MinValue(11)]
        [MaxValue(15)]
        public virtual int Foo
        {
            get
            {
                return _foo;
            }
            set
            {
                _foo = value;
            }
        }
    }
}

##minItems, maxItems The minItems and maxItems rules only apply when the type is array. This will set restrictions on the length of the array using the MinLength and MaxLength C# attributes. Note that exclusiveMinimum and exclusiveMaximum have no effect on these rules.

###example

{
    "type" : "object",
    "properties" : {
        "foo" : {
            "type" : "array",
            "items" : {
                "type" : "string"
            },
            "minItems" : 10,
            "maxItems" : 20
        }
    }
}

Generated code

namespace generated
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;


    public class DefaultClassName
    {

        private List<string> _foo;

        [MinLength(10)]
        [MaxLength(20)]
        public virtual List<string> Foo
        {
            get
            {
                return _foo;
            }
            set
            {
                _foo = value;
            }
        }
    }
}

##uniqueItems The uniqueItems rule only applies when the type is array. This will make the array a HashSet instead of a List.

###example

{
    "type" : "object",
    "properties" : {
        "foo" : {
            "type" : "array",
            "items" : {
                "type" : "string"
            },
            "uniqueItems" : true
        }
    }
}

Generated code

namespace generated
{
    using System;
    using System.Collections.Generic;


    public class DefaultClassName
    {

        private HashSet<string> _foo;

        public virtual HashSet<string> Foo
        {
            get
            {
                return _foo;
            }
            set
            {
                _foo = value;
            }
        }
    }
}

##pattern The pattern rule only applies when the type is string. This defines a regular expression that the string must match.

Make sure that besides including a valid regular expression, to escape otherwise valid regex escape sequences that are not valid JSON escape sequences, and quotes, " or ' depending on what you use in your JSON file, just so that the JSON file is valid. The program will automatically sanitize the input to work with C#.

###example

{
    "type" : "object",
    "properties" : {
        "foo" : {
            "type" : "string",
            "description" : "Match the regex \\\"dev\"'[a-c] ",
            "pattern" : "^\\\"dev\"'[a-c]\\s$"
        }
    }
}

Generated code (will match text that looks like this: \"dev"'a , with a space at the end)

namespace generated
{
    using System;
    using System.ComponentModel.DataAnnotations;


    public class DefaultClassName
    {

        // Match the regex "dev"'[a-c]
        private string _foo;

        // Match the regex "dev"'[a-c]
        [RegularExpression(@"^\\\""dev\""\'[a-c]\s$")]
        public virtual string Foo
        {
            get
            {
                return _foo;
            }
            set
            {
                _foo = value;
            }
        }
    }
}

##minLength, maxLength The minLength and maxLength rules only apply when the type is string. This defines the minimum or maximum length of a string, inclusive, using the StringLength attribute. Note that exclusiveMinimum and exclusiveMaximum have no effect on these rules.

###example

{
    "type" : "object",
    "properties" : {
        "foo" : {
            "type" : "string",
            "maxLength" : 10,
            "minLength" : 2
        }
    }
}

Generated code

namespace generated
{
    using System;
    using System.ComponentModel.DataAnnotations;


    public class DefaultClassName
    {

        private string _foo;

        [StringLength(10, MinimumLength=2)]
        public virtual string Foo
        {
            get
            {
                return _foo;
            }
            set
            {
                _foo = value;
            }
        }
    }
}

##enum The enum rule will generate a C# enum type. Currently, there is no support for setting values to the enum fields.

It will sanitize the enum fields when generating the code.

###example

{
    "type" : "object",
    "properties" : {
        "foo" : {
            "type" : "object",
            "enum" : ["one", "2two2", "Three_ _third_"]
        }
    }
}

Generated code

namespace generated
{
    using System;


    public enum Foo
    {

        One,

        _2two2,

        Three__third_,
    }

    public class DefaultClassName
    {
    }
}

##default The default rule allows you to specify default values for properties.

Currently, the following defaults are supported: int (1), double (2.2), empty list (new List<object>()).

###example

{
    "type" : "object",
    "properties" : {
        "foo" : {
            "type": "string",
            "default": "hello"
        },
        "number" : {
            "type": "integer",
            "default": 10
        }
    }
}

Generated code

namespace generated
{
    using System;


    public class DefaultClassName
    {

        private string _foo;

        private int _number;

        public DefaultClassName()
        {
            _foo = "hello";
            _number = 10;
        }

        public virtual string Foo
        {
            get
            {
                return _foo;
            }
            set
            {
                _foo = value;
            }
        }

        public virtual int Number
        {
            get
            {
                return _number;
            }
            set
            {
                _number = value;
            }
        }
    }
}

##title The title rule is essentially the class name for the schema.

example

{
    "title" : "NewClassName",
    "type" : "object"
}

Generated code

namespace generated
{
    using System;


    public class NewClassName
    {
    }
}

##description The description rule is essentially the comment for the given entity. It can be used as a top-level rule, or as a child of a particular property.

example

{
    "title" : "NewClassName",
    "type" : "object",
    "description" : "Description for class!",
    "properties" : {
        "foo" : {
            "type" : "string",
            "description" : "Description for foo!"
        }
    }
}

Generated code

namespace generated
{
    using System;


    // Description for class!
    public class NewClassName
    {

        // Description for foo!
        private string _foo;

        // Description for foo!
        public virtual string Foo
        {
            get
            {
                return _foo;
            }
            set
            {
                _foo = value;
            }
        }
    }
}

##extends The extends rule allows a class to extend another class. The extended class will be generated in addition.

###sub-rules $ref - Location of the schema file to extend.

example

{
    "type" : "object",
    "extends" : {
        "$ref" : "data-set.json"
    }
}

Generated code

namespace generated
{
    using System;
    using generated;


    public class DefaultClassName : DataSet
    {
    }
}

##$ref The ref rule allows linking to external JSON schemas. It will look for the schema relative to the current file. See example schemas in \Schemas.

Currently, there is support for http://, https://, file://, absolute paths (C:/...), and relative paths (../Schemas/...).

###example

{
  "$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
    }
  }
}

Generated code for DataSet

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;
            }
        }
    }
}

Custom Types

##csharpType Takes in: String representing fully qualified name to class.

The csharpType rule allows you to specify a fully qualified name. This will override the namespace passed in, and the class name will override the title of the JSON schema. Below is an excerpt from the example above.

{
    "cities": {
      "type": "array",
      "description": "A large town",
      "items": {
        "$ref": "city.json"
      },
      "uniqueItems": true
    }
}

Inside city.json, "csharpType": "com.cvent.country.entities.City" is defined at the top level. Even though the passed in namespace might be generated, City will generate a file with the namespace com.cvent.country.entities, in the appropriate directory with the appropriate imports.

Wrote C:\Users\SLiu\Projects\json-schema-2-poco\Examples\Generated\com\cvent\country\entities\Flag.cs
Wrote C:\Users\SLiu\Projects\json-schema-2-poco\Examples\Generated\com\cvent\country\entities\City.cs
Wrote C:\Users\SLiu\Projects\json-schema-2-poco\Examples\Generated\generated\Country.cs

##csharpInterfaces Takes in: String array representing interface class names (fully qualified or not) that the POCO implements.

###example

{
    "type" : "object",
    "csharpInterfaces" : ["Cloneable", "System.String", "System.NotARealClass", "com.cvent.board.core.Hello"]
}

Generated code

namespace generated
{
    using System;
    using generated;
    using com.cvent.board.core;


    public class DefaultClassName : Cloneable, String, NotARealClass, Hello
    {
    }
}