forked from fsprojects/FSharp.Json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transforms.fs
26 lines (22 loc) · 1.55 KB
/
Transforms.fs
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
namespace FSharp.Json
/// Contains popular implementations of [ITypeTransform]
module Transforms =
open System
/// Implementation of [ITypeTransform] converting DateTime into int64 as epoch time.
type DateTimeEpoch() =
interface ITypeTransform with
member x.targetType () = (fun _ -> typeof<int64>) ()
member x.toTargetType value = (fun (v: obj) -> int64(((v :?> DateTime) - DateTime(1970, 1, 1)).TotalSeconds) :> obj) value
member x.fromTargetType value = (fun (v: obj) -> DateTime(1970, 1, 1).Add(TimeSpan.FromSeconds(float(v :?> int64))) :> obj) value
/// Implementation of [ITypeTransform] converting DateTimeOffset into int64 as epoch time.
type DateTimeOffsetEpoch() =
interface ITypeTransform with
member x.targetType () = (fun _ -> typeof<int64>) ()
member x.toTargetType value = (fun (v: obj) -> int64(((v :?> DateTimeOffset) - DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan(0L))).TotalSeconds) :> obj) value
member x.fromTargetType value = (fun (v: obj) -> DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan(0L)).Add(TimeSpan.FromSeconds(float(v :?> int64))) :> obj) value
/// Implementation of [ITypeTransform] converting Uri into string.
type UriTransform() =
interface ITypeTransform with
member x.targetType () = (fun _ -> typeof<string>) ()
member x.toTargetType value = (fun (v: obj) -> (v:?> System.Uri).ToString() :> obj) value
member x.fromTargetType value = (fun (v: obj) -> Uri(v :?> string) :> obj) value