JamesNK/Newtonsoft.Json · error · ArgumentException
Can not convert {0} to BigInteger.
Error message
Can not convert {0} to BigInteger. What it means
Thrown by the private helper ToBigInteger(JToken) (exposed via the BigInteger cast operator, guarded by HAVE_BIG_INTEGER) when the token is not a BigInteger-compatible JValue. ArgumentException {0} is the resolved JTokenType; allowed types are BigIntegerTypes ({Integer, Float, String, Comment, Raw, Boolean, Bytes}) — broader than NumberTypes because Bytes is allowed. Non-nullable helper, so a JSON null token throws.
Source
Thrown at Src/Newtonsoft.Json/Linq/JToken.cs:1543
{
throw new ArgumentException("Can not convert {0} to Uri.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
}
if (v.Value == null)
{
return null;
}
return (v.Value is Uri uri) ? uri : new Uri(Convert.ToString(v.Value, CultureInfo.InvariantCulture)!);
}
#if HAVE_BIG_INTEGER
private static BigInteger ToBigInteger(JToken value)
{
JValue? v = EnsureValue(value);
if (v == null || !ValidateToken(v, BigIntegerTypes, false))
{
throw new ArgumentException("Can not convert {0} to BigInteger.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
}
return ConvertUtils.ToBigInteger(v.Value!);
}
private static BigInteger? ToBigIntegerNullable(JToken value)
{
JValue? v = EnsureValue(value);
if (v == null || !ValidateToken(v, BigIntegerTypes, true))
{
throw new ArgumentException("Can not convert {0} to BigInteger.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
}
if (v.Value == null)
{
return null;
}
View on GitHub (pinned to 4f73e74372)
Solutions
- Guard token["value"]?.Type is in {Integer, Float, String, Boolean, Bytes} before casting.
- Use the nullable BigInteger? cast (ToBigIntegerNullable) so JSON null returns null instead of throwing.
- Validate the string parses as an integer with BigInteger.TryParse before casting.
- If the field became a GUID/date, change the consuming type rather than forcing a BigInteger cast.
Example fix
// before
BigInteger value = (BigInteger)payload["value"]; // throws on JSON null or GUID
// after
BigInteger? value = payload["value"]?.Type == JTokenType.String
&& System.Numerics.BigInteger.TryParse((string)payload["value"]!, out var bi)
? bi : (BigInteger?)null; Defensive patterns
Strategy: validation
Validate before calling
BigInteger? value = payload["value"]?.Type == JTokenType.String
&& System.Numerics.BigInteger.TryParse((string)payload["value"]!, out var bi)
? bi : null; Type guard
static bool IsBigIntegerLike(JToken? t) =>
t is JValue v && Array.IndexOf(
new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Boolean, JTokenType.Bytes }, v.Type) >= 0; Try / catch
BigInteger value;
try { value = (BigInteger)payload["value"]; }
catch (ArgumentException) { value = BigInteger.Zero; } Prevention
- Use the nullable BigInteger? cast (ToBigIntegerNullable) for null-tolerant reads.
- BigInteger.TryParse string nodes to also catch non-numeric strings.
- BigIntegerTypes includes Bytes — but Date/Guid/Uri/Object tokens still throw.
When it happens
Trigger: Calling (BigInteger)token["value"] when the JValue Type is Date, Guid, Uri, TimeSpan, Null, Object, or Array, or the node is a JObject/JArray (EnsureValue null). A non-numeric string passes ValidateToken then throws FormatException from ConvertUtils.ToBigInteger, not this error.
Common situations: Large-integer field occasionally emitted as a date, GUID, or object; null big-integer in some records; schema drift from a numeric to a GUID/UUID string.
Related errors
- Can not convert {0} to Single.
- Can not convert {0} to UInt32.
- Can not convert {0} to UInt64.
- Can not convert {0} to String.
- Can not convert {0} to byte array.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/243c69309f86e813.
Report an issue: GitHub.