JamesNK/Newtonsoft.Json · error · ArgumentException
Can not convert {0} to Int16.
Error message
Can not convert {0} to Int16. What it means
Thrown by the explicit (non-nullable) cast operator short(JToken) at JToken.cs:838. 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 numbers fail later with OverflowException, not this message.)
Source
Thrown at Src/Newtonsoft.Json/Linq/JToken.cs:838
{
return (int)integer;
}
#endif
return Convert.ToInt32(v.Value, CultureInfo.InvariantCulture);
}
/// <summary>
/// Performs an explicit conversion from <see cref="JToken"/> to <see cref="Int16"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator short(JToken value)
{
JValue? v = EnsureValue(value);
if (v == null || !ValidateToken(v, NumberTypes, false))
{
throw new ArgumentException("Can not convert {0} to Int16.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
}
#if HAVE_BIG_INTEGER
if (v.Value is BigInteger integer)
{
return (short)integer;
}
#endif
return Convert.ToInt16(v.Value, CultureInfo.InvariantCulture);
}
/// <summary>
/// Performs an explicit conversion from <see cref="JToken"/> to <see cref="UInt16"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
[CLSCompliant(false)]View on GitHub (pinned to 4f73e74372)
Solutions
- Use the nullable cast (short?)token and coalesce: ((short?)token) ?? (short)0.
- Guard with token.Type == JTokenType.Integer || token.Type == JTokenType.Float before the cast.
- Use token.Value<short?>() to tolerate null/missing.
- Deserialize into a nullable short POCO property.
Example fix
// before short code = (short)token["code"]; // throws when code is null // after short code = (short?)token["code"] ?? (short)0;
Defensive patterns
Strategy: type-guard
Validate before calling
// Verify the token is a number-compatible scalar before the non-nullable short cast
static bool CanToShort(JToken? t) =>
t is JValue v && (v.Type == JTokenType.Integer || v.Type == JTokenType.Float
|| v.Type == JTokenType.String || v.Type == JTokenType.Boolean); Type guard
static short? SafeShort(JToken? t) => t switch {
JValue v when v.Type is JTokenType.Integer or JTokenType.Float
or JTokenType.String or JTokenType.Boolean => (short?)t,
JValue v when v.Type is JTokenType.Null or JTokenType.Undefined => null,
_ => null
}; Try / catch
try
{
short s = (short)token;
}
catch (ArgumentException ex) when (ex.Message.StartsWith("Can not convert"))
{
// token is null/container/date; default to 0
} Prevention
- Use the nullable cast (short?)token for fields that may be JSON null.
- Check token.Type is Integer/Float before the non-nullable short cast.
- Prefer token.Value<short?>() which tolerates missing/null.
- Deserialize into a nullable short POCO property.
When it happens
Trigger: Casting a JSON null JValue to (short)token; casting a Date/Object/Array/Guid to short; an omitted field parsed as null.
Common situations: A small-integer field (e.g. a port or enum index) that came back null or as an object; reading a Date-typed value as a short code.
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/99fbae1b95832bbf.
Report an issue: GitHub.