{"record":{"id":"6fce3f885e39b214","repo":"JamesNK/Newtonsoft.Json","slug":"can-not-convert-0-to-uint64","errorCode":null,"errorMessage":"Can not convert {0} to UInt64.","messagePattern":"Can not convert (.+?) to UInt64\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JToken.cs","lineNumber":1235,"sourceCode":"        }\n\n        /// <summary>\n        /// Performs an explicit conversion from <see cref=\"JToken\"/> to <see cref=\"Nullable{T}\"/> of <see cref=\"UInt64\"/>.\n        /// </summary>\n        /// <param name=\"value\">The value.</param>\n        /// <returns>The result of the conversion.</returns>\n        [CLSCompliant(false)]\n        public static explicit operator ulong?(JToken? value)\n        {\n            if (value == null)\n            {\n                return null;\n            }\n\n            JValue? v = EnsureValue(value);\n            if (v == null || !ValidateToken(v, NumberTypes, true))\n            {\n                throw new ArgumentException(\"Can not convert {0} to UInt64.\".FormatWith(CultureInfo.InvariantCulture, GetType(value)));\n            }\n\n#if HAVE_BIG_INTEGER\n            if (v.Value is BigInteger integer)\n            {\n                return (ulong?)integer;\n            }\n#endif\n\n            return (v.Value != null) ? (ulong?)Convert.ToUInt64(v.Value, CultureInfo.InvariantCulture) : null;\n        }\n\n        /// <summary>\n        /// Performs an explicit conversion from <see cref=\"JToken\"/> to <see cref=\"Double\"/>.\n        /// </summary>\n        /// <param name=\"value\">The value.</param>\n        /// <returns>The result of the conversion.</returns>\n        public static explicit operator double(JToken value)","sourceCodeStart":1217,"sourceCodeEnd":1253,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JToken.cs#L1217-L1253","documentation":"Thrown by the explicit cast operator 'operator ulong?(JToken?)' (Nullable<UInt64>) when the token is not resolvable to a numeric JValue. ArgumentException {0} is the resolved JTokenType; allowed types are NumberTypes ({Integer, Float, String, Comment, Raw, Boolean}). Nullable overload: null/JSON-null return null. Throw indicates a non-numeric shape (Date, Guid, Object, Array, etc.).","triggerScenarios":"Calling (ulong?)token[\"snowflakeId\"] where the resolved JTokenType is Date, Guid, TimeSpan, Uri, Bytes, Object, or Array, or the node is a JObject/JArray (EnsureValue null). A negative number passes ValidateToken but throws OverflowException, not this error.","commonSituations":"Snowflake/64-bit id field migrated to a GUID/string; id node replaced by a nested object; unsigned id cast against a payload whose schema flipped to signed/negative ids.","solutions":["Check token[\"snowflakeId\"]?.Type is Integer/Float/String/Boolean before the cast.","Prefer token[\"snowflakeId\"]?.Value<ulong>() to avoid the throw on shape mismatch.","If ids may be negative, switch to long? instead of ulong?.","If ids became GUIDs, change the consuming type to Guid? at the boundary."],"exampleFix":"// before\nulong? id = (ulong?)payload[\"snowflakeId\"]; // throws when id is a Guid\n\n// after\nulong? id = payload[\"snowflakeId\"]?.Value<ulong>();","handlingStrategy":"validation","validationCode":"ulong? id = payload[\"snowflakeId\"]?.Value<ulong>();","typeGuard":"static bool IsUnsignedNumeric(JToken? t) =>\n    t is JValue v && (v.Type == JTokenType.Integer || v.Type == JTokenType.Float);","tryCatchPattern":"ulong? id;\ntry { id = (ulong?)payload[\"snowflakeId\"]; }\ncatch (ArgumentException) { id = null; }","preventionTips":["Use long? if ids may be negative.","Switch to Guid? if the id format migrated to UUIDs.","Prefer Value<ulong>() over the explicit cast for untrusted payloads."],"tags":["json","linq-to-json","type-cast","jtoken","numeric","unsigned","newtonsoft-json"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}