Skip to content

Latest commit

 

History

History

customserializer

Episode 1 - Custom serialization

<- Back to root

The Azure Cosmos DB .NET SDK uses JsonNET as the default serialization engine for item operations.

It provides customization of the serialization process through the CosmosSerializationOptions that can be defined on the CosmosClient creation:

CosmosClient client = new CosmosClientBuilder("connection string")
    .WithApplicationName("OnDotNetRocks")
    .WithSerializerOptions(new CosmosSerializationOptions(){
        IgnoreNullValues = true,
        PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
    })
    .Build();

And going a step further, you can completely replace the serialization engine with your own by implementing the CosmosSerializer interface:

CosmosClient client = new CosmosClientBuilder("connection string")
    .WithApplicationName("OnDotNetRocks")
    .WithCustomSerializer(new MySerializer())
    .Build();

public class MySerializer : CosmosSerializer
{
    public override T FromStream<T>(Stream stream)
    {
        ...
    }

    public override Stream ToStream<T>(T input)
    {
        ....
    }
}