Last active
October 16, 2017 20:11
-
-
Save aprooks/af2ef4ac87d1edc3c55d961dcf17a001 to your computer and use it in GitHub Desktop.
DU deserialize with chiron
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
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