没有内置方法可以根据运行时类型动态更改属性的名称,但您可以自定义JsonConverter加上定制Attribute
类做你想做的事。需要使转换器在类级别进行操作,以便能够控制写入 JSON 的属性名称。它可以使用反射迭代目标类的属性,并检查是否有任何属性声明为object
已应用自定义属性。如果确实如此,并且对象的运行时类型与属性中指定的类型匹配,则使用属性中的属性名称,否则仅使用原始属性名称。
自定义属性如下所示:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
class JsonPropertyNameByTypeAttribute : Attribute
{
public string PropertyName { get; set; }
public Type ObjectType { get; set; }
public JsonPropertyNameByTypeAttribute(string propertyName, Type objectType)
{
PropertyName = propertyName;
ObjectType = objectType;
}
}
这是转换器的代码:
public class DynamicPropertyNameConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Type type = value.GetType();
JObject jo = new JObject();
foreach (PropertyInfo prop in type.GetProperties().Where(p => p.CanRead))
{
string propName = prop.Name;
object propValue = prop.GetValue(value, null);
JToken token = (propValue != null) ? JToken.FromObject(propValue, serializer) : JValue.CreateNull();
if (propValue != null && prop.PropertyType == typeof(object))
{
JsonPropertyNameByTypeAttribute att = prop.GetCustomAttributes<JsonPropertyNameByTypeAttribute>()
.FirstOrDefault(a => a.ObjectType.IsAssignableFrom(propValue.GetType()));
if (att != null)
propName = att.PropertyName;
}
jo.Add(propName, token);
}
jo.WriteTo(writer);
}
public override bool CanRead
{
get { return false; }
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
// ReadJson is not called if CanRead returns false.
throw new NotImplementedException();
}
public override bool CanConvert(Type objectType)
{
// CanConvert is not called if a [JsonConverter] attribute is used
return false;
}
}
要使用转换器,首先添加一个[JsonConverter]
attribute 到包含要动态命名的一个或多个属性的目标类。然后,将自定义属性添加到该类中的目标属性(或多个属性)。您可以根据需要添加任意数量的属性,以涵盖您期望的类型范围。
例如:
[JsonConverter(typeof(DynamicPropertyNameConverter))]
class Foo
{
public int Id { get; set; }
public string Name { get; set; }
[JsonPropertyNameByType("Vehicle", typeof(Vehicle))]
[JsonPropertyNameByType("Profile", typeof(Profile))]
public object Item { get; set; }
}
然后,像平常一样进行序列化:
string json = JsonConvert.SerializeObject(foo, Formatting.Indented);
这是一个工作演示:https://dotnetfiddle.net/75HwrV