Generate Custom JSON Schema using Newtonsoft.Json.Schema.Generation

58 Views Asked by At

We are using Newtonsoft.Json.Schema.Generation (C#, dotNet 5.0) and I would like generate a custom JSON schema based on the properties and its attributes of a class which has category attribute set. How do I achieve this?

Example:

class School
{
    [JsonProperty("Student Name", Required = Required.AllowNull)]
    [Editable(false)]
    [Category("Student")]
    [ReadOnly(true)]
    public string StudentName { get; set; }

    [Editable(true)]
    [Range(0, 1000, ErrorMessage = "Value for {0} must be between {1} and {2}.")]
    [Category("Student")]
    public int Id { get; set; } = 10;

    [JsonProperty("Parent Name", Required = Required.AllowNull)]
    [Editable(false)]
    [Category("Student")]
    public string ParentName { get; set; }


    [JsonProperty("ParentPhone", Required = Required.Default)]
    [Category("Student")]
    public string Phone { get; set; }

    [Editable(true)]
    [Category("Staff")]
    public string TeacherName { get; set; }

    [Editable(true)]
    [Range(0, 10000, ErrorMessage = "Value for {0} must be between {1} and {2}.")]
    [DefaultValue(5000)]
    [Category("Staff")]
    public int Salary { get; set; }

    [ReadOnly(true)]
    [Category("Student")]
    public bool AvailingTransport { get; set; } = true;

    [ReadOnly(true)]
    [Category("Common")]
    public string Address { get; set; }

    [ReadOnly(true)]
    [Category("Common")]
    public string MobileNumber { get; set; }
}

From the class above, I get a schema like:

{
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "Student Name": {
      "type": [
        "string",
        "null"
      ]
    },
    "Id": {
      "type": "integer",
      "minimum": 0.0,
      "maximum": 1000.0
    },
    "Parent Name": {
      "type": [
        "string",
        "null"
      ]
    },
    "ParentPhone": {
      "type": [
        "string",
        "null"
      ]
    },
    "TeacherName": {
      "type": [
        "string",
        "null"
      ]
    },
    "Salary": {
      "type": "integer",
      "default": 5000,
      "minimum": 0.0,
      "maximum": 10000.0
    },
    "AvailingTransport": {
      "type": "boolean"
    },
    "Address": {
      "type": [
        "string",
        "null"
      ]
    },
    "MobileNumber": {
      "type": [
        "string",
        "null"
      ]
    }
  },
  "required": [
    "Student Name",
    "Id",
    "Parent Name",
    "TeacherName",
    "Salary",
    "AvailingTransport",
    "Address",
    "MobileNumber"
  ]
}

But, I would like to generate JSON schema as below:

{
   "type":"object",
   "additionalProperties":false,
   "properties":{
      "Address":{
         "type":[
            "string",
            "null"
         ]
      },
      "MobileNumber":{
         "type":[
            "string",
            "null"
         ]
      },
      "Student":{
         "Student Name":{
            "type":[
               "string",
               "null"
            ]
         },
         "Id":{
            "type":"integer",
            "minimum":0.0,
            "maximum":1000.0
         },
         "Parent Name":{
            "type":[
               "string",
               "null"
            ]
         },
         "ParentPhone":{
            "type":[
               "string",
               "null"
            ]
         },
         "AvailingTransport":{
            "type":"boolean"
         },
         "required":[
            "Student Name",
            "Id",
            "Parent Name",
            "AvailingTransport"
         ]
      },
      "Staff":{
         "TeacherName":{
            "type":[
               "string",
               "null"
            ]
         },
         "Salary":{
            "type":"integer",
            "default":5000,
            "minimum":0.0,
            "maximum":10000.0
         },
         "required":[
            "TeacherName",
            "Salary"
         ]
      }
   },
   "required":[
      "Address",
      "MobileNumber"
   ]
}

JSON Data as below:

{
  "Address": "XYZ",
  "MobileNumber": "1122334455",
  "Student":{
    "Student Name": "ABC",
    "Id":1,
    "Parent Name": "ABC",
    "AvailingTransport":false
  },
  "Staff":{
    "TeacherName": "ABC",
    "Salary":5000
  }
}
0

There are 0 best solutions below