Table of Contents

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

token JToken

A JSON token of kind Newtonsoft.Json.Linq.JTokenType.Object.

Returns

Dictionary<string, object>

Exceptions

ArgumentNullException

token is null.

ArgumentException

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

token JToken

A JSON token of kind Newtonsoft.Json.Linq.JTokenType.Object.

keyComparer IEqualityComparer<string>

How keys compare, such as OrdinalIgnoreCase.

Returns

Dictionary<string, object>

Exceptions

ArgumentNullException

token or keyComparer is null.

ArgumentException

token 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.