-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathConfigService.cs
More file actions
113 lines (98 loc) · 4.26 KB
/
ConfigService.cs
File metadata and controls
113 lines (98 loc) · 4.26 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
// Copyright (c) TensorStack. All rights reserved.
// Licensed under the Apache 2.0 License.
using System;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace TensorStack.Common
{
public class ConfigService
{
/// <summary>
/// Serializes the specified configuration to file.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="configFile">The configuration file.</param>
/// <param name="configuration">The configuration.</param>
/// <param name="useRelativePaths">if set to <c>true</c> use relative paths.</param>
public static void Serialize<T>(string configFile, T configuration, bool useRelativePaths = true)
{
var serializerOptions = GetSerializerOptions(configFile, useRelativePaths);
using (var configFileStream = File.Open(configFile, FileMode.Create))
{
JsonSerializer.Serialize<T>(configFileStream, configuration, serializerOptions);
}
}
/// <summary>
/// Deserializes the specified configuration file.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="configFile">The configuration file.</param>
/// <param name="useRelativePaths">if set to <c>true</c> [use relative paths].</param>
/// <returns>T.</returns>
public static T Deserialize<T>(string configFile, bool useRelativePaths = true)
{
var serializerOptions = GetSerializerOptions(configFile, useRelativePaths);
using (var configFileStream = File.OpenRead(configFile))
{
return JsonSerializer.Deserialize<T>(configFileStream, serializerOptions);
}
}
/// <summary>
/// Gets the serializer options.
/// </summary>
/// <param name="filePath">The file path.</param>
/// <param name="useRelativePaths">if set to <c>true</c> use relative paths.</param>
private static JsonSerializerOptions GetSerializerOptions(string filePath, bool useRelativePaths = true)
{
var serializerOptions = new JsonSerializerOptions
{
WriteIndented = true,
Converters = { new JsonStringEnumConverter() }
};
if (useRelativePaths)
serializerOptions.Converters.Add(new RelativePathConverter(Path.GetDirectoryName(Path.GetFullPath(filePath))));
return serializerOptions;
}
}
internal class RelativePathConverter : JsonConverter<string>
{
private readonly string _baseDir;
/// <summary>
/// Initializes a new instance of the <see cref="RelativePathConverter"/> class.
/// </summary>
/// <param name="baseDir">The base dir.</param>
public RelativePathConverter(string baseDir)
{
_baseDir = baseDir;
}
/// <summary>
/// Replace relative paths with full path.
/// </summary>
/// <param name="reader">The reader.</param>
/// <param name="typeToConvert">The type to convert.</param>
/// <param name="options">An object that specifies serialization options to use.</param>
/// <returns>The converted value.</returns>
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var value = reader.GetString();
if (string.IsNullOrEmpty(value))
return value;
if (!Path.IsPathRooted(value) && Path.HasExtension(value))
value = Path.Combine(_baseDir, value);
return value;
}
/// <summary>
/// Replace full path with relative path.
/// </summary>
/// <param name="writer">The writer to write to.</param>
/// <param name="value">The value to convert to JSON.</param>
/// <param name="options">An object that specifies serialization options to use.</param>
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
if (Path.IsPathRooted(value))
value = Path.GetRelativePath(_baseDir, value);
writer.WriteStringValue(value);
}
}
}