Replace the default Azure Function Json pascalCase formatter with camelCase
I use dotnet-isolated
function runtime and by default it is using ObjectSerializer
to convert objects to Json. The response is formatted with PascalCase
which is doesn't look as natural Json naming style. Let's see how we can format it to camelCase
and remove nullables.
{
"Errors": []
}
I didnt find a way how to configure the serializer for the function on global level, however it quite simple to set the serilizer while writing the response.
The following code example creates a new response and writes an object
var response = req.CreateResponse(HttpStatusCode.Created);
await response.WriteAsJsonAsync(new MyClass());
First, ObjectSerializer
is one of the optional parameters for the WriteAsJsonAsync
extension method, which can be replaced with one from Newtonsoft.Net
. ObjectSerializer
is part of Azure.Core.Serialization to user its Newtonsoft.Net
implementation, you have to install Microsoft.Azure.Core.NewtonsoftJson
package.
Secondly, you need to create settings for JsonSerializer:
var s = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
var serializer = new NewtonsoftJsonObjectSerializer(s);
And the last one, use defined serializer while writing the response
await response.WriteAsJsonAsync(new MyClass(), serializer);
The response is formatted with camelCase
{
"errors": []
}
Debugging microservices is always tricky and time consuming specially without proper code level logging in place.
Meet Konso. It's developed as a solution for this challenge and can help with saving development effort for your team up to 30%. 🎯🎉
The key functions are:
🔥 Centralized logging for your microservices
🔥 Tracing with metrics and value tracking events
🔥 Ad-Hoc events exploration
🔥 Saving your queries
🔥 Create alerts and get notified
You can start collecting you project's logs in 5 minutes. 🕐💪
To Learn more about logging tool, book a free demo.
To get started for free, Create your free account now
No comments are allowed for this post