-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSerializer.cs
More file actions
47 lines (41 loc) · 1.31 KB
/
Serializer.cs
File metadata and controls
47 lines (41 loc) · 1.31 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
// Copyright (c) TensorStack. All rights reserved.
// Licensed under the Apache 2.0 License.
using System.Text.Json;
using System.Text.Json.Serialization;
namespace TensorStack.WPF
{
public static class Serializer
{
private static readonly JsonSerializerOptions _cloneOptions;
private static readonly JsonSerializerOptions _defaultOptions;
static Serializer()
{
_cloneOptions = new JsonSerializerOptions
{
ReferenceHandler = ReferenceHandler.Preserve,
WriteIndented = false
};
_defaultOptions = new JsonSerializerOptions
{
WriteIndented = true,
Converters =
{
new JsonStringEnumConverter()
}
};
}
public static string Serialize<T>(T data)
{
return JsonSerializer.Serialize(data, _defaultOptions);
}
public static T Deserialize<T>(string data)
{
return JsonSerializer.Deserialize<T>(data, _defaultOptions);
}
public static T DeepClone<T>(T obj)
{
string json = JsonSerializer.Serialize(obj, _cloneOptions);
return JsonSerializer.Deserialize<T>(json, _cloneOptions);
}
}
}