ReactiveUI icon indicating copy to clipboard operation
ReactiveUI copied to clipboard

Json serialization problem on System.Text.Json

Open VAllens opened this issue 3 years ago • 3 comments

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:

  1. mark these three properties as virtual to allow subclasses to override them.
  2. in the ReactiveObject and ReactiveRecord classes, add the JsonIgnore attribute to these three properties, but it needs to reference the System.Text.Json package.

FYI. :)

VAllens avatar Feb 21 '22 01:02 VAllens

I personally recommend the first option, which gives more flexibility while reducing the possibility of other dependencies.

VAllens avatar Feb 21 '22 01:02 VAllens

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.

HugoRoss avatar Mar 22 '22 20:03 HugoRoss

Does anyone have any suggestions?

VAllens avatar Mar 24 '22 02:03 VAllens

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;
    }

xecrets avatar Jun 05 '23 08:06 xecrets

Yeah probably time we bite the bullet and swap to using full on system.text.json.

glennawatson avatar Jun 05 '23 09:06 glennawatson

We have added support for System.Text.Json, please raise a new issue with any new findings, thank you

ChrisPulman avatar Jan 01 '24 01:01 ChrisPulman

goooooooooooooooooooooooooood !!!

VAllens avatar Jan 06 '24 08:01 VAllens

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.

github-actions[bot] avatar Jan 21 '24 00:01 github-actions[bot]