Skip to content

Commit

Permalink
Finish json for shared types ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
Freymaurer committed Mar 21, 2024
1 parent a29a7f9 commit 6b670d9
Show file tree
Hide file tree
Showing 12 changed files with 420 additions and 188 deletions.
3 changes: 1 addition & 2 deletions src/Json/ARCtrl.Json.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@
<Compile Include="ConverterOptions.fs" />
<Compile Include="Decode.fs" />
<Compile Include="Encode.fs" />
<Compile Include="ROCrateHelper.fs" />
<Compile Include="StringTable.fs" />
<Compile Include="Comment.fs" />
<Compile Include="OntologyAnnotation.fs" />
<Compile Include="OntologySourceReference.fs" />
<Compile Include="Publication.fs" />
<Compile Include="Person.fs" />
<Compile Include="Publication.fs" />
<Compile Include="Process\Value.fs" />
<Compile Include="Process\Factor.fs" />
<Compile Include="Process\FactorValue.fs" />
Expand Down
6 changes: 3 additions & 3 deletions src/Json/Comment.fs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ module CommentExtensions =
static member toJsonString(?spaces) =
fun (c:Comment) ->
Comment.encoder c
|> Encode.toJsonString (defaultArg spaces 2)
|> Encode.toJsonString (Encode.defaultSpaces spaces)

static member fromROCrateJsonString (s:string) =
Decode.fromJsonString Comment.ROCrate.decoder s
Expand All @@ -81,12 +81,12 @@ module CommentExtensions =
static member toROCrateJsonString(?spaces) =
fun (c:Comment) ->
Comment.ROCrate.encoder c
|> Encode.toJsonString (defaultArg spaces 2)
|> Encode.toJsonString (Encode.defaultSpaces spaces)

static member toISAJsonString(?spaces) =
fun (c:Comment) ->
Comment.ISAJson.encoder c
|> Encode.toJsonString (defaultArg spaces 2)
|> Encode.toJsonString (Encode.defaultSpaces spaces)

static member fromISAJsonString (s:string) =
Decode.fromJsonString Comment.ISAJson.decoder s
Expand Down
13 changes: 13 additions & 0 deletions src/Json/Decode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ module Decode =
Error fst
}

let noAdditionalProperties (allowedProperties : string seq) (decoder : Decoder<'value>) : Decoder<'value> =
let allowedProperties = Set.ofSeq allowedProperties
{ new Decoder<'value> with
member _.Decode(helpers, value) =
let getters = Decode.Getters(helpers, value)
if hasUnknownFields helpers allowedProperties value then
Error (DecoderError("Unknown fields in object", ErrorReason.BadPrimitive("",value)))
else
decoder.Decode(helpers,value)
}



let resizeArray (decoder: Decoder<'value>) : Decoder<ResizeArray<'value>> =
{ new Decoder<ResizeArray<'value>> with
member _.Decode(helpers, value) =
Expand Down
5 changes: 5 additions & 0 deletions src/Json/Encode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,8 @@ module Encode =
name,
if List.isEmpty value then Encode.nil
else value |> List.map encoder |> Encode.list


let DefaultSpaces = 0

let defaultSpaces spaces = defaultArg spaces DefaultSpaces
6 changes: 3 additions & 3 deletions src/Json/OntologyAnnotation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ module OntologyAnnotationExtensions =
static member toJsonString(?spaces) =
fun (obj:OntologyAnnotation) ->
OntologyAnnotation.encoder obj
|> Encode.toJsonString (defaultArg spaces 2)
|> Encode.toJsonString (Encode.defaultSpaces spaces)

static member fromROCrateJsonString (s:string) =
Decode.fromJsonString OntologyAnnotation.ROCrate.decoder s
Expand All @@ -125,12 +125,12 @@ module OntologyAnnotationExtensions =
static member toROCrateJsonString(?spaces) =
fun (obj:OntologyAnnotation) ->
OntologyAnnotation.ROCrate.encoder obj
|> Encode.toJsonString (defaultArg spaces 2)
|> Encode.toJsonString (Encode.defaultSpaces spaces)

static member toISAJsonString(?spaces) =
fun (obj:OntologyAnnotation) ->
OntologyAnnotation.ISAJson.encoder obj
|> Encode.toJsonString (defaultArg spaces 2)
|> Encode.toJsonString (Encode.defaultSpaces spaces)

static member fromISAJsonString (s:string) =
Decode.fromJsonString OntologyAnnotation.ISAJson.decoder s
52 changes: 28 additions & 24 deletions src/Json/OntologySourceReference.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ module OntologySourceReference =
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
]
|> Encode.choose
|> Encode.object
Expand Down Expand Up @@ -55,7 +54,7 @@ module OntologySourceReference =
|> Encode.choose
|> Encode.object

let decoder (options : ConverterOptions) : Decoder<OntologySourceReference> =
let decoder : Decoder<OntologySourceReference> =
Decode.object (fun get ->
OntologySourceReference(
?description = get.Optional.Field "description" Decode.uri,
Expand All @@ -66,31 +65,36 @@ module OntologySourceReference =
)
)

module OntologySourceReference =


let fromJsonString (s:string) =
Decode.fromJsonString decoder s
module ISAJson =
let encoder = encoder
let decoder = decoder

[<AutoOpen>]
module OntologySourceReferenceExtensions =

let fromJsonldString (s:string) =
Decode.fromJsonString (decoder (ConverterOptions(IsJsonLD=true))) s
type OntologySourceReference with

static member fromJsonString (s:string) =
Decode.fromJsonString OntologySourceReference.decoder s

let toJsonString (oa:OntologySourceReference) =
encoder (ConverterOptions()) oa
|> Encode.toJsonString 2
static member toJsonString(?spaces) =
fun (obj:OntologySourceReference) ->
OntologySourceReference.encoder obj
|> Encode.toJsonString (Encode.defaultSpaces spaces)

/// exports in json-ld format
let toJsonldString (oa:OntologySourceReference) =
encoder (ConverterOptions(SetID=true,IsJsonLD=true)) oa
|> Encode.toJsonString 2
static member fromROCrateJsonString (s:string) =
Decode.fromJsonString OntologySourceReference.ROCrate.decoder s

let toJsonldStringWithContext (a:OntologySourceReference) =
encoder (ConverterOptions(SetID=true,IsJsonLD=true)) a
|> Encode.toJsonString 2
/// exports in json-ld format
static member toROCrateJsonString(?spaces) =
fun (obj:OntologySourceReference) ->
OntologySourceReference.ROCrate.encoder obj
|> Encode.toJsonString (Encode.defaultSpaces spaces)

// let fromFile (path : string) =
// File.ReadAllText path
// |> fromString
static member toISAJsonString(?spaces) =
fun (obj:OntologySourceReference) ->
OntologySourceReference.ISAJson.encoder obj
|> Encode.toJsonString (Encode.defaultSpaces spaces)

//let toFile (path : string) (osr:OntologySourceReference) =
// File.WriteAllText(path,toString osr)
static member fromISAJsonString (s:string) =
Decode.fromJsonString OntologySourceReference.ISAJson.decoder s
Loading

0 comments on commit 6b670d9

Please sign in to comment.