question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Functions] Add ability to specify that System.Text.Json should be used for POCO bindings

See original GitHub issue

Library 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:closed
  • Created a year ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jsquirecommented, Jul 24, 2023

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.

1reaction
JoshLove-msftcommented, May 18, 2022

employee would be an instance of a POCO type Employee. BinaryData uses System.Text.Json internally.

Thanks for confirming. I just happened to stumble upon the source code for BinaryData within the System.Memory.Data package as well. Tbh, it’s another reason why the default in the bindings should be System.Text.Json and not Newtonsoft.

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrate from Newtonsoft.Json to System.Text.Json - .NET
Json [JsonConstructor] attribute lets you specify which constructor to call when deserializing to a POCO. System. Text.
Read more >
c# - Is it possible to catch somehow the remainder of JSON ...
I'm getting some JSON data from the external API. I have my POCO objects to which the data is deserialized. I use System.Text.Json....
Read more >
Add support for MissingMemberHandling to System.Text. ...
I like the idea of using a per-type handler, but how would the JSON of the missing property be deserialized into an object...
Read more >
What's next for System.Text.Json? - .NET Blog
Text. Json is to provide a fast built-in JSON stack that balances performance, security, and feature set. Every feature request is carefully ...
Read more >
Deserialization with System.Text.Json - Marc Roussy
Parse deserializes JSON to its constituent parts and makes it accessible through an object model that can represent any valid JSON. The object ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found