Json.net deserializing list gives duplicate items

前端 未结 4 1257
眼角桃花
眼角桃花 2021-02-18 15:56

I have just started using Newtonsoft.Json (Json.net). In my first simple test, I ran into a problem when deserializing generic lists. In my code sample below I serialize an obje

相关标签:
4条回答
  • 2021-02-18 16:35

    I'm pretty sure that this post is not relevant anymore, but for future reference, here a working solution. Just need to specify that ObjectCreationHandling is set to Replace, i.e. Always create new objects and not to Auto (which is the default) i.e. Reuse existing objects, create new objects when needed.

    var data = new SomeData(); 
    var json = JsonConvert.SerializeObject(data);
    Console.WriteLine("First : {0}", json);
    var data2 = JsonConvert.DeserializeObject<SomeData>(json, new JsonSerializerSettings() { ObjectCreationHandling = ObjectCreationHandling.Replace });
    var json2 = JsonConvert.SerializeObject(data2);
    Console.WriteLine("Second: {0}", json2);
    
    0 讨论(0)
  • 2021-02-18 16:45

    I encountered a similar issue with a different root cause. I was serializing and deserializing a class that looked like this:

    public class Appointment
    {
        public List<AppointmentRevision> Revisions { get; set; }
    
        public AppointmentRevision CurrentRevision
        {
            get { return Revision.LastOrDefault(); }
        }
    
        public Appointment()
        {
            Revisions = new List<AppointmentRevision>();
        }
    }
    
    public class AppointmentRevision
    {
        public List<Attendee> Attendees { get; set; }
    }
    

    When I serialized this, CurrentRevision was being serialized too. I'm not sure how, but when it was deserializing it was correctly keeping a single instance of the AppointmentRevision but creating duplicates in the Attendees list. The solution was to use the JsonIgnore attribute on the CurrentRevision property.

    public class Appointment
    {
        public List<AppointmentRevision> Revisions { get; set; }
    
        [JsonIgnore]   
        public AppointmentRevision CurrentRevision
        {
            get { return Revision.LastOrDefault(); }
        }
    
        public Appointment()
        {
            Revisions = new List<AppointmentRevision>();
        }
    }
    
    0 讨论(0)
  • 2021-02-18 16:46

    How to apply ObjectCreationHandling.Replace to selected properties when deserializing JSON?

    Turns out (I'm in 2019), you can set the list items in your constructor as you were doing in your question. I added the ObjectCreationHandling.Replace attribute above my declaration of the list, then serialising should replace anything stored in the list with the JSON.

    0 讨论(0)
  • 2021-02-18 17:01

    That is because you are adding items in the constructor. A common approach in deserializers when processing a list is basically:

    • read the list via the getter
      • if the list is null: create a new list and assign via the property setter, if one
    • deserialize each item in turn, and append (Add) to the list

    this is because most list members don't have setters, i.e.

    public List<Foo> Items {get {...}} // <=== no set
    

    Contrast to arrays, which must have a setter to be useful; hence the approach is usually:

    • deserialize each item in turn, and append (Add) to a temporary list
    • convert the list to an array (ToArray), and assign via the setter

    Some serializers give you options to control this behavior (others don't); and some serializers give you the ability to bypass the constructor completely (others don't).

    0 讨论(0)
提交回复
热议问题