Ignore properties —  C#

C# CONCEPTS

Learn how to ignore properties based on numerous measures with System.Text.Json.

Learning Objectives

When serializing C# objects to JSON using “System.Text.Json,” all public properties by default serialized. If you don’t want a few of them to appear in the result, there are several options.

The article demonstrates how to ignore

  1. Individual properties

  2. A null-value properties

  3. All read-only properties

  4. All null-value properties

  5. All default-value properties

Prerequisites

  1. Install latest visual studio community edition.

  2. Knowledge of properties in C# language.

Getting Started

The System.Text.Json namespace renders functionality for serializing to and deserializing from JSON.

Ignore Individual Properties

Use the [JsonIgnore] attribute to ignore particular property. Here’s an example class to serialize and JSON output:

public class Example
{
   public string Property1 { get; set; }    
   [JsonIgnore]     
   public string Property2 { get; set; } 
}

JSON Output

{
    "Property1":"Value1"
}

Ignore Null Value Properties

Option to specify condition with [JsonIgnore] attribute’s property. The JsonIgnoreCondition enum provides the following options:

  1. Always — The property is always ignored. If no Condition is specified, this option is assumed.

  2. Never — The property is always serialized and deserialized, regardless of the DefaultIgnoreCondition, IgnoreReadOnlyProperties, and IgnoreReadOnlyFields global settings.

  3. WhenWritingDefault — The property is ignored on serialization if it’s a reference type null, a nullable value type null, or a value type default.

  4. WhenWritingNull — The property is ignored on serialization if it’s a reference type null or a nullable value type null.

[JsonIgnore(Condition = JsonIgnoreCondition.Always)]

The following example demonstrates the usage of the [JsonIgnore] attribute’s Condition property:

public class Example
{
 [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
 public DateTime Date { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
 public int TempC { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
 public string? Summary { get; set; }
};

Ignore read-only properties

A read-only property, i.e., it contains a public getter but not a setter. To ignore all those properties, set the JsonSerializerOptions.IgnoreReadOnlyProperties to true, as shown in the below example.

var options = new JsonSerializerOptions {   
  IgnoreReadOnlyProperties = true,     
  WriteIndented = true 
};

Serialization Use

jsonString = JsonSerializer.Serialize(classObject, options);

This option affects only serialization. While deserialization, read-only properties are neglected by default.

Ignore all null-value Properties

To neglect each null-value property, set the DefaultIgnoreCondition property to WhenWritingNull, as explained in the following example:

JsonSerializerOptions options = new(){   
  DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull             };

Serialization Use

jsonString = JsonSerializer.Serialize(classObject, options);

Ignore all default-value Properties

To counter serialization of default values in properties, set the DefaultIgnoreCondition property to WhenWritingDefault, as shown below.

JsonSerializerOptions options = new(){   
  DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault             };

Serialization Use

jsonString = JsonSerializer.Serialize(classObject, options);

Thank you for reading, and I hope you liked the article.

Stay tuned on C#

View at Medium.com

#Serialization #Csharp #Dotnet #Programming #Json

Recent Posts

See All

Design Pattern – Adapter

#Aspnetcore #AdapterDesignPattern #Csharp #DesignPatterns #Dotnet According to Gang of Four, the Adapter Pattern converts the interfaces of a class into interfaces that the client requires. In other w