4

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?

5
  • "$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 the ObservableValidator type do that, by applying [JsonDerivedType(typeof(Derived))]? Or are you doing it through options somehow? Can you share a minimal reproducible example? Commented Feb 26 at 23:51
  • Thank you dbc, I actually had just realized that's where $type came from and fixed that part. Derived also 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. Commented Feb 26 at 23:54
  • 1
    You could use the typeInfo modifier 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 by ObservableValidator. Does that answer your question? Commented Feb 27 at 0:13
  • Did Serializing/Deserializing an object that is derived from Java.Lang.Object throws exception (using System.Text.Json) answer your question, or do you still need some additional help? Commented Feb 27 at 17:52
  • Thank you, that does work for me. If you want to submit an answer, I will accept it. Commented Feb 27 at 19:36

2 Answers 2

3

There's an open issue about this. Voting up there might speed up the fix.

As a work around, you can use a custom type info resolver:

public sealed class IgnoreHasErrorsTypeResolver : DefaultJsonTypeInfoResolver
{
    public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options)
    {
        JsonTypeInfo jsonTypeInfo = base.GetTypeInfo(type, options);

        if (jsonTypeInfo
            .Properties
            .FirstOrDefault(property => property.Name is nameof(ObservableValidator.HasErrors)) is JsonPropertyInfo hasErrorsJsonPropertyInfo)
        {
            jsonTypeInfo.Properties.Remove(hasErrorsJsonPropertyInfo);
        }

        return jsonTypeInfo;
    }
}

and use it like:

JsonSerializerOptions options = new()
{
    TypeInfoResolver = new IgnoreHasErrorsTypeResolver(),
};

string jsonText = JsonSerializer.Serialize(someObject, options);
Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me! Thank you... Now I can take my DependancyProperty class and serialize it.
0

I would create a model/dto and map the object from the class in question to the dto.

For example:

public class Derived: ObservableValidator {
    [Required]
    [ObservableProperty]
    public string? _name;
}

public class DerivedModel {
    [JsonPropertyName("name")] 
    public string? _name;
}


// Usage:

var originalObject = new Derived()
originalObject.name = "Hello World!";

var derivedObject = new DerivedModel();
originalObject.name = originalObject.name;

Now apply the serialization on the derivedObject.

Do note that the above code is a basic example and can be significantly improved upon by

  • using property instead of fields
  • Using https://automapper.org/ to simplify conversions from one user-defined type to another.
  • even using records for the model instead of class

Let me know if you'd like a more elaborate example with the above implementations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.