canton7 / Stylet

A very lightweight but powerful ViewModel-First MVVM framework for WPF for .NET Framework and .NET Core, inspired by Caliburn.Micro.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

System.Text.Json Json serialization / deserialization fails

phamilton4321 opened this issue · comments

Description
Stylet/PropertyChangedBase.cs is missing the attribute [System.Text.Json.Serialization.JsonIgnore] to make classes serializable and deserializable.

To Reproduce

public class Test : Stylet.PropertyChangedBase
{
 public string TestString {get; set; }
}

can be serialized, but has a "PropertyChangedDispatcher": {} object which can't be deserialized.

Version Info

  • Stylet version: 1.3.6
  • Runtime version: 6.0.1

**Solution **

[System.Xml.Serialization.XmlIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public virtual Action<Action> PropertyChangedDispatcher
{
    get { return this._propertyChangedDispatcher; }
    set { this._propertyChangedDispatcher = value; }
}

Description Stylet/PropertyChangedBase.cs is missing the attribute [System.Text.Json.Serialization.JsonIgnore] to make classes serializable and deserializable.

To Reproduce

public class Test : Stylet.PropertyChangedBase
{
 public string TestString {get; set; }
}

can be serialized, but has a "PropertyChangedDispatcher": {} object which can't be deserialized.

Version Info

  • Stylet version: 1.3.6
  • Runtime version: 6.0.1

**Solution **

[System.Xml.Serialization.XmlIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public virtual Action<Action> PropertyChangedDispatcher
{
    get { return this._propertyChangedDispatcher; }
    set { this._propertyChangedDispatcher = value; }
}

You could serialize only what you want. Just mark your class with [JsonObject(MemberSerialization.OptIn)] and properties to serialize with [JsonProperty].

For more info see docs

That would also solve it, but in my opinion is more like a workaround. Especially because the xaml ignore attribute exist in your code.

Scheduled for the next release.

Note that I strongly recommend against serializing ViewModels. They're not meant to be serialized. Create a dedicated model that's meant to be serialized instead.

Thanks a lot!
I know and normally I'm against it too. But when I found this, I had to use the VM...

(this will be closed when the next release is merged)