To override a single JsonSerializerSettings requires implementing an entire serializer class. There should be a way to override the default serializer settings to avoid forcing users to create a entire new class just for one setting.
It would also be nice to have a sample showing a simple change to the default serializer.
Without official documentation or a sample I don't even know what I don't know. Having to dig through source code and tests is painful
@nheinbaugh thanks for the suggestion. I created an issue to track it #559
Until Microsoft exposes the serializer settings, you could just take the code of the default CosmosJsonSerializer and pass in a JsonSerializerSettings. Example gist: https://gist.github.com/richstokoe/c82fc831c6b4020926f5b5f772f7cd70
@richstokoe that is the current plan. That is why this issue was created.
Sure, and I didn't mean to hinder that, just wanted to show that open source means we don't always have to wait for the samples. Keep going, v3 SDK is awesome.
@richstokoe 3.1.0 is now released with the new serializer
@richstokoe and @nheinbaugh can you please take a look at this PR #650? We need to remove the CosmosJsonDotNetSerializer. The Azure central SDK team has guideline that we need to move to the new system.text.json, and exposing the JSON settings directly conflicts with that. This PR add supports for some common JSON settings. Does this support all of your scenarios?
@j82w Thanks for the engagement!
I may have missed it in the PR but I’d personally prefer to disambiguate property casing, i.e.
CosmosPropertyNamingPolicy.Default
CosmosPropertyNamingPolicy.UpperCamelCase
CosmosPropertyNamingPolicy.LowerCamelCase
That would solve my original issue (POCO “Id” properties not serialising to “id” without having to add a JsonPropertyAttribute) and would be explicit for consumers.
@richstokoe camel case refers to the first character being lower case. Pascal case is where the first character is capitalized. Check out Camel Case vs Pascal Case. I don't see an option in Newtonsoft or system.text.JSON that support pascal case by default. They both would require a custom converter. Do you have any requirements to support pascal case?
The final solution with cosmos serialization settings it merged.
Is there support for customer converts with the current setup? I am looking to add a stringtoenumconverter.
Hi @adstep ,
To support a custom converter you will need to implement a CosmosSerializer that is set on the CosmosClientOptions. You can take a look @richstokoe implementation to see an example.
To be honest, I'm not very happy with the current solution. CosmosSerializationOptions only lets us control a very small subset of JSON.NET's JsonSerializerSettings. For instance, I really need to be able to add converters, and the only way to do that now is by reimplementing a complete serializer (which isn't very hard, since I can just copy and modify CosmosJsonDotNetSerializer, but it's not ideal).
I understand that you don't want to explicitly depend on JSON.NET, but since there's a package reference, you depend on it anyway.
I think a good approach would be the following:
CosmosJsonDotNetSerializer in a separate NuGet package, so users can have more control over the serialization settings@thomaslevesque 's suggestion will also lead to a thriving STJ implementation sitting alongside it in a matter of days, methinks.
Most helpful comment
To be honest, I'm not very happy with the current solution.
CosmosSerializationOptionsonly lets us control a very small subset of JSON.NET'sJsonSerializerSettings. For instance, I really need to be able to add converters, and the only way to do that now is by reimplementing a complete serializer (which isn't very hard, since I can just copy and modifyCosmosJsonDotNetSerializer, but it's not ideal).I understand that you don't want to explicitly depend on JSON.NET, but since there's a package reference, you depend on it anyway.
I think a good approach would be the following:
CosmosJsonDotNetSerializerin a separate NuGet package, so users can have more control over the serialization settings