Skip to content
This repository has been archived by the owner on Nov 8, 2019. It is now read-only.

AutoDecode of seq #96

Closed
SCullman opened this issue Nov 30, 2018 · 7 comments
Closed

AutoDecode of seq #96

SCullman opened this issue Nov 30, 2018 · 7 comments

Comments

@SCullman
Copy link
Contributor

open Thoth.Json
let json = "[ \"A\" ]"

// returns Ok [A]
Decode.Auto.fromString<string list>( json) |> printfn "%A"

// returns Ok A
Decode.Auto.fromString<string array>( json) |> printfn "%A"

// Error Error at: `$`
// I run into a `fail` decoder: Class types cannot be automatically deserialized: 
// System.Collections.Generic.IEnumerable`1[System.String]
Decode.Auto.fromString<string seq>( json) |> printfn "%A"

It would be nice if Sequences were supported.

@xdaDaveShaw
Copy link

This would still have to be a concrete collection underneath, it would end up been a list or array and just cast as seq.

Could you not deserialise to a list and than pipe that to List.toSeq?

@MangelMaxime
Copy link
Owner

I indeed don't really know sequence so I am not sure of the right approach. But indeed, my way to go would be to decode it like a list and then directly cast to seq.

@alfonsogarciacaro Would this be the correct solution ?

Also is System.Collections.Generic.IEnumerable always a seq ?

@SCullman
Copy link
Contributor Author

SCullman commented Dec 1, 2018

Sequences are represented by the seq<'T> type, which is an alias for System.Collections.Generic.IEnumerable. Link

I would expect decoding with array or list, and then the cast to seq.

@xdaDaveShaw Well, I had a problem decoding lists in IE11, and it turned out to be a well known problem with polyfill.io. And on the way to solve my problem I tried also arrays and finally seq.

@MangelMaxime
Copy link
Owner

Ok thanks for taking a look at the documentation @SCullman :)

@SCullman
Copy link
Contributor Author

SCullman commented Dec 1, 2018

I was curious whether/ where I would find the alias for seq in source, just for my own understanding. To my surprise I couldn't find it directly in Fable.Core, but then in in FSharp.Core/prim-types. :
type seq<'T> = IEnumerable<'T>

@MangelMaxime
Copy link
Owner

We released Thoth.Json v3 beta I think we added support for seq in it.

And you can also teach auto decoders how to support more types: https://mangelmaxime.github.io/Thoth/json/v3.html#extra-coders

@MangelMaxime
Copy link
Owner

Now tracked in thoth-org/Thoth.Json#26

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

No branches or pull requests

3 participants