Skip to content

Improve remaining JsonReaderInvalidTokenType exceptions during JSON materialization #39486

Description

@Sean-mn

What problem are you trying to solve?

Following up on #35863 (comment) and #39485, there are several remaining places in JSON materialization that still throw the generic CoreStrings.JsonReaderInvalidTokenType ("Invalid token type: '{tokenType}'.") without context about what was expected or which type/property was being materialized:

  1. IncludeJsonEntityCollection (top-level JSON collections):
    • Unlike MaterializeJsonEntityCollection (updated in Improve exceptions for invalid elements in JSON entity collections #39485), IncludeJsonEntityCollection does not currently receive IPropertyBase structuralProperty, so a null or non-object element in a top-level JSON collection still throws Invalid token type: 'Null' instead of JsonEntityCollectionNullElement / JsonEntityCollectionExpectedObject.
  2. Root token mismatches in relational JSON shaper methods:
    • MaterializeJsonStructuralType, MaterializeJsonNullableValueStructuralType, and IncludeJsonEntityReference throw JsonReaderInvalidTokenType when the root token is not StartObject (e.g., when a JSON array is stored where a JSON object is expected).
    • MaterializeJsonEntityCollection and IncludeJsonEntityCollection throw JsonReaderInvalidTokenType when the root token is not StartArray (e.g., when a JSON object is stored where a JSON array is expected).
  3. Stale tokenType passed when checking for EndObject in MaterializeJsonEntityCollection and IncludeJsonEntityCollection:
    • After invoking innerShaper, both methods check if (manager.CurrentReader.TokenType != JsonTokenType.EndObject) but pass tokenType.ToString() (which is still StartObject) instead of manager.CurrentReader.TokenType.ToString().
  4. Primitive collection JsonValueReaderWriters (JsonCollectionOfStructsReaderWriter, JsonCollectionOfNullableStructsReaderWriter, JsonCollectionOfReferencesReaderWriter):
    • Throw JsonReaderInvalidTokenType when the outer value is not StartArray or when an element token is invalid for the element type, without indicating what token/value was expected.

Describe the solution you'd like

  1. Pass IPropertyBase structuralProperty (or the declaring type and property names) to IncludeJsonEntityCollection so that top-level JSON collections throw the same JsonEntityCollectionNullElement and JsonEntityCollectionExpectedObject exceptions as MaterializeJsonEntityCollection.
  2. Replace or enhance the root-level JsonReaderInvalidTokenType throws in JSON shaper methods so they name the structural type / property and state whether a JSON object or JSON array was expected.
  3. Fix the EndObject check in MaterializeJsonEntityCollection and IncludeJsonEntityCollection to report manager.CurrentReader.TokenType instead of the stale tokenType.
  4. Include the expected token/value context in JsonReaderInvalidTokenType (or dedicated resource strings) for primitive collection JsonValueReaderWriters.

Activity

  1. added theissue type on Oct 31, 2026
  2. changed the issue type fromtoon Nov 2, 2026
  3. added this to the Backlog milestone on Nov 2, 2026
  4. Sean-mn commented on Nov 3, 2026

    @Sean-mn
    ContributorAuthor

    Thanks for triaging this. I'd like to work on it. Should I send everything as a single PR, or split it up (e.g. the EndObject fix on its own)?

  5. AndriySvyryd commented on Nov 4, 2026

    @AndriySvyryd
    Member

    @Sean-mn You can do this in a single PR if the number of changed product lines is relatively low.

  6. added a commit that references this issue on Nov 7, 2026
    58fc5c7
  7. added a commit that references this issue on Nov 13, 2026
    0d9dceb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions