{"record":{"id":"b4d7db4cf6c268c9","repo":"OrchardCMS/OrchardCore","slug":"cannot-convert-value-to-int64","errorCode":null,"errorMessage":"Cannot convert {value} to Int64","messagePattern":"Cannot convert (.+?) to Int64","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs","lineNumber":384,"sourceCode":"    {\n        return value?._jsonValue?.GetValue<short?>();\n    }\n\n    public static explicit operator int(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<int>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to Int32\");\n    }\n\n    public static explicit operator int?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<int?>();\n    }\n\n    public static explicit operator long(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<long>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to Int64\");\n    }\n\n    public static explicit operator long?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<long?>();\n    }\n\n    public static explicit operator sbyte(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<sbyte>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to SByte\");\n    }\n\n    public static explicit operator sbyte?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<sbyte?>();\n    }\n","sourceCodeStart":366,"sourceCodeEnd":402,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs#L366-L402","documentation":"JsonDynamicValue wraps a JSON value and exposes explicit conversion operators for primitive types. The explicit operator long casts _jsonValue.GetValue<long>(); if the underlying JSON value is absent, null, or not representable as an Int64, the operator returns null and throws InvalidCastException with 'Cannot convert {value} to Int64'. It signals that the requested numeric conversion is not possible for the wrapped JSON token.","triggerScenarios":"Casting a JsonDynamicValue instance to long with (long)jsonDynamicValue when the wrapped JSON token is null, not a number, or a non-integral/overflowing number (e.g. 3.14 or a value beyond Int64), causing GetValue<long>() to yield null.","commonSituations":"Reading timestamps or large ids stored as fractional numbers or numeric strings, casting on a JsonDynamicValue constructed from a null/default _jsonValue, or JSON produced by another language serializing longs as strings.","solutions":["Cast to long? (explicit operator long?) instead of long, which returns null instead of throwing, then handle the null case.","Validate the JSON value with TryGetValue/GetValue and confirm it is an integral number before casting.","Ensure the JSON field is a JSON number; if it is a string, parse it explicitly (long.TryParse) instead of casting.","Wrap the cast in try/catch for InvalidCastException if the value is genuinely optional."],"exampleFix":"// before\nlong id = (long)jsonValue; // throws when jsonValue is 3.14 or \"9007199254740993\"\n\n// after\nlong? id = (long?)jsonValue;\nif (id is null) { /* handle missing/invalid value */ }","handlingStrategy":"type-guard","validationCode":"if (jsonValue is null || (jsonValue.Type != JTokenType.Integer && jsonValue.Type != JTokenType.Float))\n    throw new InvalidOperationException(\"Expected a numeric JSON value for Int64 cast.\");","typeGuard":"static bool IsConvertibleToInt64(JsonDynamicValue v) =>\n    v?._jsonValue is { Type: JTokenType.Integer };","tryCatchPattern":"try\n{\n    long n = (long)jsonValue;\n}\ncatch (InvalidCastException ex)\n{\n    // log and fall back to a default or skip the record\n    long n = default;\n}","preventionTips":["Use the nullable cast (long?) so missing/non-numeric values yield null instead of throwing.","Reject fractional or string-encoded numbers at the deserialization boundary.","Check the JToken type with TryGetValue before casting large ids or timestamps."],"tags":["invalid-cast","json","numeric-conversion","exception"],"backgroundTag":"type-mismatch","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"}