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

How to handle literal interface instances? #69

Open
tymokvo opened this issue Apr 8, 2024 · 0 comments
Open

How to handle literal interface instances? #69

tymokvo opened this issue Apr 8, 2024 · 0 comments

Comments

@tymokvo
Copy link

tymokvo commented Apr 8, 2024

I frequently need to use the interface object expressions feature when referring to types that come from abstracted core libraries as properties in concrete types.

However, it seems that these types are not serializable in FSharp.Json. E.g.:

#r "nuget: FSharp.Json"

open FSharp.Json

type IThing =
    abstract member value: int

type Thing = { innerThing: IThing }

{
    innerThing =
        { new IThing with
            member _.value = 1
        }
}
|> Json.serialize

yields

FSharp.Json.JsonSerializationError: Unknown type: IThing
   at FSharp.Json.Core.serializeNonOption@104(JsonConfig config, Type t, JsonField jsonField, Object value)
   at FSharp.Json.Core.serializeUnwrapOption@158(JsonConfig config, Type t, JsonField jsonField, Object value)      
   at [email protected](PropertyInfo prop)
   at FSharp.Json.Core.serializeRecord@216(JsonConfig config, Type t, Object therec)
   at FSharp.Json.Json.serializeEx(JsonConfig config, Object theobj)
   at <StartupCode$FSI_0002>.$FSI_0002.main@() in C:\a.fsx:line 10 
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

Is there a way to configure this behavior? Either pass in a serializing function or simply mark these fields to be skipped?

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