0

I have a C# application that uses JSON. There is a bug and it's a little bit complicated so I'll give an example. If we have this class:

public class FatherObj
{
     public SonObj Son {get; set;}
}

public class SonObj
{
     public List<GrandChildObj> GrandChildren {get; set;}
}

Then when deserializing with JSON object FatherObj, The list of GrandChildren in the object SonObj gets duplicated. I fixed it by adding above the declaration of the list the following code:

[JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace)]

However, when I tried adding it to the JSON serialization setting of the whole application, it caused problems. So, I decided to make a JsonConverter that will only apply to List objects and when deserializing it will call the Distinct method before returning. But, JsonConverter is an abstract class, so when implementing it's methods you cannot call any base methods (as they are abstract as well). How do I call the default converter settings? I do not want to create different conversion besides the distinct.

Maya S
  • 127
  • 1
  • 9
  • Will `CollectionClearingContractResolver` from [this question](https://stackoverflow.com/questions/35482896/clear-collections-before-adding-items-when-populating-existing-objects) meet your needs? – dbc Sep 07 '16 at 07:31
  • There is no place in the code which calls "PopulateObject". Will this still work just for deserializing? – Maya S Sep 07 '16 at 07:34
  • It should do. You can set `JsonSerializerSettings.ContractResolver` then pass the settings to `DeserializeObject`. See http://www.newtonsoft.com/json/help/html/contractresolver.htm. But if it doesn't work or other problems arise, can you share the c# code that isn't working? – dbc Sep 07 '16 at 07:37
  • I will check it right now, thank you – Maya S Sep 07 '16 at 07:43
  • Thank you!!! You answered my question on a linked post. If you post your answer instead of mine, I'll accept it as the answer. – Maya S Sep 07 '16 at 15:09

1 Answers1

1

Ok, so after a bit of searching with the help from user dbc - I took his answer but from a different link: How to apply ObjectCreationHandling.Replace to selected properties when deserializing JSON?

The solution that he posted in the link: Create a custom ContractResolver (that inherits from DefaultContractResolver) with the following code:

protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
    var jsonProperty = base.CreateProperty(member, memberSerialization);
    if (jsonProperty.ObjectCreationHandling == null && jsonProperty.PropertyType.GetListType() != null)
        jsonProperty.ObjectCreationHandling = ObjectCreationHandling.Replace;
    return jsonProperty;
}

public static class TypeExtensions
{
    public static Type GetListType(this Type type)
    {
         while (type != null)
        {
            if (type.IsGenericType)
            {
                var genType = type.GetGenericTypeDefinition();
                if (genType == typeof(List<>))
                    return type.GetGenericArguments()[0];
            }
            type = type.BaseType;
        }
        return null;
    }
}
Community
  • 1
  • 1
Maya S
  • 127
  • 1
  • 9