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 serialize…


This content originally appeared on DEV Community and was authored by Sukhpinder Singh

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; } }

JSON Output

{ "Property1":"Value1"}

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)]

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

C Sharp Programming


This content originally appeared on DEV Community and was authored by Sukhpinder Singh


Print Share Comment Cite Upload Translate Updates
APA

Sukhpinder Singh | Sciencx (2021-05-10T20:01:49+00:00) Ignore properties C#. Retrieved from https://www.scien.cx/2021/05/10/ignore-properties-c/

MLA
" » Ignore properties C#." Sukhpinder Singh | Sciencx - Monday May 10, 2021, https://www.scien.cx/2021/05/10/ignore-properties-c/
HARVARD
Sukhpinder Singh | Sciencx Monday May 10, 2021 » Ignore properties C#., viewed ,<https://www.scien.cx/2021/05/10/ignore-properties-c/>
VANCOUVER
Sukhpinder Singh | Sciencx - » Ignore properties C#. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/10/ignore-properties-c/
CHICAGO
" » Ignore properties C#." Sukhpinder Singh | Sciencx - Accessed . https://www.scien.cx/2021/05/10/ignore-properties-c/
IEEE
" » Ignore properties C#." Sukhpinder Singh | Sciencx [Online]. Available: https://www.scien.cx/2021/05/10/ignore-properties-c/. [Accessed: ]
rf:citation
» Ignore properties C# | Sukhpinder Singh | Sciencx | https://www.scien.cx/2021/05/10/ignore-properties-c/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.