且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何“内联"使用json.net生成的json中的属性

更新时间:2023-02-18 15:45:58

一种方法是为ISteeringWheelIdentifier创建自己的自定义json反序列化器,在其中应为每种方向盘标识符类型实现所需的反序列化结果(请参见 http://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm )为例.然后,应为您的标识符属性[JsonConverter(typeof([新转换器的名称]]))设置一个JsonConverter属性,并且将根据您的指定对它进行反序列化.

One approach is to create your own custom json deserializer for ISteeringWheelIdentifier, in which you should implement the desired deserialization result for each steering wheel identifier type (see http://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm) for an example. Then you should set an JsonConverter attribute for your Identifier property [JsonConverter(typeof([Name of your new converter]))] and it will be deserialized however you specified.

编辑-实际实现它时,要获得所需的行为会有些技巧.您需要创建的转换器是ISteeringWheel界面的转换器.在其中,遍历所有属性,直到获得标识符属性,然后处理其序列化.举个例子:

EDIT - when actually implementing it, turns out it's a bit tricker to get the desired behavior. The converter you need to create is one for the ISteeringWheel interface. In it, go over all the properties until you get to the identifier property, and handle its serialization. To the example:

    public class SteeringWheelJsonConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return typeof(ISteeringWheel).IsAssignableFrom(objectType);
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        JObject jo = new JObject();
        Type type = value.GetType();

        foreach (var prop in type.GetProperties())
        {
            if (prop.CanRead)
            {
                var propVal = prop.GetValue(value, null);
                if (prop.Name == "Identifier")
                {
                    // Iterate over all properties of the identifier, but don't add the identifier object itself 
                    // to the serialized result.
                    Type identifierType = propVal.GetType();
                    foreach (var identifierProp in identifierType.GetProperties())
                    {
                        var identifierPropVal = identifierProp.GetValue(propVal, null);
                        jo.Add(identifierProp.Name, identifierPropVal != null ? JToken.FromObject(identifierPropVal, serializer) : null);
                    }
                }
                else
                {
                    // Add the property to the serialized result
                    jo.Add(prop.Name, propVal != null ? JToken.FromObject(propVal, serializer) : null);
                }
            }
        }

        jo.WriteTo(writer);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
        JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override bool CanRead
    {
        get { return false; }
    }
}

现在剩下的就是将属性添加到car类:

Now all that remains is to add the attribute to the car class:

public class Car
{
    public string CarBrand { get; set; }

    [JsonConverter(typeof(SteeringWheelJsonConverter))]
    public ISteeringWheel SteeringWheel { get; set; }

}