public static Dictionary<string, int> CountNested(this JObject obj, Dictionary<string, int>? countedProperties = null)
{
Dictionary<string, int> countedProps = countedProperties ?? new();
foreach (KeyValuePair<string, JToken> kvp in obj)
{
countedProps.IncrementCount(kvp.Key);
foreach (JObject childJObject in kvp.Value.Children().OfType<JObject>())
{
Dictionary<string, int> childResults = CountNested(childJObject);
foreach (KeyValuePair<string, int> resultKvp in childResults)
{
countedProps.IncrementCount(resultKvp.Key, resultKvp.Value);
}
}
}
return countedProps;
}
private static void IncrementCount(this IDictionary<string, int> dict, string key, int amount = 1)
{
if (dict.ContainsKey(key))
dict[key] += amount;
else
dict[key] = amount;
}
public static Dictionary<string, int> CountNested(this JObject obj, Dictionary<string, int>? countedProperties = null)
{
Dictionary<string, int> countedProps = countedProperties ?? new();
foreach (KeyValuePair<string, JToken> kvp in obj)
{
countedProps.IncrementCount(kvp.Key);
foreach (JObject childJObject in kvp.Value.Children().OfType<JObject>())
{
Dictionary<string, int> childResults = CountNested(childJObject);
foreach (KeyValuePair<string, int> resultKvp in childResults)
{
countedProps.IncrementCount(resultKvp.Key, resultKvp.Value);
}
}
}
return countedProps;
}
private static void IncrementCount(this IDictionary<string, int> dict, string key, int amount = 1)
{
if (dict.ContainsKey(key))
dict[key] += amount;
else
dict[key] = amount;
}