Skip to content

Commit 5150773

Browse files
committed
Start workin on new Json API layer 🚧
1 parent f65d414 commit 5150773

5 files changed

+182
-127
lines changed

src/Json/ARCtrl.Json.fsproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
<Compile Include="Decode.fs" />
3333
<Compile Include="Encode.fs" />
3434
<Compile Include="ROCrateHelper.fs" />
35-
<Compile Include="Comment.fs" />
3635
<Compile Include="StringTable.fs" />
36+
<Compile Include="Comment.fs" />
3737
<Compile Include="OntologyAnnotation.fs" />
3838
<Compile Include="OntologySourceReference.fs" />
3939
<Compile Include="Publication.fs" />

src/Json/Comment.fs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ namespace ARCtrl.Json
22

33
open Thoth.Json.Core
44
open ARCtrl
5-
open System.IO
65

76
module Comment =
87

@@ -51,7 +50,7 @@ module Comment =
5150
)
5251

5352
let encoderDisambiguatingDescription (comment : Comment) =
54-
encoder comment |> Encode.toJsonString 2 |> Encode.string
53+
encoder comment |> Encode.toJsonString 0 |> Encode.string
5554

5655
let decoderDisambiguatingDescription : Decoder<Comment> =
5756
Decode.string

src/Json/Decode.fs

+5-5
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ module Decode =
3939
true
4040
//s.StartsWith("http://") || s.StartsWith("https://")
4141

42-
let uri =
43-
{ new Decoder<URI> with
42+
let uri : Decoder<URI> =
43+
{ new Decoder<URI> with
4444
member this.Decode(s,json) =
4545
match Decode.string.Decode(s,json) with
4646
| Ok s when isURI s -> Ok s
@@ -66,12 +66,12 @@ module Decode =
6666
helpers.getProperties json
6767
|> Seq.exists (fun x -> not (knownFields |> Set.contains x))
6868

69-
let object (allowedFields : string seq) (builder: Decode.IGetters -> 'value) : Decoder<'value> =
70-
let allowedFields = Set.ofSeq allowedFields
69+
let objectNoAdditionalProperties (allowedProperties : string seq) (builder: Decode.IGetters -> 'value) : Decoder<'value> =
70+
let allowedProperties = Set.ofSeq allowedProperties
7171
{ new Decoder<'value> with
7272
member _.Decode(helpers, value) =
7373
let getters = Decode.Getters(helpers, value)
74-
if hasUnknownFields helpers allowedFields value then
74+
if hasUnknownFields helpers allowedProperties value then
7575
Error (DecoderError("Unknown fields in object", ErrorReason.BadPrimitive("",value)))
7676
else
7777
let result = builder getters

src/Json/OntologyAnnotation.fs

+80-118
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace ARCtrl.Json
22

33
open Thoth.Json.Core
4-
54
open ARCtrl
65
open System.IO
76

@@ -19,156 +18,119 @@ module AnnotationValue =
1918
Decode.map string Decode.float
2019
Decode.string
2120
]
22-
23-
module OntologySourceReference =
24-
25-
let genID (o:OntologySourceReference) =
26-
match o.File with
27-
| Some f -> f
28-
| None ->
29-
match o.Name with
30-
| Some n -> "#OntologySourceRef_" + n.Replace(" ","_")
31-
| None -> "#DummyOntologySourceRef"
32-
33-
let encoder (options : ConverterOptions) (osr : OntologySourceReference) =
34-
[
35-
if options.SetID then
36-
"@id", Encode.string (osr |> genID)
37-
if options.IsJsonLD then
38-
"@type", Encode.string "OntologySourceReference"
39-
Encode.tryInclude "description" Encode.string (osr.Description)
40-
Encode.tryInclude "file" Encode.string (osr.File)
41-
Encode.tryInclude "name" Encode.string (osr.Name)
42-
Encode.tryInclude "version" Encode.string (osr.Version)
43-
Encode.tryIncludeSeq "comments" (Comment.encoder options) (osr.Comments)
44-
if options.IsJsonLD then
45-
"@context", ROCrateContext.OntologySourceReference.context_jsonvalue
46-
]
47-
|> Encode.choose
48-
|> Encode.object
49-
50-
let decoder (options : ConverterOptions) : Decoder<OntologySourceReference> =
51-
Decode.object (fun get ->
52-
OntologySourceReference(
53-
?description = get.Optional.Field "description" Decode.uri,
54-
?file = get.Optional.Field "file" Decode.string,
55-
?name = get.Optional.Field "name" Decode.string,
56-
?version = get.Optional.Field "version" Decode.string,
57-
?comments = get.Optional.Field "comments" (Decode.resizeArray (Comment.decoder options))
58-
)
59-
)
60-
61-
let fromJsonString (s:string) =
62-
Decode.fromJsonString (decoder (ConverterOptions())) s
63-
64-
let fromJsonldString (s:string) =
65-
Decode.fromJsonString (decoder (ConverterOptions(IsJsonLD=true))) s
66-
67-
let toJsonString (oa:OntologySourceReference) =
68-
encoder (ConverterOptions()) oa
69-
|> Encode.toJsonString 2
70-
71-
/// exports in json-ld format
72-
let toJsonldString (oa:OntologySourceReference) =
73-
encoder (ConverterOptions(SetID=true,IsJsonLD=true)) oa
74-
|> Encode.toJsonString 2
75-
76-
let toJsonldStringWithContext (a:OntologySourceReference) =
77-
encoder (ConverterOptions(SetID=true,IsJsonLD=true)) a
78-
|> Encode.toJsonString 2
79-
80-
// let fromFile (path : string) =
81-
// File.ReadAllText path
82-
// |> fromString
83-
84-
//let toFile (path : string) (osr:OntologySourceReference) =
85-
// File.WriteAllText(path,toString osr)
21+
8622

8723
module OntologyAnnotation =
88-
89-
let genID (o:OntologyAnnotation) : string =
90-
match o.TermAccessionNumber with
91-
| Some ta -> URI.toString ta
92-
| None -> match o.TermSourceREF with
93-
| Some r -> "#" + r.Replace(" ","_")
94-
| None -> match o.Name with
95-
| Some n -> "#UserTerm_" + n .Replace(" ","_")
96-
| None -> "#DummyOntologyAnnotation"
97-
98-
let encoder (options : ConverterOptions) (oa : OntologyAnnotation) =
99-
let commentEncoder = if options.IsJsonLD then Comment.encoderDisambiguatingDescription else Comment.encoder options
24+
25+
let encoder (oa : OntologyAnnotation) =
10026
[
101-
if options.SetID then
102-
"@id", Encode.string (oa |> genID)
103-
if options.IsJsonLD then
104-
"@type", Encode.string "OntologyAnnotation"
10527
Encode.tryInclude "annotationValue" Encode.string (oa.Name)
10628
Encode.tryInclude "termSource" Encode.string (oa.TermSourceREF)
10729
Encode.tryInclude "termAccession" Encode.string (oa.TermAccessionNumber)
108-
Encode.tryIncludeSeq "comments" commentEncoder (oa.Comments)
109-
if options.IsJsonLD then
110-
"@context", ROCrateContext.OntologyAnnotation.context_jsonvalue
30+
Encode.tryIncludeSeq "comments" Comment.encoder (oa.Comments)
11131
]
11232
|> Encode.choose
11333
|> Encode.object
11434

11535

116-
let decoder (options : ConverterOptions) : Decoder<OntologyAnnotation> =
36+
let decoder : Decoder<OntologyAnnotation> =
11737
Decode.object (fun get ->
11838
OntologyAnnotation.create(
119-
?name = get.Optional.Field "annotationValue" (AnnotationValue.decoder options),
39+
?name = get.Optional.Field "annotationValue" (AnnotationValue.decoder),
12040
?tsr = get.Optional.Field "termSource" Decode.string,
12141
?tan = get.Optional.Field "termAccession" Decode.string,
122-
?comments = get.Optional.Field "comments" (Decode.resizeArray (Comment.decoder options))
42+
?comments = get.Optional.Field "comments" (Decode.resizeArray Comment.decoder)
12343
)
12444
)
12545

126-
127-
let compressedEncoder (stringTable : StringTableMap) (options : ConverterOptions) (oa : OntologyAnnotation) =
46+
let compressedEncoder (stringTable : StringTableMap) (oa : OntologyAnnotation) =
12847
[
129-
if options.SetID then "@id", Encode.string (oa |> genID)
130-
if options.IsJsonLD then "@type", Encode.string "OntologyAnnotation"
13148
Encode.tryInclude "a" (StringTable.encodeString stringTable) (oa.Name)
13249
Encode.tryInclude "ts" (StringTable.encodeString stringTable) (oa.TermSourceREF)
13350
Encode.tryInclude "ta" (StringTable.encodeString stringTable) (oa.TermAccessionNumber)
134-
Encode.tryIncludeSeq "comments" (Comment.encoder options) (oa.Comments)
51+
Encode.tryIncludeSeq "comments" Comment.encoder (oa.Comments)
13552
]
13653
|> Encode.choose
13754
|> Encode.object
13855

13956

140-
let compressedDecoder (stringTable : StringTableArray) (options : ConverterOptions) : Decoder<OntologyAnnotation> =
57+
let compressedDecoder (stringTable : StringTableArray) : Decoder<OntologyAnnotation> =
14158
Decode.object (fun get ->
14259
OntologyAnnotation(
14360
?name = get.Optional.Field "a" (StringTable.decodeString stringTable),
14461
?tsr = get.Optional.Field "ts" (StringTable.decodeString stringTable),
14562
?tan = get.Optional.Field "ta" (StringTable.decodeString stringTable),
146-
?comments = get.Optional.Field "comments" (Decode.resizeArray (Comment.decoder options))
63+
?comments = get.Optional.Field "comments" (Decode.resizeArray Comment.decoder)
14764
)
14865
)
14966

150-
let fromJsonString (s:string) =
151-
Decode.fromJsonString (decoder (ConverterOptions())) s
67+
module ROCrate =
68+
69+
let genID (o:OntologyAnnotation) : string =
70+
match o.TermAccessionNumber with
71+
| Some ta -> URI.toString ta
72+
| None ->
73+
match o.TermSourceREF with
74+
| Some r -> "#" + r.Replace(" ","_")
75+
| None ->
76+
match o.Name with
77+
| Some n -> "#UserTerm_" + n .Replace(" ","_")
78+
| None -> "#DummyOntologyAnnotation"
79+
80+
let encoder (oa : OntologyAnnotation) =
81+
[
82+
"@id", Encode.string (oa |> genID)
83+
"@type", Encode.string "OntologyAnnotation"
84+
Encode.tryInclude "annotationValue" Encode.string (oa.Name)
85+
Encode.tryInclude "termSource" Encode.string (oa.TermSourceREF)
86+
Encode.tryInclude "termAccession" Encode.string (oa.TermAccessionNumber)
87+
Encode.tryIncludeSeq "comments" Comment.ROCrate.encoderDisambiguatingDescription (oa.Comments)
88+
"@context", ROCrateContext.OntologyAnnotation.context_jsonvalue
89+
]
90+
|> Encode.choose
91+
|> Encode.object
92+
93+
let decoder : Decoder<OntologyAnnotation> =
94+
Decode.object (fun get ->
95+
OntologyAnnotation.create(
96+
?name = get.Optional.Field "annotationValue" AnnotationValue.decoder,
97+
?tsr = get.Optional.Field "termSource" Decode.string,
98+
?tan = get.Optional.Field "termAccession" Decode.string,
99+
?comments = get.Optional.Field "comments" (Decode.resizeArray Comment.decoder)
100+
)
101+
)
102+
103+
module ISAJson =
152104

153-
let fromJsonldString (s:string) =
154-
Decode.fromJsonString (decoder (ConverterOptions(IsJsonLD=true))) s
155-
156-
let toJsonString (oa:OntologyAnnotation) =
157-
encoder (ConverterOptions()) oa
158-
|> Encode.toJsonString 2
159-
160-
/// exports in json-ld format
161-
let toJsonldString (oa:OntologyAnnotation) =
162-
encoder (ConverterOptions(SetID=true,IsJsonLD=true)) oa
163-
|> Encode.toJsonString 2
164-
165-
let toJsonldStringWithContext (a:OntologyAnnotation) =
166-
encoder (ConverterOptions(SetID=true,IsJsonLD=true)) a
167-
|> Encode.toJsonString 2
168-
169-
//let fromFile (path : string) =
170-
// File.ReadAllText path
171-
// |> fromString
172-
173-
//let toFile (path : string) (oa:OntologyAnnotation) =
174-
// File.WriteAllText(path,toString oa)
105+
let encoder = encoder
106+
let decoder = decoder
107+
108+
[<AutoOpen>]
109+
module OntologyAnnotationExtensions =
110+
111+
type OntologyAnnotation with
112+
113+
static member fromJsonString (s:string) =
114+
Decode.fromJsonString OntologyAnnotation.decoder s
115+
116+
static member toJsonString(?spaces) =
117+
fun (obj:OntologyAnnotation) ->
118+
OntologyAnnotation.encoder obj
119+
|> Encode.toJsonString (defaultArg spaces 2)
120+
121+
static member fromROCrateJsonString (s:string) =
122+
Decode.fromJsonString OntologyAnnotation.ROCrate.decoder s
123+
124+
/// exports in json-ld format
125+
static member toROCrateJsonString(?spaces) =
126+
fun (obj:OntologyAnnotation) ->
127+
OntologyAnnotation.ROCrate.encoder obj
128+
|> Encode.toJsonString (defaultArg spaces 2)
129+
130+
static member toISAJsonString(?spaces) =
131+
fun (obj:OntologyAnnotation) ->
132+
OntologyAnnotation.ISAJson.encoder obj
133+
|> Encode.toJsonString (defaultArg spaces 2)
134+
135+
static member fromISAJsonString (s:string) =
136+
Decode.fromJsonString OntologyAnnotation.ISAJson.decoder s

src/Json/OntologySourceReference.fs

+95-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,96 @@
1-
module OntologySourceReference
1+
namespace ARCtrl.JSON
22

3+
open Thoth.Json.Core
4+
open ARCtrl
5+
open ARCtrl.Helper
6+
open ARCtrl.Json
7+
8+
module OntologySourceReference =
9+
10+
let encoder (osr : OntologySourceReference) =
11+
[
12+
Encode.tryInclude "description" Encode.string (osr.Description)
13+
Encode.tryInclude "file" Encode.string (osr.File)
14+
Encode.tryInclude "name" Encode.string (osr.Name)
15+
Encode.tryInclude "version" Encode.string (osr.Version)
16+
Encode.tryIncludeSeq "comments" Comment.encoder (osr.Comments)
17+
"@context", ROCrateContext.OntologySourceReference.context_jsonvalue
18+
]
19+
|> Encode.choose
20+
|> Encode.object
21+
22+
let decoder : Decoder<OntologySourceReference> =
23+
Decode.object (fun get ->
24+
OntologySourceReference(
25+
?description = get.Optional.Field "description" Decode.uri,
26+
?file = get.Optional.Field "file" Decode.string,
27+
?name = get.Optional.Field "name" Decode.string,
28+
?version = get.Optional.Field "version" Decode.string,
29+
?comments = get.Optional.Field "comments" (Decode.resizeArray Comment.decoder)
30+
)
31+
)
32+
33+
module ROCrate =
34+
35+
let genID (o:OntologySourceReference) =
36+
match o.File with
37+
| Some f -> f
38+
| None ->
39+
match o.Name with
40+
| Some n -> "#OntologySourceRef_" + n.Replace(" ","_")
41+
| None -> "#DummyOntologySourceRef"
42+
43+
44+
let encoder (osr : OntologySourceReference) =
45+
[
46+
"@id", Encode.string (osr |> genID)
47+
"@type", Encode.string "OntologySourceReference"
48+
Encode.tryInclude "description" Encode.string (osr.Description)
49+
Encode.tryInclude "file" Encode.string (osr.File)
50+
Encode.tryInclude "name" Encode.string (osr.Name)
51+
Encode.tryInclude "version" Encode.string (osr.Version)
52+
Encode.tryIncludeSeq "comments" Comment.encoder (osr.Comments)
53+
"@context", ROCrateContext.OntologySourceReference.context_jsonvalue
54+
]
55+
|> Encode.choose
56+
|> Encode.object
57+
58+
let decoder (options : ConverterOptions) : Decoder<OntologySourceReference> =
59+
Decode.object (fun get ->
60+
OntologySourceReference(
61+
?description = get.Optional.Field "description" Decode.uri,
62+
?file = get.Optional.Field "file" Decode.string,
63+
?name = get.Optional.Field "name" Decode.string,
64+
?version = get.Optional.Field "version" Decode.string,
65+
?comments = get.Optional.Field "comments" (Decode.resizeArray Comment.decoder)
66+
)
67+
)
68+
69+
module OntologySourceReference =
70+
71+
72+
let fromJsonString (s:string) =
73+
Decode.fromJsonString decoder s
74+
75+
let fromJsonldString (s:string) =
76+
Decode.fromJsonString (decoder (ConverterOptions(IsJsonLD=true))) s
77+
78+
let toJsonString (oa:OntologySourceReference) =
79+
encoder (ConverterOptions()) oa
80+
|> Encode.toJsonString 2
81+
82+
/// exports in json-ld format
83+
let toJsonldString (oa:OntologySourceReference) =
84+
encoder (ConverterOptions(SetID=true,IsJsonLD=true)) oa
85+
|> Encode.toJsonString 2
86+
87+
let toJsonldStringWithContext (a:OntologySourceReference) =
88+
encoder (ConverterOptions(SetID=true,IsJsonLD=true)) a
89+
|> Encode.toJsonString 2
90+
91+
// let fromFile (path : string) =
92+
// File.ReadAllText path
93+
// |> fromString
94+
95+
//let toFile (path : string) (osr:OntologySourceReference) =
96+
// File.WriteAllText(path,toString osr)

0 commit comments

Comments
 (0)