{"record":{"id":"5610075841fe4405","repo":"OrchardCMS/OrchardCore","slug":"cannot-convert-value-to-uint32","errorCode":null,"errorMessage":"Cannot convert {value} to UInt32","messagePattern":"Cannot convert (.+?) to UInt32","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs","lineNumber":422,"sourceCode":"    {\n        return value?._jsonValue?.GetValue<float>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to Float\");\n    }\n\n    public static explicit operator float?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<float?>();\n    }\n\n    public static explicit operator string?(JsonDynamicValue value)\n    {\n        return value?.ToString();\n    }\n\n    public static explicit operator ushort(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<ushort>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to UInt32\");\n    }\n\n    public static explicit operator ushort?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<ushort?>();\n    }\n\n    public static explicit operator uint(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<uint>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to UInt32\");\n    }\n\n    public static explicit operator uint?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<uint?>();\n    }\n","sourceCodeStart":404,"sourceCodeEnd":440,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs#L404-L440","documentation":"This InvalidCastException is thrown by the explicit ushort conversion operator on JsonDynamicValue when the wrapped JSON value is null or cannot be read as a ushort. Note the message text says 'UInt32' even though the target type is UInt16 — a copy-paste inaccuracy in the library. The cast operator uses GetValue<ushort>() which fails when the underlying JSON element is not a numeric value in ushort range.","triggerScenarios":"Casting a JsonDynamicValue with (ushort) when the wrapped JSON element is null, a string, an object/array, or a number outside 0-65535.","commonSituations":"Dynamic JSON data from an API where a field expected to be numeric arrives as a string or null; deserializing config-like JSON where an id/flag field is absent; iterating mixed-type JSON arrays and casting each element.","solutions":["Check the wrapped value with GetObjectValue() (type and null) before casting","Cast to ushort? instead — that operator returns null instead of throwing","Validate the value is a whole number within 0-65535 first","Use Convert.ToUInt16 on a string representation after a null check"],"exampleFix":"// before\nvar n = (ushort)dynamicValue;\n// after\nvar obj = dynamicValue?.GetObjectValue();\nif (obj is JsonElement el && el.ValueKind == JsonValueKind.Number && el.TryGetUInt16(out var n)) { /* use n */ }","handlingStrategy":"type-guard","validationCode":"if (value is null) throw new InvalidOperationException(\"Expected a numeric JSON value\");\nvar obj = value.GetObjectValue();\nif (obj is not JsonElement el || el.ValueKind != JsonValueKind.Number || !el.TryGetUInt16(out _))\n    throw new InvalidOperationException(\"JSON value is not a ushort-compatible number\");","typeGuard":"static bool IsUshortLike(JsonDynamicValue? v) =>\n    v?.GetObjectValue() is JsonElement el && el.ValueKind == JsonValueKind.Number && el.TryGetUInt16(out _);","tryCatchPattern":"ushort n;\ntry { n = (ushort)dynamicValue; }\ncatch (InvalidCastException ex) { /* log ex, use fallback */ n = 0; }","preventionTips":["Prefer the ushort? cast operator which returns null instead of throwing","Inspect GetObjectValue() type before any explicit cast","Validate numeric ranges (0-65535) when data comes from external APIs","Never cast heterogeneous JSON arrays element-wise without kind checks"],"tags":["invalid-cast","json","dynamic","numeric-conversion"],"backgroundTag":"incompatible-source-type","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}