Skip to content

Commit

Permalink
finish thoth conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Feb 29, 2024
1 parent d0a99a3 commit c482c86
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 197 deletions.
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
15 changes: 6 additions & 9 deletions src/ISA/ISA.Json/ArcTypes/ArcAssay.fs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ module ArcAssay =
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 @@ -130,9 +129,8 @@ 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
Expand All @@ -148,9 +146,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 Down
10 changes: 4 additions & 6 deletions src/ISA/ISA.Json/ArcTypes/ArcInvestigation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,16 @@ module ArcInvestigation =
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
Expand Down
15 changes: 6 additions & 9 deletions src/ISA/ISA.Json/ArcTypes/ArcStudy.fs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,8 @@ module ArcStudy =
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 @@ -129,9 +128,8 @@ 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
Expand All @@ -147,9 +145,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 Down
84 changes: 60 additions & 24 deletions src/ISA/ISA.Json/ArcTypes/ArcTable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,60 @@ module ArcTable =
yield Encode.object ["f",Encode.int from; "t", Encode.int(rowCount-1); "v",CellTable.encodeCell cellTable current]
|]
|> Encode.array

let compressedColumnDecoder (cellTable : CellTableArray) (table: ArcTable) (columnIndex : int) =
{new Decoder<unit> with
member this.Decode (helper,column) =
match (Decode.array (CellTable.decodeCell cellTable)).Decode(helper,column) with
| Ok a ->
a |> Array.iteri (fun r cell -> table.Values.Add((columnIndex,r),cell))
Ok(())
| Error err ->
let rangeDecoder =
Decode.object (fun get ->
let from = get.Required.Field "f" Decode.int
let to_ = get.Required.Field "t" Decode.int
let value = get.Required.Field "v" (CellTable.decodeCell cellTable)
for i = from to to_ do
table.Values.Add((columnIndex,i),value)
)
match (Decode.array (rangeDecoder)).Decode(helper,column) with
| Ok _ -> Ok ()
| Error err -> Error err
}

let compressedColumnDecoder (columnIndex : int) (cellTable : CellTableArray) (table: ArcTable) (column : JsonValue) =
match (Decode.array (CellTable.decodeCell cellTable)).Decode "" column with
| Ok a ->
a |> Array.iteri (fun r cell -> table.Values.Add((columnIndex,r),cell))
| Error err ->
let rangeDecoder s jv =
Decode.object (fun get ->
let from = get.Required.Field "f" Decode.int
let to_ = get.Required.Field "t" Decode.int
let value = get.Required.Field "v" (CellTable.decodeCell cellTable)
for i = from to to_ do
table.Values.Add((columnIndex,i),value)
) s jv
Decode.array (rangeDecoder) "" column |> ignore


let arrayi (decoderi: int -> Decoder<'value>) : Decoder<'value array> =
{ new Decoder<'value array> with
member _.Decode(helpers, value) =
if helpers.isArray value then
let mutable i = -1
let tokens = helpers.asArray value
let arr = Array.zeroCreate tokens.Length

(Ok arr, tokens)
||> Array.fold (fun acc value ->
i <- i + 1

match acc with
| Error _ -> acc
| Ok acc ->
match (decoderi i).Decode(helpers, value) with
| Error er ->
Error(
er
|> Decode.Helpers.prependPath (
".[" + (i.ToString()) + "]"
)
)
| Ok value ->
acc.[i] <- value
Ok acc
)
else
("", BadPrimitive("an array", value)) |> Error
}


let compressedEncoder (stringTable : StringTableMap) (oaTable : OATableMap) (cellTable : CellTableMap) (table: ArcTable) =
Encode.object [
Expand All @@ -88,9 +126,11 @@ module ArcTable =
decodedHeader,
Dictionary()
)
let columns = get.Optional.Field "c" (Decode.array Decode.value)
columns
|> Option.iter (Array.iteri (fun c col -> compressedColumnDecoder c cellTable table col))

// Columns
get.Optional.Field "c" (arrayi (compressedColumnDecoder cellTable table)) |> ignore


table

)
Expand All @@ -100,9 +140,7 @@ module ArcTableExtensions =

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

member this.ToJsonString(?spaces) : string =
let spaces = defaultArg spaces 0
Expand All @@ -118,9 +156,7 @@ module ArcTableExtensions =
let cellTable = get.Required.Field "cellTable" (CellTable.decoder stringTable oaTable)
get.Required.Field "table" (ArcTable.compressedDecoder stringTable oaTable cellTable)
)
match Decode.fromString decoder jsonString with
| Ok r -> r
| Error e -> failwithf "Error. Unable to parse json string to ArcTable: %s" e
GDecode.fromJsonString decoder jsonString

member this.ToCompressedJsonString(?spaces) : string =
let spaces = defaultArg spaces 0
Expand Down
6 changes: 4 additions & 2 deletions src/ISA/ISA/JsonTypes/Value.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace ARCtrl.ISA

open Fable.Core
open System.Globalization

[<AttachMembers>]
type Value =
Expand All @@ -10,10 +11,11 @@ type Value =
| Name of string

static member fromString (value : string) =
match System.Int32.TryParse value with
match System.Int32.TryParse value with
| (true, i) -> Value.Int i
| _ ->
match System.Double.TryParse value with

match System.Double.TryParse(value,NumberStyles.Any,CultureInfo.InvariantCulture) with
| (true, f) -> Value.Float f
| _ -> Value.Name value

Expand Down
Loading

0 comments on commit c482c86

Please sign in to comment.