Skip to content

Commit 6b670d9

Browse files
committed
Finish json for shared types ✨
1 parent a29a7f9 commit 6b670d9

12 files changed

+420
-188
lines changed

src/Json/ARCtrl.Json.fsproj

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@
3131
<Compile Include="ConverterOptions.fs" />
3232
<Compile Include="Decode.fs" />
3333
<Compile Include="Encode.fs" />
34-
<Compile Include="ROCrateHelper.fs" />
3534
<Compile Include="StringTable.fs" />
3635
<Compile Include="Comment.fs" />
3736
<Compile Include="OntologyAnnotation.fs" />
3837
<Compile Include="OntologySourceReference.fs" />
39-
<Compile Include="Publication.fs" />
4038
<Compile Include="Person.fs" />
39+
<Compile Include="Publication.fs" />
4140
<Compile Include="Process\Value.fs" />
4241
<Compile Include="Process\Factor.fs" />
4342
<Compile Include="Process\FactorValue.fs" />

src/Json/Comment.fs

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ module CommentExtensions =
7272
static member toJsonString(?spaces) =
7373
fun (c:Comment) ->
7474
Comment.encoder c
75-
|> Encode.toJsonString (defaultArg spaces 2)
75+
|> Encode.toJsonString (Encode.defaultSpaces spaces)
7676

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

8686
static member toISAJsonString(?spaces) =
8787
fun (c:Comment) ->
8888
Comment.ISAJson.encoder c
89-
|> Encode.toJsonString (defaultArg spaces 2)
89+
|> Encode.toJsonString (Encode.defaultSpaces spaces)
9090

9191
static member fromISAJsonString (s:string) =
9292
Decode.fromJsonString Comment.ISAJson.decoder s

src/Json/Decode.fs

+13
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ module Decode =
8585
Error fst
8686
}
8787

88+
let noAdditionalProperties (allowedProperties : string seq) (decoder : Decoder<'value>) : Decoder<'value> =
89+
let allowedProperties = Set.ofSeq allowedProperties
90+
{ new Decoder<'value> with
91+
member _.Decode(helpers, value) =
92+
let getters = Decode.Getters(helpers, value)
93+
if hasUnknownFields helpers allowedProperties value then
94+
Error (DecoderError("Unknown fields in object", ErrorReason.BadPrimitive("",value)))
95+
else
96+
decoder.Decode(helpers,value)
97+
}
98+
99+
100+
88101
let resizeArray (decoder: Decoder<'value>) : Decoder<ResizeArray<'value>> =
89102
{ new Decoder<ResizeArray<'value>> with
90103
member _.Decode(helpers, value) =

src/Json/Encode.fs

+5
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,8 @@ module Encode =
9595
name,
9696
if List.isEmpty value then Encode.nil
9797
else value |> List.map encoder |> Encode.list
98+
99+
100+
let DefaultSpaces = 0
101+
102+
let defaultSpaces spaces = defaultArg spaces DefaultSpaces

src/Json/OntologyAnnotation.fs

+3-3
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ module OntologyAnnotationExtensions =
116116
static member toJsonString(?spaces) =
117117
fun (obj:OntologyAnnotation) ->
118118
OntologyAnnotation.encoder obj
119-
|> Encode.toJsonString (defaultArg spaces 2)
119+
|> Encode.toJsonString (Encode.defaultSpaces spaces)
120120

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

130130
static member toISAJsonString(?spaces) =
131131
fun (obj:OntologyAnnotation) ->
132132
OntologyAnnotation.ISAJson.encoder obj
133-
|> Encode.toJsonString (defaultArg spaces 2)
133+
|> Encode.toJsonString (Encode.defaultSpaces spaces)
134134

135135
static member fromISAJsonString (s:string) =
136136
Decode.fromJsonString OntologyAnnotation.ISAJson.decoder s

src/Json/OntologySourceReference.fs

+28-24
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ module OntologySourceReference =
1414
Encode.tryInclude "name" Encode.string (osr.Name)
1515
Encode.tryInclude "version" Encode.string (osr.Version)
1616
Encode.tryIncludeSeq "comments" Comment.encoder (osr.Comments)
17-
"@context", ROCrateContext.OntologySourceReference.context_jsonvalue
1817
]
1918
|> Encode.choose
2019
|> Encode.object
@@ -55,7 +54,7 @@ module OntologySourceReference =
5554
|> Encode.choose
5655
|> Encode.object
5756

58-
let decoder (options : ConverterOptions) : Decoder<OntologySourceReference> =
57+
let decoder : Decoder<OntologySourceReference> =
5958
Decode.object (fun get ->
6059
OntologySourceReference(
6160
?description = get.Optional.Field "description" Decode.uri,
@@ -66,31 +65,36 @@ module OntologySourceReference =
6665
)
6766
)
6867

69-
module OntologySourceReference =
70-
71-
72-
let fromJsonString (s:string) =
73-
Decode.fromJsonString decoder s
68+
module ISAJson =
69+
let encoder = encoder
70+
let decoder = decoder
71+
72+
[<AutoOpen>]
73+
module OntologySourceReferenceExtensions =
7474

75-
let fromJsonldString (s:string) =
76-
Decode.fromJsonString (decoder (ConverterOptions(IsJsonLD=true))) s
75+
type OntologySourceReference with
76+
77+
static member fromJsonString (s:string) =
78+
Decode.fromJsonString OntologySourceReference.decoder s
7779

78-
let toJsonString (oa:OntologySourceReference) =
79-
encoder (ConverterOptions()) oa
80-
|> Encode.toJsonString 2
80+
static member toJsonString(?spaces) =
81+
fun (obj:OntologySourceReference) ->
82+
OntologySourceReference.encoder obj
83+
|> Encode.toJsonString (Encode.defaultSpaces spaces)
8184

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

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

91-
// let fromFile (path : string) =
92-
// File.ReadAllText path
93-
// |> fromString
94+
static member toISAJsonString(?spaces) =
95+
fun (obj:OntologySourceReference) ->
96+
OntologySourceReference.ISAJson.encoder obj
97+
|> Encode.toJsonString (Encode.defaultSpaces spaces)
9498

95-
//let toFile (path : string) (osr:OntologySourceReference) =
96-
// File.WriteAllText(path,toString osr)
99+
static member fromISAJsonString (s:string) =
100+
Decode.fromJsonString OntologySourceReference.ISAJson.decoder s

0 commit comments

Comments
 (0)