Skip to content

Add Support for Untagged Unwrapped Single Cases #135

@daniellittledev

Description

@daniellittledev

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions