{"record":{"id":"2a0d0b059d83ed0d","repo":"JamesNK/Newtonsoft.Json","slug":"can-not-convert-0-to-uint32","errorCode":null,"errorMessage":"Can not convert {0} to UInt32.","messagePattern":"Can not convert (.+?) to UInt32\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JToken.cs","lineNumber":1206,"sourceCode":"        }\n\n        /// <summary>\n        /// Performs an explicit conversion from <see cref=\"JToken\"/> to <see cref=\"Nullable{T}\"/> of <see cref=\"UInt32\"/>.\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 uint?(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 UInt32.\".FormatWith(CultureInfo.InvariantCulture, GetType(value)));\n            }\n\n#if HAVE_BIG_INTEGER\n            if (v.Value is BigInteger integer)\n            {\n                return (uint?)integer;\n            }\n#endif\n\n            return (v.Value != null) ? (uint?)Convert.ToUInt32(v.Value, CultureInfo.InvariantCulture) : null;\n        }\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)]","sourceCodeStart":1188,"sourceCodeEnd":1224,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JToken.cs#L1188-L1224","documentation":"Thrown by the explicit cast operator 'operator uint?(JToken?)' (Nullable<UInt32>) 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, so null/JSON-null return null; the throw means a structurally non-numeric shape.","triggerScenarios":"Calling (uint?)token[\"count\"] where the resolved JTokenType is Date, Guid, TimeSpan, Uri, Bytes, Object, or Array, or the node is a JObject/JArray (EnsureValue null). A negative integer string does NOT throw this — it passes ValidateToken and throws OverflowException from Convert.ToUInt32.","commonSituations":"Unsigned counter field occasionally replaced by a date or object; schema change from integer to GUID string; mis-indexed node returning a child array.","solutions":["Guard token[\"count\"]?.Type is in {Integer, Float, String, Boolean} before casting.","Use token[\"count\"]?.Value<uint>() to get a non-throwing default on mismatch.","Confirm values are non-negative before choosing the unsigned overload (else use int?/long?).","Re-check your indexing to ensure you reached the scalar counter node."],"exampleFix":"// before\nuint? count = (uint?)payload[\"count\"]; // throws when count is an object\n\n// after\nuint? count = payload[\"count\"]?.Type == JTokenType.Integer ? payload[\"count\"]!.Value<uint>() : null;","handlingStrategy":"validation","validationCode":"uint? count = payload[\"count\"]?.Type == JTokenType.Integer ? payload[\"count\"]!.Value<uint>() : null;","typeGuard":"static bool IsUnsignedNumeric(JToken? t) =>\n    t is JValue v && (v.Type == JTokenType.Integer || v.Type == JTokenType.Float);","tryCatchPattern":"uint? count;\ntry { count = (uint?)payload[\"count\"]; }\ncatch (ArgumentException) { count = null; }","preventionTips":["Verify values are non-negative before choosing uint (OverflowException otherwise).","Use token[\"count\"]?.Value<uint>() for non-throwing reads.","Guard on JTokenType.Integer/Float before the explicit cast."],"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"}