JamesNK/Newtonsoft.Json · error · InvalidOperationException

The JsonReader should not be on a token of type {0}.

Error message

The JsonReader should not be on a token of type {0}.

What it means

Thrown by the async ReadContentFromAsync default switch case when the JsonReader is positioned on a token type the loader does not handle. The loader covers the standard JSON tokens; any other/unexpected token type is an invalid reader state.

Source

Thrown at Src/Newtonsoft.Json/Linq/JContainer.Async.cs:165

                        break;
                    case JsonToken.Undefined:
                        v = JValue.CreateUndefined();
                        v.SetLineInfo(lineInfo, settings);
                        parent.Add(v);
                        break;
                    case JsonToken.PropertyName:
                        JProperty? property = ReadProperty(reader, settings, lineInfo, parent);
                        if (property != null)
                        {
                            parent = property;
                        }
                        else
                        {
                            await reader.SkipAsync().ConfigureAwait(false);
                        }
                        break;
                    default:
                        throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
                }
            } while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false));
        }
    }
}

#endif

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Use the built-in JsonTextReader over well-formed JSON input.
  2. Do not manually advance the reader before calling the async Load; let the loader drive reads.
  3. If using a custom reader, ensure it only yields standard JsonToken values and starts at None/StartArray/StartObject.

Example fix

// before
var jArr = await JArray.LoadAsync(myPartlyConsumedReader);
// after
using var reader = new JsonTextReader(new StringReader(jsonText));
var jArr = await JArray.LoadAsync(reader);
Defensive patterns

Strategy: try-catch

Validate before calling

if (!(reader is JsonTextReader)) {
    // ensure reader is positioned at a valid start token before async load
    reader.MoveToContent();
}

Try / catch

try { var token = await JArray.LoadAsync(reader); }
catch (InvalidOperationException ex) when (ex.Message.Contains("should not be on a token of type")) {
    // reader is in a bad state; re-create a JsonTextReader from the source text
}

Prevention

When it happens

Trigger: Using a custom JsonReader subclass that returns a novel or out-of-range JsonToken value; an async Load (e.g. JArray.LoadAsync) over a reader left in a bad state.

Common situations: Third-party/custom readers; corrupt or hand-advanced reader state; version skew between reader and library.

Related errors


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/4109447dfacfa0aa. Report an issue: GitHub.