Skip to content

System.Text.Json should recognize $schema as a metadata property. #114691

Open
@S-Luiten

Description

@S-Luiten

Description

System.Text.Json cannot deserialize JSON which contains the $schema metadata property.

Reproduction Steps

From #79059 (comment):

[JsonDerivedType(typeof(DerivedType), typeDiscriminator: nameof(DerivedType))]
public abstract class BaseType
{
}

public sealed class DerivedType : BaseType
{
}

internal class Program
{
    static void Main()
    {
        var text =
        """
        {
            "$type": "DerivedType",
            "$schema": "http://example.org/myschema.json",
        }
        """;
        var baseObj = JsonSerializer.Deserialize<BaseType>(text);
    }
}

Expected behavior

Deserialization should succeed without throwing an exception.

Actual behavior

JsonSerializer.Deserialize throws an exception: System.Text.Json.JsonException: Properties that start with '$' are not allowed in types that support metadata. Either escape the character or disable reference preservation and polymorphic deserialization.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Text.JsonenhancementProduct code improvement that does NOT require public API changes/additions

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions