Skip to content

Commit a29a7f9

Browse files
committed
start reworking arctrl.json process serialization
1 parent e053cd9 commit a29a7f9

12 files changed

Lines changed: 412 additions & 465 deletions

src/Json/ARCtrl.Json.fsproj

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@
3838
<Compile Include="OntologySourceReference.fs" />
3939
<Compile Include="Publication.fs" />
4040
<Compile Include="Person.fs" />
41+
<Compile Include="Process\Value.fs" />
4142
<Compile Include="Process\Factor.fs" />
43+
<Compile Include="Process\FactorValue.fs" />
44+
<Compile Include="Process\ProtocolParameter.fs" />
45+
<Compile Include="Process\MaterialType.fs" />
46+
<Compile Include="Process\MaterialAttribute.fs" />
47+
<Compile Include="Process\Component.fs" />
4248
<Compile Include="Process\Protocol.fs" />
4349
<Compile Include="Process\Material.fs" />
4450
<Compile Include="Process\Data.fs" />
@@ -56,10 +62,6 @@
5662
<Compile Include="ArcStudy.fs" />
5763
<Compile Include="ArcInvestigation.fs" />
5864
</ItemGroup>
59-
<ItemGroup>
60-
<Folder Include="Table\" />
61-
<Folder Include="Process\" />
62-
</ItemGroup>
6365
<ItemGroup>
6466
<PackageReference Include="NJsonSchema" Version="10.8.0" />
6567
<PackageReference Include="Thoth.Json.Core" Version="0.2.1" />

src/Json/Encode.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ module Encode =
7474
| _ -> Encode.nil
7575

7676
/// Try to encode the given object using the given encoder, or return Encode.nil if the object is null
77-
let tryInclude name (encoder : 'Value -> Json) (value : 'Value option) =
77+
let tryInclude (name : string) (encoder : 'Value -> Json) (value : 'Value option) =
7878
name,
7979
match value with
8080
| Some(o) -> encoder o

src/Json/Process/Component.fs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
namespace ARCtrl.Json
2+
3+
4+
open Thoth.Json.Core
5+
6+
open ARCtrl
7+
open ARCtrl.Process
8+
9+
//module Component =
10+
11+
// let genID (c:Component) =
12+
// let name = Component.composeName c.ComponentValue c.ComponentUnit
13+
// "#Component_" + name.Replace(" ","_")
14+
// // match c.ComponentName with
15+
// // | Some cn -> "#Component_" + cn.Replace(" ","_")
16+
// // | None -> "#EmptyComponent"
17+
18+
// let encoder (options : ConverterOptions) (oa : Component) =
19+
// if options.IsJsonLD then
20+
// [
21+
// if options.SetID then
22+
// "@id", Encode.string (oa |> genID)
23+
// "@type", (Encode.list [Encode.string "Component"])
24+
// if oa.ComponentType.IsSome then
25+
// Encode.tryInclude "category" Encode.string (oa.ComponentType.Value.Name)
26+
// Encode.tryInclude "categoryCode" Encode.string (oa.ComponentType.Value.TermAccessionNumber)
27+
// if oa.ComponentValue.IsSome then "value", Encode.string (oa.ValueText)
28+
// if oa.ComponentValue.IsSome && oa.ComponentValue.Value.IsAnOntology then
29+
// Encode.tryInclude "valueCode" Encode.string (oa.ComponentValue.Value.AsOntology()).TermAccessionNumber
30+
// if oa.ComponentUnit.IsSome then Encode.tryInclude "unit" Encode.string (oa.ComponentUnit.Value.Name)
31+
// if oa.ComponentUnit.IsSome then Encode.tryInclude "unitCode" Encode.string (oa.ComponentUnit.Value.TermAccessionNumber)
32+
// "@context", ROCrateContext.Component.context_jsonvalue
33+
// ]
34+
// else
35+
// [
36+
// if options.SetID then
37+
// "@id", Encode.string (oa |> genID)
38+
// "componentName", Encode.string (Component.composeName oa.ComponentValue oa.ComponentUnit)
39+
// Encode.tryInclude "componentType" (OntologyAnnotation.encoder options) (oa.ComponentType)
40+
// ]
41+
// |> Encode.choose
42+
// |> Encode.object
43+
44+
// let decoder (options : ConverterOptions) : Decoder<Component> =
45+
// if not options.IsJsonLD then
46+
// Decode.object (fun get ->
47+
// let name = get.Optional.Field "componentName" GDecode.uri
48+
// let value, unit =
49+
// match name with
50+
// | Some n ->
51+
// let v,u = Component.decomposeName n
52+
// Some v, u
53+
// | None -> None, None
54+
// {
55+
// ComponentName = None
56+
// ComponentValue = value
57+
// ComponentUnit = unit
58+
// ComponentType = get.Optional.Field "componentType" (OntologyAnnotation.decoder options)
59+
// }
60+
// )
61+
// else
62+
// Decode.object (fun get ->
63+
// let categoryName = get.Optional.Field "category" (Decode.string)
64+
// let categoryCode = get.Optional.Field "categoryCode" (Decode.string)
65+
// let category =
66+
// match categoryName,categoryCode with
67+
// | None,None -> None
68+
// | _ -> Some (OntologyAnnotation.make None categoryName None categoryCode None)
69+
// let valueName = get.Optional.Field "value" (Value.decoder options)
70+
// let valueCode = get.Optional.Field "valueCode" (Decode.string)
71+
// let value =
72+
// match valueName,valueCode with
73+
// | Some (Value.Name name), Some code ->
74+
// let oa = OntologyAnnotation.make None (Some name) None (Some (URI.fromString code)) None
75+
// let vo = Value.Ontology(oa)
76+
// Some vo
77+
// | None, Some code ->
78+
// let oa = OntologyAnnotation.make None None None (Some (URI.fromString code)) None
79+
// let vo = Value.Ontology(oa)
80+
// Some vo
81+
// | Some (Value.Name name), None -> valueName
82+
// | Some (Value.Float name), None -> valueName
83+
// | Some (Value.Int name), None -> valueName
84+
// | _ -> None
85+
// let unitName = get.Optional.Field "unit" (Decode.string)
86+
// let unitCode = get.Optional.Field "unitCode" (Decode.string)
87+
// let unit =
88+
// match unitName,unitCode with
89+
// | None,None -> None
90+
// | _ -> Some (OntologyAnnotation.make None unitName None unitCode None)
91+
// {
92+
// ComponentName = None
93+
// ComponentValue = value
94+
// ComponentUnit = unit
95+
// ComponentType = category
96+
// }
97+
// )
98+
99+
// let fromJsonString (s:string) =
100+
// GDecode.fromJsonString (decoder (ConverterOptions())) s
101+
// let fromJsonldString (s:string) =
102+
// GDecode.fromJsonString (decoder (ConverterOptions(IsJsonLD=true))) s
103+
104+
// let toJsonString (p:Component) =
105+
// encoder (ConverterOptions()) p
106+
// |> Encode.toJsonString 2
107+
108+
// /// exports in json-ld format
109+
// let toJsonldString (p:Component) =
110+
// encoder (ConverterOptions(SetID=true,IsJsonLD=true)) p
111+
// |> Encode.toJsonString 2
112+
113+
// let toJsonldStringWithContext (a:Component) =
114+
// encoder (ConverterOptions(SetID=true,IsJsonLD=true)) a
115+
// |> Encode.toJsonString 2
116+
117+
// //let fromFile (path : string) =
118+
// // File.ReadAllText path
119+
// // |> fromString
120+
121+
// //let toFile (path : string) (p:Component) =
122+
// // File.WriteAllText(path,toString p)
123+
124+
125+
//module Component =
126+
127+
// module ISAJson =
128+
129+
// let encoder

src/Json/Process/Factor.fs

Lines changed: 30 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -7,188 +7,39 @@ open ARCtrl
77
open ARCtrl.Process
88
open System.IO
99

10-
module Value =
11-
12-
let encoder (options : ConverterOptions) (value : Value) =
13-
match value with
14-
| Value.Float f ->
15-
Encode.float f
16-
| Value.Int i ->
17-
Encode.int i
18-
| Value.Name s ->
19-
Encode.string s
20-
| Value.Ontology s ->
21-
OntologyAnnotation.encoder options s
22-
23-
let decoder (options : ConverterOptions) : Decoder<Value> =
24-
{
25-
26-
new Decoder<Value> with
27-
member this.Decode (s,json) =
28-
match Decode.int.Decode(s,json) with
29-
| Ok i -> Ok (Value.Int i)
30-
| Error _ ->
31-
match Decode.float.Decode(s,json) with
32-
| Ok f -> Ok (Value.Float f)
33-
| Error _ ->
34-
match OntologyAnnotation.decoder(options).Decode(s,json) with
35-
| Ok f -> Ok (Value.Ontology f)
36-
| Error _ ->
37-
match Decode.string.Decode(s,json) with
38-
| Ok s -> Ok (Value.Name s)
39-
| Error e -> Error e}
40-
41-
42-
43-
let fromJsonString (s:string) =
44-
Decode.fromJsonString (decoder (ConverterOptions())) s
45-
46-
let fromJsonldString (s:string) =
47-
Decode.fromJsonString (decoder (ConverterOptions(IsJsonLD=true))) s
48-
49-
let toJsonString (v:Value) =
50-
encoder (ConverterOptions()) v
51-
|> Encode.toJsonString 2
52-
53-
//let fromFile (path : string) =
54-
// File.ReadAllText path
55-
// |> fromString
56-
57-
//let toFile (path : string) (v:Value) =
58-
// File.WriteAllText(path,toString v)
5910

6011
module Factor =
61-
62-
let genID (f:Factor) : string =
63-
match f.Name with
64-
| Some n -> "#Factor_" + n.Replace(" ","_")
65-
| None -> "#EmptyFactor"
66-
67-
let encoder (options : ConverterOptions) (oa : Factor) =
68-
[
69-
if options.SetID then
70-
"@id", Encode.string (oa |> genID)
71-
if options.IsJsonLD then
72-
"@type", (Encode.list [Encode.string "Factor"])
73-
Encode.tryInclude "factorName" Encode.string (oa.Name)
74-
if options.IsJsonLD then
75-
if oa.FactorType.IsSome then
76-
Encode.tryInclude "annotationValue" Encode.string (oa.Name)
77-
Encode.tryInclude "termSource" Encode.string (oa.FactorType.Value.TermSourceREF)
78-
Encode.tryInclude "termAccession" Encode.string (oa.FactorType.Value.TermAccessionNumber)
79-
else
80-
Encode.tryInclude "factorType" (OntologyAnnotation.encoder options) (oa.FactorType)
81-
Encode.tryIncludeSeq "comments" (Comment.encoder options) (oa.Comments |> Option.defaultValue (ResizeArray()))
82-
if options.IsJsonLD then
83-
"@context", ROCrateContext.Factor.context_jsonvalue
84-
]
85-
|> Encode.choose
86-
|> Encode.object
87-
88-
let decoder (options : ConverterOptions) : Decoder<Factor> =
89-
Decode.object (fun get ->
90-
{
91-
Name = get.Optional.Field "factorName" Decode.string
92-
FactorType = get.Optional.Field "factorType" (OntologyAnnotation.decoder options)
93-
Comments = get.Optional.Field "comments" (Decode.array (Comment.decoder options))
94-
}
95-
)
96-
97-
let fromJsonString (s:string) =
98-
GDecode.fromJsonString (decoder (ConverterOptions())) s
99-
let fromJsonldString (s:string) =
100-
GDecode.fromJsonString (decoder (ConverterOptions(IsJsonLD=true))) s
10112

102-
let toJsonString (f:Factor) =
103-
encoder (ConverterOptions()) f
104-
|> Encode.toJsonString 2
13+
module ISAJson =
14+
15+
let encoder (value : Factor) =
16+
[
17+
Encode.tryInclude "factorName" Encode.string value.Name
18+
Encode.tryInclude "factorType" OntologyAnnotation.ISAJson.encoder value.FactorType
19+
Encode.tryIncludeSeq "comments" Comment.ISAJson.encoder (value.Comments |> Option.defaultValue (ResizeArray()))
20+
]
21+
|> Encode.choose
22+
|> Encode.object
23+
24+
let decoder : Decoder<Factor> =
25+
Decode.object (fun get ->
26+
{
27+
Name = get.Optional.Field "factorName" Decode.string
28+
FactorType = get.Optional.Field "factorType" (OntologyAnnotation.ISAJson.decoder)
29+
Comments = get.Optional.Field "comments" (Decode.resizeArray (Comment.ISAJson.decoder))
30+
}
31+
)
32+
33+
34+
[<AutoOpen>]
35+
module FactorExtensions =
10536

106-
/// exports in json-ld format
107-
let toJsonldString (f:Factor) =
108-
encoder (ConverterOptions(SetID=true,IsJsonLD=true)) f
109-
|> Encode.toJsonString 2
110-
111-
let toJsonldStringWithContext (a:Factor) =
112-
encoder (ConverterOptions(SetID=true,IsJsonLD=true)) a
113-
|> Encode.toJsonString 2
114-
115-
//let fromFile (path : string) =
116-
// File.ReadAllText path
117-
// |> fromString
118-
119-
//let toFile (path : string) (f:Factor) =
120-
// File.WriteAllText(path,toString f)
121-
122-
123-
module FactorValue =
124-
125-
let genID (fv:FactorValue) : string =
126-
match fv.ID with
127-
| Some id -> URI.toString id
128-
| None -> "#EmptyFactorValue"
129-
130-
let encoder (options : ConverterOptions) (oa : FactorValue) =
131-
[
132-
if options.SetID then
133-
"@id", Encode.string (oa |> genID)
134-
else
135-
Encode.tryInclude "@id" Encode.string (oa.ID)
136-
if options.IsJsonLD then
137-
"@type", (Encode.list [Encode.string "FactorValue"])
138-
"additionalType", Encode.string "FactorValue"
139-
if options.IsJsonLD then
140-
if oa.Category.IsSome then
141-
Encode.tryInclude "categoryName" Encode.string (oa.Category.Value.Name)
142-
if oa.Category.IsSome && oa.Category.Value.FactorType.IsSome then
143-
Encode.tryInclude "category" Encode.string (oa.Category.Value.FactorType.Value.Name)
144-
if oa.Category.IsSome && oa.Category.Value.FactorType.IsSome then
145-
Encode.tryInclude "categoryCode" Encode.string (oa.Category.Value.FactorType.Value.TermAccessionNumber)
146-
if oa.Value.IsSome then "value", Encode.string (oa.ValueText)
147-
if oa.Value.IsSome && oa.Value.Value.IsAnOntology then
148-
Encode.tryInclude "valueCode" Encode.string (oa.Value.Value.AsOntology()).TermAccessionNumber
149-
if oa.Unit.IsSome then Encode.tryInclude "unit" Encode.string (oa.Unit.Value.Name)
150-
if oa.Unit.IsSome then Encode.tryInclude "unitCode" Encode.string (oa.Unit.Value.TermAccessionNumber)
151-
else
152-
Encode.tryInclude "category" (Factor.encoder options) (oa.Category)
153-
Encode.tryInclude "value" (Value.encoder options) (oa.Value)
154-
Encode.tryInclude "unit" (OntologyAnnotation.encoder options) (oa.Unit)
155-
if options.IsJsonLD then
156-
"@context", ROCrateContext.FactorValue.context_jsonvalue
157-
]
158-
|> Encode.choose
159-
|> Encode.object
160-
161-
let decoder (options : ConverterOptions) : Decoder<FactorValue> =
162-
Decode.object (fun get ->
163-
{
164-
ID = get.Optional.Field "@id" GDecode.uri
165-
Category = get.Optional.Field "category" (Factor.decoder options)
166-
Value = get.Optional.Field "value" (Value.decoder options)
167-
Unit = get.Optional.Field "unit" (OntologyAnnotation.decoder options)
168-
}
169-
)
170-
171-
let fromJsonString (s:string) =
172-
GDecode.fromJsonString (decoder (ConverterOptions())) s
173-
let fromJsonldString (s:string) =
174-
GDecode.fromJsonString (decoder (ConverterOptions(IsJsonLD=true))) s
175-
176-
let toJsonString (f:FactorValue) =
177-
encoder (ConverterOptions()) f
178-
|> Encode.toJsonString 2
179-
180-
/// exports in json-ld format
181-
let toJsonldString (f:FactorValue) =
182-
encoder (ConverterOptions(SetID=true,IsJsonLD=true)) f
183-
|> Encode.toJsonString 2
184-
185-
let toJsonldStringWithContext (a:FactorValue) =
186-
encoder (ConverterOptions(SetID=true,IsJsonLD=true)) a
187-
|> Encode.toJsonString 2
37+
type Factor with
18838

189-
//let fromFile (path : string) =
190-
// File.ReadAllText path
191-
// |> fromString
39+
static member fromISAJsonString (s:string) =
40+
Decode.fromJsonString Factor.ISAJson.decoder s
19241

193-
//let toFile (path : string) (f:FactorValue) =
194-
// File.WriteAllText(path,toString f)
42+
static member toISAJsonString(?spaces) =
43+
fun (f:Factor) ->
44+
Factor.ISAJson.encoder f
45+
|> Encode.toJsonString (Option.defaultValue 2 spaces)

0 commit comments

Comments
 (0)