Skip to content

Improve consistency of JSONPath in JsonException #31955

Closed

Description

@jozkee

Also consider the new metadata properties added for ReferenceHandling.

Consider this test.
Should we build the path based on where was the reader when we throw?

  • If the answer is yes, then the array scenario on line 71 is wrong - The path points to a missing element after the colon.
JsonException e = Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<IList<object>>("[1,")); 
Assert.Contains("Path: $[1] | LineNumber: 0 | BytePositionInLine: 2.", e.Message); 
  • If the answer is no, then the dictionary scenario on line 80 is wrong - The path points to the key already read successfully ($.key) while the error is after the colon.
e = Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<IDictionary<string, int>>("{\"key\":1,")); 
Assert.Contains("Path: $.key | LineNumber: 0 | BytePositionInLine: 8.", e.Message); 

whereas on the IncompleteTestClass scenario on line 86 that uses the exact same payload, the path points to the root ($).

e = Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<IncompleteTestClass>("{\"key\":1,")); 
Assert.Contains("$ | LineNumber: 0 | BytePositionInLine: 8.", e.Message); 

IMO the behavior should be point to where the error was or in case of incorrect syntax, to the closest, which is the previous element/property.
e.g:
[1, -> $[0] (Since element zero was the last correct).
{"key":1, -> $.key (Since property key was the last correct).
{"key":1, "IncompleteProp -> $.key (Since property key was the last correct).

Activity

  1. added this to the 5.0 milestone on Feb 8, 2020
  2. added
    untriaged
    on Feb 8, 2020
  3. removed
    untriaged
    on Feb 21, 2020
  4. layomia commented on Apr 7, 2020

    @layomia
    Contributor

    From @steveharter in #30259:

    Per discussion about JSON path support, the accuracy may be "off by one" when referencing arrays in certain conditions. See dotnet/corefx#37938 (comment).

    This is essentially related to reader functionality, since the deserializer doesn't handle the parsing.

  5. modified the milestones: 5.0.0, Future on Jun 25, 2020
  6. eiriktsarpalis commented on Oct 15, 2021

    @eiriktsarpalis
    Member

    Per discussion about JSON path support, the accuracy may be "off by one" when referencing arrays in certain conditions.

    I believe this was fixed in #57681

  7. ghost locked as resolved and limited conversation to collaborators on Nov 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions