using System.Collections.Generic;
using Newtonsoft.Json.Linq;
public static void Main()
Two = DateTimeOffset.UtcNow,
TwoNullable = (DateTimeOffset?)DateTimeOffset.UtcNow,
Dict = new Dictionary<string, object>
{ "thisa", new { Now = 0 }},
{ "thisb", new { Now = 0 }},
{ "thisc", new { Now = 5 }},
{ "thisd", new { Now = 23 }},
{ "thise", new { Now = 1 }},
Console.WriteLine("Result: " + CountNonDefaultPropertiesEntry(testObj));
public static int CountNonDefaultPropertiesEntry(object obj)
var serialized = JsonConvert.SerializeObject(obj, new JsonSerializerSettings{DefaultValueHandling = DefaultValueHandling.Ignore});
var jObj = JObject.Parse(serialized);
return CountNonDefaultPropertiesJToken(jObj);
static int CountNonDefaultPropertiesJToken(JToken token)
if (token.Type == JTokenType.Object)
foreach (var child in token.Value<JObject>())
sum += CountNonDefaultPropertiesJToken(child.Value);
else if (token.Type == JTokenType.Array)
foreach (var child in token.Value<JArray>())
sum += CountNonDefaultPropertiesJToken(child);