{"record":{"id":"52e6f11571f243b2","repo":"JamesNK/Newtonsoft.Json","slug":"cannot-convert-0-to-biginteger","errorCode":null,"errorMessage":"Cannot convert {0} to BigInteger.","messagePattern":"Cannot convert (.+?) to BigInteger\\.","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Utilities/ConvertUtils.cs","lineNumber":334,"sourceCode":"            if (value is long l)\n            {\n                return new BigInteger(l);\n            }\n            if (value is uint u)\n            {\n                return new BigInteger(u);\n            }\n            if (value is ulong @ulong)\n            {\n                return new BigInteger(@ulong);\n            }\n\n            if (value is byte[] bytes)\n            {\n                return new BigInteger(bytes);\n            }\n\n            throw new InvalidCastException(\"Cannot convert {0} to BigInteger.\".FormatWith(CultureInfo.InvariantCulture, value.GetType()));\n        }\n\n        public static object FromBigInteger(BigInteger i, Type targetType)\n        {\n            if (targetType == typeof(decimal))\n            {\n                return (decimal)i;\n            }\n            if (targetType == typeof(double))\n            {\n                return (double)i;\n            }\n            if (targetType == typeof(float))\n            {\n                return (float)i;\n            }\n            if (targetType == typeof(ulong))\n            {","sourceCodeStart":316,"sourceCodeEnd":352,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs#L316-L352","documentation":"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.","triggerScenarios":"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().","commonSituations":"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.","solutions":["Ensure the source JSON value is a number or a numeric string so it lands as a supported type.","Write a custom JsonConverter for BigInteger that normalizes the input (e.g. Convert.ToInt64 then ToBigInteger).","If the source is a smaller integer type, convert it to long/string before it reaches ToBigInteger.","Avoid mapping bool/char/DateTime fields to BigInteger."],"exampleFix":"// before: value is a bool/char/short -> falls through -> throws\n// after: pre-convert to a supported type\nBigInteger b = ConvertUtils.ToBigInteger(Convert.ToInt64(value));","handlingStrategy":"type-guard","validationCode":"// Pre-convert uncommon numeric types to one ToBigInteger accepts before calling it.\nstatic BigInteger SafeToBigInteger(object value) {\n    if (value is bool b) return b ? 1 : 0;\n    if (value is char c) return (long)c;\n    if (value is sbyte sb) return (long)sb;\n    if (value is byte bt) return (long)bt;\n    if (value is short s) return (long)s;\n    if (value is ushort us) return (long)us;\n    return ConvertUtils.ToBigInteger(value); // handles int,long,uint,ulong,float,double,decimal,string,byte[],BigInteger\n}","typeGuard":"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[];","tryCatchPattern":"try { return ConvertUtils.ToBigInteger(value); } catch (InvalidCastException) { return SafeToBigInteger(value); }","preventionTips":["Keep BigInteger sources as numbers or numeric strings.","Register a JsonConverter for BigInteger that normalizes bool/char/short inputs.","Avoid mapping non-numeric fields (bool, char, DateTime) to BigInteger."],"tags":["conversion","biginteger","type-mismatch","newtonsoft-json"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}