Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure Cosmos DB .NET 3.0 SDK to serialize with camel case?

My .NET POCOs are in ProperCase. My json is in camelCase. How can I configure version 3.0 of the .NET SDK to convert when serializing/deserializing to/from Cosmos DB?

I know I can add the attribute [JsonProperty(PropertyName = "myProperty")] to each property but how can I tell the SDK to do this for all properties by default?

I'm trying to get away from adding an attribute for this to every property.

like image 769
user1843640 Avatar asked May 10 '19 20:05

user1843640


2 Answers

var client = new CosmosClientBuilder("URI", "Key")
        .WithSerializerOptions(new CosmosSerializationOptions {  PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase })
        .Build();
like image 155
gradx Avatar answered Sep 20 '22 18:09

gradx


It's a bit quick and dirty but to keep you moving until Microsoft exposes the serializer settings of the default serializer, you could copy the existing CosmosJsonSerializer and pass in the JsonSerializerSettings to the constructor.

I've created a gist you can take here:

Newtonsoft JSON.NET Serializer for Cosmos .net SDK v3

like image 31
Rich S Avatar answered Sep 19 '22 18:09

Rich S