Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freerangeeggs/f58c39060d0ddd2349ac7d2cbda23b6c to your computer and use it in GitHub Desktop.
Save freerangeeggs/f58c39060d0ddd2349ac7d2cbda23b6c to your computer and use it in GitHub Desktop.
FSharp.Json Union Discriminator as Field
#r "nuget: FSharp.Json"
open FSharp.Json
[<JsonUnion(Mode = UnionMode.CaseKeyDiscriminatorField, CaseKeyField="version")>]
type VersionedDto =
| V0 of DtoV0
| V1 of DtoV1
and DtoV0 = {
Prop1: string
Prop2: int
}
and DtoV1 = {
Prop1: string
Prop2: int
ExtraProp: bool
}
let dtos = [
V0 { Prop1 = "Hello"; Prop2 = 42}
V1 { Prop1 = "Hello"; Prop2 = 42; ExtraProp = false}
]
let json = Json.serialize dtos
printfn "%s" json
let deserialized = Json.deserialize<VersionedDto list> json
printfn "%A" deserialized
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment