-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathEncode.fs
More file actions
663 lines (591 loc) · 23.9 KB
/
Copy pathEncode.fs
File metadata and controls
663 lines (591 loc) · 23.9 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
namespace Thoth.Json.Net
[<RequireQualifiedAccess>]
module Encode =
open System.Globalization
open System.Collections.Generic
open Newtonsoft.Json
open Newtonsoft.Json.Linq
open System.IO
///**Description**
/// Encode a string
///
///**Parameters**
/// * `value` - parameter of type `string`
///
///**Output Type**
/// * `Value`
///
///**Exceptions**
///
let string (value : string) : JsonValue =
JValue(value) :> JsonValue
///**Description**
/// Encode a GUID
///
///**Parameters**
/// * `value` - parameter of type `System.Guid`
///
///**Output Type**
/// * `Value`
///
///**Exceptions**
///
let guid (value : System.Guid) : JsonValue =
value.ToString() |> string
///**Description**
/// Encode a Float. `Infinity` and `NaN` are encoded as `null`.
///
///**Parameters**
/// * `value` - parameter of type `float`
///
///**Output Type**
/// * `Value`
///
///**Exceptions**
///
let float (value : float) : JsonValue =
JValue(value) :> JsonValue
let float32 (value : float32) : JsonValue =
JValue(value) :> JsonValue
///**Description**
/// Encode a Decimal.
///
///**Parameters**
/// * `value` - parameter of type `decimal`
///
///**Output Type**
/// * `Value`
///
///**Exceptions**
///
let decimal (value : decimal) : JsonValue =
JValue(value.ToString(CultureInfo.InvariantCulture)) :> JsonValue
///**Description**
/// Encode null
///
///**Parameters**
///
///**Output Type**
/// * `Value`
///
///**Exceptions**
///
let nil : JsonValue =
JValue.CreateNull() :> JsonValue
///**Description**
/// Encode a bool
///**Parameters**
/// * `value` - parameter of type `bool`
///
///**Output Type**
/// * `Value`
///
///**Exceptions**
///
let bool (value : bool) : JsonValue =
JValue(value) :> JsonValue
///**Description**
/// Encode an object
///
///**Parameters**
/// * `values` - parameter of type `(string * Value) list`
///
///**Output Type**
/// * `Value`
///
///**Exceptions**
///
let object (values : (string * JsonValue) list) : JsonValue =
values
|> List.map (fun (key, value) ->
JProperty(key, value)
)
|> JObject :> JsonValue
///**Description**
/// Encode an array
///
///**Parameters**
/// * `values` - parameter of type `Value array`
///
///**Output Type**
/// * `Value`
///
///**Exceptions**
///
let array (values : JsonValue array) : JsonValue =
JArray(values) :> JsonValue
///**Description**
/// Encode a list
///**Parameters**
/// * `values` - parameter of type `Value list`
///
///**Output Type**
/// * `Value`
///
///**Exceptions**
///
let list (values : JsonValue list) : JsonValue =
JArray(values) :> JsonValue
let seq (values : JsonValue seq) : JsonValue =
JArray(values) :> JsonValue
///**Description**
/// Encode a dictionary
///**Parameters**
/// * `values` - parameter of type `Map<string, Value>`
///
///**Output Type**
/// * `Value`
///
///**Exceptions**
///
let dict (values : Map<string, JsonValue>) =
values
|> Map.toList
|> object
let bigint (value : bigint) : JsonValue =
JValue(value.ToString(CultureInfo.InvariantCulture)) :> JsonValue
let datetime (value : System.DateTime) : JsonValue =
JValue(value.ToString("O", CultureInfo.InvariantCulture)) :> JsonValue
/// The DateTime is always encoded using UTC representation
let datetimeOffset (value : System.DateTimeOffset) : JsonValue =
JValue(value.ToString("O", CultureInfo.InvariantCulture)) :> JsonValue
let timespan (value : System.TimeSpan) : JsonValue =
JValue(value.ToString()) :> JsonValue
let sbyte (value : sbyte) : JsonValue =
JValue(value.ToString(CultureInfo.InvariantCulture)) :> JsonValue
let byte (value : byte) : JsonValue =
JValue(value.ToString(CultureInfo.InvariantCulture)) :> JsonValue
let int16 (value : int16) : JsonValue =
JValue(value.ToString(CultureInfo.InvariantCulture)) :> JsonValue
let uint16 (value : uint16) : JsonValue =
JValue(value.ToString(CultureInfo.InvariantCulture)) :> JsonValue
let int (value : int) : JsonValue =
JValue(value) :> JsonValue
let uint32 (value : uint32) : JsonValue =
JValue(value) :> JsonValue
let int64 (value : int64) : JsonValue =
JValue(value.ToString(CultureInfo.InvariantCulture)) :> JsonValue
let uint64 (value : uint64) : JsonValue =
JValue(value.ToString(CultureInfo.InvariantCulture)) :> JsonValue
let unit () : JsonValue =
JValue.CreateNull() :> JsonValue
let tuple2
(enc1 : Encoder<'T1>)
(enc2 : Encoder<'T2>)
(v1, v2) : JsonValue =
[| enc1 v1
enc2 v2 |] |> array
let tuple3
(enc1 : Encoder<'T1>)
(enc2 : Encoder<'T2>)
(enc3 : Encoder<'T3>)
(v1, v2, v3) : JsonValue =
[| enc1 v1
enc2 v2
enc3 v3 |] |> array
let tuple4
(enc1 : Encoder<'T1>)
(enc2 : Encoder<'T2>)
(enc3 : Encoder<'T3>)
(enc4 : Encoder<'T4>)
(v1, v2, v3, v4) : JsonValue =
[| enc1 v1
enc2 v2
enc3 v3
enc4 v4 |] |> array
let tuple5
(enc1 : Encoder<'T1>)
(enc2 : Encoder<'T2>)
(enc3 : Encoder<'T3>)
(enc4 : Encoder<'T4>)
(enc5 : Encoder<'T5>)
(v1, v2, v3, v4, v5) : JsonValue =
[| enc1 v1
enc2 v2
enc3 v3
enc4 v4
enc5 v5 |] |> array
let tuple6
(enc1 : Encoder<'T1>)
(enc2 : Encoder<'T2>)
(enc3 : Encoder<'T3>)
(enc4 : Encoder<'T4>)
(enc5 : Encoder<'T5>)
(enc6 : Encoder<'T6>)
(v1, v2, v3, v4, v5, v6) : JsonValue =
[| enc1 v1
enc2 v2
enc3 v3
enc4 v4
enc5 v5
enc6 v6 |] |> array
let tuple7
(enc1 : Encoder<'T1>)
(enc2 : Encoder<'T2>)
(enc3 : Encoder<'T3>)
(enc4 : Encoder<'T4>)
(enc5 : Encoder<'T5>)
(enc6 : Encoder<'T6>)
(enc7 : Encoder<'T7>)
(v1, v2, v3, v4, v5, v6, v7) : JsonValue =
[| enc1 v1
enc2 v2
enc3 v3
enc4 v4
enc5 v5
enc6 v6
enc7 v7 |] |> array
let tuple8
(enc1 : Encoder<'T1>)
(enc2 : Encoder<'T2>)
(enc3 : Encoder<'T3>)
(enc4 : Encoder<'T4>)
(enc5 : Encoder<'T5>)
(enc6 : Encoder<'T6>)
(enc7 : Encoder<'T7>)
(enc8 : Encoder<'T8>)
(v1, v2, v3, v4, v5, v6, v7, v8) : JsonValue =
[| enc1 v1
enc2 v2
enc3 v3
enc4 v4
enc5 v5
enc6 v6
enc7 v7
enc8 v8 |] |> array
////////////
// Enum ///
/////////
module Enum =
let byte<'TEnum when 'TEnum : enum<byte>> (value : 'TEnum) : JsonValue =
LanguagePrimitives.EnumToValue value
|> byte
let sbyte<'TEnum when 'TEnum : enum<sbyte>> (value : 'TEnum) : JsonValue =
LanguagePrimitives.EnumToValue value
|> sbyte
let int16<'TEnum when 'TEnum : enum<int16>> (value : 'TEnum) : JsonValue =
LanguagePrimitives.EnumToValue value
|> int16
let uint16<'TEnum when 'TEnum : enum<uint16>> (value : 'TEnum) : JsonValue =
LanguagePrimitives.EnumToValue value
|> uint16
let int<'TEnum when 'TEnum : enum<int>> (value : 'TEnum) : JsonValue =
LanguagePrimitives.EnumToValue value
|> int
let uint32<'TEnum when 'TEnum : enum<uint32>> (value : 'TEnum) : JsonValue =
LanguagePrimitives.EnumToValue value
|> uint32
///**Description**
/// Convert a `Value` into a prettified string.
///**Parameters**
/// * `space` - parameter of type `int` - Amount of indentation
/// * `value` - parameter of type `obj` - Value to convert
///
///**Output Type**
/// * `string`
///
///**Exceptions**
///
let toString (space: int) (token: JsonValue) : string =
let format = if space = 0 then Formatting.None else Formatting.Indented
use stream = new StringWriter(NewLine = "\n")
use jsonWriter = new JsonTextWriter(
stream,
Formatting = format,
Indentation = space )
token.WriteTo(jsonWriter)
stream.ToString()
//////////////////
// Reflection ///
////////////////
open FSharp.Reflection
type private EncoderCrate<'T>(enc: Encoder<'T>) =
inherit BoxedEncoder()
override __.Encode(value: obj): JsonValue =
enc (unbox value)
member __.UnboxedEncoder = enc
let boxEncoder (d: Encoder<'T>): BoxedEncoder =
EncoderCrate(d) :> BoxedEncoder
let unboxEncoder<'T> (d: BoxedEncoder): Encoder<'T> =
(d :?> EncoderCrate<'T>).UnboxedEncoder
let private (|StringifiableType|_|) (t: System.Type): (obj->string) option =
let fullName = t.FullName
if fullName = typeof<string>.FullName then
Some unbox
elif fullName = typeof<System.Guid>.FullName then
Some(fun (v: obj) -> (v :?> System.Guid).ToString())
else None
#if !NETFRAMEWORK
let private (|StringEnum|_|) (typ : System.Type) =
typ.CustomAttributes
|> Seq.tryPick (function
| attr when attr.AttributeType.FullName = typeof<Fable.Core.StringEnumAttribute>.FullName -> Some attr
| _ -> None
)
let private (|CompiledName|_|) (caseInfo : UnionCaseInfo) =
caseInfo.GetCustomAttributes()
|> Seq.tryPick (function
| :? CompiledNameAttribute as att -> Some att.CompiledName
| _ -> None)
let private (|LowerFirst|Forward|) (args : IList<System.Reflection.CustomAttributeTypedArgument>) =
args
|> Seq.tryPick (function
| rule when rule.ArgumentType.FullName = typeof<Fable.Core.CaseRules>.FullName -> Some rule
| _ -> None
)
|> function
| Some rule ->
match rule.Value with
| :? int as value ->
printfn "%A" value
match value with
| 0 -> Forward
| 1 -> LowerFirst
| _ -> LowerFirst // should not happen
| _ -> LowerFirst // should not happen
| None ->
LowerFirst
#endif
let rec private autoEncodeRecordsAndUnions extra (caseStrategy : CaseStrategy) (skipNullField : bool) (t: System.Type) : BoxedEncoder =
// Add the encoder to extra in case one of the fields is recursive
let encoderRef = ref Unchecked.defaultof<_>
let extra = extra |> Map.add t.FullName encoderRef
let encoder =
if FSharpType.IsRecord(t, allowAccessToPrivateRepresentation=true) then
let setters =
FSharpType.GetRecordFields(t, allowAccessToPrivateRepresentation=true)
|> Array.map (fun fi ->
let targetKey = Util.Casing.convert caseStrategy fi.Name
fun (source: obj) (target: JObject) ->
let value = FSharpValue.GetRecordField(source, fi)
if not skipNullField || (skipNullField && not (isNull value)) then // Discard null fields
let encoder = autoEncoder extra caseStrategy skipNullField (value.GetType())
target.[targetKey] <- encoder.Encode value
target)
boxEncoder(fun (source: obj) ->
(JObject(), setters) ||> Seq.fold (fun target set -> set source target) :> JsonValue)
elif FSharpType.IsUnion(t, allowAccessToPrivateRepresentation=true) then
boxEncoder(fun (value: obj) ->
let info, fields = FSharpValue.GetUnionFields(value, t, allowAccessToPrivateRepresentation=true)
match fields.Length with
| 0 ->
#if !NETFRAMEWORK
match t with
// Replicate Fable behaviour when using StringEnum
| StringEnum t ->
match info with
| CompiledName name -> string name
| _ ->
match t.ConstructorArguments with
| LowerFirst ->
let name = info.Name.[..0].ToLowerInvariant() + info.Name.[1..]
string name
| Forward -> string info.Name
| _ -> string info.Name
#else
string info.Name
#endif
| len ->
let fieldTypes = info.GetFields()
let target = Array.zeroCreate<JsonValue> (len + 1)
target.[0] <- string info.Name
for i = 1 to len do
let encoder = autoEncoder extra caseStrategy skipNullField fieldTypes.[i-1].PropertyType
target.[i] <- encoder.Encode(fields.[i-1])
array target)
else
failwithf "Cannot generate auto encoder for %s. Please pass an extra encoder." t.FullName
encoderRef := encoder
encoder
and private genericSeq (encoder: BoxedEncoder) =
boxEncoder(fun (xs: obj) ->
let ar = JArray()
for x in xs :?> System.Collections.IEnumerable do
ar.Add(encoder.Encode(x))
ar :> JsonValue)
and private autoEncoder (extra: Map<string, ref<BoxedEncoder>>) caseStrategy (skipNullField : bool) (t: System.Type) : BoxedEncoder =
let fullname = t.FullName
match Map.tryFind fullname extra with
| Some encoderRef -> boxEncoder(fun v -> encoderRef.contents.BoxedEncoder v)
| None ->
if t.IsArray then
t.GetElementType() |> autoEncoder extra caseStrategy skipNullField |> genericSeq
elif t.IsGenericType then
if FSharpType.IsTuple(t) then
let encoders =
FSharpType.GetTupleElements(t)
|> Array.map (autoEncoder extra caseStrategy skipNullField)
boxEncoder(fun (value: obj) ->
FSharpValue.GetTupleFields(value)
|> Seq.mapi (fun i x -> encoders.[i].Encode x) |> seq)
else
let fullname = t.GetGenericTypeDefinition().FullName
if fullname = typedefof<obj option>.FullName then
// Evaluate lazily so we don't need to generate the encoder for null values
let encoder = lazy autoEncoder extra caseStrategy skipNullField t.GenericTypeArguments.[0]
boxEncoder(fun (value: obj) ->
if isNull value then nil
else
let _, fields = FSharpValue.GetUnionFields(value, t, allowAccessToPrivateRepresentation=true)
encoder.Value.Encode fields.[0])
elif fullname = typedefof<obj list>.FullName
|| fullname = typedefof<Set<string>>.FullName then
// I don't know how to support seq for now.
// || fullname = typedefof<obj seq>.FullName
t.GenericTypeArguments.[0] |> autoEncoder extra caseStrategy skipNullField |> genericSeq
elif fullname = typedefof< Map<string, obj> >.FullName then
let keyType = t.GenericTypeArguments.[0]
let valueType = t.GenericTypeArguments.[1]
let valueEncoder = valueType |> autoEncoder extra caseStrategy skipNullField
let kvProps = typedefof<KeyValuePair<obj,obj>>.MakeGenericType(keyType, valueType).GetProperties()
match keyType with
| StringifiableType toString ->
boxEncoder(fun (value: obj) ->
let target = JObject()
for kv in value :?> System.Collections.IEnumerable do
let k = kvProps.[0].GetValue(kv)
let v = kvProps.[1].GetValue(kv)
target.[toString k] <- valueEncoder.Encode v
target :> JsonValue)
| _ ->
let keyEncoder = keyType |> autoEncoder extra caseStrategy skipNullField
boxEncoder(fun (value: obj) ->
let target = JArray()
for kv in value :?> System.Collections.IEnumerable do
let k = kvProps.[0].GetValue(kv)
let v = kvProps.[1].GetValue(kv)
target.Add(JArray [|keyEncoder.Encode k; valueEncoder.Encode v|])
target :> JsonValue)
else
autoEncodeRecordsAndUnions extra caseStrategy skipNullField t
elif t.IsEnum then
let enumType = System.Enum.GetUnderlyingType(t).FullName
if enumType = typeof<sbyte>.FullName then
boxEncoder sbyte
elif enumType = typeof<byte>.FullName then
boxEncoder byte
elif enumType = typeof<int16>.FullName then
boxEncoder int16
elif enumType = typeof<uint16>.FullName then
boxEncoder uint16
elif enumType = typeof<int>.FullName then
boxEncoder int
elif enumType = typeof<uint32>.FullName then
boxEncoder uint32
else
failwithf
"""Cannot generate auto encoder for %s.
Thoth.Json.Net only support the folluwing enum types:
- sbyte
- byte
- int16
- uint16
- int
- uint32
If you can't use one of these types, please pass an extra encoder.
""" t.FullName
else
if fullname = typeof<bool>.FullName then
boxEncoder bool
elif fullname = typeof<unit>.FullName then
boxEncoder unit
elif fullname = typeof<string>.FullName then
boxEncoder string
elif fullname = typeof<sbyte>.FullName then
boxEncoder sbyte
elif fullname = typeof<byte>.FullName then
boxEncoder byte
elif fullname = typeof<int16>.FullName then
boxEncoder int16
elif fullname = typeof<uint16>.FullName then
boxEncoder uint16
elif fullname = typeof<int>.FullName then
boxEncoder int
elif fullname = typeof<uint32>.FullName then
boxEncoder uint32
elif fullname = typeof<float>.FullName then
boxEncoder float
elif fullname = typeof<float32>.FullName then
boxEncoder float32
// These number types require extra libraries in Fable. To prevent penalizing
// all users, extra encoders (withInt64, etc) must be passed when they're needed.
// elif fullname = typeof<int64>.FullName then
// boxEncoder int64
// elif fullname = typeof<uint64>.FullName then
// boxEncoder uint64
// elif fullname = typeof<bigint>.FullName then
// boxEncoder bigint
// elif fullname = typeof<decimal>.FullName then
// boxEncoder decimal
elif fullname = typeof<System.DateTime>.FullName then
boxEncoder datetime
elif fullname = typeof<System.DateTimeOffset>.FullName then
boxEncoder datetimeOffset
elif fullname = typeof<System.TimeSpan>.FullName then
boxEncoder timespan
elif fullname = typeof<System.Guid>.FullName then
boxEncoder guid
elif fullname = typeof<obj>.FullName then
boxEncoder(fun (v: obj) -> JValue(v) :> JsonValue)
else
autoEncodeRecordsAndUnions extra caseStrategy skipNullField t
let private makeExtra (extra: ExtraCoders option) =
match extra with
| None -> Map.empty
| Some e -> Map.map (fun _ (enc,_) -> ref enc) e.Coders
module Auto =
/// This API is only implemented inside Thoth.Json.Net for now
/// The goal of this API is to provide better interop when consuming Thoth.Json.Net from a C# project
type LowLevel =
/// ATTENTION: Use this only when other arguments (isCamelCase, extra) don't change
static member generateEncoderCached<'T> (t: System.Type, ?caseStrategy : CaseStrategy, ?extra: ExtraCoders, ?skipNullField: bool): Encoder<'T> =
let caseStrategy = defaultArg caseStrategy PascalCase
let skipNullField = defaultArg skipNullField true
let key =
t.FullName
|> (+) (Operators.string caseStrategy)
|> (+) (extra |> Option.map (fun e -> e.Hash) |> Option.defaultValue "")
let encoderCrate =
Cache.Encoders.Value.GetOrAdd(key, fun _ ->
autoEncoder (makeExtra extra) caseStrategy skipNullField t)
fun (value: 'T) ->
encoderCrate.Encode value
type Auto =
/// ATTENTION: Use this only when other arguments (caseStrategy, extra) don't change
static member generateEncoderCached<'T>(?caseStrategy : CaseStrategy, ?extra: ExtraCoders, ?skipNullField: bool): Encoder<'T> =
let t = typeof<'T>
Auto.LowLevel.generateEncoderCached(t, ?caseStrategy = caseStrategy, ?extra = extra, ?skipNullField=skipNullField)
static member generateEncoder<'T>(?caseStrategy : CaseStrategy, ?extra: ExtraCoders, ?skipNullField: bool): Encoder<'T> =
let caseStrategy = defaultArg caseStrategy PascalCase
let skipNullField = defaultArg skipNullField true
let encoderCrate = autoEncoder (makeExtra extra) caseStrategy skipNullField typeof<'T>
fun (value: 'T) ->
encoderCrate.Encode value
static member toString(space : int, value : 'T, ?caseStrategy : CaseStrategy, ?extra: ExtraCoders, ?skipNullField: bool) : string =
let encoder = Auto.generateEncoder(?caseStrategy=caseStrategy, ?extra=extra, ?skipNullField=skipNullField)
encoder value |> toString space
///**Description**
/// Convert a `Value` into a prettified string.
///**Parameters**
/// * `space` - parameter of type `int` - Amount of indentation
/// * `value` - parameter of type `obj` - Value to convert
///
///**Output Type**
/// * `string`
///
///**Exceptions**
///
[<System.Obsolete("Please use toString instead")>]
let encode (space: int) (token: JsonValue) : string = toString space token
///**Description**
/// Encode an option
///**Parameters**
/// * `encoder` - parameter of type `'a -> Value`
///
///**Output Type**
/// * `'a option -> Value`
///
///**Exceptions**
///
let option (encoder : 'a -> JsonValue) =
Option.map encoder >> Option.defaultWith (fun _ -> nil)