@@ -53,6 +53,27 @@ static FileHelper()
53
53
}
54
54
}
55
55
56
+ /// <summary>
57
+ /// Loads the specified JSON file and deserializes its content as a <typeparamref name="TValue"/>.
58
+ /// </summary>
59
+ /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
60
+ /// <param name="path">JSON file path.</param>
61
+ /// <param name="jsonSerializerOptions">Deserialization options.</param>
62
+ /// <param name="cancellationToken">A token that may be used to cancel the read operation.</param>
63
+ /// <returns>A <typeparamref name="TValue"/>.</returns>
64
+ public static async Task < TValue > LoadFromJsonFileAsync < TValue > ( string path , JsonSerializerOptions ? jsonSerializerOptions = null , CancellationToken cancellationToken = default ) where TValue : class , new ( )
65
+ {
66
+ path = GetAbsolutePath ( path ) ;
67
+ if ( ! File . Exists ( path ) )
68
+ return new ( ) ;
69
+
70
+ var fileStream = new FileStream ( path , FileMode . Open ) ;
71
+ await using ( fileStream . ConfigureAwait ( false ) )
72
+ {
73
+ return await JsonSerializer . DeserializeAsync < TValue > ( fileStream , jsonSerializerOptions , cancellationToken ) . ConfigureAwait ( false ) ?? new ( ) ;
74
+ }
75
+ }
76
+
56
77
/// <summary>
57
78
/// Serializes the provided value as JSON and saves to the specified file.
58
79
/// </summary>
@@ -89,4 +110,41 @@ public static async Task SaveToJsonFileAsync<TValue>(
89
110
if ( canReplace )
90
111
File . Replace ( newPath , path , $ "{ path } .old") ;
91
112
}
113
+
114
+ /// <summary>
115
+ /// Serializes the provided value as JSON and saves to the specified file.
116
+ /// </summary>
117
+ /// <typeparam name="TValue">The type of the value to serialize.</typeparam>
118
+ /// <param name="path">JSON file path.</param>
119
+ /// <param name="value">The value to save.</param>
120
+ /// <param name="jsonSerializerOptions">Serialization options.</param>
121
+ /// <param name="cancellationToken">A token that may be used to cancel the write operation.</param>
122
+ /// <returns>A task that represents the asynchronous write operation.</returns>
123
+ public static async Task SaveToJsonFileAsync < TValue > (
124
+ string path ,
125
+ TValue value ,
126
+ JsonSerializerOptions ? jsonSerializerOptions = null ,
127
+ CancellationToken cancellationToken = default )
128
+ {
129
+ path = GetAbsolutePath ( path ) ;
130
+
131
+ var directoryPath = Path . GetDirectoryName ( path ) ;
132
+ if ( string . IsNullOrEmpty ( directoryPath ) )
133
+ throw new ArgumentException ( "Invalid path." , nameof ( path ) ) ;
134
+
135
+ _ = Directory . CreateDirectory ( directoryPath ) ;
136
+
137
+ // File.Replace throws an exception when the destination file does not exist.
138
+ var canReplace = File . Exists ( path ) ;
139
+ var newPath = canReplace ? $ "{ path } .new" : path ;
140
+ var fileStream = new FileStream ( newPath , FileMode . Create ) ;
141
+
142
+ await using ( fileStream . ConfigureAwait ( false ) )
143
+ {
144
+ await JsonSerializer . SerializeAsync ( fileStream , value , jsonSerializerOptions , cancellationToken ) ;
145
+ }
146
+
147
+ if ( canReplace )
148
+ File . Replace ( newPath , path , $ "{ path } .old") ;
149
+ }
92
150
}
0 commit comments