Custom serialisation of C# poco's for DocumentDb

后端 未结 2 1443
情书的邮戳
情书的邮戳 2020-12-18 04:14

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 serialisa

2条回答
  •  时光说笑
    2020-12-18 04:45

    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

提交回复
热议问题