Json.Net duplicates private list items in certain conditions?

Sep 27, 2012 at 8:36 AM
Edited Sep 27, 2012 at 9:03 AM

Currently I'm trying to utilize Json.Net serializer in my projects.

One of the problems that I've faced recentry is that deserialized object gets all of it's private list items duplicated. It can be reproduced like this:

  1. Create class with private field of List<> type, where List<> generic parameter is some other class
  2. Add public property, that gets/sets private field
  3. Create instance of that class, add 1 item to inner list, using property.
  4. Serialize with new DefaultContractResolver that is setup to see private class data;
  5. Deserialize

Code to reproduce the problem:

    public class ClassParam
   
{
       
public int? ParamOne
       
{
            get
;
            set
;
       
}
   
}
   
public class ClassWithParams
   
{
       
private List<ClassParam> _privateFieid = new List<ClassParam>();

       
public List<ClassParam> PropertWithBackingField
       
{
            get
{ return _privateFieid; }
            set
{ _privateFieid = value; }
       
}

       
public void AddElementToPrivateField(ClassParam classParam)
       
{
            _privateFieid
.Add(classParam);
       
}

   
}
   
[Test]
   
public void Test()
   
{
           
var instance = new ClassWithParams();

       
var param1 = new ClassParam { ParamOne = 1 };

        instance
.PropertWithBackingField.Add(param1);

       
var contractResolver = new DefaultContractResolver();
        contractResolver
.DefaultMembersSearchFlags |= BindingFlags.NonPublic;

       
string serializedInstance = JsonConvert.SerializeObject(instance,
                                                                   
Formatting.Indented,
                                                                   
new JsonSerializerSettings()
                                                                   
{
                                                                       
ContractResolver = contractResolver

                                                                   
});

       
var deserializeInstance = JsonConvert.DeserializeObject(serializedInstance, typeof(ClassWithParams),
                                                                         
new JsonSerializerSettings()
                                                                         
{
                                                                             
ContractResolver = contractResolver
                                                                         
});
   
}

1

When I remove public property PropertWithBackingField from ClassWithParams it's all ok. The problem is gone as well when I don't use custom setup for ContractResolver. But I do need to serialize private data of my classes as soon as not all of it is exposed via public properties.

What's wrong with my code? Are there any subtleties or is it a bug?

Upd

I want to emphasis that I do need to serialize private fields as soon as in my real-world usecase I have not all fields exposed via public properties.