具有ID的C#class到json模式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了具有ID的C#class到json模式相关的知识,希望对你有一定的参考价值。
我有C#类。我尝试使用newtonsoft库生成json模式。
JsonSchemaGenerator generator = new JsonSchemaGenerator();
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
JsonSchema schema = generator.Generate(typeof(MyClass));
此代码生成json-schema。但它不会为最低级别的属性生成Id字段。
如何获取架构中所有属性的ID?
答案
没有示例代码和输出猜测需要你最低级别的意思。我假设你指的是私有变量。如果要创建类,可以使用[JsonProperty]属性标记私有变量以包含它。
TestSchema.GetSchema();
代码
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");
}
}
就个人而言,如果你真的需要在类中列出私有变量,我会使用System.Reflection
//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);
}
以上是关于具有ID的C#class到json模式的主要内容,如果未能解决你的问题,请参考以下文章