簡體   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