Zaid-Ajaj / Fable.Remoting

Type-safe communication layer (RPC-style) for F# featuring Fable and .NET Apps
https://zaid-ajaj.github.io/Fable.Remoting/
MIT License
272 stars 54 forks source link

[Question] DotnetClient - serialization of private record type #269

Open Evangelink opened 3 years ago

Evangelink commented 3 years ago

Hi,

I am wondering if it's possible to pass some extra information to the dotnet client serializer on how to deserialize some type?

I have defined a NonEmptyList type which is a simple wrapper around a List with the record as private.

#r "nuget: Fable.Remoting.DotnetClient"

open System

open Fable.Remoting.Json
open Newtonsoft.Json

let converter = FableJsonConverter()

let roundTrip<'T> (obj: 'T) =
    let result = JsonConvert.SerializeObject(obj, converter)
    JsonConvert.DeserializeObject<'T>(result, converter)
    |> printfn "%A"

type NonEmptyList<'T> =
    private {​ ​​List:'T list }​​​

type TestRecord = { ​​​TestList: NonEmptyList<string> }​​​
let testRecord = { ​​​TestList = { ​​​List = [ "" ] } ​​​}​​​

// Run the following and you have an error about private ctor (error makes sense). Remove the `private` keyword before the record and everything works fine.
roundTrip<TestRecord>(testRecord)

I would like to be able to deserialize this type but it seems that there is no way for me to pass a JsonConverter object. What would you recommend?

Zaid-Ajaj commented 2 years ago

Hi @Evangelink, I think the only right now for the FableJsonConverter is to accept types with public constructors. I need to double check

jannesiera commented 2 years ago

Is there a way to add support for this? We use this pattern a lot so that means we currently either can't use Fable.Remoting or we have to make a bunch of constructors public while they really shouldn't be (and has the potential to introduce bugs when someone forgets that the default constructor is off limits).

Zaid-Ajaj commented 2 years ago

Hi @jannesiera it has been a while since last time I looked into this issue and to be honest I don't know the status of it right now, if you could have a go at it, that would be great! I believe it was possible before so now not sure why it didn't work any more. I'll be happy to review PRs, then make time merge and publish the fixes

jannesiera commented 2 years ago

Unfortunately, I have very little time at the moment and won't be able to allocate the resources for this.