thoth-org/Thoth.Json

Mutualise `encodeArray`, `encodeList`, `encodeSeq`

Closed this issue · 4 comments

In the encoder helpers we have several ways to encode a "sequence" of items.

abstract encodeArray: 'JsonValue array -> 'JsonValue
abstract encodeList: 'JsonValue list -> 'JsonValue
abstract encodeSeq: 'JsonValue seq -> 'JsonValue

Should we remove all of them except one ?

I suppose the only reason for keeping them separate would be to avoid a transformation phase when supported by the JSON backend. Would mapping them have a lot of impact on performance?

For example, Newtonsoft backend is implemented has which means we pass the "sequence" as is:

member _.encodeArray values = JArray(values)
member _.encodeList values = JArray(values)
member _.encodeSeq values = JArray(values)

For other backend we do a mapping:

member _.encodeArray values = values
member this.encodeList values =
values |> List.toArray |> this.encodeArray
member this.encodeSeq values =
values |> Seq.toArray |> this.encodeArray

@njlr What do you think about keeping only one of the member? If we keep only one which one should we keep?

I know that some people say that F# libraries should expose seq should it be this one? Or does it have a performance cost and we should favour ResizeArray or array?

njlr commented

I think it's better to have several functions.

  1. There are potential performance benefits that some back-ends may utilize
  2. Some F# users like to specify types in their by using specialized functions

e.g.

let foo xs = 
  xs 
  |> Array.map (fun x -> x + 1) // Makes xs an array
  1. There are potential performance benefits that some back-ends may utilize

Agree

2. Some F# users like to specify types in their by using specialized functions

I don't understand the relation as helpers are internal APIs.

But we will keep the separate APIs, does this means we should add a new one for ResizeArray too ? 🤔

njlr commented

I don't understand the relation as helpers are internal APIs.

Ah, I was responding to removing Encode.list, Encode.array, etc. in favor of just Encode.seq

But we will keep the separate APIs, does this means we should add a new one for ResizeArray too ? 🤔

I think this makes sense to add!

Ah, I was responding to removing Encode.list, Encode.array, etc. in favor of just Encode.seq

Ah yes, no I would still have exposed the different types for the Encode/Decode API