Share via

Azure WebApi JsonSerializerSettings behaving differently

Mark Johnson 0 Reputation points
2023-12-19T22:57:35.26+00:00

On Thursday, 12/14 before 1:15pm CST, JSON serialization/deserialization behaved as expected on my Azure WebApi service. However, after that time, it seems as though our setting in the WebApiConfig.cs's Register method:

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
    NullValueHandling = NullValueHandling.Ignore
};

seems to be ignored, and now, when we serialize to json, we're getting properties with null values.

We're using .Net Framework 4.8.

On another, pre-production, instance of our service, that setting still seems to be respected, so we don't see properties serialized that have null values.

We had no changes to the service around that time, so it seems to be an infrastructure change.

What types of changes could have been made that would change the behavior of this setting?

Azure App Service
Azure App Service

Azure App Service is a service used to create and deploy scalable, mission-critical web apps.

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Bryan Trach 17,842 Reputation points Microsoft Employee Moderator
    2023-12-29T21:02:55.3666667+00:00

    @Mark Johnson With it being the holiday season, there are not any infrastructure patches allowed during this time unless they are security related or a hot fix. Even then, those have to be business critical for them to be approved. It would be highly unlikely there has been an infrastructure change in the middle of our no updates window.

    One thing you can try is to explicitly set the NullValueHandling property on the JsonSerializerSettings object that you pass to the JsonConvert.SerializeObject method. This will ensure that the NullValueHandling setting is applied during serialization, regardless of the value of the JsonConvert.DefaultSettings property.

    For example:

    var settings = new JsonSerializerSettings
    {
        NullValueHandling = NullValueHandling.Ignore
    };
    
    var json = JsonConvert.SerializeObject(obj, settings);
    
    
    

    If this doesn't solve the issue, you can try checking the version of the Newtonsoft.Json package that your project is using. It's possible that an update to the package has caused a behavior change. You can try downgrading to a previous version of the package to see if that resolves the issue.

    Finally, you can try enabling tracing in your Azure WebApi service to see if there are any errors or warnings related to JSON serialization/deserialization. This can help you identify the root cause of the issue.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.