-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTemplate.Json.fs
101 lines (79 loc) · 3.54 KB
/
Template.Json.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
module ARCtrl.Template.Json
open ARCtrl.Template
open ARCtrl.ISA
open System
open ARCtrl
open Thoth.Json.Core
open ARCtrl.ISA.Json
//https://thoth-org.github.io/Thoth.Json/documentation/auto/extra-coders.html#ready-to-use-extra-coders
module Organisation =
let encode = (fun (org: Organisation) -> org.ToString()) >> Encode.string
let decode =
Decode.string
|> Decode.andThen (fun textValue ->
Organisation.ofString textValue
|> Decode.succeed
)
module Template =
let encode (template: Template) =
let personEncoder = ARCtrl.ISA.Json.Person.encoder (ConverterOptions())
let oaEncoder = ARCtrl.ISA.Json.OntologyAnnotation.encoder (ConverterOptions())
Encode.object [
"id", Encode.guid template.Id
"table", ArcTable.encoder template.Table
"name", Encode.string template.Name
"description", Encode.string template.Description
"organisation", Organisation.encode template.Organisation
"version", Encode.string template.Version
"authors", Encode.list [
for a in template.Authors do yield personEncoder a
]
"endpoint_repositories", Encode.list [
for oa in template.EndpointRepositories do yield oaEncoder oa
]
"tags", Encode.list [
for oa in template.Tags do yield oaEncoder oa
]
"last_updated", Encode.datetime template.LastUpdated
]
let decode : Decoder<Template> =
let personDecoder = ARCtrl.ISA.Json.Person.decoder (ConverterOptions())
let oaDecoder = ARCtrl.ISA.Json.OntologyAnnotation.decoder (ConverterOptions())
Decode.object(fun get ->
Template.create(
get.Required.Field "id" Decode.guid,
get.Required.Field "table" ArcTable.decoder,
get.Required.Field "name" Decode.string,
get.Required.Field "description" Decode.string,
get.Required.Field "organisation" Organisation.decode,
get.Required.Field "version" Decode.string,
get.Required.Field "authors" (Decode.array personDecoder),
get.Required.Field "endpoint_repositories" (Decode.array oaDecoder),
get.Required.Field "tags" (Decode.array oaDecoder),
get.Required.Field "last_updated" Decode.datetimeUtc
)
)
let fromJsonString (jsonString: string) =
try GDecode.fromJsonString decode jsonString with
| exn -> failwithf "Error. Given json string cannot be parsed to Template: %A" exn
let toJsonString (spaces: int) (template:Template) =
GEncode.toJsonString spaces (encode template)
module Templates =
let encode (templateList: (string*Template) []) =
templateList
|> Array.toList
|> List.map (fun (p: string, t: Template) -> p , Template.encode t)
|> Encode.object
let decode =
Decode.dict Template.decode
let fromJsonString (jsonString: string) =
try GDecode.fromJsonString decode jsonString with
| exn -> failwithf "Error. Given json string cannot be parsed to Templates map: %A" exn
let toJsonString (spaces: int) (templateList: (string*Template) []) =
GEncode.toJsonString spaces (encode templateList)
[<AutoOpen>]
module Extension =
type Template with
member this.ToJson(?spaces: int) =
let spaces = defaultArg spaces 0
Template.toJsonString(spaces)