DEV Community

Cover image for Ignore properties C#
Sukhpinder Singh
Sukhpinder Singh

Posted on • Edited on

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

  • Individual properties

  • A null-value properties

  • All read-only properties

  • All null-value properties

  • All default-value properties

Prerequisites

  • Install latest visual studio community edition.

  • 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; } }
Enter fullscreen mode Exit fullscreen mode

JSON Output

{ "Property1":"Value1"}
Enter fullscreen mode Exit fullscreen mode

Ignore Null Value Properties

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

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

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

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

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

[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
Enter fullscreen mode Exit fullscreen mode

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; }};
Enter fullscreen mode Exit fullscreen mode

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 };
Enter fullscreen mode Exit fullscreen mode

Serialization Use

jsonString = JsonSerializer.Serialize(classObject, options);
Enter fullscreen mode Exit fullscreen mode

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 };
Enter fullscreen mode Exit fullscreen mode

Serialization Use

jsonString = JsonSerializer.Serialize(classObject, options);
Enter fullscreen mode Exit fullscreen mode

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 };
Enter fullscreen mode Exit fullscreen mode

Serialization Use

jsonString = JsonSerializer.Serialize(classObject, options);
Enter fullscreen mode Exit fullscreen mode

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

Stay tuned on C

C Sharp Programming

Top comments (0)