{"record":{"id":"fe11457998183612","repo":"OrchardCMS/OrchardCore","slug":"cannot-convert-value-to-int16","errorCode":null,"errorMessage":"Cannot convert {value} to Int16","messagePattern":"Cannot convert (.+?) to Int16","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs","lineNumber":362,"sourceCode":"    {\n        return value?._jsonValue?.GetValue<double?>();\n    }\n\n    public static explicit operator Guid(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<Guid>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to Guid\");\n    }\n\n    public static explicit operator Guid?(JsonDynamicValue value)\n    {\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","sourceCodeStart":344,"sourceCodeEnd":380,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs#L344-L380","documentation":"JsonDynamicValue wraps a JSON value and exposes explicit conversion operators for primitive types. The explicit operator short casts _jsonValue.GetValue<short>(); if the underlying JSON value is absent, null, or not representable as an Int16, the operator returns null and throws InvalidCastException with 'Cannot convert {value} to Int16'. It signals that the requested numeric conversion is not possible for the wrapped JSON token.","triggerScenarios":"Casting a JsonDynamicValue instance to short with (short)jsonDynamicValue when the wrapped JSON token is null, not a number, or a number outside the Int16 range (-32768..32767), causing GetValue<short>() to yield null.","commonSituations":"Reading JSON payloads where a field is a string like \"42\" instead of a number, casting large numbers (e.g. 70000) to short, casting on a JsonDynamicValue that was constructed from a null/default _jsonValue, or schema changes in serialized data altering a field's type.","solutions":["Cast to short? (explicit operator short?) instead of short, which returns null instead of throwing, then handle the null case.","Validate the JSON value with TryGetValue/GetValue and check the numeric type and range before casting.","Ensure the JSON field is stored as a JSON number within Int16 range; fix the producing side or the schema if it is a string.","Wrap the cast in try/catch for InvalidCastException if the value is genuinely optional."],"exampleFix":"// before\nshort port = (short)jsonValue; // throws when jsonValue is \"8080\" (string) or 70000\n\n// after\nshort? port = (short?)jsonValue;\nif (port 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 Int16 cast.\");\nif (jsonValue.Value<long>() is < short.MinValue or > short.MaxValue)\n    throw new InvalidOperationException($\"Value {jsonValue} is outside Int16 range.\");","typeGuard":"static bool IsConvertibleToInt16(JsonDynamicValue v) =>\n    v?._jsonValue is { Type: JTokenType.Integer } t && t.Value<long>() is >= short.MinValue and <= short.MaxValue;","tryCatchPattern":"try\n{\n    short n = (short)jsonValue;\n}\ncatch (InvalidCastException ex)\n{\n    // log and fall back to a default or skip the record\n    short n = default;\n}","preventionTips":["Prefer the nullable casts (short?) which never throw, and treat null as 'not convertible'.","Check the JToken type and range before casting numeric JSON values.","Keep JSON producers emitting numbers (not quoted numbers) for numeric fields."],"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"}