2

I have the following code:

var data = new {
    name = "Jon Doe",
    properties = new
    {
        homes = new Dictionary<string, string>()
    },
    age = 34
}

var settings = new JsonSerializerSettings {
    ContractResolver = new MyResolver()
};

var serializationResult = JsonConvert.SerializeObject(data, settings)

The Json.Net contract resolver MyResolver is defined as follow:

class MyResolver : DefaultContractResolver {
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        JsonProperty property = base.CreateProperty(member, memberSerialization);

        if (property.PropertyType.GetTypeInfo().IsGenericType &&
            typeof(IDictionary).IsAssignableFrom(property.PropertyType.GetGenericTypeDefinition()))
        {
            property.ShouldSerialize =
                instance =>
                {
                    var value = instance.GetType().GetProperty(property.PropertyName).GetValue(instance, null);
                    return (value as IDictionary).Count > 0;
                };
        }

        return property;
        }
}

My problem is that the json result of the serialization is the following:

{
  "name": "Jon Doe",
  "properties": {},
  "age": 34
}

but I would like to get rid of the property "properties": {}, which means that I would like to obtain the json

{
  "name": "Jon Doe",
  "age": 34
}

I have done unsuccessful attempts to get rid of these kind of properties that result to be empty after removing their childs, how could I accomplish this?

Raul Alonso
  • 453
  • 3
  • 13
  • 1
    You are omitting the empty dictionary here (the `homes` property), do you want to omit the parent of that instead? – DavidG Jul 10 '17 at 14:52
  • did you try to use settings.NullValueHandling = NullValueHandling.Ignore; – hasan Jul 10 '17 at 14:55
  • I have DTOs which may have a nested property at any depth level that is a Dictionary. I want to remove that Dictionary property based on some criteria and if some of the parent properties result to be empty after that operation I would like to remove them too. – Raul Alonso Jul 10 '17 at 15:00
  • I tried with settings.NullValueHandling = NullValueHandling.Ignore, but I don't want to ignore all default values. – Raul Alonso Jul 10 '17 at 15:01
  • So how do you define the properties that you want to exclude? – DavidG Jul 10 '17 at 15:03
  • [Try it](https://stackoverflow.com/questions/36884902/how-to-omit-ignore-skip-empty-object-literals-in-the-produced-json) – Ivan R. Jul 10 '17 at 15:49

1 Answers1

0

instanceis already the specified type. You can simply cast:

instance =>
                {
                    var value = (IDictionary)instance;
                    return value != null && value.Keys.Count() > 0
                };

Try if that works. If not, check with debug what the contents of value are and post back.

EDIT: I see this is about a nested property. Please provide the code of the inherited classes.

CthenB
  • 800
  • 6
  • 17