Ardalis.ApiClient icon indicating copy to clipboard operation
Ardalis.ApiClient copied to clipboard

Allow customization of JsonSerializerOptions

Open scleaver opened this issue 1 year ago • 0 comments

As a user of the API client library, I'd like to request the ability to customize the JsonSerializerOptions used for serialization and deserialization of JSON data. Currently, the JsonSerializerOptions are implemented within the HttpResponse class and cannot be modified or overridden by the library users. This limitation prevents us from using custom JsonConverterFactory instances, handling specific serialization scenarios, or applying custom naming policies.

Allowing customization of JsonSerializerOptions would provide users with greater flexibility and control over the JSON serialization process. This could be achieved by exposing the JsonSerializerOptions as a public property or through a method that allows users to provide their own JsonSerializerOptions instance.

Here is a potential solution:

  1. Expose a public property or method in the library's main class to set the JsonSerializerOptions. For example:
public class ApiClient
{
    public JsonSerializerOptions JsonSerializerOptions { get; set; } = new JsonSerializerOptions();

    // ...
}
  1. Use the JsonSerializerOptions provided by the user when calling JsonSerializer.Deserialize methods in the library.

This change would enable users to easily customize the JSON serialization process and handle various scenarios, such as:

  • Using custom JsonConverterFactory instances to handle specific data types or scenarios.
  • Applying custom naming policies (e.g., camelCase, snake_case) to match the API's conventions.
  • Configuring other JsonSerializerOptions settings, such as handling null values or date formats.

I believe this improvement would make the library more flexible and user-friendly. Please consider implementing this feature in the next release of the API client library.

scleaver avatar May 07 '23 06:05 scleaver