wdl icon indicating copy to clipboard operation
wdl copied to clipboard

Allow serialization/deserialization of `Pair` to/from JSON array

Open jdidion opened this issue 2 years ago • 3 comments

In WDL 1.1, serialization/deserializaton of Pair is not supported.

Since JSON arrays are allowed to be heterogeneous, we should support serialization of Pair[X, Y] to [X, Y] (as long as X and Y are both serializable) and deserialization of a heterogeneous two-element array [X, Y] to Pair[X, Y]. A homogeneous two-element array [X, X] would still deserialize to Array[X] and would require coercion to Pair[X, X].

This would also enable serialization of Map[X, Y] to an array of arrays, for X types other than String.

This change will affect the following areas of the spec:

  • write_json will support serialization of Pairs and Maps of any type
  • read_json will support deserialization of a heterogeneous two-element arrays to a Pair, and of an array of heterogeneous two-element arrays to a Map
  • Coercion of Array[X] to Pair[X, X] will be supported for an array of exactly two elements
  • The standard input/output JSON format will use array as the serialization format for a Pair, and array-of-arrays as the serialization format for a Map with non-String keys

jdidion avatar Mar 28 '23 17:03 jdidion

Overall I am supportive of this idea, although I think the expectation has been a pair would be serialized into something that looks like: { "left": ... , "right": }

I wonder how common the situation is where someone would want a heterogenous pair

patmagee avatar Mar 31 '23 14:03 patmagee

That is an option.

I like using an array because it doesn't require us to introduce a new custom format. Trying to deserialize a heterogeneous array into anything other than a Pair will fail. You could deserialize a homogeneous Pair into an array, but you could also deserialize {"left": X, "right": X} to a Map. There's no unambiguous serialization format for a Pair so in that case I'd argue to just go for the simplest solution.

jdidion avatar Mar 31 '23 14:03 jdidion

we should determine what the current engines do in this scenario, do they fail or do they have some sort of intermediary type conversion

patmagee avatar May 24 '23 19:05 patmagee