2

I am using .net Maui MVVM source generators to create properties as observable properties. I am using System.Text.Json.Serialization to serialize the properties of a class to JSON. When I use [JsonIgnore] on some properties they still get serialized to JSON. Is there any other way of ignoring properties?

I assume that the problem is that I am putting the decorator on the private property declaration and not the public one as the public ones are created in dependencies -> analyzers -> CommunityToolkit.Mvvm.SourceGenerators.

enter image description here

  • Are you sure you want that model to serialize? In MVVM you typically bind to the viewmodel and serialize the model. – Ralf Nov 28 '22 at 11:54
  • Don't think there is another way to ignore. But you could try to set the attribute in a different file: https://stackoverflow.com/questions/3782405/can-i-define-properties-in-partial-classes-then-mark-them-with-attributes-in-an – Good Night Nerd Pride Nov 28 '22 at 12:03
  • I can't seem to find the blog post, but propagation of adornments to the generated property will look like `[property: JsonIgnore]`. I don't know if that is in production build yet. – ToolmakerSteve Nov 29 '22 at 04:17
  • According to the [official document about the [JsonIgnore] attribute](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/ignore-properties?pivots=dotnet-7-0), it should work. You can try to use `[JsonIgnore(Condition = JsonIgnoreCondition.Always)]` – Liyun Zhang - MSFT Nov 29 '22 at 09:54
  • PLEASE copy code into question as text, rather than as an image. I had to manually re-type that, to use it in my answer. – ToolmakerSteve Nov 29 '22 at 20:17

1 Answers1

3

I can't seem to find the blog post, but propagation of attribute to the generated property is this syntax: [property: SomePropertyAttributeHere]:

[ObservableProperty]
[property: JsonIgnore]
RecommendationsType recommendationsList;

This generates a property with the attribute attached:

[JsonIgnore]
RecommendationsType RecommendationsList
{
    get ...
    set ...
}
ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
  • This feature is production ready and I thought of the same, but this syntax does not allow to specify a target property. So it cannot be used in `AssemblyInfo.cs` for instance, because there is no way to tell the compiler which property is supposed to get the attribute. I think it's only supposed to be used in the "vicinity" of properties (e.g. in `record Foo([property: NotNullWhen(...)] string? Bar, ...);`). – Good Night Nerd Pride Nov 29 '22 at 11:29
  • Right, it is always used with a property Generator. I don't know anything about `AssemblyInfo.cs`, but for this question it goes with `[ObservableProperty]` attribute. It gets transferred to the generated property. Updated answer. – ToolmakerSteve Nov 29 '22 at 20:22
  • But this would require OP to change the generated code, because they have to set the attribute on the backing field. – Good Night Nerd Pride Nov 30 '22 at 11:25
  • No, the backing field is what they have in *their* source. It is what is shown in question. The public property is what gets generated. (If their source had the public property, then they would simply put `[JsonIgnore]` on it, and be done.) – ToolmakerSteve Nov 30 '22 at 19:02
  • Oh yes, of course. I forgot. – Good Night Nerd Pride Dec 01 '22 at 08:56