C# examples

Simple model

A simple model with some scalar properties.
{
  "definitions": {
    "Student": {
      "description": "A simple student struct",
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        }
      }
    }
  },
  "root": "Student"
}

Student.cs

using System.Text.Json.Serialization;

namespace TypeSchema.DTO;

/// <summary>
/// A simple student struct
/// </summary>
public class Student
{
    [JsonPropertyName("firstName")]
    public string? FirstName { get; set; }

    [JsonPropertyName("lastName")]
    public string? LastName { get; set; }

    [JsonPropertyName("age")]
    public int? Age { get; set; }

}


Model with inheritance

A student struct which extends from a different struct.
{
  "definitions": {
    "Human": {
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        }
      }
    },
    "Student": {
      "parent": {
        "type": "reference",
        "target": "Human"
      },
      "type": "struct",
      "properties": {
        "studentId": {
          "type": "string"
        }
      }
    }
  },
  "root": "Student"
}

Human.cs

using System.Text.Json.Serialization;

namespace TypeSchema.DTO;

public class Human
{
    [JsonPropertyName("firstName")]
    public string? FirstName { get; set; }

    [JsonPropertyName("lastName")]
    public string? LastName { get; set; }

    [JsonPropertyName("age")]
    public int? Age { get; set; }

}

Student.cs

using System.Text.Json.Serialization;

namespace TypeSchema.DTO;

public class Student : Human
{
    [JsonPropertyName("studentId")]
    public string? StudentId { get; set; }

}


Model with reference

A student struct which reference a faculty struct.
{
  "definitions": {
    "Student": {
      "description": "A student struct with an assigned faculty",
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        },
        "faculty": {
          "type": "reference",
          "target": "Faculty"
        }
      }
    },
    "Faculty": {
      "type": "struct",
      "properties": {
        "name": {
          "type": "string"
        }
      }
    }
  },
  "root": "Student"
}

Student.cs

using System.Text.Json.Serialization;

namespace TypeSchema.DTO;

/// <summary>
/// A student struct with an assigned faculty
/// </summary>
public class Student
{
    [JsonPropertyName("firstName")]
    public string? FirstName { get; set; }

    [JsonPropertyName("lastName")]
    public string? LastName { get; set; }

    [JsonPropertyName("age")]
    public int? Age { get; set; }

    [JsonPropertyName("faculty")]
    public Faculty? Faculty { get; set; }

}

Faculty.cs

using System.Text.Json.Serialization;

namespace TypeSchema.DTO;

public class Faculty
{
    [JsonPropertyName("name")]
    public string? Name { get; set; }

}


Map with string values

A student struct which contains a map with arbitrary string values.
{
  "definitions": {
    "Student": {
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        },
        "properties": {
          "type": "map",
          "schema": {
            "type": "string"
          }
        }
      }
    }
  },
  "root": "Student"
}

Student.cs

using System.Text.Json.Serialization;

namespace TypeSchema.DTO;

public class Student
{
    [JsonPropertyName("firstName")]
    public string? FirstName { get; set; }

    [JsonPropertyName("lastName")]
    public string? LastName { get; set; }

    [JsonPropertyName("age")]
    public int? Age { get; set; }

    [JsonPropertyName("properties")]
    public System.Collections.Generic.Dictionary<string, string>? Properties { get; set; }

}


Model with discriminator

A struct which uses a discriminator mapping.
{
  "definitions": {
    "Human": {
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "location": {
          "type": "reference",
          "target": "Location"
        }
      }
    },
    "Location": {
      "type": "struct",
      "base": true,
      "properties": {
        "type": {
          "type": "string"
        }
      },
      "discriminator": "type",
      "mapping": {
        "Web": "web",
        "World": "world"
      }
    },
    "Web": {
      "parent": {
        "type": "reference",
        "target": "Location"
      },
      "type": "struct",
      "properties": {
        "url": {
          "type": "string"
        }
      }
    },
    "World": {
      "parent": {
        "type": "reference",
        "target": "Location"
      },
      "type": "struct",
      "properties": {
        "lat": {
          "type": "string"
        },
        "long": {
          "type": "string"
        }
      }
    }
  },
  "root": "Human"
}

Human.cs

using System.Text.Json.Serialization;

namespace TypeSchema.DTO;

public class Human
{
    [JsonPropertyName("firstName")]
    public string? FirstName { get; set; }

    [JsonPropertyName("lastName")]
    public string? LastName { get; set; }

    [JsonPropertyName("location")]
    public Location? Location { get; set; }

}

Location.cs

using System.Text.Json.Serialization;

namespace TypeSchema.DTO;

[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
[JsonDerivedType(typeof(Web), typeDiscriminator: "web")]
[JsonDerivedType(typeof(World), typeDiscriminator: "world")]
public abstract class Location
{
    [JsonPropertyName("type")]
    public string? Type { get; set; }

}

Web.cs

using System.Text.Json.Serialization;

namespace TypeSchema.DTO;

public class Web : Location
{
    [JsonPropertyName("url")]
    public string? Url { get; set; }

}

World.cs

using System.Text.Json.Serialization;

namespace TypeSchema.DTO;

public class World : Location
{
    [JsonPropertyName("lat")]
    public string? Lat { get; set; }

    [JsonPropertyName("long")]
    public string? Long { get; set; }

}


Model with generics

A struct which uses generics.
{
  "definitions": {
    "Student": {
      "type": "struct",
      "properties": {
        "matricleNumber": {
          "type": "integer"
        }
      }
    },
    "StudentMap": {
      "type": "struct",
      "parent": {
        "type": "reference",
        "target": "Map",
        "template": {
          "T": "Student"
        }
      }
    },
    "Map": {
      "type": "struct",
      "properties": {
        "totalResults": {
          "type": "integer"
        },
        "entries": {
          "type": "array",
          "schema": {
            "type": "generic",
            "name": "T"
          }
        }
      }
    }
  },
  "root": "StudentMap"
}

Student.cs

using System.Text.Json.Serialization;

namespace TypeSchema.DTO;

public class Student
{
    [JsonPropertyName("matricleNumber")]
    public int? MatricleNumber { get; set; }

}

Map.cs

using System.Text.Json.Serialization;

namespace TypeSchema.DTO;

public class Map<T>
{
    [JsonPropertyName("totalResults")]
    public int? TotalResults { get; set; }

    [JsonPropertyName("entries")]
    public System.Collections.Generic.List<T>? Entries { get; set; }

}

StudentMap.cs

using System.Text.Json.Serialization;

namespace TypeSchema.DTO;

public class StudentMap : Map<Student>
{
}


Import other TypeSchema specification

A struct which references an external TypeSchema.
{
  "import": {
    "my_ns": "file:///generic.json"
  },
  "definitions": {
    "Faculty": {
      "type": "struct",
      "properties": {
        "description": {
          "type": "string"
        },
        "students": {
          "type": "array",
          "schema": {
            "type": "reference",
            "target": "my_ns:StudentMap"
          }
        }
      }
    }
  },
  "root": "Faculty"
}

Faculty.cs

using System.Text.Json.Serialization;

namespace TypeSchema.DTO;

public class Faculty
{
    [JsonPropertyName("description")]
    public string? Description { get; set; }

    [JsonPropertyName("students")]
    public System.Collections.Generic.List<StudentMap>? Students { get; set; }

}


Edit this page