Repository navigation
Closed
Copy link
Description
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:
IncludeJsonEntityCollection(top-level JSON collections):- Unlike
MaterializeJsonEntityCollection(updated in Improve exceptions for invalid elements in JSON entity collections #39485),IncludeJsonEntityCollectiondoes not currently receiveIPropertyBase structuralProperty, so anullor non-object element in a top-level JSON collection still throwsInvalid token type: 'Null'instead ofJsonEntityCollectionNullElement/JsonEntityCollectionExpectedObject.
- Unlike
- Root token mismatches in relational JSON shaper methods:
MaterializeJsonStructuralType,MaterializeJsonNullableValueStructuralType, andIncludeJsonEntityReferencethrowJsonReaderInvalidTokenTypewhen the root token is notStartObject(e.g., when a JSON array is stored where a JSON object is expected).MaterializeJsonEntityCollectionandIncludeJsonEntityCollectionthrowJsonReaderInvalidTokenTypewhen the root token is notStartArray(e.g., when a JSON object is stored where a JSON array is expected).
- Stale
tokenTypepassed when checking forEndObjectinMaterializeJsonEntityCollectionandIncludeJsonEntityCollection:- After invoking
innerShaper, both methods checkif (manager.CurrentReader.TokenType != JsonTokenType.EndObject)but passtokenType.ToString()(which is stillStartObject) instead ofmanager.CurrentReader.TokenType.ToString().
- After invoking
- Primitive collection
JsonValueReaderWriters (JsonCollectionOfStructsReaderWriter,JsonCollectionOfNullableStructsReaderWriter,JsonCollectionOfReferencesReaderWriter):- Throw
JsonReaderInvalidTokenTypewhen the outer value is notStartArrayor when an element token is invalid for the element type, without indicating what token/value was expected.
- Throw
Describe the solution you'd like
- Pass
IPropertyBase structuralProperty(or the declaring type and property names) toIncludeJsonEntityCollectionso that top-level JSON collections throw the sameJsonEntityCollectionNullElementandJsonEntityCollectionExpectedObjectexceptions asMaterializeJsonEntityCollection. - Replace or enhance the root-level
JsonReaderInvalidTokenTypethrows in JSON shaper methods so they name the structural type / property and state whether a JSON object or JSON array was expected. - Fix the
EndObjectcheck inMaterializeJsonEntityCollectionandIncludeJsonEntityCollectionto reportmanager.CurrentReader.TokenTypeinstead of the staletokenType. - Include the expected token/value context in
JsonReaderInvalidTokenType(or dedicated resource strings) for primitive collectionJsonValueReaderWriters.
Activity
Sean-mn commentedon Nov 3, 2026
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
EndObjectfix on its own)?AndriySvyryd commentedon Nov 4, 2026
@Sean-mn You can do this in a single PR if the number of changed product lines is relatively low.
Improve remaining JsonReaderInvalidTokenType exceptions during JSON m…
Improve remaining JsonReaderInvalidTokenType exceptions during JSON m…