-
Notifications
You must be signed in to change notification settings - Fork 49
Description
To be able to serialise into a typical (untagged) typescript union I propose adding support for a JsonUnionEncoding.Untagged ||| JsonUnionEncoding.UnwrapSingleFieldCases combination that support untagged and unwrapped cases.
Types such as the one below are quite common, and adding a tag is unnecessary and unwanted in this case.
type Value = number | number[]
The equivalent type in FSharp would be as follows.
type Value = Choice<decimal, decimal list>
If we support primitive types with the constraint of one unique type per case we can also support deserializing without ambiguity.
Looking at the type type ChoiceOf5 = Choice<int, string, bool, int list, Object> we can deserialize the type knowing only the JsonTokenType. So the following type can be round trip-able without any tags.
Choice1Of5 1 = "1"
Choice2Of5 "F#" = "\"F#\""
Choice3Of5 false = "false"
Choice4Of5 [1,2] = "[1,2]"
Choice5Of5 { name = "Object" } = "{name:\"Object\"}"
Duplicate types could fail or attempt to pick the first matching type.