{"record":{"id":"ca5dce6106af89f3","repo":"OrchardCMS/OrchardCore","slug":"cannot-convert-value-to-int32","errorCode":null,"errorMessage":"Cannot convert {value} to Int32","messagePattern":"Cannot convert (.+?) to Int32","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs","lineNumber":373,"sourceCode":"    {\n        return value?._jsonValue?.GetValue<Guid?>();\n    }\n\n    public static explicit operator short(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<short>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to Int16\");\n    }\n\n    public static explicit operator short?(JsonDynamicValue value)\n    {\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","sourceCodeStart":355,"sourceCodeEnd":391,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs#L355-L391","documentation":"JsonDynamicValue wraps a JSON value and exposes explicit conversion operators for primitive types. The explicit operator int casts _jsonValue.GetValue<int>(); if the underlying JSON value is absent, null, or not representable as an Int32, the operator returns null and throws InvalidCastException with 'Cannot convert {value} to Int32'. It signals that the requested numeric conversion is not possible for the wrapped JSON token.","triggerScenarios":"Casting a JsonDynamicValue instance to int with (int)jsonDynamicValue when the wrapped JSON token is null, not a number (e.g. a JSON string or object), or a number outside the Int32 range, causing GetValue<int>() to yield null.","commonSituations":"Reading a JSON id or count field that is a string (\"123\"), casting a fractional or very large number that Json.NET's GetValue<int> rejects, casting on a JsonDynamicValue built from a null token, or upstream data changing from number to string.","solutions":["Cast to int? (explicit operator int?) instead of int, 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 within Int32 range before casting.","Ensure the JSON field is stored as a JSON number; fix the producer or deserialize the string via int.Parse when it is intentionally a string.","Wrap the cast in try/catch for InvalidCastException if the value is genuinely optional."],"exampleFix":"// before\nint count = (int)jsonValue; // throws when jsonValue is \"123\" (string)\n\n// after\nint? count = (int?)jsonValue;\nif (count is null) { /* handle missing/invalid value */ }","handlingStrategy":"type-guard","validationCode":"if (jsonValue is null || jsonValue.Type != JTokenType.Integer)\n    throw new InvalidOperationException(\"Expected an integer JSON value for Int32 cast.\");","typeGuard":"static bool IsConvertibleToInt32(JsonDynamicValue v) =>\n    v?._jsonValue is { Type: JTokenType.Integer } t && t.Value<long>() is >= int.MinValue and <= int.MaxValue;","tryCatchPattern":"try\n{\n    int n = (int)jsonValue;\n}\ncatch (InvalidCastException ex)\n{\n    // log and fall back to a default or skip the record\n    int n = default;\n}","preventionTips":["Prefer the nullable cast (int?) which returns null instead of throwing.","Confirm the JSON token is JTokenType.Integer before casting.","Watch for upstream schema changes that turn numeric fields into strings."],"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"}