Skip to content

Add support for MissingMemberHandling to System.Text.Json #37483

@SigmundVik

Description

@SigmundVik

We would love to migrate our ASP.NET Core 3 application from using Newtonsoft.Json to System.Text.Json. However, the lack of support/workaround for MissingMemberHandling is a showstopper, because we need a robust way to detect and report deficient [FromBody] model binding. Without this feature, mistakes in the input JSON document can go undetected, and that is not acceptable.

Is adding MissingMemberHandling something that is planned for System.Text.Json?

EDIT @eiriktsarpalis: API proposal & usage examples added

API Proposal

namespace System.Text.Json;

public enum JsonMissingMemberHandling { Ignore = 0, Error = 1 }

public partial class JsonSerializerOptions
{
     // Global configuration
     public JsonMissingMemberHandling MissingMemberHandling { get; set; } = JsonMissingMemberHandling.Ignore;
}
namespace System.Text.Json.Serialization;

// Per-type attribute-level configuration
[AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct, AllowMultiple=false, Inherited=false)]
public class JsonMissingMemberHandlingAttribute : JsonAttribute
{
       public JsonMissingMemberHandlingAttribute(JsonMissingMemberHandling missingMemberHandling);
       public JsonMissingMemberHandlingAttribute MissingMemberHandling { get; }
}

namespace System.Text.Json.Serialization.Metadata;

public partial class JsonTypeInfo
{
      // Per-type configuration via contract customization.
      public JsonMissingMemberHandling MissingMemberHandling { get; set; } = JsonMissingMemberHandling.Ignore;
}

API Usage

JsonSerializer.Deserialize<MyPoco>("""{ "jsonPropertyNotBindingToPocoProperty" : null}"""); // JsonException: member not found

[JsonMissingMemberHandling(JsonMissingMember.Error)]
public class MyPoco { }

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions