JamesNK/Newtonsoft.Json · error · ArgumentException
Can not convert {0} to Char.
Error message
Can not convert {0} to Char. What it means
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.
Source
Thrown at Src/Newtonsoft.Json/Linq/JToken.cs:792
return (v.Value != null) ? (double?)Convert.ToDouble(v.Value, CultureInfo.InvariantCulture) : null;
}
/// <summary>
/// Performs an explicit conversion from <see cref="JToken"/> to <see cref="Nullable{T}"/> of <see cref="Char"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator char?(JToken? value)
{
if (value == null)
{
return null;
}
JValue? v = EnsureValue(value);
if (v == null || !ValidateToken(v, CharTypes, true))
{
throw new ArgumentException("Can not convert {0} to Char.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
}
#if HAVE_BIG_INTEGER
if (v.Value is BigInteger integer)
{
return (char?)integer;
}
#endif
return (v.Value != null) ? (char?)Convert.ToChar(v.Value, CultureInfo.InvariantCulture) : null;
}
/// <summary>
/// Performs an explicit conversion from <see cref="JToken"/> to <see cref="Int32"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator int(JToken value)View on GitHub (pinned to 4f73e74372)
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.
Example fix
// before
char? c = (char?)token["initial"]; // throws when initial is a boolean
// after
char? c = (token["initial"]?.Type is JTokenType.String)
? (char?)token["initial"]
: null; Defensive patterns
Strategy: type-guard
Validate before calling
// Verify the token is a char-compatible scalar (CharTypes excludes Boolean; nullable tolerates JSON null)
static bool CanToNullableChar(JToken? t) =>
t is not JValue v || v.Type is JTokenType.Integer or JTokenType.Float
or JTokenType.String or JTokenType.Null or JTokenType.Undefined; Type guard
static char? SafeChar(JToken? t) => t switch {
JValue v when v.Type is JTokenType.Integer or JTokenType.Float
or JTokenType.String => (char?)t,
JValue v when v.Type is JTokenType.Null or JTokenType.Undefined => null,
_ => null // Boolean, Date, container, etc.
}; Try / catch
try
{
char? c = (char?)token;
}
catch (ArgumentException ex) when (ex.Message.StartsWith("Can not convert"))
{
// token is Boolean/Date/container; default to null
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: A single-character field represented as a JSON boolean; a char field that became an object/array; reading a Guid/Uri as a char.
Related errors
- Can not convert {0} to Boolean.
- Can not convert {0} to DateTimeOffset.
- Can not convert {0} to Int64.
- Can not convert {0} to DateTime.
- Can not convert {0} to Decimal.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/f3db26a43d59e358.
Report an issue: GitHub.