For example, I have a class that derives from ObservableValidator and my resulting serialization then includes HasErrors. I want to only include my derived class's properties and don't have access to modify the base class. Is there a way to do this?
public class Derived: ObservableValidator {
[Required]
[JsonPropertyName("name")]
[ObservableProperty]
private string? _name;
}
serialized json:
{
"name": "Test",
"HasErrors": false
}
How can I make name the only property written to my json?
NewtonSoft has what I want with this decorator, but haven't found an equivalent in System.Text.Json.
[JsonObject(MemberSerialization.OptIn)]
public class File
Could be related to similar issue, I have in addition to this one in which the MVVM generator doesn't propagate the [JsonIgnore]. Maybe need to refactor with non-observable record type class. Is there any other way of ignoring a property during JSON serialization instead of using [JsonIgnore] decorator?
"$type": "Derived",will only show up if you enable support for polymorphism -- and it's not enabled by default. So how are you enabling it? Does theObservableValidatortype do that, by applying[JsonDerivedType(typeof(Derived))]? Or are you doing it through options somehow? Can you share a minimal reproducible example?$typecame from and fixed that part.Derivedalso implemented a small interface (that I can edit) which is where it was from. As I don't need to deserialize, it wasn't needed.JsonExtensions.IgnorePropertiesDeclaredBy<ObservableValidator>()from this answer to Serializing/Deserializing an object that is derived from Java.Lang.Object throws exception (using System.Text.Json) to skip serialization of all properties declared byObservableValidator. Does that answer your question?