Method ToDictionary
- Namespace
- RuleWright.Json.NewtonsoftJson
- Assembly
- RuleWright.Json.NewtonsoftJson.dll
ToDictionary(JToken)
Recursively converts a JSON object into a dictionary fact. Nested objects become nested
dictionaries; arrays become object?[]; numbers become long when
integral, otherwise decimal when exactly representable, otherwise
double. Keys match exactly (ordinal, case-sensitive); to match field paths
without regard to case, use ToDictionary(JToken, IEqualityComparer<string>).
public static Dictionary<string, object?> ToDictionary(JToken token)
Parameters
tokenJTokenA JSON token of kind Newtonsoft.Json.Linq.JTokenType.Object.
Returns
Exceptions
- ArgumentNullException
tokenis null.- ArgumentException
tokenis not a JSON object.
ToDictionary(JToken, IEqualityComparer<string>)
Recursively converts a JSON object into a dictionary fact whose keys, at every level, compare
with keyComparer. The interpreter looks each field-path segment up through
the dictionary's own comparer, so with OrdinalIgnoreCase a rule
reading Order.Total finds a camelCase payload's order.total, just as a typed
fact's members already match. Values convert exactly as in ToDictionary(JToken).
public static Dictionary<string, object?> ToDictionary(JToken token, IEqualityComparer<string> keyComparer)
Parameters
tokenJTokenA JSON token of kind Newtonsoft.Json.Linq.JTokenType.Object.
keyComparerIEqualityComparer<string>How keys compare, such as OrdinalIgnoreCase.
Returns
Exceptions
- ArgumentNullException
tokenorkeyCompareris null.- ArgumentException
tokenis not a JSON object, or one object has two differently spelled properties thatkeyComparertreats as the same key (such asTotalandtotalunder a case-insensitive comparer). That is reported rather than letting one value silently replace the other.