Workflow SDK - Add System.Text.Json default options
Describe the feature
We would like the ability to configure default System.Text.Json options in the Dapr .NET SDK, such as custom JsonConverters for example.
This feature would enable developers to apply default STJ configuration behavior.
It could be implemented by exposing a way to register global JsonSerializerOptions, or at least a hook to customize or extend the default behavior.
Use case examples:
- Add a default JsonConverter for polymorphic deserialization.
This is particularly useful for applications that already use System.Text.Json and want Dapr SDK to align with their existing serialization logic.
Release Note
RELEASE NOTE: ADD Support for configuring default System.Text.Json options (e.g., converters, naming policy) in the Dapr .NET SDK.
Additional : we can override STJ configuration in Dapr client for example (from the doc https://docs.dapr.io/developing-applications/sdks/dotnet/dotnet-client/dotnet-daprclient-usage/#dependency-injection)
services.AddDaprClient(daprBuilder => {
daprBuilder.UseJsonSerializerOptions(new JsonSerializerOptions {
WriteIndented = true,
MaxDepth = 8
});
daprBuilder.UseTimeout(TimeSpan.FromSeconds(30));
});
But it's not the case for Workflow/Activity STJ usage.
I too was looking to do this. It turns out that it is possible now (but a bit awkward to find out how):
using Dapr.DurableTask
// either:
services.Configure<DurableTaskClientOptions>(options =>
{
options.DataConverter = new JsonDataConverter(yourOptions);
});
// or:
services.AddSingleton<DataConverter>(new JsonDataConverter(yourOptions));
I would be happy to take on making this more ergonomic, but it will need some design decisions.
Options might include...
1. Property on WorkflowRuntimeOptions
If set, then used to configure DurableTaskClientOptions:
public sealed class WorkflowRuntimeOptions
{
public JsonSerializerOptions? DefaultJsonSerializerOptions { get; set; }
}
Use would look like:
_ = services.AddDaprWorkflow(options =>
{
options.DefaultJsonSerializerOptions = JsonSharedOptions.RelaxedJsonEscaping;
options.RegisterOrchestrations();
});
2. Expose Action<DurableTaskClientOptions> for configuration via AddDaprWorkflow overload:
Use would look like:
_ = services.AddDaprWorkflow(
runtimeOptions =>
{
runtimeOptions.RegisterOrchestrations();
},
clientOptions =>
{
clientOptions.DataConverter = new JsonDataConverter(JsonSharedOptions.RelaxedJsonEscaping);
});
But I am not sure how much 'leakage' of DurableTask types into Workflow is desirable.
3. Add a DaprWorkflowClientOptions type
To me, WorkflowRuntimeOptions does not feel like the right place for configuring the workflow service client. Introducing a DaprWorkflowClientOptions type also avoids directly exposing underlying DurableTask types. It would also provide a natural place for future configuration of things influencing workflow service client behaviour.
public sealed class DaprWorkflowClientOptions
{
public JsonSerializerOptions? DefaultJsonSerializerOptions { get; set; }
}