{"record":{"id":"0e9447e7e621b2f9","repo":"JamesNK/Newtonsoft.Json","slug":"unexpected-token-type-0","errorCode":null,"errorMessage":"Unexpected token type: {0}","messagePattern":"Unexpected token type: (.+?)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/JsonValidatingReader.cs","lineNumber":259,"sourceCode":"                                        if (schema.Items.Count > (_currentScope.ArrayItemCount - 1))\n                                        {\n                                            schemas.Add(schema.Items[_currentScope.ArrayItemCount - 1]);\n                                        }\n                                    }\n\n                                    if (schema.AllowAdditionalItems && schema.AdditionalItems != null)\n                                    {\n                                        schemas.Add(schema.AdditionalItems);\n                                    }\n                                }\n                            }\n\n                            return schemas;\n                        }\n                    case JTokenType.Constructor:\n                        return EmptySchemaList;\n                    default:\n                        throw new ArgumentOutOfRangeException(\"TokenType\", \"Unexpected token type: {0}\".FormatWith(CultureInfo.InvariantCulture, _currentScope.TokenType));\n                }\n            }\n        }\n\n        private void RaiseError(string message, JsonSchemaModel schema)\n        {\n            IJsonLineInfo lineInfo = this;\n\n            string exceptionMessage = (lineInfo.HasLineInfo())\n                ? message + \" Line {0}, position {1}.\".FormatWith(CultureInfo.InvariantCulture, lineInfo.LineNumber, lineInfo.LinePosition)\n                : message;\n\n            OnValidationEvent(new JsonSchemaException(exceptionMessage, null, Path, lineInfo.LineNumber, lineInfo.LinePosition));\n        }\n\n        private void OnValidationEvent(JsonSchemaException exception)\n        {\n            ValidationEventHandler handler = ValidationEventHandler;","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/JsonValidatingReader.cs#L241-L277","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n// reader emits Comment tokens into validating reader\n\n// after\nvar textReader = new JsonTextReader(input); // does not emit schema-breaking tokens\nusing var validating = new JsonValidatingReader(textReader);","handlingStrategy":"try-catch","validationCode":"if (tokenType is JsonToken.Comment or JsonToken.Raw)\n{\n    // skip/filter token before validation\n}","typeGuard":"static bool IsSchemaHandledToken(JTokenType t)\n    => t is JTokenType.Object or JTokenType.Array or JTokenType.Constructor\n        or JTokenType.Property or JTokenType.None;","tryCatchPattern":"try\n{\n    while (validating.Read()) { /* ... */ }\n}\ncatch (ArgumentOutOfRangeException ex) when (ex.Message.Contains(\"Unexpected token type\"))\n{\n    // strip non-handled tokens (Comment/Raw) from the source and retry\n}","preventionTips":["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."],"tags":["schema-validation","json-reader","unexpected-token"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}