Configure System.Text.Json to allow serializing or deserializing enum from string.

ASP.NET core

For some reason, by default, the System.Text.json library does not convert strings into enums.

To overcome this, I usually configure it this way:


services.AddControllers()
      .AddJsonOptions(options =>
      {
      	options.JsonSerializerOptions.PropertyNamingPolicy = null;
        options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
      });

Post a Comment

Previous Post Next Post