Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom serialisation of C# poco's for DocumentDb

Is it possible to change the default serialisation of C# poco's for documentDb? The id-property for instance seem to be required to be lower case, but the default serialisation of the Id property is upper case. Ideally we would like all json properties to start with lower case characters. The only way we found so far is to decorate the properties with [JsonProperty(PropertyName = "id")] but it's not very elegant.

like image 818
Markus Ahlstrand Avatar asked Sep 16 '14 08:09

Markus Ahlstrand


People also ask

Can you customize serialization?

Customized serialization can be implemented using the following two methods: private void writeObject(ObjectOutputStream oos) throws Exception: This method will be executed automatically by the jvm(also known as Callback Methods) at the time of serialization.

What is custom serialization in C#?

Custom serialization is the process of controlling the serialization and deserialization of a type. By controlling serialization, it's possible to ensure serialization compatibility, which is the ability to serialize and deserialize between versions of a type without breaking the core functionality of the type.

Why is serialization required?

Serialization allows the developer to save the state of an object and re-create it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions such as: Sending the object to a remote application by using a web service.


2 Answers

Currently you can't change the default serializer of DocumentDB, you can however serialize it using your own library or JSON.NET and store the JSON string to the collection by doing:

await client.CreateDocumentAsync(collectionLink, Resource.LoadFrom<Document>(stream)); 

where stream a stream to your json string (can be from a file, or from an in-memory string, etc). You can find more info on the internet archive's edition of my blog post, which used to reside here

Edit: JSON serializer settings is supported in the DocumentDB .NET SDK 1.16.0+. https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-sdk-dotnet

like image 120
Michalis Avatar answered Sep 29 '22 12:09

Michalis


Here a couple ways to get lower-case or camel-case properties in your DocumentDB document:

  • Use [JsonProperty(PropertyName = "id")] as you mentioned.

  • Change the C# property in the POCO to lower case.

  • Have your POCO extend Microsoft.Azure.Documents.Document from the DocumentDB .NET Library, which has an Id property (that I believe uses [JsonProperty(PropertyName = "id")] behind the scenes).

  • Instead of using the default serializer, you can use the Json.NET library to serialize using it's camel-case resolver. Mats Karlsson has a pretty good blog post on this here: http://www.matskarlsson.se/blog/serialize-net-objects-as-camelcase-json

Edit: JSON serializer settings is supported in the DocumentDB .NET SDK 1.16.0+. https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-sdk-dotnet

like image 44
Andrew Liu Avatar answered Sep 29 '22 12:09

Andrew Liu