{"record":{"id":"c4adae887931d1c3","repo":"JamesNK/Newtonsoft.Json","slug":"can-not-convert-0-to-uint16","errorCode":null,"errorMessage":"Can not convert {0} to UInt16.","messagePattern":"Can not convert (.+?) to UInt16\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JToken.cs","lineNumber":862,"sourceCode":"                return (short)integer;\n            }\n#endif\n\n            return Convert.ToInt16(v.Value, CultureInfo.InvariantCulture);\n        }\n\n        /// <summary>\n        /// Performs an explicit conversion from <see cref=\"JToken\"/> to <see cref=\"UInt16\"/>.\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 ushort(JToken value)\n        {\n            JValue? v = EnsureValue(value);\n            if (v == null || !ValidateToken(v, NumberTypes, false))\n            {\n                throw new ArgumentException(\"Can not convert {0} to UInt16.\".FormatWith(CultureInfo.InvariantCulture, GetType(value)));\n            }\n\n#if HAVE_BIG_INTEGER\n            if (v.Value is BigInteger integer)\n            {\n                return (ushort)integer;\n            }\n#endif\n\n            return Convert.ToUInt16(v.Value, CultureInfo.InvariantCulture);\n        }\n\n        /// <summary>\n        /// Performs an explicit conversion from <see cref=\"JToken\"/> to <see cref=\"Char\"/>.\n        /// </summary>\n        /// <param name=\"value\">The value.</param>\n        /// <returns>The result of the conversion.</returns>\n        [CLSCompliant(false)]","sourceCodeStart":844,"sourceCodeEnd":880,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JToken.cs#L844-L880","documentation":"Thrown by the explicit (non-nullable) cast operator ushort(JToken) at JToken.cs:862. The token type must be in NumberTypes = {Integer, Float, String, Comment, Raw, Boolean}. A JSON null, a Date/Guid/Uri/Bytes value, or a container (JObject/JArray) fails this ArgumentException. Negative numbers would fail later with OverflowException.","triggerScenarios":"Casting a JSON null JValue to (ushort)token; casting a Date/Object/Array/Guid to ushort; an omitted unsigned field parsed as null.","commonSituations":"A port number or unsigned identifier that came back null or as an object; reading a Date value as a ushort.","solutions":["Use the nullable cast (ushort?)token and coalesce: ((ushort?)token) ?? (ushort)0.","Guard with token.Type == JTokenType.Integer before the cast.","Use token.Value<ushort?>() to tolerate null/missing.","Deserialize into a nullable ushort POCO property."],"exampleFix":"// before\nushort port = (ushort)token[\"port\"]; // throws when port is null\n\n// after\nushort port = (ushort?)token[\"port\"] ?? (ushort)0;","handlingStrategy":"type-guard","validationCode":"// Verify the token is a number-compatible scalar before the non-nullable ushort cast\nstatic bool CanToUShort(JToken? t) =>\n    t is JValue v && (v.Type == JTokenType.Integer || v.Type == JTokenType.Float\n                      || v.Type == JTokenType.String || v.Type == JTokenType.Boolean);","typeGuard":"static ushort? SafeUShort(JToken? t) => t switch {\n    JValue v when v.Type is JTokenType.Integer or JTokenType.Float\n                       or JTokenType.String or JTokenType.Boolean => (ushort?)t,\n    JValue v when v.Type is JTokenType.Null or JTokenType.Undefined => null,\n    _ => null\n};","tryCatchPattern":"try\n{\n    ushort u = (ushort)token;\n}\ncatch (ArgumentException ex) when (ex.Message.StartsWith(\"Can not convert\"))\n{\n    // token is null/container/date; default to 0\n}","preventionTips":["Use the nullable cast (ushort?)token for fields that may be JSON null.","Check token.Type is Integer/Float before the non-nullable ushort cast.","Prefer token.Value<ushort?>() which tolerates missing/null.","Deserialize into a nullable ushort POCO property."],"tags":["linq-to-json","jtoken","cast","uint16","argumentexception"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}