繁体   English   中英

c#中的Json反序列化问题,缺少ReadOnlyCollection属性值

[英]Json deserialization Issue in c#, missing ReadOnlyCollection property value

我正在尝试使用 ReadOnlyCollection 类型属性反序列化对象。 在下面的代码中,我试图使用 json 中的“演员”值填充“演员”值,但它不起作用。

我的代码`

    using System;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Serialization;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;

    namespace GeneralConsolApp
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                Movie movie = new Movie()
                {
                    MovieID = 1,
                    MovieName = "Die Hard",
                };
                movie.AddActor(new Actor() { ActorID = 101,ActorName= "Bruce Willis"});
                movie.AddActor(new Actor() { ActorID = 102, ActorName = "Samuel L. Jackson" });

                Console.WriteLine("after Actors count : " + movie.Actors.Count);

                var serializedMovie = JsonConvert.SerializeObject(movie);

                var deserializeMovie = JsonConvert.DeserializeObject<Movie>(serializedMovie, new JsonSerializerSettings
                                                                                            {
                                                                                                ContractResolver = new TestJsonDefaultContractResolver()
                                                                                            });

                Console.WriteLine("after Actors count : " + deserializeMovie.Actors.Count);

            }
        }

        class Movie
        {
            public int MovieID { get; set; }
            public string MovieName { get; set; }

            public System.Collections.ObjectModel.ReadOnlyCollection<Actor> Actors { get { return actors.AsReadOnly();  } }

            private List<Actor> actors = new List<Actor>();

            public void AddActor(Actor actor)
            {
                actors.Add(actor);
            }

            public void RemoveActor(Actor actor)


            {
                foreach (Actor actor2 in Actors)
                {
                    if (actor2.ActorID == actor.ActorID)
                        actors.Remove(actor2);
                }
            }
        }

        class Actor
        {
            public int ActorID { get; set; }
            public string ActorName { get; set; }
        }

public class TestJsonDefaultContractResolver : DefaultContractResolver
    {
        protected override JsonProperty CreateProperty(
            MemberInfo member,
            MemberSerialization memberSerialization)
        {
            var prop = base.CreateProperty(member, memberSerialization);

            if (!prop.Writable)
            {
                if (prop.DeclaringType == typeof(Movie) && prop.PropertyName == "Actors")
                {
                    //// how to map Actors to actors

                    //prop.ItemConverter = null;  ??? how to use this ?
                }
            }

            return prop;
        }

        protected override IValueProvider CreateMemberValueProvider(MemberInfo member)
        {
            return base.CreateMemberValueProvider(member); //// or use this ??
        }
    }
}

我正在尝试覆盖 DefaultContractResolver 并计划在我们进行 Deserialization 时将“Actors”值映射到“actors”。 为此,在“CreateProperty”函数中,我尝试使用下面的代码,但我不知道如何做到这一点。

if (!prop.Writable)
            {
                if (prop.DeclaringType == typeof(Movie) && prop.PropertyName == "Actors")
                {
                    //// how to map Actors to actors

                    //prop.ItemConverter = null;  ??? how to use this ?
                }
            }

在线运行代码

您不需要任何合同解析器。 使用这个电影类

class Movie
{
    public int MovieID { get; set; }
    public string MovieName { get; set; }
    [JsonIgnore]
    public System.Collections.ObjectModel.ReadOnlyCollection<Actor> Actors { get { return actors.AsReadOnly(); } }
    [JsonProperty("Actors")]
    private List<Actor> actors;

    public void AddActor(Actor actor)
    {
        if (actors == null) actors = new List<Actor>();
        actors.Add(actor);
    }

    public void RemoveActor(Actor actor)
    {
        if (Actors != null)
            foreach (Actor actor2 in Actors)
            {
                if (actor2.ActorID == actor.ActorID)
                    actors.Remove(actor2);
            }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM