简体   繁体   中英

C# class to json schema with IDs

I have C# class. I tried generating a json schema using newtonsoft library.

JsonSchemaGenerator generator = new JsonSchemaGenerator();
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;

JsonSchema schema = generator.Generate(typeof(MyClass));

This code generates json-schema. But it doesn't generate Id field for the properties at the lowest level.

How can I get ids for all the properties in the schema?

Without example code and output guesswork is needed on what you mean by lowest level. I assume you are referring to private variables. If you are creating the class you can mark private variables with the [JsonProperty] attribute to include it.

TestSchema.GetSchema();

The Code

public class TestSchema
{
    public static void GetSchema()
    {

        //using Newtonsoft.Json;
        //using Newtonsoft.Json.Schema;
        //using Newtonsoft.Json.Schema.Generation;
        //using Newtonsoft.Json.Serialization;

        JSchemaGenerator generator = new JSchemaGenerator();

        //Generator settings
        generator.GenerationProviders.Add(new StringEnumGenerationProvider());
        generator.DefaultRequired = Required.Default;
        generator.SchemaLocationHandling = SchemaLocationHandling.Inline;
        generator.SchemaReferenceHandling = SchemaReferenceHandling.All;
        generator.SchemaIdGenerationHandling = SchemaIdGenerationHandling.TypeName;
        generator.ContractResolver = new CamelCasePropertyNamesContractResolver();

        //Kratos
        JSchema schema = generator.Generate(typeof(Kratos));
        string json = schema.ToString();
        System.Diagnostics.Debug.WriteLine(json);

        //Person
        schema = generator.Generate(typeof(Person));
        json = schema.ToString();
        System.Diagnostics.Debug.WriteLine(json);
    }
}

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    private string hidden { get; set; }
}


public class Kratos
{
    [JsonProperty(Required = Required.Always)]
    public static string Title = "God of War";
    public static string notIncluded = "Boo hoo";

    [JsonProperty(Required = Required.Always)]
    public const bool bWillHaveRevenge = true;
    //Yes
    public string mainWeapon = "Blades of Exile";
    [JsonProperty(Required = Required.Always)]
    private int nLivingFamily = 0;
    public GodsOfOlympus TopPriority = GodsOfOlympus.Zeus;


    public GodsOfOlympus KillableGods { get { return GodsOfOlympus.ALLTHEGODS; } }

    [JsonProperty]
    private GodsOfOlympus DefeatedGods = GodsOfOlympus.Poseidon | GodsOfOlympus.Hades;


    [Flags]
    public enum GodsOfOlympus : byte
    {
        Zeus = 0x1,
        Hades = 0x2,
        Poseidon = 0x4,
        Athena = 0x8,
        Hermes = 0x10,
        Helios = 0x20,
        Hera = 0x40,
        Aphrodite = 0x80,
        ALLTHEGODS = Zeus | Hades | Poseidon | Athena | Hermes | Helios | Hera | Aphrodite
    }

    public Kratos()
    {
        System.Diagnostics.Debug.WriteLine("KRAAAATOOOOSSS");
    }

}

Personally, I'd use System.Reflection if you really needed to list private variables in a class

//using System.Reflection;
FieldInfo[] KratosFields = typeof(Kratos).GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
foreach (FieldInfo fi in KratosFields)
{
    System.Diagnostics.Debug.WriteLine("[" + fi.MemberType.ToString() + "]" + fi.Name + "::" + fi.FieldType.Name);
}

PropertyInfo[] KratosProps = typeof(Kratos).GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
foreach (PropertyInfo pi in KratosProps)
{
    System.Diagnostics.Debug.WriteLine("[" + pi.MemberType.ToString() + "]" + pi.Name + "::" + pi.PropertyType.Name);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM