使用 Json.net 序列化时如何根据类型更改属性名称?

2023-11-30

我有一个类型的属性object我必须根据它的类型更改名称。应该非常类似于[XmlElement("PropertyName", typeof(PropertyType))]XML 的属性。

例如,我有一个房产public object Item { get; set; }

如果在运行时我的属性具有以下类型Vehicle,我想将我的财产名称更改为“车辆”;如果它有一个类型Profile,我想将我的财产名称更改为“个人资料”。


没有内置方法可以根据运行时类型动态更改属性的名称,但您可以自定义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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Json.net 序列化时如何根据类型更改属性名称? 的相关文章

随机推荐