JsonValueExtensionsGetEnumOrDefaultTEnum(JsonValue, Boolean, TEnum, String, JsonValueType) Method

Gets the specified JsonValue as TEnum if expectedType is Undefined or matches the Type property of the specified json parameter and it can be converted to TEnum; otherwise, returns defaultValue.

Definition

Namespace: KGySoft.Json
Assembly: KGySoft.Json (in KGySoft.Json.dll) Version: 3.0.0
C#
public static TEnum GetEnumOrDefault<TEnum>(
	this JsonValue json,
	bool ignoreFormat,
	TEnum defaultValue = null,
	string? flagsSeparator = null,
	JsonValueType expectedType = JsonValueType.Undefined
)
where TEnum : struct, new()

Parameters

json  JsonValue
The JsonValue to be converted to TEnum.
ignoreFormat  Boolean
to remove underscores or hyphens, and ignore case when parsing the value; otherwise, .
defaultValue  TEnum  (Optional)
The value to be returned if the conversion fails. This parameter is optional.
Default value: .
flagsSeparator  String  (Optional)
Specifies the separator character if the json value consists of multiple flags. This parameter is optional.
Default value: , which uses the default "," separator.
expectedType  JsonValueType  (Optional)
The expected Type of the specified json parameter, or Undefined to allow any type. This parameter is optional.
Default value: Undefined.

Type Parameters

TEnum
The type of the enumeration. Must be an Enum type.

Return Value

TEnum
A TEnum value if json could be converted; otherwise, defaultValue.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type JsonValue. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

See Also