Search code examples
aspnetboilerplateabp-frameworkjsonconverter

Abp Framework global json converter cannot be configure


I would like to define custom json converter and replace with AbpStringToEnumConverter in abp framework. I am trying to change JsonSerializerOptions.Converters with below code but it's not working.

public override void ConfigureServices(ServiceConfigurationContext context)
{
    var configuration = context.Services.GetConfiguration();
    var hostingEnvironment = context.Services.GetHostingEnvironment();

    ConfigureEnumCodeStringConverter();
    ...
}

private void ConfigureEnumCodeStringConverter()
{
    Configure<AbpSystemTextJsonSerializerOptions>(options =>
    {
        var stringToEnumFactory = options.JsonSerializerOptions.Converters.Single(x => x.GetType() == typeof(AbpStringToEnumFactory));
        options.JsonSerializerOptions.Converters.Remove(stringToEnumFactory);
        options.JsonSerializerOptions.Converters.Add(new EnumToCodeFactory());
    });
}

In addition, when i add converter attribute to requestDto property, thats working but i dont want to use attribute, i want to define global converter.

Example:

 [Required]
 [JsonConverter(typeof(EnumToCodeConverter<Gender>))]
 public Gender Gender { get; set; }

Solution

  • AbpSystemTextJsonSerializerOptions works with custom converter IJSonSerializer in service layer, I tested and saw that. I need was to use converter in API(swagger), for this I realized that I had to configure the same in Mvc.JsonOptions and customer converter worked as I wanted.