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

Serialization issue with type implementing IEnumerable #278

Open
Evangelink opened this issue Nov 1, 2021 · 1 comment
Open

Serialization issue with type implementing IEnumerable #278

Evangelink opened this issue Nov 1, 2021 · 1 comment

Comments

@Evangelink
Copy link

Issue relates to #269 but is a little different.

With the code given on #269 making the type public solves the issue but the code sample was a little too simplified.

My real type is as follow:

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

    member this.Head = this.List.Head
    member this.Tail = this.List.Tail
    member this.Length = this.List.Length
    member this.Item index = this.List.[index]

    interface IEnumerable<'T> with
        member this.GetEnumerator() = (this.List :> _ seq).GetEnumerator()

    interface IEnumerable with
        member this.GetEnumerator() = (this.List :> _ seq).GetEnumerator() :> IEnumerator

    interface IReadOnlyCollection<'T> with
        member this.Count = this.Length

and this time, even removing the private keyword (hence allowing a pubic ctor) still results in a serialization issue Cannot create and populate list type NonEmptyList.

This seems to be a known issue for Newtonsoft when implementing IEnumerable<T> (see for example https://stackoverflow.com/questions/36926867/deserializing-json-into-a-list-of-objects-cannot-create-and-populate-list-type) but the issue is solved on recent versions (see https://stackoverflow.com/a/36927705/4625433) as long as the type contains a public constructor accepting a IEnumerable<T> argument which isn't the case for our fsharp types.

I haven't dig deeper to understand if there is anything that could be done on your side but I thought that might be better to post it.

@Zaid-Ajaj
Copy link
Owner

I'll have a look!

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

2 participants