Json serialization problem on System.Text.Json
System.Text.Json does not use the System.Runtime.Serialization.IgnoreDataMember attribute, it uses the System.Text.Json.Serialization.JsonIgnore attribute defined by itself.
Therefore, Changing and Changed and ThrownExceptions are serialized.
I have two suggestions:
- mark these three properties as
virtualto allow subclasses to override them. - in the
ReactiveObjectandReactiveRecordclasses, add theJsonIgnoreattribute to these three properties, but it needs to reference theSystem.Text.Jsonpackage.
FYI. :)
I personally recommend the first option, which gives more flexibility while reducing the possibility of other dependencies.
A quick check shows that there are other usages of the IgnoreDataMember in the repo:
- ReactiveUI\Platforms\android\ReactiveViewHost.cs
- ReactiveUI\ReactiveObject\ReactiveObject.cs
- ReactiveUI\ReactiveObject\ReactiveRecord.cs
- ReactiveUI\Routing\RoutingState.cs
- ReactiveUI.AndroidSupport\ReactiveRecyclerViewViewHolder.cs
- ReactiveUI.AndroidX\ReactiveRecyclerViewViewHolder.cs
- ReactiveUI.Tests\ReactiveObject\Mocks\OaphNameOfTestFixture.cs
- ReactiveUI.Tests\ReactiveObject\Mocks\OaphTestFixture.cs
- ReactiveUI.Tests\ReactiveObject\Mocks\TestFixture.cs
- ReactiveUI.Tests\ReactiveObject\Mocks\WhenAnyTestFixture.cs
Whether these are all candidates for Json serialization with System.Text.Json I can't say.
Does anyone have any suggestions?
Seeing as System.Text.Json appears to be the .NET way forward, which also supports source generation for reflection-free serialization, and thus assembly trimming etc, adding the reference to System.Text.Json seems fairly non-controversial.
As a work around until this is fixed in a better way, one way or another, the following hack can work for the special case of serializing a ReactiveObject (beware, it's a hack, there might be some edge cases not handled, but it does handle dangling commas). Also, in real code, use the [GeneratedRegex] attribute for performance.
private static string CleanUpSerializedReactiveObjectHack(string json)
{
json = Regex.Replace(json, @"\s*""(Changing|Changed|ThrownExceptions)"":\s*\{},?\n?", string.Empty);
json = Regex.Replace(json, @",(\s*})", "$1");
return json;
}
Yeah probably time we bite the bullet and swap to using full on system.text.json.
We have added support for System.Text.Json, please raise a new issue with any new findings, thank you
goooooooooooooooooooooooooood !!!
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.