OrchardCMS/OrchardCore · error · InvalidCastException

Cannot convert to Byte

Error message

Cannot convert {value} to Byte

What it means

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.

Solutions

  1. Use the explicit byte? cast or GetValue<byte?>() so null yields null instead of throwing.
  2. Parse from the string form when the token is textual: byte.TryParse(value.ToString(), out var b).
  3. Check the JValue type/range before casting (token is integer and fits 0–255).
  4. Catch InvalidCastException around the cast when data shape is not guaranteed.

Example fix

// before
byte level = (byte)field.Value; // throws for "200" (string)
// after
var level = byte.TryParse(field.Value?.ToString(), out var b) ? b : (byte)0;
Defensive patterns

Strategy: type-guard

Validate before calling

bool IsByte(JsonDynamicValue v) =>
    v?._jsonValue != null && v._jsonValue.Type == JTokenType.Integer &&
    v._jsonValue.Value<long>() is >= 0 and <= 255;

Type guard

static bool TryGetByte(JsonDynamicValue v, out byte value)
{
    if (v?._jsonValue?.Type == JTokenType.Integer) { value = v._jsonValue.Value<byte>(); return true; }
    if (byte.TryParse(v?.ToString(), out var b)) { value = b; return true; }
    value = default; return false;
}

Try / catch

byte level;
try { level = (byte)field.Value; }
catch (InvalidCastException) { level = byte.TryParse(field.Value?.ToString(), out var b) ? b : (byte)0; }

Prevention

When it happens

Trigger: Casting (byte)someJsonDynamicValue when the token is a string "5", a double 5.5, a negative number, a value > 255, or null.

Common situations: Numeric values imported as JSON strings; values serialized as double that overflow byte range; dynamic content data with unexpected nulls.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13). Data as JSON: /api/errors/f791ff7ef2f4efde. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs:285

    public static bool operator >(JsonDynamicValue left, JsonDynamicValue right) => !ReferenceEquals(left, null) && left.CompareTo(right) > 0;

    public static bool operator >=(JsonDynamicValue left, JsonDynamicValue right) => ReferenceEquals(left, null) ? ReferenceEquals(right, null) : left.CompareTo(right) >= 0;

    public static explicit operator bool(JsonDynamicValue value)
    {
        return value?._jsonValue?.GetValue<bool>()
            ?? throw new InvalidCastException($"Cannot convert {value} to Boolean");
    }

    public static explicit operator bool?(JsonDynamicValue value)
    {
        return value?._jsonValue?.GetValue<bool?>();
    }

    public static explicit operator byte(JsonDynamicValue value)
    {
        return value?._jsonValue?.GetValue<byte>()
            ?? throw new InvalidCastException($"Cannot convert {value} to Byte");
    }

    public static explicit operator byte?(JsonDynamicValue value)
    {
        return value?._jsonValue?.GetValue<byte?>();
    }

    public static explicit operator char(JsonDynamicValue value)
    {
        return value?._jsonValue?.GetValue<char>()
            ?? throw new InvalidCastException($"Cannot convert {value} to Char");
    }

    public static explicit operator char?(JsonDynamicValue value)
    {
        return value?._jsonValue?.GetValue<char?>();
    }

View on GitHub (pinned to 4306c0717f)