CosmosDB Camel Case Serializer

Naming public properties in .NET is usually done using Pascal case. However, CosmosDB uses camel case for special properties on an object such as “id”. So how do we square this circle?

Looking at the examples from the Azure Samples Cosmos To Do App, attributes seem to be the answer. As seen on the Item model class, the attribute JsonProperty(PropertyName = "id") is used. Adding that property to every class seems a bit laborious and prone to fat-finger syndrome.

In order to use those attributes, we also have to tie our solution to the Newtonsoft.Json package. Dotnet Core has a built-in JSON library in the System.Text.Json namespace. The Microsoft.Azure.Cosmos library has plans to ditch Newtonsoft.Json and use System.Text.Json in the next major release of the library. Adding these attributes all over the models seems like an even worse idea.

Using the following code snippet when creating the CosmosClient will serialize all properties using camel case without the need for peppering soon to be deprecated attributes all over the code:

var options = new CosmosClientOptions
{
  SerializerOptions = new CosmosSerializationOptions
  {
     PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
  }
};

var client = new CosmosClient(account, key, options);

2020-05-30