{"record":{"id":"243c69309f86e813","repo":"JamesNK/Newtonsoft.Json","slug":"can-not-convert-0-to-biginteger","errorCode":null,"errorMessage":"Can not convert {0} to BigInteger.","messagePattern":"Can not convert (.+?) to BigInteger\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JToken.cs","lineNumber":1543,"sourceCode":"            {\n                throw new ArgumentException(\"Can not convert {0} to Uri.\".FormatWith(CultureInfo.InvariantCulture, GetType(value)));\n            }\n\n            if (v.Value == null)\n            {\n                return null;\n            }\n\n            return (v.Value is Uri uri) ? uri : new Uri(Convert.ToString(v.Value, CultureInfo.InvariantCulture)!);\n        }\n\n#if HAVE_BIG_INTEGER\n        private static BigInteger ToBigInteger(JToken value)\n        {\n            JValue? v = EnsureValue(value);\n            if (v == null || !ValidateToken(v, BigIntegerTypes, false))\n            {\n                throw new ArgumentException(\"Can not convert {0} to BigInteger.\".FormatWith(CultureInfo.InvariantCulture, GetType(value)));\n            }\n\n            return ConvertUtils.ToBigInteger(v.Value!);\n        }\n\n        private static BigInteger? ToBigIntegerNullable(JToken value)\n        {\n            JValue? v = EnsureValue(value);\n            if (v == null || !ValidateToken(v, BigIntegerTypes, true))\n            {\n                throw new ArgumentException(\"Can not convert {0} to BigInteger.\".FormatWith(CultureInfo.InvariantCulture, GetType(value)));\n            }\n\n            if (v.Value == null)\n            {\n                return null;\n            }\n","sourceCodeStart":1525,"sourceCodeEnd":1561,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JToken.cs#L1525-L1561","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nBigInteger value = (BigInteger)payload[\"value\"]; // throws on JSON null or GUID\n\n// after\nBigInteger? value = payload[\"value\"]?.Type == JTokenType.String\n    && System.Numerics.BigInteger.TryParse((string)payload[\"value\"]!, out var bi)\n    ? bi : (BigInteger?)null;","handlingStrategy":"validation","validationCode":"BigInteger? value = payload[\"value\"]?.Type == JTokenType.String\n    && System.Numerics.BigInteger.TryParse((string)payload[\"value\"]!, out var bi)\n    ? bi : null;","typeGuard":"static bool IsBigIntegerLike(JToken? t) =>\n    t is JValue v && Array.IndexOf(\n        new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Boolean, JTokenType.Bytes }, v.Type) >= 0;","tryCatchPattern":"BigInteger value;\ntry { value = (BigInteger)payload[\"value\"]; }\ncatch (ArgumentException) { value = BigInteger.Zero; }","preventionTips":["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."],"tags":["json","linq-to-json","type-cast","jtoken","numeric","biginteger","newtonsoft-json"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}