Extensions and utility functions for System.Text.Json.
Sometimes it is more appropriate to handle data from a JSON document as a dictionary. For instance the merging functionality makes use of dictionary objects.
Converting a JsonDocument
or JsonElement
object into a dictionary is implemented in the ExtensionMethods
class, in the ToDictionary
methods.
The method requires that the JSON element being converted represents an object, as shown below.
JsonElement.ValueKind == JsonValueKind.Object
The method then enumerates the object’s properties adding each property as an item in the dictionary. The value of each property is handled differently depending on the type of value. The method supports all different types defined in the JsonValueKind
enumeration.
Array
: The value is added as an array to the resulting dictionary using the ValueList
class.False
/True
: The value is added as a boolean value.Null
/Undefined
: The value is added as null
.Number
: The value is added as a number. The method uses three different numeric types when attempting to parse a number. Parsing is attempted in the following order:
Object
: The value is converted into another ValueDictionary
instance and added to the resulting dictionary.String
: The value is added as a string.