CosmosDBAsyncCollector uses Newtonsoft instead of System.Text.Json (.NET Core 3.1 and Azure Function runtime version ~3)
See original GitHub issueAdoption of System.Text.Json is encouraged for applications targeting .NET Core 3+. The CosmosDBAsyncCollector output binding uses Newtonsoft as its JSON Serializer.
For Azure Functions on runtime version ~3 targeting .NET Core 3.1 it would be preferred if all of the bindings directly supported System.Text.Json (preferably by default).
Repro steps
-
Create an empty Cosmos DB database
example-db
collectionexample-collection
. -
Create an Azure Function App project targeting runtime ~3 and .NET Core 3.1.
-
Set
example-db-connection-string
in thelocal.settings.json
. -
Target Azure Functions runtime ~3 and .NET Core 3.1, then add a
FunctionExample.cs
to your project with the following code:
...
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
</PropertyGroup>
...
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
namespace FunctionAppExample
{
public class MyModel
{
[JsonPropertyName("id")]
public string DocumentId { get; set; }
}
public static class FunctionExample
{
[FunctionName("FunctionExample")]
public static async Task Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer,
[CosmosDB("example-db",
"example-collection",
ConnectionStringSetting = "example-db-connection-string")]
IAsyncCollector<MyModel> output,
ILogger log)
{
var thing = new MyModel
{
DocumentId = "abc"
};
await output.AddAsync(thing);
}
}
}
-
Run the function host and allow
FunctionExample
to execute once. -
Inspect the document created by the function in
example-collection
.
Expected behavior
Document is serialized by System.Text.Json
:
Actual behavior
Document is serialized by Newtonsoft
:
Known workarounds
- Use Newtonsoft on all models consumed in the bindings.
- Explicitly perform the serialization outside of the bindings.
Related information
Relevant code:
Possibly other Azure Function bindings and triggers follow the same pattern (not investigated).
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:6 (2 by maintainers)
Top GitHub Comments
V3 SDK uses Newtonsoft.Json as default serialization engine, so it’s the same behavior for the 4.0.0-preview package. But you can plug in/replace with any serializer you want. We don’t have official docs yet but you can leverage the new serialization factory. In your Startup.cs file:
You can have any custom implementation of CosmosSerializer, for example, one for System.Text.Json: https://github.com/Azure/azure-cosmos-dotnet-v3/tree/master/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson
@ealsur I noticed this PR #717 that seems to upgrade to the V3 SDK, should be expect that if we are using the preview nuget package for the cosmos extension? I tried it recently and it didn’t seem to respect system.text.json annotation, which I assume means we are still using Newtonsoft under the hood.