Allow JSON parsing to happen in an extension method (#63)

This commit is contained in:
Patrick Stevens
2024-01-08 00:50:33 +00:00
committed by GitHub
parent ad2eeaaa4f
commit 948fbfbc84
12 changed files with 284 additions and 37 deletions

View File

@@ -108,3 +108,68 @@ module JsonRecordType =
E = E
F = F
}
namespace ConsumePlugin
/// Module containing JSON parsing extension members for the ToGetExtensionMethod type
[<AutoOpen>]
module ToGetExtensionMethodJsonParseExtension =
///Extension methods for JSON parsing
type ToGetExtensionMethod with
/// Parse from a JSON node.
static member jsonParse (node : System.Text.Json.Nodes.JsonNode) : ToGetExtensionMethod =
let Sailor =
(match node.["sailor"] with
| null ->
raise (
System.Collections.Generic.KeyNotFoundException (
sprintf "Required key '%s' not found on JSON object" ("sailor")
)
)
| v -> v)
.AsValue()
.GetValue<float> ()
let Soldier =
(match node.["soldier"] with
| null ->
raise (
System.Collections.Generic.KeyNotFoundException (
sprintf "Required key '%s' not found on JSON object" ("soldier")
)
)
| v -> v)
.AsValue()
.GetValue<string> ()
|> System.Uri
let Tailor =
(match node.["tailor"] with
| null ->
raise (
System.Collections.Generic.KeyNotFoundException (
sprintf "Required key '%s' not found on JSON object" ("tailor")
)
)
| v -> v)
.AsValue()
.GetValue<int> ()
let Tinker =
(match node.["tinker"] with
| null ->
raise (
System.Collections.Generic.KeyNotFoundException (
sprintf "Required key '%s' not found on JSON object" ("tinker")
)
)
| v -> v)
.AsValue()
.GetValue<string> ()
{
Tinker = Tinker
Tailor = Tailor
Soldier = Soldier
Sailor = Sailor
}