{"record":{"id":"688741c26b8ea41f","repo":"JamesNK/Newtonsoft.Json","slug":"can-not-convert-0-to-sbyte","errorCode":null,"errorMessage":"Can not convert {0} to SByte.","messagePattern":"Can not convert (.+?) to SByte\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JToken.cs","lineNumber":933,"sourceCode":"                return (byte)integer;\n            }\n#endif\n\n            return Convert.ToByte(v.Value, CultureInfo.InvariantCulture);\n        }\n\n        /// <summary>\n        /// Performs an explicit conversion from <see cref=\"Newtonsoft.Json.Linq.JToken\"/> to <see cref=\"System.SByte\"/>.\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 sbyte(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 SByte.\".FormatWith(CultureInfo.InvariantCulture, GetType(value)));\n            }\n\n#if HAVE_BIG_INTEGER\n            if (v.Value is BigInteger integer)\n            {\n                return (sbyte)integer;\n            }\n#endif\n\n            return Convert.ToSByte(v.Value, CultureInfo.InvariantCulture);\n        }\n\n        /// <summary>\n        /// Performs an explicit conversion from <see cref=\"JToken\"/> to <see cref=\"Nullable{T}\"/> of <see cref=\"Int32\"/> .\n        /// </summary>\n        /// <param name=\"value\">The value.</param>\n        /// <returns>The result of the conversion.</returns>\n        public static explicit operator int?(JToken? value)","sourceCodeStart":915,"sourceCodeEnd":951,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JToken.cs#L915-L951","documentation":"Thrown by the explicit (non-nullable) cast operator sbyte(JToken) at JToken.cs:933. 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. Out-of-range values fail later with OverflowException.","triggerScenarios":"Casting a JSON null JValue to (sbyte)token; casting a Date/Object/Array/Guid to sbyte; an omitted sbyte field parsed as null.","commonSituations":"A signed-byte field that came back null or as an object; reading a Date value as an sbyte.","solutions":["Use the nullable cast (sbyte?)token and coalesce: ((sbyte?)token) ?? (sbyte)0.","Guard with token.Type == JTokenType.Integer before the cast.","Use token.Value<sbyte?>() to tolerate null/missing.","Deserialize into a nullable sbyte POCO property."],"exampleFix":"// before\nsbyte code = (sbyte)token[\"code\"]; // throws when code is null\n\n// after\nsbyte code = (sbyte?)token[\"code\"] ?? (sbyte)0;","handlingStrategy":"type-guard","validationCode":"// Verify the token is a number-compatible scalar before the non-nullable sbyte cast\nstatic bool CanToSByte(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 sbyte? SafeSByte(JToken? t) => t switch {\n    JValue v when v.Type is JTokenType.Integer or JTokenType.Float\n                       or JTokenType.String or JTokenType.Boolean => (sbyte?)t,\n    JValue v when v.Type is JTokenType.Null or JTokenType.Undefined => null,\n    _ => null\n};","tryCatchPattern":"try\n{\n    sbyte s = (sbyte)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 (sbyte?)token for fields that may be JSON null.","Check token.Type is Integer/Float before the non-nullable sbyte cast.","Prefer token.Value<sbyte?>() which tolerates missing/null.","Deserialize into a nullable sbyte POCO property."],"tags":["linq-to-json","jtoken","cast","sbyte","argumentexception"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}