3

using the below config in startup.cs services.AddControllers() .AddNewtonsoftJson(opt => opt.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter()))

but not able to serialize the enum to string values. Only integer values are displayed in the swagger page. Could some please help me how to fix this?.

1 Answers1

0

You have several ways to achieve this.

  • Add the [JsonConverter(typeof(StringEnumConverter))] attribute on the Enum field.

    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;
    
    [JsonConverter(typeof(StringEnumConverter))]
    public Gender Gender { get; set; }
    
  • Add formatting parameter during serialization.

    string value = JsonConvert.SerializeObject(objectlist,new Newtonsoft.Json.Converters.StringEnumConverter());

You can also refer to this.

LouraQ
  • 6,443
  • 2
  • 6
  • 16