SystemTextJson allows comments in JSON

Today in this article, we will see System.Text.Json allow comments in JSON or allows trailing commas in JSON in .NET or ASP.NET Core applications while performing serialize or deserialize.

As a default behavior, any serialization or deserialization operation using System.Text.Json doesn’t support any comments in the JSON and also doesn’t support any trailing commas.

Today in this article, we will cover below aspects,

Using the property JsonSerializerOptions defined with the property “ReadCommentHandling = JsonCommentHandling.Skip” we can easily enable comments of any format in the JSON.

We shall also see how to enable this setting globally for all serialized or deserialized operations, especially in ASP.NET Core-based applications.

Add/Allow comments in JSON

Let’s see how to enable comments while serializing or deserializing.

Comments can be available in the JSON using any of the below approaches

Comments

//This is Street


/*This is zip code*/

Below is a basic example of comments in JSON,

{
  "Bank": "Bank of ABCD",
  "BranchInfo": [
    {
        "Location": 
          {
            "Street": "address1", //This is Street
            "Zip": "Zip",/*This is zip code*/
            "State": "USA"
          }  
    }
  ]
}

As a default behavior System.Text.Json serialize or deserialize will produce an error.

To overcome an error, please use the below JsonSerializerOptions defined with the property

ReadCommentHandling = JsonCommentHandling.Skip

   

 JsonSerializerOptions option = new()
            {
                ReadCommentHandling = JsonCommentHandling.Skip
            };


     var soureObject = JsonSerializer.Deserialize<CheckingAccountSource>(jsonText, option);

Allow trailing commas in JSON

If you have trailing commas you might get the below error,

System.Text.Json.JsonException: 'The JSON object contains a trailing comma at the end which is not supported in this mode. 

JsonSerializerOptions option = new()
            {
                ReadCommentHandling = JsonCommentHandling.Skip,
                AllowTrailingCommas = true
            };


            var soureObject = System.Text.Json.JsonSerializer.Deserialize<CheckingAccountSource>(jsonText, option);

Enable Global Serialization settings – ASP.NET Core

The above-discussed techniques apply the JSON serialization settings globally for all the controllers in the ASP.NET Core API or MVC-based application.

Using services.AddMvc or service.AddController along with AddJsonOptions helps us enable global settings for all controllers in a service.

Example:

 




public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers().AddJsonOptions(option =>
            option.JsonSerializerOptions.ReadCommentHandling = JsonCommentHandling.Skip);

..

..
        }





Useful references

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *