4

Using c# 8 and .netcore 3.1.

I've read HERE that Utf8Json library process json serialization and deserialization faster that NewtonsoftJson.

We've recently upgraded our servers code from .netcore 2.2 to 3.1 mostly for performance improvements. Thus, it is reasonable that we also use the best serialization library.

So my questions are:

  1. In Startup.cs there is this

    services.AddControllers().AddNewtonsoftJson(options =>
    {
        options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    });
    

And I want it to use a different library, so I found out that I can use .AddJsonOptions but I cannot figure out how to set default serializer, even after using my google-fu skills.

  1. Since I've been using [JsonProperty("<name>")] everywhere in my code in order to reduce json string size, do I need to format everything for the new serializer or is there a way to make him consider the property attribute ? (attribute is Newtonsoft)

Thanks.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Ori Refael
  • 2,888
  • 3
  • 37
  • 68

1 Answers1

5

@Ori you can use Utf8json in net core 3.1 projects.

Use

[DataMember(Name = "RoleType")] public string Role_Type { get; set; }

Instead of

[JsonProperty("<name>")]

To use Utf8json formatters in Asp.Net core you need add the formatters as mentioned below.

    public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews()


    // Add Utf8Json formatters
    .AddMvcOptions(option =>
    {
        option.OutputFormatters.Clear();
        option.OutputFormatters.Add(new JsonOutputFormatter (StandardResolver.Default));
        option.InputFormatters.Clear();
        option.InputFormatters.Add(new JsonInputFormatter ());
    });
}

You can also refer below link for the formatters. https://github.com/neuecc/Utf8Json/blob/master/src/Utf8Json.AspNetCoreMvcFormatter/Formatter.cs

I am using utf8json and its working great for us.

Praveena M
  • 522
  • 4
  • 10