@@ -17,24 +17,58 @@ module Sample =
1717 | None -> " #EmptySample"
1818
1919 let encoder ( oa : Sample ) =
20+
21+ let additionalProperties =
22+ oa.Characteristics |> Option.defaultValue [] |> List.map MaterialAttributeValue.ROCrate.encoder
23+ |> List.append ( oa.FactorValues |> Option.defaultValue [] |> List.map FactorValue.ROCrate.encoder)
2024 [
2125 " @id" , Encode.string ( oa |> genID)
2226 " @type" , ( Encode.list [ Encode.string " Sample" ])
2327 Encode.tryInclude " name" Encode.string ( oa.Name)
24- Encode.tryIncludeListOpt " characteristics" MaterialAttributeValue.ROCrate.encoder ( oa.Characteristics)
25- Encode.tryIncludeListOpt " factorValues" FactorValue.ROCrate.encoder ( oa.FactorValues)
28+ Encode.tryIncludeList " additionalProperties" id additionalProperties
2629 " @context" , ROCrateContext.Sample.context_ jsonvalue
2730 ]
2831 |> Encode.choose
2932 |> Encode.object
3033
31- let decoder : Decoder < Sample > =
34+ let additionalPropertyDecoder =
35+ { new Decoder< MaterialAttributeValue option * FactorValue option> with
36+ member this.Decode ( s , json ) =
37+ let additionalType =
38+ if s.hasProperty " additionalType" json then
39+ match Decode.string.Decode( s, s.getProperty( " additionalType" , json)) with
40+ | Ok s -> s
41+ | _ -> " "
42+ else " "
43+ if additionalType = " FactorValue" then
44+ match FactorValue.ROCrate.decoder.Decode( s, json) with
45+ | Ok fv -> Ok ( None, Some fv)
46+ | Error e -> Error e
47+ else
48+ match MaterialAttributeValue.ROCrate.decoder.Decode( s, json) with
49+ | Ok ma -> Ok ( Some ma, None)
50+ | Error e -> Error e
51+ }
52+
53+ let decoder : Decoder < Sample > =
54+
3255 Decode.object ( fun get ->
56+ let additionalProperties = get.Optional.Field " additionalProperties" ( Decode.list additionalPropertyDecoder)
57+ let characteristics , factors =
58+ match additionalProperties with
59+ | None -> None, None
60+ | Some additionalProperties ->
61+ additionalProperties
62+ |> List.choose fst
63+ |> Helper.Option.fromValueWithDefault [],
64+ additionalProperties
65+ |> List.choose snd
66+ |> Helper.Option.fromValueWithDefault []
3367 {
3468 ID = get.Optional.Field " @id" Decode.uri
3569 Name = get.Optional.Field " name" Decode.string
36- Characteristics = get.Optional.Field " characteristics" ( Decode.list MaterialAttributeValue.ROCrate.decoder )
37- FactorValues = get.Optional.Field " factorValues " ( Decode.list FactorValue.ROCrate.decoder )
70+ Characteristics = characteristics
71+ FactorValues = factors
3872 DerivesFrom = get.Optional.Field " derivesFrom" ( Decode.list Source.ROCrate.decoder)
3973 }
4074 )
0 commit comments