C# Programming

A passionate self-taught .Net developer from India. Focuses on ASP.Net | ASP.Net Core | .Net & C# advance design, best practices & experiences to make developer

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# Publication, LinkedIn, Instagram, Twitter, Dev.to, Pinterest, Substack, Wix.

Buy Me A Coffee

How to start mining dogecoins with your laptop hash.

Dogecoin began as a joke on the Internet approximately seven years ago by two developers Billy Markus and Jackson Palmer. Today it doesn’t appear to be a joke with a market cap of over 6 billion dollars.

Software for Mining

Step 1

Visit the **Unmineable** website and download the windows software as shown below.

Step 2

Extract and run the software. Click continue, and there are two options available to mine Doge and many other coins.

  • **Graphics Card(GPU)– **mine coins at a higher speed

  • CPU– relatively slower

The article demonstrates mining using the “CPU” option.

Step 3

Select coin and input the wallet address and referral code.

Referral code: “dl5m-b4ki”. It helps in a discount mining fees. Supported algorithms: Ethash, Etchash, RandomX, and KawPow.

1% mining fee for all coins. Earn passive rewards and get a lower fee by using our referral system!

Address: Can be a hard wallet or an exchange wallet address

Step 4

Click start, and it will begin mining the particular coin.

> # “Within 1 hr, I can mine the above-shown doge coins, and with a higher hash rate, it's much faster”

Summary

  • Once you mined 30 doge coins, it's transferrable to the wallet address.

  • Click on the website button to see the progress on the unMineabl