Skip to content

Commit

Permalink
Merge pull request #444 from nfdi4plants/rework-jsonIO-dependency
Browse files Browse the repository at this point in the history
JsonIO rework
  • Loading branch information
HLWeil authored Sep 27, 2024
2 parents 32875d5 + 29d1bb2 commit 9af0c63
Show file tree
Hide file tree
Showing 28 changed files with 140 additions and 136 deletions.
4 changes: 2 additions & 2 deletions src/ARCtrl/WebRequest/WebRequest.Node.fs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module ARCtrl.WebRequestHelpers.NodeJs
module ARCtrl.WebRequestHelpers.NodeJs

#if FABLE_COMPILER_JAVASCRIPT
open Fable.Core
open Fable.SimpleHttp

#if FABLE_COMPILER_JAVASCRIPT
open Fable.Core.JsInterop

open Fable.SimpleHttp
Expand Down
6 changes: 3 additions & 3 deletions src/Json/ARC.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ARCtrl.Json
namespace ARCtrl.Json

open Thoth.Json.Core

Expand All @@ -17,8 +17,8 @@ module ARC =
Encode.tryInclude "@type" Encode.string (Some "CreativeWork")
Encode.tryInclude "@id" Encode.string (Some "ro-crate-metadata.json")
Encode.tryInclude "about" Investigation.ROCrate.encoder (Some isa)
"conformsTo", ROCrateContext.ROCrate.conformsTo_jsonvalue
"@context", ROCrateContext.ROCrate.context_jsonvalue
"conformsTo", ROCrateContext.ROCrate.conformsTo_jsonvalue |> Some
"@context", ROCrateContext.ROCrate.context_jsonvalue |> Some
]
|> Encode.choose
|> Encode.object
Expand Down
8 changes: 4 additions & 4 deletions src/Json/ARCtrl.Json.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="NJsonSchema" Version="10.8.0" />
<PackageReference Include="Thoth.Json.Core" Version="0.2.1" />
<PackageReference Include="Thoth.Json.Newtonsoft" Version="0.1.0" />
<PackageReference Condition="'$(FABLE_COMPILER_PYTHON)' == 'true'" Include="Thoth.Json.Python" Version="0.2.0" />
<PackageReference Condition="'$(FABLE_COMPILER_JAVASCRIPT)' == 'true'" Include="Thoth.Json.JavaScript" Version="0.1.0" />
<PackageReference Include="Thoth.Json.Core" Version="0.4.0" />
<PackageReference Include="Thoth.Json.Newtonsoft" Version="0.2.0" />
<PackageReference Include="FsSpreadsheet.Js" Version="6.3.0-alpha.3" />
<PackageReference Include="FsSpreadsheet.Py" Version="6.3.0-alpha.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Core\ARCtrl.Core.fsproj" />
Expand Down
20 changes: 10 additions & 10 deletions src/Json/Assay.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Assay =

let encoder (assay:ArcAssay) =
[
"Identifier", Encode.string assay.Identifier
"Identifier", Encode.string assay.Identifier |> Some
Encode.tryInclude "MeasurementType" OntologyAnnotation.encoder assay.MeasurementType
Encode.tryInclude "TechnologyType" OntologyAnnotation.encoder assay.TechnologyType
Encode.tryInclude "TechnologyPlatform" OntologyAnnotation.encoder assay.TechnologyPlatform
Expand Down Expand Up @@ -43,7 +43,7 @@ module Assay =

let encoderCompressed (stringTable : StringTableMap) (oaTable : OATableMap) (cellTable : CellTableMap) (assay:ArcAssay) =
[
"Identifier", Encode.string assay.Identifier
"Identifier", Encode.string assay.Identifier |> Some
Encode.tryInclude "MeasurementType" OntologyAnnotation.encoder assay.MeasurementType
Encode.tryInclude "TechnologyType" OntologyAnnotation.encoder assay.TechnologyType
Encode.tryInclude "TechnologyPlatform" OntologyAnnotation.encoder assay.TechnologyPlatform
Expand Down Expand Up @@ -84,19 +84,19 @@ module Assay =
let dataFiles = ProcessSequence.getData processes

[
"@id", Encode.string (a |> genID)
"@type", (Encode.list [ Encode.string "Assay"])
"additionalType", Encode.string "Assay"
"identifier", Encode.string a.Identifier
"filename", Encode.string fileName
"@id", Encode.string (a |> genID) |> Some
"@type", (Encode.list [ Encode.string "Assay"]) |> Some
"additionalType", Encode.string "Assay" |> Some
"identifier", Encode.string a.Identifier |> Some
"filename", Encode.string fileName |> Some
Encode.tryInclude "measurementType" OntologyAnnotation.ROCrate.encoderPropertyValue a.MeasurementType
Encode.tryInclude "technologyType" OntologyAnnotation.ROCrate.encoderDefinedTerm a.TechnologyType
Encode.tryInclude "technologyPlatform" OntologyAnnotation.ROCrate.encoderDefinedTerm a.TechnologyPlatform
Encode.tryIncludeSeq "performers" Person.ROCrate.encoder a.Performers
Encode.tryIncludeList "dataFiles" Data.ROCrate.encoder dataFiles
Encode.tryIncludeList "processSequence" (Process.ROCrate.encoder studyName (Some a.Identifier)) processes
Encode.tryIncludeSeq "comments" Comment.ROCrate.encoder a.Comments
"@context", ROCrateContext.Assay.context_jsonvalue
"@context", ROCrateContext.Assay.context_jsonvalue |> Some
]
|> Encode.choose
|> Encode.object
Expand Down Expand Up @@ -139,7 +139,7 @@ module Assay =
|> Encode.tryIncludeList "dataFiles" (Data.ISAJson.encoder idMap)
let units = ProcessSequence.getUnits processes
[
"filename", Encode.string fileName
"filename", Encode.string fileName |> Some
Encode.tryInclude "@id" Encode.string (ROCrate.genID a |> Some)
Encode.tryInclude "measurementType" (OntologyAnnotation.ISAJson.encoder idMap) a.MeasurementType
Encode.tryInclude "technologyType" (OntologyAnnotation.ISAJson.encoder idMap) a.TechnologyType
Expand Down Expand Up @@ -199,7 +199,7 @@ module AssayExtensions =

static member fromCompressedJsonString (s: string) =
try Decode.fromJsonString (Compression.decode Assay.decoderCompressed) s with
| e -> failwithf "Error. Unable to parse json string to ArcStudy: %s" e.Message
| e -> failwithf "Error. Unable to parse json string to ArcAssay: %s" e.Message

static member toCompressedJsonString(?spaces) =
fun (obj:ArcAssay) ->
Expand Down
6 changes: 3 additions & 3 deletions src/Json/Comment.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ module Comment =

let encoder (comment : Comment) =
[
"@id", Encode.string (comment |> genID)
"@type", Encode.string "Comment"
"@id", Encode.string (comment |> genID) |> Some
"@type", Encode.string "Comment" |> Some
Encode.tryInclude "name" Encode.string (comment.Name)
Encode.tryInclude "value" Encode.string (comment.Value)
"@context", ROCrateContext.Comment.context_jsonvalue
"@context", ROCrateContext.Comment.context_jsonvalue |> Some
]
|> Encode.choose
|> Encode.object
Expand Down
6 changes: 3 additions & 3 deletions src/Json/Data.fs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ module Data =

let encoder (oa : Data) =
[
"@id", Encode.string (oa |> genID)
"@type", (Encode.list [Encode.string "Data"])
"@id", Encode.string (oa |> genID) |> Some
"@type", (Encode.list [Encode.string "Data"]) |> Some
Encode.tryInclude "name" Encode.string (oa.Name)
Encode.tryInclude "type" DataFile.ROCrate.encoder oa.DataType
Encode.tryInclude "encodingFormat" Encode.string oa.Format
Encode.tryInclude "usageInfo" Encode.string oa.SelectorFormat
Encode.tryIncludeSeq "comments" Comment.ROCrate.encoder oa.Comments
"@context", ROCrateContext.Data.context_jsonvalue
"@context", ROCrateContext.Data.context_jsonvalue |> Some
]
|> Encode.choose
|> Encode.object
Expand Down
2 changes: 1 addition & 1 deletion src/Json/DataMap/DataContext.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module DataContext =

let encoder (dc:DataContext) =
[
"data", Data.encoder dc
"data", Data.encoder dc |> Some
Encode.tryInclude "explication" OntologyAnnotation.encoder dc.Explication
Encode.tryInclude "unit" OntologyAnnotation.encoder dc.Unit
Encode.tryInclude "objectType" OntologyAnnotation.encoder dc.ObjectType
Expand Down
43 changes: 21 additions & 22 deletions src/Json/Encode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ open Fable.Core.JsInterop
[<RequireQualifiedAccess>]
module Encode =

let inline toJsonString spaces (value : Json) =
let inline toJsonString spaces (value : IEncodable) =
#if FABLE_COMPILER_PYTHON
Thoth.Json.Python.Encode.toString spaces value
#endif
Expand All @@ -25,44 +25,43 @@ module Encode =
Thoth.Json.Newtonsoft.Encode.toString spaces value
#endif

let inline choose (kvs : (string * Json) list) =
let inline choose (kvs : (string * IEncodable option) list) =
kvs
|> List.choose (fun (k,v) ->
if v = Encode.nil then None
else Some (k,v)
|> List.choose (fun (k,v) ->
v
|> Option.map (fun v -> k,v)
)

/// Try to encode the given object using the given encoder, or return Encode.nil if the object is null
let tryInclude (name : string) (encoder : 'Value -> Json) (value : 'Value option) =
let tryInclude (name : string) (encoder : 'Value -> IEncodable) (value : 'Value option) =
name,
match value with
| Some(o) -> encoder o
| _ -> Encode.nil
value
|> Option.map encoder

/// Try to encode the given object using the given encoder, or return Encode.nil if the object is null
let tryIncludeSeq name (encoder : 'Value -> Json) (value : #seq<'Value>) =
let tryIncludeSeq name (encoder : 'Value -> IEncodable) (value : #seq<'Value>) =
name,
if Seq.isEmpty value then Encode.nil
else value |> Seq.map encoder |> Encode.seq
if Seq.isEmpty value then None
else value |> Seq.map encoder |> Encode.seq |> Some

let tryIncludeArray name (encoder : 'Value -> Json) (value : 'Value array) =
let tryIncludeArray name (encoder : 'Value -> IEncodable) (value : 'Value array) =
name,
if Array.isEmpty value then Encode.nil
else value |> Array.map encoder |> Encode.array
if Array.isEmpty value then None
else value |> Array.map encoder |> Encode.array |> Some

let tryIncludeList name (encoder : 'Value -> Json) (value : 'Value list) =
let tryIncludeList name (encoder : 'Value -> IEncodable) (value : 'Value list) =
name,
if List.isEmpty value then Encode.nil
else value |> List.map encoder |> Encode.list
if List.isEmpty value then None
else value |> List.map encoder |> Encode.list |> Some

let tryIncludeListOpt name (encoder : 'Value -> Json) (value : 'Value list option) =
let tryIncludeListOpt name (encoder : 'Value -> IEncodable) (value : 'Value list option) =
name,
match value with
| Some(o) ->
if List.isEmpty o then Encode.nil
else o |> List.map encoder |> Encode.list
if List.isEmpty o then None
else o |> List.map encoder |> Encode.list |> Some
| _ ->
Encode.nil
None

let DefaultSpaces = 0

Expand Down
6 changes: 3 additions & 3 deletions src/Json/IDTable.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ARCtrl.Json
namespace ARCtrl.Json

open System.Collections.Generic
open Thoth.Json.Core
Expand All @@ -8,15 +8,15 @@ open ARCtrl.Helper

module IDTable =

type IDTableWrite = Dictionary<URI,Json>
type IDTableWrite = Dictionary<URI,IEncodable>

type IDTableRead = Dictionary<URI,obj>

let encodeID id =
["@id",Encode.string id]
|> Encode.object

let encode (genID: 'Value -> URI) (encoder : 'Value -> Json) (value : 'Value) (table:IDTableWrite) =
let encode (genID: 'Value -> URI) (encoder : 'Value -> IEncodable) (value : 'Value) (table:IDTableWrite) =
let id = genID value
if table.ContainsKey id then
encodeID id
Expand Down
26 changes: 13 additions & 13 deletions src/Json/Investigation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Investigation =

let encoder (inv : ArcInvestigation) =
[
"Identifier", Encode.string inv.Identifier
"Identifier", Encode.string inv.Identifier |> Some
Encode.tryInclude "Title" Encode.string inv.Title
Encode.tryInclude "Description" Encode.string inv.Description
Encode.tryInclude "SubmissionDate" Encode.string inv.SubmissionDate
Expand Down Expand Up @@ -50,7 +50,7 @@ module Investigation =

let encoderCompressed (stringTable : StringTableMap) (oaTable : OATableMap) (cellTable : CellTableMap) (inv : ArcInvestigation) =
[
"Identifier", Encode.string inv.Identifier
"Identifier", Encode.string inv.Identifier |> Some
Encode.tryInclude "Title" Encode.string inv.Title
Encode.tryInclude "Description" Encode.string inv.Description
Encode.tryInclude "SubmissionDate" Encode.string inv.SubmissionDate
Expand Down Expand Up @@ -100,11 +100,11 @@ module Investigation =

let encoder (oa : ArcInvestigation) =
[
"@id", Encode.string (oa |> genID)
"@type", Encode.string "Investigation"
"additionalType", Encode.string "Investigation"
"identifier", Encode.string oa.Identifier
"filename", Encode.string ArcInvestigation.FileName
"@id", Encode.string (oa |> genID) |> Some
"@type", Encode.string "Investigation" |> Some
"additionalType", Encode.string "Investigation" |> Some
"identifier", Encode.string oa.Identifier |> Some
"filename", Encode.string ArcInvestigation.FileName |> Some
Encode.tryInclude "title" Encode.string oa.Title
Encode.tryInclude "description" Encode.string oa.Description
Encode.tryInclude "submissionDate" Encode.string oa.SubmissionDate
Expand All @@ -114,7 +114,7 @@ module Investigation =
Encode.tryIncludeSeq "people" Person.ROCrate.encoder oa.Contacts
Encode.tryIncludeSeq "studies" (Study.ROCrate.encoder None) oa.Studies
Encode.tryIncludeSeq "comments" Comment.ROCrate.encoder oa.Comments
"@context", ROCrateContext.Investigation.context_jsonvalue
"@context", ROCrateContext.Investigation.context_jsonvalue |> Some
]
|> Encode.choose
|> Encode.object
Expand Down Expand Up @@ -155,8 +155,8 @@ module Investigation =
Encode.tryInclude "@type" Encode.string (Some "CreativeWork")
Encode.tryInclude "@id" Encode.string (Some "ro-crate-metadata.json")
Encode.tryInclude "about" encoder (Some oa)
"conformsTo", ROCrateContext.ROCrate.conformsTo_jsonvalue
"@context", ROCrateContext.ROCrate.context_jsonvalue
"conformsTo", ROCrateContext.ROCrate.conformsTo_jsonvalue |> Some
"@context", ROCrateContext.ROCrate.context_jsonvalue |> Some
]
|> Encode.choose
|> Encode.object
Expand All @@ -167,9 +167,9 @@ module Investigation =

let encoder idMap (inv: ArcInvestigation) =
[
"@id", Encode.string (inv |> ROCrate.genID)
"filename", Encode.string ArcInvestigation.FileName
"identifier", Encode.string (inv.Identifier)
"@id", Encode.string (inv |> ROCrate.genID) |> Some
"filename", Encode.string ArcInvestigation.FileName |> Some
"identifier", Encode.string (inv.Identifier) |> Some
Encode.tryInclude "title" Encode.string (inv.Title)
Encode.tryInclude "description" Encode.string (inv.Description)
Encode.tryInclude "submissionDate" Encode.string (inv.SubmissionDate)
Expand Down
12 changes: 6 additions & 6 deletions src/Json/OntologyAnnotation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ module OntologyAnnotation =

let encoderDefinedTerm (oa : OntologyAnnotation) =
[
"@id", Encode.string (oa |> genID)
"@type", Encode.string "OntologyAnnotation"
"@id", Encode.string (oa |> genID) |> Some
"@type", Encode.string "OntologyAnnotation" |> Some
Encode.tryInclude "annotationValue" Encode.string (oa.Name)
Encode.tryInclude "termSource" Encode.string (oa.TermSourceREF)
Encode.tryInclude "termAccession" Encode.string (oa.TermAccessionNumber)
Encode.tryIncludeSeq "comments" Comment.ROCrate.encoderDisambiguatingDescription (oa.Comments)
"@context", ROCrateContext.OntologyAnnotation.context_jsonvalue
"@context", ROCrateContext.OntologyAnnotation.context_jsonvalue |> Some
]
|> Encode.choose
|> Encode.object
Expand All @@ -102,13 +102,13 @@ module OntologyAnnotation =

let encoderPropertyValue (oa : OntologyAnnotation) =
[
"@id", Encode.string (oa |> genID)
"@type", Encode.string "PropertyValue"
"@id", Encode.string (oa |> genID) |> Some
"@type", Encode.string "PropertyValue" |> Some

Encode.tryInclude "category" Encode.string oa.Name
Encode.tryInclude "categoryCode" Encode.string oa.TermAccessionNumber
Encode.tryIncludeSeq "comments" Comment.ROCrate.encoderDisambiguatingDescription (oa.Comments)
"@context", ROCrateContext.PropertyValue.context_jsonvalue
"@context", ROCrateContext.PropertyValue.context_jsonvalue |> Some
]
|> Encode.choose
|> Encode.object
Expand Down
8 changes: 4 additions & 4 deletions src/Json/OntologySourceReference.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ARCtrl.Json
namespace ARCtrl.Json

open Thoth.Json.Core
open ARCtrl
Expand Down Expand Up @@ -42,14 +42,14 @@ module OntologySourceReference =

let encoder (osr : OntologySourceReference) =
[
"@id", Encode.string (osr |> genID)
"@type", Encode.string "OntologySourceReference"
"@id", Encode.string (osr |> genID) |> Some
"@type", Encode.string "OntologySourceReference" |> Some
Encode.tryInclude "description" Encode.string (osr.Description)
Encode.tryInclude "file" Encode.string (osr.File)
Encode.tryInclude "name" Encode.string (osr.Name)
Encode.tryInclude "version" Encode.string (osr.Version)
Encode.tryIncludeSeq "comments" Comment.encoder (osr.Comments)
"@context", ROCrateContext.OntologySourceReference.context_jsonvalue
"@context", ROCrateContext.OntologySourceReference.context_jsonvalue |> Some
]
|> Encode.choose
|> Encode.object
Expand Down
Loading

0 comments on commit 9af0c63

Please sign in to comment.