-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathSerdes.fs
executable file
·47 lines (39 loc) · 2.08 KB
/
Serdes.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
namespace FsCodec.NewtonsoftJson
open Newtonsoft.Json
open System.Runtime.InteropServices
/// Serializes to/from strings using the supplied Settings
type Serdes(options : JsonSerializerSettings) =
/// <summary>The <c>JsonSerializerSettings</c> used by this instance.</summary>
member _.Options : JsonSerializerSettings = options
/// Serializes given value to a JSON string.
member _.Serialize<'T>(value : 'T) =
JsonConvert.SerializeObject(value, options)
/// Deserializes value of given type from JSON string.
member x.Deserialize<'T>(json : string) : 'T =
JsonConvert.DeserializeObject<'T>(json, options)
/// Serializes given value to a JSON string.
[<System.Obsolete "Please use non-static Serdes instead">]
static member Serialize<'T>
( /// Value to serialize.
value : 'T,
/// Use indentation when serializing JSON. Defaults to false.
[<Optional; DefaultParameterValue false>] ?indent : bool) : string =
let options = (if indent = Some true then Settings.Create(indent = true) else Settings.Create())
JsonConvert.SerializeObject(value, options)
/// Serializes given value to a JSON string with custom options
[<System.Obsolete "Please use non-static Serdes instead">]
static member Serialize<'T>
( /// Value to serialize.
value : 'T,
/// Settings to use (use other overload to use Settings.Create() profile)
settings : JsonSerializerSettings) : string =
JsonConvert.SerializeObject(value, settings)
/// Deserializes value of given type from JSON string.
[<System.Obsolete "Please use non-static Serdes instead">]
static member Deserialize<'T>
( /// Json string to deserialize.
json : string,
/// Settings to use (defaults to Settings.Create() profile)
[<Optional; DefaultParameterValue null>] ?settings : JsonSerializerSettings) : 'T =
let settings = match settings with Some x -> x | None -> Settings.Create()
JsonConvert.DeserializeObject<'T>(json, settings)