Table of Contents

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

element JsonElement

A JSON element of kind Object.

Returns

Dictionary<string, object>

Exceptions

ArgumentException

element is 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

element JsonElement

A JSON element of kind Object.

keyComparer IEqualityComparer<string>

How keys compare, such as OrdinalIgnoreCase.

Returns

Dictionary<string, object>

Exceptions

ArgumentNullException

keyComparer is null.

ArgumentException

element is not a JSON object, or one object has two differently spelled properties that keyComparer treats as the same key (such as Total and total under a case-insensitive comparer). That is reported rather than letting one value silently replace the other.