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.
-
The ability to set the JsonSerialization settings on the DocumentDB SDK API is currently in development by the Azure team, so it should be available at some point in the future: https://feedback.azure.com/forums/263030-documentdb/suggestions/6422364-allow-me-to-set-jsonserializersetting – abrown Jul 06 '17 at 09:30
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://learn.microsoft.com/en-us/azure/cosmos-db/sql-api-sdk-dotnet

- 7,885
- 27
- 37

- 543
- 1
- 4
- 10
-
2
-
1
-
Even so client supports custom serialization settings linq provider seems to be ignoring it. – Vitaliy Kalinin Jan 29 '18 at 18:41
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://learn.microsoft.com/en-us/azure/cosmos-db/sql-api-sdk-dotnet

- 7,885
- 27
- 37

- 8,045
- 38
- 47
-
Hi aliuy, The question is not on how to serialize to caml-case using Json.NET but rather how to change the default serializer for documentDb. – Markus Ahlstrand Sep 18 '14 at 08:28
-
-
1While I was able to change the default serialization in Json.NET (by setting JsonConvert.DefaultSettings) and save property names in camel case, the query didn't work. I guess DocumentDb uses case-sensitive comparison on property names. So in a query obj.Id == document.id, the ids won't match. – Petr Havlicek Aug 21 '16 at 00:58
-
1Yes, as @PetrHavlicek notes, setting the JsonSerializer settings in the DocumentClient options works when WRITING the doc to the db. However, if you use the LINQ provider it doesn't honor these settings when it builds the query expression. You have 2 options as I see it, 1) Ditch LINQ and switch to building the SQL 2) Decorate your models with [JsonProperty(PropertyName'yourCamelCasePropName')] – Ryan CrawCour Jul 26 '17 at 02:44