Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for System.Text.Json #31

Open
BluMichele opened this issue Jan 30, 2024 · 0 comments
Open

Add support for System.Text.Json #31

BluMichele opened this issue Jan 30, 2024 · 0 comments

Comments

@BluMichele
Copy link

Since ASP.NET Core 3.0 the default serializer is System.Text.Json which means that on most of the modern services an API such as this would not work:

[HttpPost("api/sms/delivery-reports")]
    public IActionResult ReceiveDeliveryReport([FromBody] SmsDeliveryResult deliveryResult)
    {
        foreach (var result in deliveryResult.Results)
        {
            System.Diagnostics.Debug.WriteLine($"{result.MessageId} - {result.Status.Name}");
        }
        return Ok();
    }

(from your example of using webhooks)

The issue is that the models are decorated only with WCF and Newtonsoft's attributes like [DataMember(Name = "results", EmitDefaultValue = false)] or [JsonConstructor]. When a user uses one of your models in their controllers ASP.NET will try to deserialize the JSON using System.Text.Json.JsonSerializer.Deserialize but will fail because the capitalization of properties is not the same as expected.
To solve this you should add attributes such as [JsonPropertyName] where you use DataMember

I hope to have been useful :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant