{"record":{"id":"f791ff7ef2f4efde","repo":"OrchardCMS/OrchardCore","slug":"cannot-convert-value-to-byte","errorCode":null,"errorMessage":"Cannot convert {value} to Byte","messagePattern":"Cannot convert (.+?) to Byte","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs","lineNumber":285,"sourceCode":"    public static bool operator >(JsonDynamicValue left, JsonDynamicValue right) => !ReferenceEquals(left, null) && left.CompareTo(right) > 0;\n\n    public static bool operator >=(JsonDynamicValue left, JsonDynamicValue right) => ReferenceEquals(left, null) ? ReferenceEquals(right, null) : left.CompareTo(right) >= 0;\n\n    public static explicit operator bool(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<bool>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to Boolean\");\n    }\n\n    public static explicit operator bool?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<bool?>();\n    }\n\n    public static explicit operator byte(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<byte>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to Byte\");\n    }\n\n    public static explicit operator byte?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<byte?>();\n    }\n\n    public static explicit operator char(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<char>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to Char\");\n    }\n\n    public static explicit operator char?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<char?>();\n    }\n","sourceCodeStart":267,"sourceCodeEnd":303,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs#L267-L303","documentation":"The explicit (byte) cast operator on JsonDynamicValue calls GetValue<byte>() and throws InvalidCastException when the underlying value is null or not representable as a byte. Like the other primitive casts, it is strict — no numeric coercion across token types.","triggerScenarios":"Casting (byte)someJsonDynamicValue when the token is a string \"5\", a double 5.5, a negative number, a value > 255, or null.","commonSituations":"Numeric values imported as JSON strings; values serialized as double that overflow byte range; dynamic content data with unexpected nulls.","solutions":["Use the explicit byte? cast or GetValue<byte?>() so null yields null instead of throwing.","Parse from the string form when the token is textual: byte.TryParse(value.ToString(), out var b).","Check the JValue type/range before casting (token is integer and fits 0–255).","Catch InvalidCastException around the cast when data shape is not guaranteed."],"exampleFix":"// before\nbyte level = (byte)field.Value; // throws for \"200\" (string)\n// after\nvar level = byte.TryParse(field.Value?.ToString(), out var b) ? b : (byte)0;","handlingStrategy":"type-guard","validationCode":"bool IsByte(JsonDynamicValue v) =>\n    v?._jsonValue != null && v._jsonValue.Type == JTokenType.Integer &&\n    v._jsonValue.Value<long>() is >= 0 and <= 255;","typeGuard":"static bool TryGetByte(JsonDynamicValue v, out byte value)\n{\n    if (v?._jsonValue?.Type == JTokenType.Integer) { value = v._jsonValue.Value<byte>(); return true; }\n    if (byte.TryParse(v?.ToString(), out var b)) { value = b; return true; }\n    value = default; return false;\n}","tryCatchPattern":"byte level;\ntry { level = (byte)field.Value; }\ncatch (InvalidCastException) { level = byte.TryParse(field.Value?.ToString(), out var b) ? b : (byte)0; }","preventionTips":["Use the byte? cast when the value may be null or missing.","Check the token is an integer within 0–255 before casting.","Parse string-encoded numbers explicitly with byte.TryParse."],"tags":["json","dynamic","cast"],"backgroundTag":"type-mismatch","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"}