Repository navigation
Expand file tree
/
Copy pathJsonExtensions.fs
More file actions
418 lines (334 loc) · 17.8 KB
/
Copy pathJsonExtensions.fs
File metadata and controls
418 lines (334 loc) · 17.8 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
namespace FSharp.Data
open System
open System.Globalization
open System.Runtime.CompilerServices
open System.Runtime.InteropServices
open FSharp.Data
open FSharp.Data.Runtime
open FSharp.Core
/// Extension methods that can be used to work with JsonValue in a less safe, but more convenient way.
/// This module also provides the dynamic operator.
[<Extension>]
type JsonExtensions =
/// Get a sequence of key-value pairs representing the properties of an object
[<Extension>]
static member Properties(x: JsonValue) =
match x with
| JsonValue.Record properties -> properties
| _ -> [||]
/// Get property of a JSON object. Fails if the value is not an object
/// or if the property is not present
[<Extension>]
static member GetProperty(x, propertyName) =
match x with
| JsonValue.Record properties ->
match Array.tryFind (fst >> (=) propertyName) properties with
| Some(_, value) -> value
| None ->
failwithf "Didn't find property '%s' in %s" propertyName
<| x.ToString JsonSaveOptions.DisableFormatting
| _ -> failwithf "Not an object: %s" <| x.ToString JsonSaveOptions.DisableFormatting
/// Try to get a property of a JSON value.
/// Returns None if the value is not an object or if the property is not present.
[<Extension>]
static member TryGetProperty(x, propertyName) =
match x with
| JsonValue.Record properties -> Array.tryFind (fst >> (=) propertyName) properties |> Option.map snd
| _ -> None
/// Assuming the value is an object, get value with the specified name
[<Extension>]
static member inline Item(x, propertyName) =
JsonExtensions.GetProperty(x, propertyName)
/// Get all the elements of a JSON value.
/// Returns an empty array if the value is not a JSON array.
[<Extension>]
static member AsArray(x: JsonValue) =
match x with
| (JsonValue.Array elements) -> elements
| _ -> [||]
/// Get all the elements of a JSON value (assuming that the value is an array)
[<Extension>]
static member inline GetEnumerator(x) =
JsonExtensions.AsArray(x).GetEnumerator()
/// Try to get the value at the specified index, if the value is a JSON array.
[<Extension>]
static member inline Item(x, index) = JsonExtensions.AsArray(x).[index]
/// Get the string value of an element (assuming that the value is a scalar)
/// Returns the empty string for JsonValue.Null
[<Extension>]
static member AsString(x, [<Optional>] ?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
match JsonConversions.AsString false cultureInfo x with
| Some s -> s
| _ -> failwithf "Not a string: %s" <| x.ToString(JsonSaveOptions.DisableFormatting)
/// Get a number as an integer (assuming that the value fits in integer)
[<Extension>]
static member AsInteger(x, [<Optional>] ?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
match JsonConversions.AsInteger cultureInfo x with
| Some i -> i
| _ -> failwithf "Not an int: %s" <| x.ToString(JsonSaveOptions.DisableFormatting)
/// Get a number as a 64-bit integer (assuming that the value fits in 64-bit integer)
[<Extension>]
static member AsInteger64(x, [<Optional>] ?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
match JsonConversions.AsInteger64 cultureInfo x with
| Some i -> i
| _ -> failwithf "Not an int64: %s" <| x.ToString(JsonSaveOptions.DisableFormatting)
/// Get a number as a decimal (assuming that the value fits in decimal)
[<Extension>]
static member AsDecimal(x, [<Optional>] ?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
match JsonConversions.AsDecimal cultureInfo x with
| Some d -> d
| _ -> failwithf "Not a decimal: %s" <| x.ToString(JsonSaveOptions.DisableFormatting)
/// Get a number as a float (assuming that the value is convertible to a float)
[<Extension>]
static member AsFloat(x, [<Optional>] ?cultureInfo, [<Optional>] ?missingValues) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
let missingValues = defaultArg missingValues TextConversions.DefaultMissingValues
match JsonConversions.AsFloat missingValues false cultureInfo x with
| Some f -> f
| _ -> failwithf "Not a float: %s" <| x.ToString(JsonSaveOptions.DisableFormatting)
/// Get the boolean value of an element (assuming that the value is a boolean)
[<Extension>]
static member AsBoolean(x) =
match JsonConversions.AsBoolean x with
| Some b -> b
| _ -> failwithf "Not a boolean: %s" <| x.ToString(JsonSaveOptions.DisableFormatting)
/// Get the datetime value of an element (assuming that the value is a string
/// containing well-formed ISO date or MSFT JSON date)
[<Extension>]
static member AsDateTime(x, [<Optional>] ?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
match JsonConversions.AsDateTime cultureInfo x with
| Some d -> d
| _ -> failwithf "Not a datetime: %s" <| x.ToString(JsonSaveOptions.DisableFormatting)
/// Get the datetime offset value of an element (assuming that the value is a string
/// containing well-formed ISO date time with offset or MSFT JSON datetime with offset)
[<Extension>]
static member AsDateTimeOffset(x, [<Optional>] ?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
match JsonConversions.AsDateTimeOffset cultureInfo x with
| Some d -> d
| _ ->
failwithf "Not a datetime offset: %s"
<| x.ToString(JsonSaveOptions.DisableFormatting)
/// Get the timespan value of an element (assuming that the value is a string
/// containing well-formed time span)
[<Extension>]
static member AsTimeSpan(x, [<Optional>] ?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
match JsonConversions.AsTimeSpan cultureInfo x with
| Some t -> t
| _ -> failwithf "Not a time span: %s" <| x.ToString(JsonSaveOptions.DisableFormatting)
/// Get the guid value of an element (assuming that the value is a guid)
[<Extension>]
static member AsGuid(x) =
match JsonConversions.AsGuid x with
| Some g -> g
| _ -> failwithf "Not a guid: %s" <| x.ToString(JsonSaveOptions.DisableFormatting)
/// Get inner text of an element
[<Extension>]
static member InnerText(x) =
match JsonConversions.AsString false CultureInfo.InvariantCulture x with
| Some str -> str
| None ->
JsonExtensions.AsArray(x)
|> Array.map (fun e -> JsonExtensions.InnerText(e))
|> String.Concat
/// Provides the dynamic operator for getting a property of a JSON object
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module JsonExtensions =
/// Get a property of a JSON object
let (?) (jsonObject: JsonValue) propertyName = jsonObject.GetProperty(propertyName)
type JsonValue with
member x.Properties =
match x with
| JsonValue.Record properties -> properties
| _ -> [||]
// TODO: needs more consideration
#if ENABLE_JSONEXTENSIONS_OPTIONS
/// Extension methods that can be used to work with JsonValue in more convenient way.
/// This module also provides the dynamic operator.
module Options =
open System.Runtime.CompilerServices
type JsonValue with
/// Get a sequence of key-value pairs representing the properties of an object
member x.Properties =
match x with
| JsonValue.Record properties -> properties
| _ -> [||]
/// Try to get a property of a JSON value.
/// Returns None if the value is not an object or if the property is not present.
member x.TryGetProperty(propertyName) =
match x with
| JsonValue.Record properties -> Array.tryFind (fst >> (=) propertyName) properties |> Option.map snd
| _ -> None
/// Try to get a property of a JSON value.
/// Returns None if the value is not a JSON object or if the property is not present.
member x.Item
with get (propertyName) = x.TryGetProperty(propertyName)
/// Get all the elements of a JSON value.
/// Returns an empty array if the value is not a JSON array.
member x.AsArray() =
match x with
| JsonValue.Array elements -> elements
| _ -> [||]
/// Get all the elements of a JSON value (assuming that the value is an array)
member inline x.GetEnumerator() = x.AsArray().GetEnumerator()
/// Try to get the value at the specified index, if the value is a JSON array.
member x.Item
with get (index) = x.AsArray().[index]
/// Get the string value of an element (assuming that the value is a scalar)
member x.AsString(?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
JsonConversions.AsString false cultureInfo x
/// Get a number as an integer (assuming that the value fits in integer)
member x.AsInteger(?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
JsonConversions.AsInteger cultureInfo x
/// Get a number as a 64-bit integer (assuming that the value fits in 64-bit integer)
member x.AsInteger64(?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
JsonConversions.AsInteger64 cultureInfo x
/// Get a number as a decimal (assuming that the value fits in decimal)
member x.AsDecimal(?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
JsonConversions.AsDecimal cultureInfo x
/// Get a number as a float (assuming that the value is convertible to a float)
member x.AsFloat(?cultureInfo, [<Optional>] ?missingValues) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
let missingValues = defaultArg missingValues TextConversions.DefaultMissingValues
JsonConversions.AsFloat missingValues true cultureInfo x
/// Get the boolean value of an element (assuming that the value is a boolean)
member x.AsBoolean(?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
JsonConversions.AsBoolean cultureInfo x
/// Get the datetime value of an element (assuming that the value is a string
/// containing well-formed ISO date or MSFT JSON date)
member x.AsDateTime(?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
JsonConversions.AsDateTime cultureInfo x
/// Get the datetime offset value of an element (assuming that the value is a string
/// containing well-formed ISO date time with offset)
member x.AsDateTimeOffset(?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
JsonConversions.AsDateTimeOffset cultureInfo x
/// Get the timespan value of an element (assuming that the value is a string
/// containing well-formed time span)
member x.AsTimeSpan(?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
JsonConversions.AsTimeSpan cultureInfo x
/// Get the guid value of an element (assuming that the value is a guid)
member x.AsGuid() = JsonConversions.AsGuid x
/// Get inner text of an element
member x.InnerText =
match x.AsString() with
| Some str -> str
| None -> x.AsArray() |> Array.map (fun e -> e.InnerText) |> String.Concat
[<Extension>]
[<AbstractClass>]
type JsonValueOptionExtensions() =
/// Get a sequence of key-value pairs representing the properties of an object
[<Extension>]
static member Properties(x) =
match x with
| Some(json: JsonValue) -> json.Properties
| None -> [||]
/// Try to get a property of a JSON value.
/// Returns None if the value is not an object or if the property is not present.
[<Extension>]
static member TryGetProperty(x, propertyName) =
match x with
| Some(JsonValue.Record properties) -> Array.tryFind (fst >> (=) propertyName) properties |> Option.map snd
| _ -> None
/// Try to get a property of a JSON value.
/// Returns None if the value is not a JSON object or if the property is not present.
[<Extension>]
static member inline Item(x, propertyName) =
JsonValueOptionExtensions.TryGetProperty(x, propertyName)
/// Get all the elements of a JSON value.
/// Returns an empty array if the value is not a JSON array.
[<Extension>]
static member AsArray(x) =
match x with
| Some(JsonValue.Array elements) -> elements
| _ -> [||]
/// Get all the elements of a JSON value (assuming that the value is an array)
[<Extension>]
static member inline GetEnumerator(x) =
JsonValueOptionExtensions.AsArray(x).GetEnumerator()
/// Try to get the value at the specified index, if the value is a JSON array.
[<Extension>]
static member inline Item(x, index) =
JsonValueOptionExtensions.AsArray(x).[index]
/// Get the string value of an element (assuming that the value is a scalar)
[<Extension>]
static member AsString(x, [<Optional>] ?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
x |> Option.bind (JsonConversions.AsString false cultureInfo)
/// Get a number as an integer (assuming that the value fits in integer)
[<Extension>]
static member AsInteger(x, [<Optional>] ?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
x |> Option.bind (JsonConversions.AsInteger cultureInfo)
/// Get a number as a 64-bit integer (assuming that the value fits in 64-bit integer)
[<Extension>]
static member AsInteger64(x, [<Optional>] ?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
x |> Option.bind (JsonConversions.AsInteger64 cultureInfo)
/// Get a number as a decimal (assuming that the value fits in decimal)
[<Extension>]
static member AsDecimal(x, [<Optional>] ?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
x |> Option.bind (JsonConversions.AsDecimal cultureInfo)
/// Get a number as a float (assuming that the value is convertible to a float)
[<Extension>]
static member AsFloat(x, [<Optional>] ?cultureInfo, [<Optional>] ?missingValues) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
let missingValues = defaultArg missingValues TextConversions.DefaultMissingValues
x |> Option.bind (JsonConversions.AsFloat missingValues true cultureInfo)
/// Get the boolean value of an element (assuming that the value is a boolean)
[<Extension>]
static member AsBoolean(x, [<Optional>] ?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
x |> Option.bind (JsonConversions.AsBoolean cultureInfo)
/// Get the datetime value of an element (assuming that the value is a string
/// containing well-formed ISO date or MSFT JSON date)
[<Extension>]
static member AsDateTime(x, [<Optional>] ?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
x |> Option.bind (JsonConversions.AsDateTime cultureInfo)
/// Get the datetime offset value of an element (assuming that the value is a string
/// containing well-formed ISO date time with offset)
[<Extension>]
static member AsDateTimeOffset(x, [<Optional>] ?cultureInfo) =
let cultureInfo = defaultArg cultureInfo CultureInfo.InvariantCulture
x |> Option.bind (JsonConversions.AsDateTimeOffset cultureInfo)
/// Get the timespan value of an element (assuming that the value is a timespan)
[<Extension>]
static member AsTimeSpan(x) =
x |> Option.bind JsonConversions.AsTimeSpan
/// Get the guid value of an element (assuming that the value is a guid)
[<Extension>]
static member AsGuid(x) = x |> Option.bind JsonConversions.AsGuid
/// Get inner text of an element
[<Extension>]
static member InnerText(x) =
match JsonValueOptionExtensions.AsString(x) with
| Some str -> str
| None ->
JsonValueOptionExtensions.AsArray(x)
|> Array.map (fun e -> e.InnerText)
|> String.Concat
/// <exclude />
type JsonValueOverloads = JsonValueOverloads
with
static member inline ($)(x: JsonValue, JsonValueOverloads) =
fun propertyName -> x.TryGetProperty propertyName
static member inline ($)(x: JsonValue option, JsonValueOverloads) =
fun propertyName -> x |> Option.bind (fun x -> x.TryGetProperty propertyName)
/// Get property of a JSON value (assuming that the value is an object)
let inline (?) x (propertyName: string) = (x $ JsonValueOverloads) propertyName
#endif