{"record":{"id":"ba0c0f9bb1bb2f98","repo":"JamesNK/Newtonsoft.Json","slug":"can-not-convert-from-biginteger-to-0","errorCode":null,"errorMessage":"Can not convert from BigInteger to {0}.","messagePattern":"Can not convert from BigInteger to (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Utilities/ConvertUtils.cs","lineNumber":366,"sourceCode":"            {\n                return (float)i;\n            }\n            if (targetType == typeof(ulong))\n            {\n                return (ulong)i;\n            }\n            if (targetType == typeof(bool))\n            {\n                return i != 0;\n            }\n\n            try\n            {\n                return System.Convert.ChangeType((long)i, targetType, CultureInfo.InvariantCulture);\n            }\n            catch (Exception ex)\n            {\n                throw new InvalidOperationException(\"Can not convert from BigInteger to {0}.\".FormatWith(CultureInfo.InvariantCulture, targetType), ex);\n            }\n        }\n#endif\n\n#region TryConvert\n        internal enum ConvertResult\n        {\n            Success = 0,\n            CannotConvertNull = 1,\n            NotInstantiableType = 2,\n            NoValidConversion = 3\n        }\n\n        [RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]\n        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]\n        public static object Convert(object initialValue, CultureInfo culture, Type targetType)\n        {\n            switch (TryConvertInternal(initialValue, culture, targetType, out object? value))","sourceCodeStart":348,"sourceCodeEnd":384,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs#L348-L384","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before: BigInteger -> string throws inside Convert.ChangeType\n// after: custom converter\npublic override void WriteJson(JsonWriter w, object v, JsonSerializer s)\n    => w.WriteValue(((BigInteger)v).ToString(CultureInfo.InvariantCulture));","handlingStrategy":"type-guard","validationCode":"// Route BigInteger to a supported target type; convert others explicitly.\nstatic object SafeFromBigInteger(BigInteger i, Type target) {\n    if (target == typeof(string)) return i.ToString(System.Globalization.CultureInfo.InvariantCulture);\n    if (target == typeof(Guid)) return Guid.Empty; // or parse from string\n    if (target == typeof(DateTime)) throw new InvalidOperationException(\"Map via string manually.\");\n    return ConvertUtils.FromBigInteger(i, target); // decimal/double/float/ulong/bool/long-convertible\n}","typeGuard":"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);","tryCatchPattern":"try { return ConvertUtils.FromBigInteger(i, target); } catch (InvalidOperationException) { /* handle string/guid/datetime explicitly */ }","preventionTips":["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."],"tags":["conversion","biginteger","newtonsoft-json"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}