{"record":{"id":"8f4bde72cbf82288","repo":"OrchardCMS/OrchardCore","slug":"cannot-convert-value-to-uint64","errorCode":null,"errorMessage":"Cannot convert {value} to UInt64","messagePattern":"Cannot convert (.+?) to UInt64","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs","lineNumber":444,"sourceCode":"    {\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\n    public static explicit operator ulong(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<ulong>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to UInt64\");\n    }\n\n    public static explicit operator ulong?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<ulong?>();\n    }\n\n    public static explicit operator byte[]?(JsonDynamicValue value)\n    {\n        if (value?._jsonValue.GetObjectValue() is string str)\n        {\n            return Convert.FromBase64String(str);\n        }\n\n        throw new InvalidCastException($\"Cannot convert {value} to Byte array\");\n    }\n\n    public static explicit operator TimeSpan(JsonDynamicValue value)","sourceCodeStart":426,"sourceCodeEnd":462,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs#L426-L462","documentation":"This InvalidCastException is thrown by the explicit ulong conversion operator on JsonDynamicValue when the wrapped JSON value is null or cannot be read as a ulong. GetValue<ulong>() effectively returns null when the underlying JsonElement is not a number, triggering the ?? throw. The message correctly names the target type UInt64.","triggerScenarios":"Casting a JsonDynamicValue with (ulong) when the wrapped JSON element is null, a string, an object/array, or a number outside ulong range (e.g. 64-bit ids serialized as strings).","commonSituations":"Snowflake/timestamp-style ids that arrive as JSON strings; null fields from partial API responses; casting heterogeneous JSON array elements.","solutions":["Cast to ulong? instead — that operator returns null instead of throwing","Check GetObjectValue() for null and numeric kind before casting","Parse with ulong.TryParse on the string representation when ids are strings","Validate the numeric range before the cast"],"exampleFix":"// before\nvar id = (ulong)dynamicValue;\n// after\nvar id = (ulong?)dynamicValue ?? throw new InvalidOperationException(\"Expected numeric id\");","handlingStrategy":"type-guard","validationCode":"var obj = value?.GetObjectValue();\nif (obj is JsonElement el && el.ValueKind == JsonValueKind.Number && el.TryGetUInt64(out _)) { /* safe */ }\nelse if (obj is string s && ulong.TryParse(s, out _)) { /* string-encoded id, parse manually */ }","typeGuard":"static bool IsUlongLike(JsonDynamicValue? v) =>\n    v?.GetObjectValue() is JsonElement el && el.ValueKind == JsonValueKind.Number && el.TryGetUInt64(out _);","tryCatchPattern":"ulong id;\ntry { id = (ulong)dynamicValue; }\ncatch (InvalidCastException ex) { /* log, fallback */ id = 0UL; }","preventionTips":["Prefer the ulong? cast operator which returns null instead of throwing","Treat 64-bit ids serialized as strings separately (ulong.TryParse)","Check for JSON null before casting fields from partial responses","Check ValueKind == Number before casting"],"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-16T04:17:20.429Z"}