-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonDecoder.fsx
More file actions
308 lines (244 loc) · 10.2 KB
/
JsonDecoder.fsx
File metadata and controls
308 lines (244 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
open System.Text.Json
open System
let printInfo e =
printfn "%A" e
e
module Decode =
type ErrorType =
| Custom of msg: string
| BadType of given: JsonValueKind * expected: JsonValueKind
| EmptyValue of given: JsonValueKind * expected: JsonValueKind
| FieldError of FieldError
| FieldNotFound of fieldName: string
| Multiple of ErrorType list
and FieldError = { Error: ErrorType; FieldName: string }
and FieldSchemaType = { FieldName: string; Type: DecoderSchemaType }
and ObjectSchemaType = { Name: string; Fields: FieldSchemaType list }
and DecoderSchemaType =
| Raw of kind: JsonValueKind
| Array of DecoderSchemaType
| Optional of DecoderSchemaType
| Object of ObjectSchemaType
type FieldDecoder<'a> = { Type: FieldSchemaType; Decode: JsonElement -> Result<'a, ErrorType> }
type Decoder<'a> = {
Scheme: DecoderSchemaType
Decode: JsonElement -> Result<'a, ErrorType> }
let decode d (json: string) =
json
|> JsonDocument.Parse
|> _.RootElement
|> d.Decode
let getScheme d =
d.Scheme
let fieldError fieldName error =
{ Error = error; FieldName = fieldName } |> FieldError
let fieldNotFound fieldName = FieldNotFound fieldName
let string =
let decodeStr (json: JsonElement) =
match json.ValueKind with
| JsonValueKind.String -> json.GetString() |> Ok
| JsonValueKind.Null | JsonValueKind.Undefined ->
EmptyValue(json.ValueKind, JsonValueKind.String) |> Error
| _ -> BadType (json.ValueKind, JsonValueKind.String) |> Error
{ Scheme = Raw JsonValueKind.String; Decode = decodeStr }
let int =
let decodeInt (json: JsonElement) =
match json.ValueKind with
| JsonValueKind.Number -> json.GetInt32() |> Ok
| JsonValueKind.Null | JsonValueKind.Undefined ->
EmptyValue(json.ValueKind, JsonValueKind.Number) |> Error
| _ -> BadType (json.ValueKind, JsonValueKind.Number) |> Error
{ Scheme = Raw JsonValueKind.Number; Decode = decodeInt }
let option decoder =
let decodeOpt (json: JsonElement) =
let handleOptField =
function
| Error(FieldNotFound _)
| Error(EmptyValue _) -> Ok None
| Error e -> Error e
| Ok e -> Some e |> Ok
match json.ValueKind with
| JsonValueKind.Null
| JsonValueKind.Undefined -> Ok None
| _ -> decoder.Decode json |> handleOptField
{ Scheme = Optional decoder.Scheme; Decode = decodeOpt }
let field (fieldName: string) decoder : FieldDecoder<'a> =
let decodeField (json: JsonElement) =
match json.ValueKind with
| JsonValueKind.Object ->
match json.TryGetProperty fieldName with
| true, value ->
value |> decoder.Decode |> Result.mapError (fieldError fieldName)
| false, _ -> fieldNotFound fieldName |> Error
| JsonValueKind.Null | JsonValueKind.Undefined ->
EmptyValue(json.ValueKind, JsonValueKind.Object) |> Error
| _ -> BadType (json.ValueKind, JsonValueKind.Object) |> Error
{ Type = { FieldName = fieldName; Type = decoder.Scheme }; Decode = decodeField }
let toUnit _ = ()
let obj2 name f (decoder1: FieldDecoder<'a>) (decoder2: FieldDecoder<'b>) =
let decodeFields (json: JsonElement) =
match decoder1.Decode json, decoder2.Decode json with
| Ok v1, Ok v2 -> f v1 v2 |> Ok
| r1, r2 ->
[r1 |> Result.map toUnit; r2 |> Result.map toUnit;]
|> List.choose (function Ok _ -> None | Error e -> Some e)
|> Multiple
|> Error
{ Scheme = Object { Name = name; Fields = [decoder1.Type; decoder2.Type] }; Decode = decodeFields }
type Leader = { name: string; salary: int }
type Customer = { name: string; age: int option; leader: Leader }
let rec collectErr e =
match e with
| Decode.Multiple errs ->
errs |> List.collect collectErr
| e -> [e]
type ObjectDecoderBuilderState<'a> = { Decoder: Decode.Decoder<'a> }
type ObjectDecoderBuilder(name: string) =
//called when merging two fields as one decoder (let! .. and! .. return)
member this.MergeSources(source1: Decode.FieldDecoder<'a>, source2: Decode.FieldDecoder<'b>): ObjectDecoderBuilderState<'a * 'b> =
let decodeFields (json: JsonElement) =
match source1.Decode json, source2.Decode json with
| Ok v1, Ok v2 -> (v1, v2) |> Ok
| r1, r2 ->
[r1 |> Result.map Decode.toUnit; r2 |> Result.map Decode.toUnit;]
|> List.collect (function Ok _ -> [] | Error e -> collectErr e)
|> Decode.Multiple
|> Error
{
Decoder = { Scheme = Decode.Object { Name = name; Fields = [source1.Type; source2.Type] }
Decode = decodeFields }
}
//called when merging a new field into a decoder (let! .. and! .. and! .. return)
member this.MergeSources(source1: Decode.FieldDecoder<'a>, source2: ObjectDecoderBuilderState<'b>): ObjectDecoderBuilderState<'a * 'b> =
let decodeFields (json: JsonElement) =
match source1.Decode json, source2.Decoder.Decode json with
| Ok v1, Ok v2 -> (v1, v2) |> Ok
| r1, r2 ->
[r1 |> Result.map Decode.toUnit; r2 |> Result.map Decode.toUnit;]
|> List.collect (function Ok _ -> [] | Error e -> collectErr e)
|> Decode.Multiple
|> Error
let fieldsTypes =
match source2.Decoder.Scheme with
| Decode.Object { Fields = fields } -> fields
| _ -> []
{
Decoder = { Scheme = Decode.Object { Name = name; Fields = source1.Type :: fieldsTypes }
Decode = decodeFields }
}
//called when mapping a list of fields into a decoder
member this.BindReturn(source: ObjectDecoderBuilderState<'a>, f: 'a -> 'b): Decode.Decoder<'b> =
{
Scheme = source.Decoder.Scheme
Decode = fun json -> source.Decoder.Decode json |> Result.map f
}
//called when mapping a single field into a decoder
member this.BindReturn(source: Decode.FieldDecoder<'a>, f: 'a -> 'b): Decode.Decoder<'b> =
{
Scheme = Decode.Object { Name = name; Fields = [source.Type] }
Decode = fun json -> source.Decode json |> Result.map f
}
open Decode
let objectDecoder(name: string) =
ObjectDecoderBuilder name
let leaderDecoder =
objectDecoder "Leader" {
let! name = string |> field "name"
and! salary = int |> field "salary"
return { name = name; salary = salary }
}
let customerDecoder =
objectDecoder "Customer" {
let! age = option int |> field "age"
// and! name = string |> field "name"
and! leader = leaderDecoder |> field "leader"
return { age = age; name = "name"; leader = leader }
}
(*
let leaderCodec =
codec "Leader" {
let! name =
string "name"
|> title "Name"
|> description "Leader name"
|> example "John"
|> encodeFrom _.name
and! salary =
decimal "salary"
|> encodeFrom _.salary
return { name = name; salary = salary }
}
let customerCodec =
codec "Customer" {
let! age =
int |> field "age"
|> title "Age"
|> description "Customer age"
|> example 29
|> encodeFrom _.age
and! name =
string |> field "name"
|> title "Name"
|> description "Customer name"
|> example "Iuri"
|> encodeFrom _.name
and! leader =
object |> field "leader" leaderCodec.Schema
|> title "Leader"
|> description "Customer leader"
|> example leaderCodec.Schema.Example
|> encodeFrom _.leader
return { age = age; name = name; leader = leader }
}
let leaderEncoder =
encoder "Leader" {
encode _.name <| (string |> field "name")
encode _.salary
decimal
|> field "salary"
|> title "Salary"
|> description "Leader salary"
|> example 30
}
let leaderDecoder =
decoder "Leader" {
let! name = string |> field "name"
and! salary =
int
|> field "salary"
|> title "Salary"
|> description "Leader salary"
|> example 30
return { name = name; salary = salary }
}
let encodeLeader =
Codec.encodeWith leaderCodec
let decodeLeader =
Codec.decodeWith leaderCodec
*)
type Field<'a, 'b> = { Name: string; Encoder: 'a -> string; Getter: 'b -> 'a }
type ObjectEncoderBuilder() =
member val private Fields = [] with get, set
[<CustomOperation("field")>]
member this.Field(name: string, encoder: 'b -> string, getter: 'a -> 'b) =
let l = { Name = name; Encoder = encoder; Getter = getter } :: this.Fields
this.Fields <- l
member this.Delay(f: unit -> Field<'a, 'b>) =
let field = f()
let l = field :: this.Fields
this.Fields <- l
this
let objectEncoder =
ObjectEncoderBuilder()
let customerEncoder =
objectEncoder {
field "name" Encode.string _.name
field "age" Encode.string _.age >> string
}
customerDecoder
|> getScheme
|> printInfo
"""{"name": "Iuri", "age": 29, "leader": {"name": "John", "salary": 30}}"""
|> decode customerDecoder
|> Result.mapError printInfo
|> Result.iter (printfn "%A")