Skip to content

[System.Text.Json] JsonException Path should be a JsonPath #29596

@pranavkm

Description

@pranavkm

This wasn't correctly captured as part of #28960, but it would be way more useful for JsonException.Path to be Json Path. This is similar to the way Newtonsoft.Json reports it's deserialization errors and is a bit more useful to identity what is problematic. Consider:

var badData = "{ \"SubModel\": [{ \"Id\": false }] }";
try
{
    // JsonConvert.DeserializeObject<TestModel>(badData);
    System.Text.Json.Serialization.JsonSerializer.Parse<TestModel>(badData);
}
catch (JsonException ex)
{
    System.Console.WriteLine(ex.Path);
}

public class TestModel
{
    public List<TestSubModel> SubModel { get; set; }
}

public class TestSubModel
{
    public int Id { get; set; }
}

Output: [console1.TestModel].SubModel.Id

The same data through Json.NET produces SubModel[0].Id which tells which segment of the Json payload is problematic.

Metadata

Metadata

Assignees

Labels

area-System.Text.JsonblockingMarks issues that we want to fast track in order to unblock other important work

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions