Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Types that mutually recur are unable to be serialized with System.Text.Json. #299

Open
jmfallecker opened this issue Mar 20, 2023 · 0 comments

Comments

@jmfallecker
Copy link

I have a Client/Server setup as comes with the template project. I'm using Remoting to communicate between Client/Server. The types I have are mutually recursive as such:

type A = { Bs: B seq } and B = { As = A seq }

I have a DataService defined that implements the IRemoteService interface and a "FakeDataService" that inherits from RemoteHandler.

The goal is to send back fake data (dummy data, but implemented properly) from the Server to the Client via the DataService interface.

The error is given as such:

fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HMP9CBTKP4T3", Request id "0HMP9CBTKP4T3:00000027": An unhandled exception was thrown by the application.
System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 64. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles. Path: $.

What I've attempted includes using ReferenceHandler.Preserve. I used the following function to provide customized JsonSerializerOptions in both Client/Server Startup.fs:

let serializerOptions = fun (options: JsonSerializerOptions) ->
options.ReferenceHandler <- ReferenceHandler.Preserve
JsonFSharpOptions.Default().AddToJsonSerializerOptions(options)

from Client/Startup.fs:

builder.Services.AddRemoting(builder.HostEnvironment, serializerOptions) |> ignore

from Server/Startup.fs:

services.AddRemoting<FakeDataService.DatabaseAccess>(Client.serializerOptions)

It would be ideal if we could choose to use a different Json library, if possible, instead of only having System.Text.Json some other libraries that handle FSharp Serialization will succeed with cyclical references.

Please let me know if there's any more info I can provide. I'll also be playing with the source code to see if I can get a fix for this in place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant