SystemTextJson Ignore property for serialization

Today in this article, we shall see how to perform serialization or deserialization using System.Text.Json ignore property when null or empty.

System.Text.Json Ignore property when null or empty is allowed using JsonSerializerOptions with defined specific conditions at the property or global level.

Today in this article, we will cover below aspects,

We will see three basic approaches to achieve the same,

  • Using JsonSerializerOptions at the Method or object level
  • Using JsonSerializerOptions at the Property level
  • Using JsonSerializerOptions at the global level – Ex . Startup.cs

Please note that we will be using JsonIgnoreCondition extensively to ignore any null or empty value properties

Options using JsonIgnoreCondition controls how the System.Text.Json.Serialization.JsonIgnoreAttribute ignores properties while performing serialization and deserialization.

JsonIgnoreCondition is an enum type that allows below options to be used,

  • Never = 0 This is the default property which indicates Property will always be serialized and deserialized, regardless of sonSerializerOptions.IgnoreNullValues

  • Always= 1 Property will always be ignored
  • WhenWritingDefault =2 – This indicates Property will only be ignored if it is null.
  • WhenWritingNull = 3 – This indicates if the property is of type references, then it will be ignored during serialization.

Mostly used JsonSerializerOptions.IgnoreNullValues options is obsolete and its recommended to use DefaultIgnoreCondition to JsonIgnoreCondition.WhenWritingNull.

Using JsonSerializerOptions at the Method or object level

Ignore when null

Below is more easy way to handle the null value or empty values as required.

         


           JsonSerializerOptions option = new()
            {

                DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull

            };

            var stringresult = JsonSerializer.Serialize(soureObject, option);


SystemTextJson Ignore property

Using JsonSerializerOptions at the Property level

You can attribute the JsonIgnoreAttribute at the property level for even better control on what property you want to enable for ignoring during serializations or deserialization.

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]

public class Source
    {
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public Employee Employee { get; set; }
        public string Address { get; set; }
        public string Id { get; set; }
        public object FirstName { get; internal set; }
    }

Using JsonSerializerOptions at the global level

JsonSerializerOptions can be defined at API level supporting any implicit serialization or deserialization easily using the below option,

Example

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers().AddJsonOptions(option =>
            {
                option.JsonSerializerOptions.DefaultIgnoreCondition = 
                JsonIgnoreCondition.WhenWritingNull;
            });

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "SystemTSIgnoreNullValues", Version = "v1" });
            });
        }

Other 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 *