Skip to content

System.Text.Json's Path does not properly escape quotes #129794

Description

@Alxandr

Description

I have a json property with a (on purpose, for testing purposes) horrible property name, defined with [JsonPropertyName("~c$u%s''t.o\"m[5]/")]. I noticed that when the converter of this property throws a JsonException, the Path looks like this: "$['~c$u%s''t.o\"m[5]/']". The single quotes in the middle should be escaped.

Reproduction Steps

using System.Text.Json;
using System.Text.Json.Serialization;
using System;

JsonSerializer.Deserialize<Test>("""{"~c$u%s''t.o\"m[5]/": "5"}""");

class Test {
    [JsonPropertyName("~c$u%s''t.o\"m[5]/")]
    public Guid Foo { get; set; }
}

Result: Unhandled exception. System.Text.Json.JsonException: The JSON value could not be converted to System.Guid. Path: $['~c$u%s''t.o"m[5]/'] | LineNumber: 0 | BytePositionInLine: 26.

Expected behavior

The Path should be $['~c$u%s\'\'t.o"m[5]/']

Actual behavior

The Path should be $['~c$u%s''t.o"m[5]/']

Regression?

No response

Known Workarounds

If interpreting the path, only special-case [' and '].

Configuration

Tested on .NET 10 on linux x64. I would not imagine this to be configuration specific though.

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions