@@ -28,26 +28,6 @@ public static class FileHelper
2828 }
2929 }
3030
31- /// <summary>
32- /// Loads the specified JSON file and deserializes its content as a <typeparamref name="TValue"/>.
33- /// </summary>
34- /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
35- /// <param name="path">JSON file path.</param>
36- /// <param name="jsonSerializerOptions">Deserialization options.</param>
37- /// <param name="cancellationToken">A token that may be used to cancel the read operation.</param>
38- /// <returns>A <typeparamref name="TValue"/>.</returns>
39- public static async Task < TValue > LoadFromJsonFileAsync < TValue > ( string path , JsonSerializerOptions ? jsonSerializerOptions = null , CancellationToken cancellationToken = default ) where TValue : class , new ( )
40- {
41- if ( ! File . Exists ( path ) )
42- return new ( ) ;
43-
44- var fileStream = new FileStream ( path , FileMode . Open ) ;
45- await using ( fileStream . ConfigureAwait ( false ) )
46- {
47- return await JsonSerializer . DeserializeAsync < TValue > ( fileStream , jsonSerializerOptions , cancellationToken ) . ConfigureAwait ( false ) ?? new ( ) ;
48- }
49- }
50-
5131 /// <summary>
5232 /// Serializes the provided value as JSON and saves to the specified file.
5333 /// </summary>
@@ -76,33 +56,4 @@ public static async Task SaveToJsonFileAsync<TValue>(
7656 if ( canReplace )
7757 File . Replace ( newPath , path , $ "{ path } .old") ;
7858 }
79-
80- /// <summary>
81- /// Serializes the provided value as JSON and saves to the specified file.
82- /// </summary>
83- /// <typeparam name="TValue">The type of the value to serialize.</typeparam>
84- /// <param name="path">JSON file path.</param>
85- /// <param name="value">The value to save.</param>
86- /// <param name="jsonSerializerOptions">Serialization options.</param>
87- /// <param name="cancellationToken">A token that may be used to cancel the write operation.</param>
88- /// <returns>A task that represents the asynchronous write operation.</returns>
89- public static async Task SaveToJsonFileAsync < TValue > (
90- string path ,
91- TValue value ,
92- JsonSerializerOptions ? jsonSerializerOptions = null ,
93- CancellationToken cancellationToken = default )
94- {
95- // File.Replace throws an exception when the destination file does not exist.
96- var canReplace = File . Exists ( path ) ;
97- var newPath = canReplace ? $ "{ path } .new" : path ;
98- var fileStream = new FileStream ( newPath , FileMode . Create ) ;
99-
100- await using ( fileStream . ConfigureAwait ( false ) )
101- {
102- await JsonSerializer . SerializeAsync ( fileStream , value , jsonSerializerOptions , cancellationToken ) ;
103- }
104-
105- if ( canReplace )
106- File . Replace ( newPath , path , $ "{ path } .old") ;
107- }
10859}
0 commit comments