JamesNK/Newtonsoft.Json · error · ArgumentOutOfRangeException
Unexpected token type: {0}
Error message
Unexpected token type: {0} What it means
Thrown by JsonValidatingReader's CurrentSchemas getter default case when the current scope token type is none of the handled cases (None, Object, Array/Property via PropertyName, Constructor). This indicates an unexpected/unhandled JTokenType during schema validation and is a bug or unsupported-token scenario in the validating reader, surfaced as ArgumentOutOfRangeException naming the offending token type.
Source
Thrown at Src/Newtonsoft.Json/JsonValidatingReader.cs:259
if (schema.Items.Count > (_currentScope.ArrayItemCount - 1))
{
schemas.Add(schema.Items[_currentScope.ArrayItemCount - 1]);
}
}
if (schema.AllowAdditionalItems && schema.AdditionalItems != null)
{
schemas.Add(schema.AdditionalItems);
}
}
}
return schemas;
}
case JTokenType.Constructor:
return EmptySchemaList;
default:
throw new ArgumentOutOfRangeException("TokenType", "Unexpected token type: {0}".FormatWith(CultureInfo.InvariantCulture, _currentScope.TokenType));
}
}
}
private void RaiseError(string message, JsonSchemaModel schema)
{
IJsonLineInfo lineInfo = this;
string exceptionMessage = (lineInfo.HasLineInfo())
? message + " Line {0}, position {1}.".FormatWith(CultureInfo.InvariantCulture, lineInfo.LineNumber, lineInfo.LinePosition)
: message;
OnValidationEvent(new JsonSchemaException(exceptionMessage, null, Path, lineInfo.LineNumber, lineInfo.LinePosition));
}
private void OnValidationEvent(JsonSchemaException exception)
{
ValidationEventHandler handler = ValidationEventHandler;View on GitHub (pinned to 4f73e74372)
Solutions
- Strip Comment/Raw/unexpected tokens (e.g. read from JsonTextReader with default settings, or filter) before validation.
- Use JsonSchema (or migrate to System.Text.Schema / Newtonsoft.Json.Schema) with a schema whose constructs match the token types emitted.
- If you control the reader, only emit Object/Array/PropertyName/Primitive tokens during validation.
Example fix
// before // reader emits Comment tokens into validating reader // after var textReader = new JsonTextReader(input); // does not emit schema-breaking tokens using var validating = new JsonValidatingReader(textReader);
Defensive patterns
Strategy: try-catch
Validate before calling
if (tokenType is JsonToken.Comment or JsonToken.Raw)
{
// skip/filter token before validation
} Type guard
static bool IsSchemaHandledToken(JTokenType t)
=> t is JTokenType.Object or JTokenType.Array or JTokenType.Constructor
or JTokenType.Property or JTokenType.None; Try / catch
try
{
while (validating.Read()) { /* ... */ }
}
catch (ArgumentOutOfRangeException ex) when (ex.Message.Contains("Unexpected token type"))
{
// strip non-handled tokens (Comment/Raw) from the source and retry
} Prevention
- Feed only standard tokens (Object/Array/PropertyName/primitives) to JsonValidatingReader.
- Filter Comment/Raw tokens out of the pipeline before validation.
- Prefer JsonTextReader as the validation source.
When it happens
Trigger: A JSON token stream containing a token type the validating reader's schema model does not handle (e.g. Comment, Raw, or an unexpected JTokenType) while a schema scope is active; custom readers injecting non-standard tokens.
Common situations: Feeding a reader that emits Comment/Raw tokens into JsonValidatingReader; library version mismatch where new token types are not yet handled; malformed or preprocessed JSON introducing unusual tokens.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/0e9447e7e621b2f9.
Report an issue: GitHub.