{"record":{"id":"f3db26a43d59e358","repo":"JamesNK/Newtonsoft.Json","slug":"can-not-convert-0-to-char","errorCode":null,"errorMessage":"Can not convert {0} to Char.","messagePattern":"Can not convert (.+?) to Char\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JToken.cs","lineNumber":792,"sourceCode":"            return (v.Value != null) ? (double?)Convert.ToDouble(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=\"Char\"/>.\n        /// </summary>\n        /// <param name=\"value\">The value.</param>\n        /// <returns>The result of the conversion.</returns>\n        public static explicit operator char?(JToken? value)\n        {\n            if (value == null)\n            {\n                return null;\n            }\n\n            JValue? v = EnsureValue(value);\n            if (v == null || !ValidateToken(v, CharTypes, true))\n            {\n                throw new ArgumentException(\"Can not convert {0} to Char.\".FormatWith(CultureInfo.InvariantCulture, GetType(value)));\n            }\n\n#if HAVE_BIG_INTEGER\n            if (v.Value is BigInteger integer)\n            {\n                return (char?)integer;\n            }\n#endif\n\n            return (v.Value != null) ? (char?)Convert.ToChar(v.Value, CultureInfo.InvariantCulture) : null;\n        }\n\n        /// <summary>\n        /// Performs an explicit conversion from <see cref=\"JToken\"/> to <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":774,"sourceCodeEnd":810,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JToken.cs#L774-L810","documentation":"Thrown by the explicit nullable cast operator char?(JToken?) at JToken.cs:792. The valid type set is CharTypes = {Integer, Float, String, Comment, Raw} — note Boolean is NOT included (unlike NumberTypes). With nullable:true, JSON null/undefined pass. The error fires for Boolean, Date, Guid, Uri, Bytes, Object, Array, or any non-JValue token.","triggerScenarios":"Casting a Boolean JValue (true/false) to (char?)token; casting a Date/Guid/Uri or a JObject/JArray to char?. A JSON null returns null.","commonSituations":"A single-character field represented as a JSON boolean; a char field that became an object/array; reading a Guid/Uri as a char.","solutions":["Check token.Type is String or Integer before the cast; remember Boolean is rejected even though it is accepted by number casts.","Use token.Value<char?>() to get default for incompatible tokens.","Validate the field is a 1-character string scalar in the JSON.","Deserialize into a nullable char POCO property."],"exampleFix":"// before\nchar? c = (char?)token[\"initial\"]; // throws when initial is a boolean\n\n// after\nchar? c = (token[\"initial\"]?.Type is JTokenType.String)\n    ? (char?)token[\"initial\"]\n    : null;","handlingStrategy":"type-guard","validationCode":"// Verify the token is a char-compatible scalar (CharTypes excludes Boolean; nullable tolerates JSON null)\nstatic bool CanToNullableChar(JToken? t) =>\n    t is not JValue v || v.Type is JTokenType.Integer or JTokenType.Float\n        or JTokenType.String or JTokenType.Null or JTokenType.Undefined;","typeGuard":"static char? SafeChar(JToken? t) => t switch {\n    JValue v when v.Type is JTokenType.Integer or JTokenType.Float\n                       or JTokenType.String => (char?)t,\n    JValue v when v.Type is JTokenType.Null or JTokenType.Undefined => null,\n    _ => null // Boolean, Date, container, etc.\n};","tryCatchPattern":"try\n{\n    char? c = (char?)token;\n}\ncatch (ArgumentException ex) when (ex.Message.StartsWith(\"Can not convert\"))\n{\n    // token is Boolean/Date/container; default to null\n}","preventionTips":["Remember char casts reject Boolean even though number casts accept it.","Check token.Type is String or Integer before casting.","Use token.Value<char?>() for tolerant default handling.","Deserialize into a nullable char POCO property."],"tags":["linq-to-json","jtoken","cast","nullable-char","argumentexception"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}