JamesNK/Newtonsoft.Json · error · InvalidCastException

Cannot convert {0} to BigInteger.

Error message

Cannot convert {0} to BigInteger.

What it means

ConvertUtils.ToBigInteger (ConvertUtils.cs:288-335) converts a value to BigInteger. It explicitly handles BigInteger, string, float, double, decimal, int, long, uint, ulong, and byte[]. Any other runtime type (char, bool, short, byte-as-object, DateTime, custom object) falls through to the final throw of InvalidCastException at ConvertUtils.cs:334.

Source

Thrown at Src/Newtonsoft.Json/Utilities/ConvertUtils.cs:334

            if (value is long l)
            {
                return new BigInteger(l);
            }
            if (value is uint u)
            {
                return new BigInteger(u);
            }
            if (value is ulong @ulong)
            {
                return new BigInteger(@ulong);
            }

            if (value is byte[] bytes)
            {
                return new BigInteger(bytes);
            }

            throw new InvalidCastException("Cannot convert {0} to BigInteger.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
        }

        public static object FromBigInteger(BigInteger i, Type targetType)
        {
            if (targetType == typeof(decimal))
            {
                return (decimal)i;
            }
            if (targetType == typeof(double))
            {
                return (double)i;
            }
            if (targetType == typeof(float))
            {
                return (float)i;
            }
            if (targetType == typeof(ulong))
            {

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Ensure the source JSON value is a number or a numeric string so it lands as a supported type.
  2. Write a custom JsonConverter for BigInteger that normalizes the input (e.g. Convert.ToInt64 then ToBigInteger).
  3. If the source is a smaller integer type, convert it to long/string before it reaches ToBigInteger.
  4. Avoid mapping bool/char/DateTime fields to BigInteger.

Example fix

// before: value is a bool/char/short -> falls through -> throws
// after: pre-convert to a supported type
BigInteger b = ConvertUtils.ToBigInteger(Convert.ToInt64(value));
Defensive patterns

Strategy: type-guard

Validate before calling

// Pre-convert uncommon numeric types to one ToBigInteger accepts before calling it.
static BigInteger SafeToBigInteger(object value) {
    if (value is bool b) return b ? 1 : 0;
    if (value is char c) return (long)c;
    if (value is sbyte sb) return (long)sb;
    if (value is byte bt) return (long)bt;
    if (value is short s) return (long)s;
    if (value is ushort us) return (long)us;
    return ConvertUtils.ToBigInteger(value); // handles int,long,uint,ulong,float,double,decimal,string,byte[],BigInteger
}

Type guard

static bool CanToBigInteger(object v) => v is BigInteger || v is string || v is float || v is double || v is decimal || v is int || v is long || v is uint || v is ulong || v is byte[];

Try / catch

try { return ConvertUtils.ToBigInteger(value); } catch (InvalidCastException) { return SafeToBigInteger(value); }

Prevention

When it happens

Trigger: A BigInteger target property receives a value whose runtime type is not in the supported set — e.g. a bool, char, short, DateTime, or a custom convertible object passed by a converter or by Convert().

Common situations: BigInteger property fed by a bool/char/short; a custom TypeConverter or JsonConverter returning an unusual type; cross-framework value boxing producing unexpected runtime types.

Related errors


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/52e6f11571f243b2. Report an issue: GitHub.