In Management SDK, the method arguments sent to clients are serialized into JSON. We have several ways to customize JSON serialization. We will show all the ways in the order from the most recommended to the least recommended.
The most recommended way is to use a general abstract class ObjectSerializer
, because it supports different JSON serialization libraries such as System.Text.Json
and Newtonsoft.Json
and it applies to all the transport types. Usually you don't need to implement ObjectSerializer
yourself, as handy JSON implementations for System.Text.Json
and Newtonsoft.Json
are already provided.
The builtin JsonObjectSerializer
uses System.Text.Json.JsonSerializer
to for serialization/deserialization. Here is a sample to use camel case naming for JSON serialization:
var serviceManager = new ServiceManagerBuilder()
.WithOptions(o =>
{
o.ConnectionString = "***";
o.UseJsonObjectSerializer(new JsonObjectSerializer(new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
}));
})
.BuildServiceManager();
First install the package Microsoft.Azure.Core.NewtonsoftJson
from Nuget using .NET CLI:
dotnet add package Microsoft.Azure.Core.NewtonsoftJson
Here is a sample to use camel case naming with NewtonsoftJsonObjectSerializer
:
var serviceManager = new ServiceManagerBuilder()
.WithOptions(o =>
{
o.ConnectionString = "***";
o.UseJsonObjectSerializer(new NewtonsoftJsonObjectSerializer(new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
}));
})
.BuildServiceManager();
You can also implement ObjectSerializer
on your own. The following links might help:
This method is only for Newtonsoft.Json
users. Here is a sample to use camel case naming:
var serviceManager = new ServiceManagerBuilder()
.WithNewtonsoftJson(o =>
{
o.PayloadSerializerSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
})
.BuildServiceManager();
This method only applies to transient transport type. Don't use this.
var serviceManager = new ServiceManagerBuilder()
.WithOptions(o =>
{
o.JsonSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
})
.BuildServiceManager();