Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I am trying to create a JSON schema with the usage of Json.NET Schema. Anyway, my class has JsonConverter attribute, like in the example below:

public class Building
{
[JsonConverter(typeof(BuildConverter))]
public string Name { get; set; }
public string Phone { get; set; }
public string Type { get; set; }
public string Address { get; set; }
}


When generating a schema from the above class getting an empty schema {{}}, which is caused by the mentioned attribute.

What I have tried:

I wanted to create a JSchemaGenerationProvider for my class but still cannot get the correct schema.

public class CustomSchemaProvider : JSchemaGenerationProvider
   {
    public override JSchema GetSchema(JSchemaTypeGenerationContext context)
    {
        if (context.ObjectType == typeof(MyType))
        {
            return CreateSchemaWithFormat(context.ObjectType, context.Required);
        }
        return null;
    }

    private JSchema CreateSchemaWithFormat(Type type, Required required)
    {
        JSchemaGenerator generator = new JSchemaGenerator();
        JSchema schema = generator.Generate(type, required != Required.Always);

        return schema;
    }
}


Usage:

    public void ValidateSchema()
{
    JSchemaGenerator generator = new JSchemaGenerator();
    generator.GenerationProviders.Add(new CustomSchemaProvider());

    JSchema schema = generator.Generate(typeof(Building));
    Console.WriteLine(schema);
}

Could someone please take a look and suggest what I am doing wrong and how to get the correct schema against {{}}? I will appreciate any help.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900