Skip to content

Expose a new JsonSerializerDefaults providing stricter defaults for cloud applications. #108526

@eiriktsarpalis

Description

@eiriktsarpalis

Background

We recently got feedback from the Azure team that the built-in JsonSerializerOptions defaults are not fit for purpose from the perspective of their recommended best practices. More specifically:

  1. All defaults serialize enums as strings
  2. The JsonSerializerDefaults.Web setting uses case insensitive property binding
  3. The JsonSerializerDefaults.Web setting allows deserializing numbers from strings.
  4. All defaults tolerate unmapped JSON members
  5. All configurations tolerate duplicate JSON properties

This issue proposes that we expose a new JsonSerializerDefaults value (let's call it JsonSerializerDefaults.Strict_V1 for now) that enables the following settings:

  • Applies the JsonNamingPolicy.CamelCase property naming policy
  • Applies the JsonStringEnumConverter for all enum types with allowIntegerValues set to false.
  • Applies the JsonUnmappedMemberHandling.Disallow policy
  • Disallows duplicate JSON properties.
  • Preserves case sensitive property binding
  • Enables both RespectNullableAnnotations and RespectRequiredConstructorParameters settings.

API proposal

namespace System.Text.Json

public enum JsonSerializerDefaults
{
    General = 0, // The default configuration set
    Web = 1,     // The default configuration set used by aspnetcore
+   Strict_V1 = 2 // The proposed configuration set
}

public partial class JsonSerializerOptions
{
    public static JsonSerializerOptions Default { get; }
    public static JsonSerializerOptions Web { get; }
+   public static JsonSerializerOptions Strict_V1 { get; }
}

cc @JeffreyRichter @GrabYourPitchforks

Metadata

Metadata

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Text.Jsonpartner-impactThis issue impacts a partner who needs to be kept updated

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions