Json.NET - 控制类对象的属性反序列化 [英] Json.NET - controlling class object-properties deserialization

查看:143
本文介绍了Json.NET - 控制类对象的属性反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有反序列化与JSON.Net模型类链接

I have a model class Link which is deserialized with JSON.Net.

public class Link
{
    [JsonConstructor]
    internal Link(int id)
    {
        Id = id;
    }

    public int Id { get; internal set; }

    [JsonProperty("title")]
    public string Title { get; internal set; }

    [JsonProperty("description")]
    public string Description { get; internal set; }

... and so on

    public Avatar AuthorAvatar { get; internal set; }
}



头像包含三个属性: DefaultImageUri SmallImageUri MediumImageUri 。是否有可能在链接它将使用对象序列化创建头像对象: author_avatar author_avatar_small author_avatar_medium json的领域?

Avatar contains three properties: DefaultImageUri, SmallImageUri, MediumImageUri. Is it possible to create Avatar object on Link object deserialization which would use: author_avatar, author_avatar_small, author_avatar_medium json fields?

推荐答案

我相信你可以通过编写自己的 JsonConverter 下面是一个例子(我省略了系列化的一部分,但实现将是非常相似的反序列化)实现这一点:

I believe You can achieve this by writing your own JsonConverter here is an example (I omitted the serialization part, but the implementation would be very similar to De-serialization):

示例:

class Program
{
    private static void Main(string[] args)
    {
        var json = @"{  
                        id:1,
                        title: 'link title',
                        description: 'link description',
                        author_avatar:'link',
                        author_avatar_small:'small link',
                        author_avatar_medium:'medium link',
                     }";

        var obj = JsonConvert.DeserializeObject<Link>(json);
    }
}



类定义:

[JsonConverter(typeof(LinkSerializer))]
public class Link
{
    [JsonConstructor]
    public Link(int id)
    {
        Id = id;
    }

    [JsonIgnore]
    public int Id { get; internal set; }

    [JsonProperty("title")]
    public string Title { get; internal set; }

    [JsonProperty("description")]
    public string Description { get; internal set; }


    public Avatar AuthorAvatar { get; internal set; }
}

public class Avatar
{
    [JsonProperty("author_avatar")]
    public string DefaultImageUri { get; internal set; }
    [JsonProperty("author_avatar_small")]
    public string SmallImageUri { get; internal set; }
    [JsonProperty("author_avatar_medium")]
    public string MediumImageUri { get; internal set; }
}

自定义链路串行器:

public class LinkSerializer : JsonConverter
{

    public override bool CanConvert(Type objectType)
    {
        return typeof (Link) == objectType;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var jObject = JObject.Load(reader);

        //NOTE:I changed .ctor to publec to simplify the process, we can also check for JsonConstructor attribute on constructors and the call appropriate one
        var value = existingValue ?? Activator.CreateInstance(objectType, jObject["id"].Value<int>());
        Populate(objectType, jObject, value);

        var avatar = Activator.CreateInstance<Avatar>(); //Fill avatar object
        Populate(avatar.GetType(),jObject,avatar);

        objectType.GetProperty("AuthorAvatar").SetValue(value,avatar); //set avatar object

        return value;
    }

    private static void Populate(Type objectType, JObject jObject, object value)
    {
        var properties =
            objectType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

        foreach (var p in properties)
        {
            var ignore = p.GetCustomAttribute<JsonIgnoreAttribute>();
            if (ignore != null)
                continue;

            var custom = p.GetCustomAttribute<JsonPropertyAttribute>();
            var name = custom != null ? custom.PropertyName : p.Name;

            var token = jObject[name];
            var obj = token != null
                ? token.ToObject(p.PropertyType)
                : p.PropertyType.IsValueType ? Activator.CreateInstance(p.PropertyType) : null;

            p.SetValue(value, obj);
        }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        //we just want to deserialize the object so we don't need it here, but the implementation would be very similar to deserialization
    }

这篇关于Json.NET - 控制类对象的属性反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆