Is there any other way of ignoring a property during JSON serialization instead of using [JsonIgnore] decorator?
Asked Answered
P

2

5

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

Pirate answered 28/11, 2022 at 11:30 Comment(5)
Are you sure you want that model to serialize? In MVVM you typically bind to the viewmodel and serialize the model.Demeanor
Don't think there is another way to ignore. But you could try to set the attribute in a different file: #3782905Stellastellar
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.Due
According to the [official document about the [JsonIgnore] attribute](learn.microsoft.com/en-us/dotnet/standard/serialization/…), it should work. You can try to use [JsonIgnore(Condition = JsonIgnoreCondition.Always)]Wellmeaning
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.Due
D
7

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

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

This generates a property with the attribute attached:

[JsonIgnore]
RecommendationsType RecommendationsList
{
    get ...
    set ...
}
Due answered 29/11, 2022 at 4:18 Comment(6)
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, ...);).Stellastellar
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.Due
But this would require OP to change the generated code, because they have to set the attribute on the backing field.Stellastellar
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.)Due
Oh yes, of course. I forgot.Stellastellar
Works great. Are there any documentations about this?Dynamics
G
0

If after adding [JsonIgnore], it still shows in the serialized string. Probably it's a readonly property and you can add IgnoreReadOnlyProperties = true in the JsonSerializerOptions when serializing. It worked for me, hope it will work you :)

private static JsonSerializerOptions JsonSerializerOptions => new(){
     IgnoreReadOnlyProperties = true,
     DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
Groovy answered 17/4 at 6:38 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Agranulocytosis

© 2022 - 2024 — McMap. All rights reserved.