[Functions] Add ability to specify that System.Text.Json should be used for POCO bindings
See original GitHub issueLibrary name and version
Microsoft.Azure.WebJobs.Extensions.ServiceBus 5.3.0
Query/Question
How do I configure the ServiceBus bindings to use System.Text.Json rather than the unexpected default of Newtonsoft.Json?
I created a POCO message class using JsonElement
like this:
using System.Text.Json;
namespace MyNamespace;
public sealed record GenericSendRealTimeEventPayload
{
public JsonElement? EventData { get; init; }
public string? EventTarget { get; init; }
public EventTargetTypes? EventTargetType { get; init; }
}
and then spent the next couple hours tracking down why the Model Binding was not populating the JsonElement property like the System.Text.Json docs state. This was especially confusing as my unit test:
[Fact]
public void DeserializesAsExpected()
{
string jsonEventData = JsonSerializer.Serialize(ModelMocks.ValidEventData);
GenericSendRealTimeEventPayload expectedPayload = new GenericSendRealTimeEventPayload
{
EventData = JsonDocument.Parse(jsonEventData).RootElement,
EventTarget = ModelMocks.ValidPayload.EventTarget,
EventTargetType = ModelMocks.ValidPayload.EventTargetType,
};
string jsonPayload = JsonSerializer.Serialize(ModelMocks.ValidPayload);
GenericSendRealTimeEventPayload? actualPayload = JsonSerializer.Deserialize<GenericSendRealTimeEventPayload>(jsonPayload);
Assert.Equal(expectedPayload.ToString(), actualPayload?.ToString());
}
is passing as expected.
I finally discovered this repo and that the default json serializer is still unexpectedly Newtonsoft.Json. Unfortunately, I can’t seem to find any documentation mentioning how to change it (or even what the default is).
Could you please help me understand what I need to do to enable System.Text.Json serializer for my SreviceBus bindings?
Environment
<PropertyGroup>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<PublishReadyToRun>true</PublishReadyToRun>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>
Developing locally on Win 10 using Visual Studio 2022 (17.2.0) and .NET 6
Issue Analytics
- State:
- Created a year ago
- Comments:11 (6 by maintainers)
Top GitHub Comments
After further discussion, this is not a feature that we’ll be moving forward with at present. If, in the future, the Functions platform moves its standard away from Newtonsoft, the Service Bus extensions will follow. Mitigation is to bind to
ServiceBusReceivedMessage
and perform deserialization of the payload directly. Closing this out.The decision was made to leave Newtonsoft as the default for backwards compatibility with prior versions of the extensions. I think we can take the idea of providing configurability here as a feature request for the extensions.