JamesNK/Newtonsoft.Json · error · InvalidOperationException
Can not convert from BigInteger to {0}.
Error message
Can not convert from BigInteger to {0}. What it means
ConvertUtils.FromBigInteger (ConvertUtils.cs:337-368) converts a BigInteger into a target type. It handles decimal, double, float, ulong, and bool directly, then falls back to System.Convert.ChangeType((long)i, targetType). If that throws (target isn't convertible from long — e.g. string, Guid, DateTime — or the value overflows long), the exception is wrapped and rethrown as InvalidOperationException at ConvertUtils.cs:366.
Source
Thrown at Src/Newtonsoft.Json/Utilities/ConvertUtils.cs:366
{
return (float)i;
}
if (targetType == typeof(ulong))
{
return (ulong)i;
}
if (targetType == typeof(bool))
{
return i != 0;
}
try
{
return System.Convert.ChangeType((long)i, targetType, CultureInfo.InvariantCulture);
}
catch (Exception ex)
{
throw new InvalidOperationException("Can not convert from BigInteger to {0}.".FormatWith(CultureInfo.InvariantCulture, targetType), ex);
}
}
#endif
#region TryConvert
internal enum ConvertResult
{
Success = 0,
CannotConvertNull = 1,
NotInstantiableType = 2,
NoValidConversion = 3
}
[RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]
[RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
public static object Convert(object initialValue, CultureInfo culture, Type targetType)
{
switch (TryConvertInternal(initialValue, culture, targetType, out object? value))View on GitHub (pinned to 4f73e74372)
Solutions
- Map the BigInteger to one of the directly supported targets: decimal, double, float, ulong, bool, or a long-convertible type (int/long/short).
- For string output, add a custom JsonConverter that returns bi.ToString(CultureInfo.InvariantCulture).
- For Guid/DateTime targets, convert via an intermediate string and parse explicitly.
- Guard against overflow: check bi against target min/max before conversion.
Example fix
// before: BigInteger -> string throws inside Convert.ChangeType
// after: custom converter
public override void WriteJson(JsonWriter w, object v, JsonSerializer s)
=> w.WriteValue(((BigInteger)v).ToString(CultureInfo.InvariantCulture)); Defensive patterns
Strategy: type-guard
Validate before calling
// Route BigInteger to a supported target type; convert others explicitly.
static object SafeFromBigInteger(BigInteger i, Type target) {
if (target == typeof(string)) return i.ToString(System.Globalization.CultureInfo.InvariantCulture);
if (target == typeof(Guid)) return Guid.Empty; // or parse from string
if (target == typeof(DateTime)) throw new InvalidOperationException("Map via string manually.");
return ConvertUtils.FromBigInteger(i, target); // decimal/double/float/ulong/bool/long-convertible
} Type guard
static bool CanFromBigInteger(Type t) => t == typeof(decimal) || t == typeof(double) || t == typeof(float) || t == typeof(ulong) || t == typeof(bool) || t == typeof(long) || t == typeof(int) || t == typeof(short) || t == typeof(byte) || t == typeof(string);
Try / catch
try { return ConvertUtils.FromBigInteger(i, target); } catch (InvalidOperationException) { /* handle string/guid/datetime explicitly */ } Prevention
- Map BigInteger targets only to long-convertible or directly-supported types.
- Add a custom JsonConverter that handles BigInteger -> string/Guid/DateTime.
- Guard overflow: check i against target min/max before narrowing.
When it happens
Trigger: Deserializing a large integer into a target type that cannot be produced from a long: string, Guid, DateTime, TimeSpan, Uri, or a custom type with no long conversion; or a BigInteger whose magnitude exceeds long range being narrowed.
Common situations: BigInteger values mapped to string/DateTime/Guid properties; overflow when a very large BigInteger is narrowed into int/long; custom types without a long-based TypeConverter.
Related errors
- Cannot convert {0} to BigInteger.
- Can not convert {0} to BigInteger.
- Can not convert null {0} into non-nullable {1}.
- Target type {0} is not a value type or a non-abstract class.
- Can not convert from {0} to {1}.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/ba0c0f9bb1bb2f98.
Report an issue: GitHub.