Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thoth.Json migration #316

Merged
merged 10 commits into from
Feb 29, 2024
Merged
28 changes: 12 additions & 16 deletions src/ARCtrl/Templates/Template.Json.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
open ARCtrl.Template
open ARCtrl.ISA
open System
#if FABLE_COMPILER
open Thoth.Json
#else
open Thoth.Json.Net
open ARCtrl
#endif

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

Expand All @@ -25,7 +23,7 @@ module Organisation =

module Template =

open ARCtrl.ISA.Json


let encode (template: Template) =
let personEncoder = ARCtrl.ISA.Json.Person.encoder (ConverterOptions())
Expand Down Expand Up @@ -68,12 +66,11 @@ module Template =
)

let fromJsonString (jsonString: string) =
match Decode.fromString decode jsonString with
| Ok template -> template
| Error exn -> failwithf "Error. Given json string cannot be parsed to Template: %A" exn
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) =
Encode.toString spaces (encode template)
GEncode.toJsonString spaces (encode template)

module Templates =

Expand All @@ -84,16 +81,15 @@ module Templates =
|> Encode.object

let decode =
let d = Decode.dict Template.decode
Decode.fromString d
Decode.dict Template.decode


let fromJsonString (jsonString: string) =
match decode jsonString with
| Ok templateMap -> templateMap.Values |> Array.ofSeq
| Error exn -> failwithf "Error. Given json string cannot be parsed to Templates map: %A" exn
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) []) =
Encode.toString spaces (encode templateList)
GEncode.toJsonString spaces (encode templateList)


[<AutoOpen>]
Expand Down
5 changes: 3 additions & 2 deletions src/ISA/ISA.Json/ARCtrl.ISA.Json.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="NJsonSchema" Version="10.8.0" />
<PackageReference Include="Thoth.Json" Version="10.1.0" />
<PackageReference Include="Thoth.Json.Net" Version="11.0.0" />
<PackageReference Include="Thoth.Json.Core" Version="0.2.1" />
<PackageReference Include="Thoth.Json.JavaScript" Version="0.1.0" />
<PackageReference Include="Thoth.Json.Newtonsoft" Version="0.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ISA\ARCtrl.ISA.fsproj" />
Expand Down
35 changes: 15 additions & 20 deletions src/ISA/ISA.Json/ArcTypes/ArcAssay.fs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
namespace ARCtrl.ISA.Json

#if FABLE_COMPILER
open Thoth.Json
#else
open Thoth.Json.Net
#endif
open Thoth.Json.Core

open ARCtrl.ISA

module JsonHelper =
Expand Down Expand Up @@ -107,27 +104,27 @@ module ArcAssay =
/// exports in json-ld format
let toJsonldString (a:ArcAssay) =
Assay.encoder (ConverterOptions(SetID=true,IncludeType=true)) None (a.ToAssay())
|> Encode.toString 2
|> GEncode.toJsonString 2

let toJsonldStringWithContext (a:ArcAssay) =
Assay.encoder (ConverterOptions(SetID=true,IncludeType=true,IncludeContext=true)) None (a.ToAssay())
|> Encode.toString 2
|> GEncode.toJsonString 2

let fromJsonString (s:string) =
GDecode.fromJsonString (Assay.decoder (ConverterOptions())) s
|> ArcAssay.fromAssay

let toJsonString (a:ArcAssay) =
Assay.encoder (ConverterOptions()) None (a.ToAssay())
|> Encode.toString 2
|> GEncode.toJsonString 2

let toArcJsonString (a:ArcAssay) : string =
let spaces = 0
Encode.toString spaces (encoder a)
GEncode.toJsonString spaces (encoder a)

let fromArcJsonString (jsonString: string) =
match Decode.fromString decoder jsonString with
| Ok a -> a
| Error e -> failwithf "Error. Unable to parse json string to ArcAssay: %s" e
try GDecode.fromJsonString decoder jsonString with
| e -> failwithf "Error. Unable to parse json string to ArcAssay: %s" e.Message

[<AutoOpen>]
module ArcAssayExtensions =
Expand All @@ -136,13 +133,12 @@ module ArcAssayExtensions =

type ArcAssay with
static member fromArcJsonString (jsonString: string) : ArcAssay =
match Decode.fromString ArcAssay.decoder jsonString with
| Ok a -> a
| Error e -> failwithf "Error. Unable to parse json string to ArcAssay: %s" e
try GDecode.fromJsonString ArcAssay.decoder jsonString with
| e -> failwithf "Error. Unable to parse json string to ArcAssay: %s" e.Message

member this.ToArcJsonString(?spaces) : string =
let spaces = defaultArg spaces 0
Encode.toString spaces (ArcAssay.encoder this)
GEncode.toJsonString spaces (ArcAssay.encoder this)

static member toArcJsonString (a:ArcAssay) = a.ToArcJsonString()

Expand All @@ -154,9 +150,8 @@ module ArcAssayExtensions =
let cellTable = get.Required.Field "cellTable" (CellTable.decoder stringTable oaTable)
get.Required.Field "assay" (ArcAssay.compressedDecoder stringTable oaTable cellTable)
)
match Decode.fromString decoder jsonString with
| Ok r -> r
| Error e -> failwithf "Error. Unable to parse json string to ArcAssay: %s" e
try GDecode.fromJsonString decoder jsonString with
| e -> failwithf "Error. Unable to parse json string to ArcAssay: %s" e.Message

member this.ToCompressedJsonString(?spaces) : string =
let spaces = defaultArg spaces 0
Expand All @@ -171,6 +166,6 @@ module ArcAssayExtensions =
"stringTable", StringTable.arrayFromMap stringTable |> StringTable.encoder
"assay", arcAssay
]
Encode.toString spaces jObject
GEncode.toJsonString spaces jObject

static member toCompressedJsonString (a:ArcAssay) = a.ToCompressedJsonString()
28 changes: 12 additions & 16 deletions src/ISA/ISA.Json/ArcTypes/ArcInvestigation.fs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
namespace ARCtrl.ISA.Json

#if FABLE_COMPILER
open Thoth.Json
#else
open Thoth.Json.Net
#endif
open Thoth.Json.Core

open ARCtrl.ISA

open JsonHelper
Expand Down Expand Up @@ -63,39 +60,38 @@ module ArcInvestigation =
/// exports in json-ld format
let toJsonldString (a:ArcInvestigation) =
Investigation.encoder (ConverterOptions(SetID=true,IncludeType=true)) (a.ToInvestigation())
|> Encode.toString 2
|> GEncode.toJsonString 2

let toJsonldStringWithContext (a:ArcInvestigation) =
Investigation.encoder (ConverterOptions(SetID=true,IncludeType=true,IncludeContext=true)) (a.ToInvestigation())
|> Encode.toString 2
|> GEncode.toJsonString 2

let fromJsonString (s:string) =
GDecode.fromJsonString (Investigation.decoder (ConverterOptions())) s
|> ArcInvestigation.fromInvestigation

let toJsonString (a:ArcInvestigation) =
Investigation.encoder (ConverterOptions()) (a.ToInvestigation())
|> Encode.toString 2
|> GEncode.toJsonString 2

let toArcJsonString (a:ArcInvestigation) : string =
let spaces = 0
Encode.toString spaces (encoder a)
GEncode.toJsonString spaces (encoder a)

let fromArcJsonString (jsonString: string) =
match Decode.fromString decoder jsonString with
| Ok a -> a
| Error e -> failwithf "Error. Unable to parse json string to ArcInvestigation: %s" e
try GDecode.fromJsonString decoder jsonString with
| e -> failwithf "Error. Unable to parse json string to ArcInvestigation: %s" e.Message

[<AutoOpen>]
module ArcInvestigationExtensions =

type ArcInvestigation with
static member fromArcJsonString (jsonString: string) : ArcInvestigation =
match Decode.fromString ArcInvestigation.decoder jsonString with
| Ok r -> r
| Error e -> failwithf "Error. Unable to parse json string to ArcInvestigation: %s" e
try GDecode.fromJsonString ArcInvestigation.decoder jsonString with
| e -> failwithf "Error. Unable to parse json string to ArcInvestigation: %s" e.Message

member this.ToArcJsonString(?spaces) : string =
let spaces = defaultArg spaces 0
Encode.toString spaces (ArcInvestigation.encoder this)
GEncode.toJsonString spaces (ArcInvestigation.encoder this)

static member toArcJsonString(a:ArcInvestigation) = a.ToArcJsonString()
35 changes: 15 additions & 20 deletions src/ISA/ISA.Json/ArcTypes/ArcStudy.fs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
namespace ARCtrl.ISA.Json

#if FABLE_COMPILER
open Thoth.Json
#else
open Thoth.Json.Net
#endif
open Thoth.Json.Core

open ARCtrl.ISA

open JsonHelper
Expand Down Expand Up @@ -106,27 +103,27 @@ module ArcStudy =
/// exports in json-ld format
let toJsonldString (a:ArcStudy) (assays: ResizeArray<ArcAssay>) =
Study.encoder (ConverterOptions(SetID=true,IncludeType=true)) (a.ToStudy(assays))
|> Encode.toString 2
|> GEncode.toJsonString 2

let toJsonldStringWithContext (a:ArcStudy) (assays: ResizeArray<ArcAssay>) =
Study.encoder (ConverterOptions(SetID=true,IncludeType=true,IncludeContext=true)) (a.ToStudy(assays))
|> Encode.toString 2
|> GEncode.toJsonString 2

let fromJsonString (s:string) =
GDecode.fromJsonString (Study.decoder (ConverterOptions())) s
|> ArcStudy.fromStudy

let toJsonString (a:ArcStudy) (assays: ResizeArray<ArcAssay>) =
Study.encoder (ConverterOptions()) (a.ToStudy(assays))
|> Encode.toString 2
|> GEncode.toJsonString 2

let toArcJsonString (a:ArcStudy) : string =
let spaces = 0
Encode.toString spaces (encoder a)
GEncode.toJsonString spaces (encoder a)

let fromArcJsonString (jsonString: string) =
match Decode.fromString decoder jsonString with
| Ok a -> a
| Error e -> failwithf "Error. Unable to parse json string to ArcStudy: %s" e
try GDecode.fromJsonString decoder jsonString with
| e -> failwithf "Error. Unable to parse json string to ArcStudy: %s" e.Message

[<AutoOpen>]
module ArcStudyExtensions =
Expand All @@ -135,13 +132,12 @@ module ArcStudyExtensions =

type ArcStudy with
static member fromArcJsonString (jsonString: string) : ArcStudy =
match Decode.fromString ArcStudy.decoder jsonString with
| Ok r -> r
| Error e -> failwithf "Error. Unable to parse json string to ArcStudy: %s" e
try GDecode.fromJsonString ArcStudy.decoder jsonString with
| e -> failwithf "Error. Unable to parse json string to ArcStudy: %s" e.Message

member this.ToArcJsonString(?spaces) : string =
let spaces = defaultArg spaces 0
Encode.toString spaces (ArcStudy.encoder this)
GEncode.toJsonString spaces (ArcStudy.encoder this)

static member toArcJsonString(a:ArcStudy) = a.ToArcJsonString()

Expand All @@ -153,9 +149,8 @@ module ArcStudyExtensions =
let cellTable = get.Required.Field "cellTable" (CellTable.decoder stringTable oaTable)
get.Required.Field "study" (ArcStudy.compressedDecoder stringTable oaTable cellTable)
)
match Decode.fromString decoder jsonString with
| Ok r -> r
| Error e -> failwithf "Error. Unable to parse json string to ArcAssay: %s" e
try GDecode.fromJsonString decoder jsonString with
| e -> failwithf "Error. Unable to parse json string to ArcAssay: %s" e.Message

member this.ToCompressedJsonString(?spaces) : string =
let spaces = defaultArg spaces 0
Expand All @@ -170,7 +165,7 @@ module ArcStudyExtensions =
"stringTable", StringTable.arrayFromMap stringTable |> StringTable.encoder
"study", arcStudy
]
Encode.toString spaces jObject
GEncode.toJsonString spaces jObject

static member toCompressedJsonString (s : ArcStudy) =
s.ToCompressedJsonString()
Loading
Loading