除了使用 [JsonIgnore] 装饰器之外,还有其他方法可以在 JSON 序列化过程中忽略属性吗?

问题描述 投票:0回答:1

我正在使用 .net Maui MVVM 源生成器将属性创建为可观察属性。我正在使用 System.Text.Json.Serialization 将类的属性序列化为 JSON。当我在某些属性上使用 [JsonIgnore] 时,它们仍然会序列化为 JSON。还有其他忽略属性的方法吗?

我认为问题是我将装饰器放在私有属性声明上,而不是公共属性声明上,因为公共属性是在依赖项 -> 分析器 -> CommunityToolkit.Mvvm.SourceGenerators 中创建的。

c# data-binding maui json-serialization community-toolkit-mvvm
1个回答
5
投票

我似乎找不到博客文章,但将属性传播到生成的属性是这样的语法:

[property: SomePropertyAttributeHere]

using CommunityToolkit.Mvvm.ComponentModel;
using System.Text.Json.Serialization;
...
[ObservableProperty]
[property: JsonIgnore]
RecommendationsType recommendationsList;

这会生成一个带有附加属性的属性:

[JsonIgnore]
RecommendationsType RecommendationsList
{
    get ...
    set ...
}
© www.soinside.com 2019 - 2024. All rights reserved.