Home Backend Development C++ How to Exclude Properties from JSON Serialization in C#?

How to Exclude Properties from JSON Serialization in C#?

Jan 23, 2025 pm 04:48 PM

How to Exclude Properties from JSON Serialization in C#?

How to exclude JSON serialization attributes in C#

When serializing an object to JSON, it is often necessary to exclude certain properties to reduce the size of the serialized data or to maintain privacy. This article explores two common methods for excluding JSON serialization of public properties.

Method 1: Use the [JsonIgnore] attribute

If you are using the popular Json.Net library, the [JsonIgnore] attribute provides a direct way to exclude properties. By annotating a property with this attribute, you can instruct the serializer to ignore it during both serialization and deserialization.

For example:

public class Car
{
  // 包含在 JSON 中
  public string Model { get; set; }
  public DateTime Year { get; set; }
  public List<string> Features { get; set; }

  // 排除
  [JsonIgnore]
  public DateTime LastModified { get; set; }
}
Copy after login

Method 2: Use DataContract and DataMember properties

Alternatively, you can use the DataContract and DataMember properties provided by the System.Runtime.Serialization namespace. The DataContract attribute marks the class as serializable, and the DataMember attribute specifies the properties to include.

[DataContract]
public class Computer
{
  // 包含在 JSON 中
  [DataMember]
  public string Name { get; set; }
  [DataMember]
  public decimal SalePrice { get; set; }

  // 排除
  public string Manufacture { get; set; }
  public int StockCount { get; set; }
  public decimal WholeSalePrice { get; set; }
  public DateTime NextShipmentDate { get; set; }
}
Copy after login

Detailed explanation

Both methods work by manipulating the serialization process. The [JsonIgnore] attribute instructs the Json.Net serializer to skip annotated properties, while the DataMember attribute explicitly specifies which properties to serialize.

While the [JsonIgnore] attribute is easier to use, the DataContract and DataMember attributes provide more control over serialization and deserialization. They also allow you to control other aspects of serialization, such as the name of the property and the serialization format.

The above is the detailed content of How to Exclude Properties from JSON Serialization in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the types of values ​​returned by c language functions? What determines the return value? What are the types of values ​​returned by c language functions? What determines the return value? Mar 03, 2025 pm 05:52 PM

What are the types of values ​​returned by c language functions? What determines the return value?

Gulc: C library built from scratch Gulc: C library built from scratch Mar 03, 2025 pm 05:46 PM

Gulc: C library built from scratch

C language function format letter case conversion steps C language function format letter case conversion steps Mar 03, 2025 pm 05:53 PM

C language function format letter case conversion steps

What are the definitions and calling rules of c language functions and what are the What are the definitions and calling rules of c language functions and what are the Mar 03, 2025 pm 05:53 PM

What are the definitions and calling rules of c language functions and what are the

distinct usage and phrase sharing distinct usage and phrase sharing Mar 03, 2025 pm 05:51 PM

distinct usage and phrase sharing

Where is the return value of the c language function stored in memory? Where is the return value of the c language function stored in memory? Mar 03, 2025 pm 05:51 PM

Where is the return value of the c language function stored in memory?

How does the C   Standard Template Library (STL) work? How does the C Standard Template Library (STL) work? Mar 12, 2025 pm 04:50 PM

How does the C Standard Template Library (STL) work?

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? Mar 12, 2025 pm 04:52 PM

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently?

See all articles