Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@aprooks
Last active October 16, 2017 20:11
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 aprooks/af2ef4ac87d1edc3c55d961dcf17a001 to your computer and use it in GitHub Desktop.
Save aprooks/af2ef4ac87d1edc3c55d961dcf17a001 to your computer and use it in GitHub Desktop.
DU deserialize with chiron
type EventOne =
{ Property: string }
static member FromJson(_: EventOne) =
json {
let! p = Json.read "property"
return {Property = p}
}
type EventTwo =
{ Property: int }
static member FromJson(_: EventTwo) =
json {
let! p = Json.read "property"
return {Property = p}
}
type Event =
| One of EventOne
| Two of EventTwo
type Test1 =
{ Data: Event
EventType: string }
static member FromJson(_: Test1) =
json {
let! eventType = Json.read "eventType"
let! data =
match eventType with
| "one" ->
json {
let! e = Json.read "data"
return e|> One
}
| "two" ->
json{
let! e = Json.read "data"
return e|> Two
}
| _ -> failwith "Not supported"
return {
Data = data
EventType = eventType
}
}
let js = """
{
"eventType": "one",
"data": {
"property": "ABC"
}
}
"""
[<Fact>]
let asdf () =
Json.parse js
|> Json.deserialize =!
{
Data = One ({Property="ABC"})
EventType = "one"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment