Nov 16

Before 3.3.1 if you wanted to control serialization you would have to decorate your model with attributes, for example if your REST API used snake case naming convention:

public class User
{
  [JsonProperty("phone_number")]
  public string PhoneNumber { get; set; }
}

As of 3.3.1 you can provide JsonSerializerSettings to the config – this saves you having to add the attributes to properties individually, which will make your code cleaner. For example if your REST API used snake case naming convention:

// When directly creating a RestClient instance
dynamic restClient = new RestClient("https://dalsoft.co.uk", new Config()
	.SetJsonSerializerSettings(new JsonSerializerSettings { ContractResolver =  new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() } }));

// When using IHttpClientFactory
 services
	.AddRestClient(Name, "https://dalsoft.co.uk")
	.SetJsonSerializerSettings(new JsonSerializerSettings { ContractResolver =  new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() } });

Now the SnakeCaseNamingStrategy ContractResolver is used by default when deserializing responses from your REST API.

To find out more about DalSoft.RestClient including the new JsonSerializerSettings head over to https://restclient.dalsoft.io

Tagged with:
preload preload preload