-
Notifications
You must be signed in to change notification settings - Fork 38
/
BackwardCompatible.fs
74 lines (62 loc) · 3.07 KB
/
BackwardCompatible.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
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
namespace Microsoft.FSharpLu.Json
open Newtonsoft.Json
open Microsoft.FSharp.Reflection
/// Backward compatible version of the Compat F# datatype serializer.
/// It serializes using the Compact format but can deserialize Json produced
/// in both Compact and Default format.
type BackwardCompatible =
/// Serialize an object to Json with the specified converter
static member inline public serialize = Compact.serialize
/// Serialize an object to Json with the specified converter and save the result to a file
static member inline public serializeToFile = Compact.serializeToFile
/// Serialize an object to Json with the specified converter and write the result to a stream
static member inline public serializeToStream = Compact.serializeToStream
/// Try to deserialize json to an object of type ^T
static member inline tryDeserialize< ^T > json =
Helpers.tryDeserializeWithBoth< ^T, string>
Compact.deserialize
Default.deserialize
ignore
json
|> Helpers.exceptionToString
/// Try to read Json from a file and desrialized it to an object of type ^T
static member inline tryDeserializeFile< ^T > file =
Helpers.tryDeserializeWithBoth< ^T, string>
Compact.deserializeFile
Default.deserializeFile
ignore
file
|> Helpers.exceptionToString
/// Try to deserialize a stream to an object of type ^T
static member inline tryDeserializeStream< ^T > (stream:System.IO.Stream) =
if not <| stream.CanSeek then
failwith "BackwardCompat.deserializeStream only works with stream supporting the Seek() operator."
Helpers.tryDeserializeWithBoth< ^T, System.IO.Stream>
Compact.deserializeStream
Default.deserializeStream
(fun () -> stream.Seek(0L, System.IO.SeekOrigin.Begin) |> ignore)
stream
|> Helpers.exceptionToString
/// Deserialize a Json to an object of type ^T
static member inline deserialize< ^T > (json:string) : ^T =
Helpers.tryDeserializeWithBoth< ^T, string>
Compact.deserialize
Default.deserialize
ignore
json |> Helpers.unboxOrRaise
/// Read Json from a file and desrialized it to an object of type ^T
static member inline deserializeFile< ^T > file =
Helpers.tryDeserializeWithBoth< ^T, string>
Compact.deserializeFile
Default.deserializeFile
ignore
file |> Helpers.unboxOrRaise
/// Deserialize a stream to an object of type ^T
static member inline public deserializeStream< ^T > (stream:System.IO.Stream) =
if not <| stream.CanSeek then
failwith "BackwardCompat.deserializeStream only works with stream supporting the Seek() operator."
Helpers.tryDeserializeWithBoth< ^T, System.IO.Stream>
Compact.deserializeStream
Default.deserializeStream
(fun () -> stream.Seek(0L, System.IO.SeekOrigin.Begin) |> ignore)
stream |> Helpers.unboxOrRaise