Method ToDictionary
- Namespace
- RuleWright.Json.SystemText
- Assembly
- RuleWright.Json.SystemText.dll
ToDictionary(JsonElement)
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(JsonElement, IEqualityComparer<string>).
public static Dictionary<string, object?> ToDictionary(JsonElement element)
Parameters
elementJsonElementA JSON element of kind Object.
Returns
Exceptions
- ArgumentException
elementis not a JSON object.
ToDictionary(JsonElement, 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(JsonElement).
public static Dictionary<string, object?> ToDictionary(JsonElement element, IEqualityComparer<string> keyComparer)
Parameters
elementJsonElementA JSON element of kind Object.
keyComparerIEqualityComparer<string>How keys compare, such as OrdinalIgnoreCase.
Returns
Exceptions
- ArgumentNullException
keyCompareris null.- ArgumentException
elementis 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.