Created
October 29, 2021 02:33
-
-
Save VivekRajagopal/bb32f4f4bad8d16ff4165ebd5f5c24a5 to your computer and use it in GitHub Desktop.
FSharp.Json Union Discriminator as Field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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