Table of Contents

Class JsonDeserializer

Namespace
Xunit.Sdk

A special-purpose untyped deserializer for JSON. JSON strings are returned as string, JSON numbers are returned as decimal, JSON booleans are returns as bool, JSON objects are returned as IReadOnlyDictionary<string, object?>, JSON arrays are returned as object?[], and JSON null values are returned as null. Static methods exist here to help retrieve values from object dictionaries as well as convert to the commonly supported data types (bool, DateTimeOffset, decimal, Enum, int, long, string, and trait dictionaries (which are decoded to IReadOnlyDictionary<string, IReadOnlyList<string>>), as well as arrays of all the supported types (except trait dictionaries). Developers who need support for other types are encouraged to encode and decode then as strings as needed (for examples, you can see how Enum and DateTimeOffset values are handled here).

public static class JsonDeserializer
Inheritance
JsonDeserializer
Inherited Members

Remarks

These types are made public for third parties only for the purpose of serializing and deserializing messages that are sent across the process boundary (that is, types which implement IMessageSinkMessage). Any other usage is not supported.

Methods

TryDeserialize(string, out object?)

public static bool TryDeserialize(string json, out object? result)

Parameters

json string
result object

Returns

bool

TryGetArray(IReadOnlyDictionary<string, object?>, string)

Tries to get an untyped array value from a deserialized JSON object.

public static object?[]? TryGetArray(IReadOnlyDictionary<string, object?> obj, string key)

Parameters

obj IReadOnlyDictionary<string, object>

The deserialized JSON object

key string

The key for the value

Returns

object[]

TryGetArray(object?)

Tries to get an untyped array value from a deserialized JSON value.

public static object?[]? TryGetArray(object? value)

Parameters

value object

The deserialized JSON value

Returns

object[]

TryGetArrayOfBoolean(IReadOnlyDictionary<string, object?>, string)

Tries to get an array of bool values from a deserialized JSON object. If the value isn't an array, or if any of the values in the array aren't compatible with bool, then returns null. Null values in the array are not permitted.

public static bool[]? TryGetArrayOfBoolean(IReadOnlyDictionary<string, object?> obj, string key)

Parameters

obj IReadOnlyDictionary<string, object>

The deserialized JSON object

key string

The key for the value

Returns

bool[]

TryGetArrayOfBoolean(object?)

Tries to get an array of bool values from a deserialized JSON value. If the value isn't an array, or if any of the values in the array aren't compatible with bool, then returns null. Null values in the array are not permitted.

public static bool[]? TryGetArrayOfBoolean(object? value)

Parameters

value object

The deserialized JSON value

Returns

bool[]

TryGetArrayOfDateTimeOffset(IReadOnlyDictionary<string, object?>, string)

Tries to get an array of DateTimeOffset values from a deserialized JSON object. If the value isn't an array, or if any of the values in the array aren't compatible with DateTimeOffset, then returns null. Null values in the array are not permitted.

public static DateTimeOffset[]? TryGetArrayOfDateTimeOffset(IReadOnlyDictionary<string, object?> obj, string key)

Parameters

obj IReadOnlyDictionary<string, object>

The deserialized JSON object

key string

The key for the value

Returns

DateTimeOffset[]

TryGetArrayOfDateTimeOffset(object?)

Tries to get an array of DateTimeOffset values from a deserialized JSON value. If the value isn't an array, or if any of the values in the array aren't compatible with DateTimeOffset, then returns null. Null values in the array are not permitted.

public static DateTimeOffset[]? TryGetArrayOfDateTimeOffset(object? value)

Parameters

value object

The deserialized JSON value

Returns

DateTimeOffset[]

TryGetArrayOfDecimal(IReadOnlyDictionary<string, object?>, string)

Tries to get an array of decimal values from a deserialized JSON object. If the value isn't an array, or if any of the values in the array aren't compatible with decimal, then returns null. Null values in the array are not permitted.

public static decimal[]? TryGetArrayOfDecimal(IReadOnlyDictionary<string, object?> obj, string key)

Parameters

obj IReadOnlyDictionary<string, object>

The deserialized JSON object

key string

The key for the value

Returns

decimal[]

TryGetArrayOfDecimal(object?)

Tries to get an array of decimal values from a deserialized JSON value. If the value isn't an array, or if any of the values in the array aren't compatible with decimal, then returns null. Null values in the array are not permitted.

public static decimal[]? TryGetArrayOfDecimal(object? value)

Parameters

value object

The deserialized JSON value

Returns

decimal[]

TryGetArrayOfEnum<TEnum>(IReadOnlyDictionary<string, object?>, string)

Tries to get an array of TEnum values from a deserialized JSON object. If the value isn't an array, or if any of the values in the array aren't compatible with TEnum, then returns null. Null values in the array are not permitted.

public static TEnum[]? TryGetArrayOfEnum<TEnum>(IReadOnlyDictionary<string, object?> obj, string key) where TEnum : struct, Enum

Parameters

obj IReadOnlyDictionary<string, object>

The deserialized JSON object

key string

The key for the value

Returns

TEnum[]

Type Parameters

TEnum

TryGetArrayOfEnum<TEnum>(object?)

Tries to get an array of TEnum values from a deserialized JSON value. If the value isn't an array, or if any of the values in the array aren't compatible with TEnum, then returns null. Null values in the array are not permitted.

public static TEnum[]? TryGetArrayOfEnum<TEnum>(object? value) where TEnum : struct, Enum

Parameters

value object

The deserialized JSON value

Returns

TEnum[]

Type Parameters

TEnum

TryGetArrayOfInt(IReadOnlyDictionary<string, object?>, string)

Tries to get an array of int values from a deserialized JSON object. If the value isn't an array, or if any of the values in the array aren't compatible with int, then returns null. Null values in the array are not permitted.

public static int[]? TryGetArrayOfInt(IReadOnlyDictionary<string, object?> obj, string key)

Parameters

obj IReadOnlyDictionary<string, object>

The deserialized JSON object

key string

The key for the value

Returns

int[]

TryGetArrayOfInt(object?)

Tries to get an array of int values from a deserialized JSON value. If the value isn't an array, or if any of the values in the array aren't compatible with int, then returns null. Null values in the array are not permitted.

public static int[]? TryGetArrayOfInt(object? value)

Parameters

value object

The deserialized JSON value

Returns

int[]

TryGetArrayOfLong(IReadOnlyDictionary<string, object?>, string)

Tries to get an array of long values from a deserialized JSON object. If the value isn't an array, or if any of the values in the array aren't compatible with long, then returns null. Null values in the array are not permitted.

public static long[]? TryGetArrayOfLong(IReadOnlyDictionary<string, object?> obj, string key)

Parameters

obj IReadOnlyDictionary<string, object>

The deserialized JSON object

key string

The key for the value

Returns

long[]

TryGetArrayOfLong(object?)

Tries to get an array of long values from a deserialized JSON value. If the value isn't an array, or if any of the values in the array aren't compatible with long, then returns null. Null values in the array are not permitted.

public static long[]? TryGetArrayOfLong(object? value)

Parameters

value object

The deserialized JSON value

Returns

long[]

TryGetArrayOfNullableString(IReadOnlyDictionary<string, object?>, string)

Tries to get an array of string values from a deserialized JSON object. If the value isn't an array, or if any of the values in the array aren't compatible with string, then returns null. Null values in the array are permitted.

public static string?[]? TryGetArrayOfNullableString(IReadOnlyDictionary<string, object?> obj, string key)

Parameters

obj IReadOnlyDictionary<string, object>

The deserialized JSON object

key string

The key for the value

Returns

string[]

TryGetArrayOfNullableString(object?)

Tries to get an array of string values from a deserialized JSON value. If the value isn't an array, or if any of the values in the array aren't compatible with string, then returns null. Null values in the array are permitted.

public static string?[]? TryGetArrayOfNullableString(object? value)

Parameters

value object

The deserialized JSON value

Returns

string[]

TryGetArrayOfString(IReadOnlyDictionary<string, object?>, string)

Tries to get an array of string values from a deserialized JSON object. If the value isn't an array, or if any of the values in the array aren't compatible with string, then returns null. Null values in the array are not permitted.

public static string[]? TryGetArrayOfString(IReadOnlyDictionary<string, object?> obj, string key)

Parameters

obj IReadOnlyDictionary<string, object>

The deserialized JSON object

key string

The key for the value

Returns

string[]

TryGetArrayOfString(object?)

Tries to get an array of string values from a deserialized JSON value. If the value isn't an array, or if any of the values in the array aren't compatible with string, then returns null. Null values in the array are not permitted.

public static string[]? TryGetArrayOfString(object? value)

Parameters

value object

The deserialized JSON value

Returns

string[]

TryGetBoolean(IReadOnlyDictionary<string, object?>, string)

Tries to get a bool value from a deserialized JSON object.

public static bool? TryGetBoolean(IReadOnlyDictionary<string, object?> obj, string key)

Parameters

obj IReadOnlyDictionary<string, object>

The deserialized JSON object

key string

The key for the value

Returns

bool?

TryGetBoolean(object?)

Tries to get a bool value from a deserialized JSON value.

public static bool? TryGetBoolean(object? value)

Parameters

value object

The deserialized JSON value

Returns

bool?

TryGetDateTimeOffset(IReadOnlyDictionary<string, object?>, string)

Tries to get a DateTimeOffset value from a deserialized JSON object.

public static DateTimeOffset? TryGetDateTimeOffset(IReadOnlyDictionary<string, object?> obj, string key)

Parameters

obj IReadOnlyDictionary<string, object>

The deserialized JSON object

key string

The key for the value

Returns

DateTimeOffset?

TryGetDateTimeOffset(object?)

Tries to get a DateTimeOffset value from a deserialized JSON value.

public static DateTimeOffset? TryGetDateTimeOffset(object? value)

Parameters

value object

The deserialized JSON value

Returns

DateTimeOffset?

TryGetDecimal(IReadOnlyDictionary<string, object?>, string)

Tries to get a decimal value from a deserialized JSON object.

public static decimal? TryGetDecimal(IReadOnlyDictionary<string, object?> obj, string key)

Parameters

obj IReadOnlyDictionary<string, object>

The deserialized JSON object

key string

The key for the value

Returns

decimal?

TryGetDecimal(object?)

Tries to get a decimal value from a deserialized JSON value.

public static decimal? TryGetDecimal(object? value)

Parameters

value object

The deserialized JSON value

Returns

decimal?

TryGetEnum<TEnum>(IReadOnlyDictionary<string, object?>, string)

Tries to get an Enum value from a deserialized JSON object.

public static TEnum? TryGetEnum<TEnum>(IReadOnlyDictionary<string, object?> obj, string key) where TEnum : struct, Enum

Parameters

obj IReadOnlyDictionary<string, object>

The deserialized JSON object

key string

The key for the value

Returns

TEnum?

Type Parameters

TEnum

TryGetEnum<TEnum>(object?)

Tries to get an Enum value from a deserialized JSON value.

public static TEnum? TryGetEnum<TEnum>(object? value) where TEnum : struct, Enum

Parameters

value object

The deserialized JSON value

Returns

TEnum?

Type Parameters

TEnum

TryGetInt(IReadOnlyDictionary<string, object?>, string)

Tries to get an int value from a deserialized JSON object.

public static int? TryGetInt(IReadOnlyDictionary<string, object?> obj, string key)

Parameters

obj IReadOnlyDictionary<string, object>

The deserialized JSON object

key string

The key for the value

Returns

int?

TryGetInt(object?)

Tries to get an int value from a deserialized JSON value.

public static int? TryGetInt(object? value)

Parameters

value object

The deserialized JSON value

Returns

int?

TryGetLong(IReadOnlyDictionary<string, object?>, string)

Tries to get a long value from a deserialized JSON object.

public static long? TryGetLong(IReadOnlyDictionary<string, object?> obj, string key)

Parameters

obj IReadOnlyDictionary<string, object>

The deserialized JSON object

key string

The key for the value

Returns

long?

TryGetLong(object?)

Tries to get an long value from a deserialized JSON value.

public static long? TryGetLong(object? value)

Parameters

value object

The deserialized JSON value

Returns

long?

TryGetObject(IReadOnlyDictionary<string, object?>, string)

Tries to get an untyped object value from a deserialized JSON object.

public static IReadOnlyDictionary<string, object?>? TryGetObject(IReadOnlyDictionary<string, object?> obj, string key)

Parameters

obj IReadOnlyDictionary<string, object>

The deserialized JSON object

key string

The key for the value

Returns

IReadOnlyDictionary<string, object>

TryGetObject(object?)

Tries to get an untyped object value from a deserialized JSON value.

public static IReadOnlyDictionary<string, object?>? TryGetObject(object? value)

Parameters

value object

The deserialized JSON value

Returns

IReadOnlyDictionary<string, object>

TryGetString(IReadOnlyDictionary<string, object?>, string, bool)

Tries to get a string value from a deserialized JSON object.

public static string? TryGetString(IReadOnlyDictionary<string, object?> obj, string key, bool defaultEmptyString = false)

Parameters

obj IReadOnlyDictionary<string, object>

The deserialized JSON object

key string

The key for the value

defaultEmptyString bool

Flag to indicate if a default empty string should be returned instead of null

Returns

string

Returns the value if present; null, otherwise.

TryGetString(object?, bool)

Tries to get an long value from a deserialized JSON value.

public static string? TryGetString(object? value, bool defaultEmptyString = false)

Parameters

value object

The deserialized JSON value

defaultEmptyString bool

Flag to indicate if a default empty string should be returned instead of null

Returns

string

Returns the value if present; null, otherwise.

TryGetTraits(IReadOnlyDictionary<string, object?>, string, bool)

Tries to get a trait dictionary value from a deserialized JSON object.

public static IReadOnlyDictionary<string, IReadOnlyCollection<string>>? TryGetTraits(IReadOnlyDictionary<string, object?> obj, string key, bool defaultEmptyDictionary = true)

Parameters

obj IReadOnlyDictionary<string, object>

The deserialized JSON object

key string

The key for the value

defaultEmptyDictionary bool

Flag to indicate if a default empty dictionary should be returned instead of null

Returns

IReadOnlyDictionary<string, IReadOnlyCollection<string>>

TryGetTraits(object?)

Tries to get a string value from a deserialized JSON value.

public static IReadOnlyDictionary<string, IReadOnlyCollection<string>>? TryGetTraits(object? value)

Parameters

value object

The deserialized JSON value

Returns

IReadOnlyDictionary<string, IReadOnlyCollection<string>>

Returns the value if present; null, otherwise.