{"record":{"id":"6e7daca42e701814","repo":"OrchardCMS/OrchardCore","slug":"cannot-convert-value-to-byte-array","errorCode":null,"errorMessage":"Cannot convert {value} to Byte array","messagePattern":"Cannot convert (.+?) to Byte array","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs","lineNumber":459,"sourceCode":"    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)\n    {\n        if (value?._jsonValue?.GetObjectValue() is string str)\n        {\n            return TimeSpan.Parse(str, CultureInfo.InvariantCulture);\n        }\n\n        throw new InvalidCastException($\"Cannot convert {value} to TimeSpan\");\n    }\n\n    public static explicit operator TimeSpan?(JsonDynamicValue value)\n    {\n        var str = value?._jsonValue?.GetObjectValue() as string;\n\n        return str is not null\n            ? TimeSpan.Parse(str, CultureInfo.InvariantCulture)","sourceCodeStart":441,"sourceCodeEnd":477,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs#L441-L477","documentation":"This InvalidCastException is thrown by the explicit byte[] conversion operator on JsonDynamicValue when the wrapped JSON value is not a string, because base64 decoding requires the underlying GetObjectValue() to yield a string. JSON has no native byte[] type, so the library only accepts a base64-encoded string.","triggerScenarios":"Casting a JsonDynamicValue with (byte[]) when the wrapped value is a number, boolean, object, array, or null rather than a base64 string.","commonSituations":"Binary data fields that a producer serialized as a plain array of numbers instead of a base64 string; null fields; non-base64 strings will also fail inside Convert.FromBase64String.","solutions":["Ensure the JSON field is a base64-encoded string before casting","Get the value as string first and use Convert.FromBase64String inside your own try/catch or TryFromBase64String","Validate with GetObjectValue() is string before the cast","Fix the producer to serialize binary as base64 string"],"exampleFix":"// before\nvar bytes = (byte[])dynamicValue;\n// after\nif (dynamicValue?.GetObjectValue() is string s && Convert.TryFromBase64String(s, new byte[s.Length * 3 / 4], out _))\n{\n    var bytes = Convert.FromBase64String(s);\n}","handlingStrategy":"type-guard","validationCode":"if (value?.GetObjectValue() is not string b64 || !Convert.TryFromBase64String(b64, new byte[b64.Length * 3 / 4], out _))\n    throw new InvalidOperationException(\"JSON value is not a base64 string\");","typeGuard":"static bool IsBase64String(JsonDynamicValue? v) =>\n    v?.GetObjectValue() is string s && s.Length % 4 == 0 && Convert.TryFromBase64String(s, new byte[s.Length * 3 / 4], out _);","tryCatchPattern":"byte[] bytes;\ntry { bytes = (byte[])dynamicValue; }\ncatch (InvalidCastException ex) { /* log, fallback */ bytes = Array.Empty<byte>(); }","preventionTips":["Ensure producers serialize binary data as base64 strings, not numeric arrays","Reject or convert numeric-array binary fields before casting","Validate base64 format (length, charset) before casting","Handle JSON null explicitly"],"tags":["invalid-cast","json","base64","binary-data"],"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"}