| | | 1 | | using System.Text.Json; |
| | | 2 | | |
| | | 3 | | namespace ArturRios.Util.IO; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides asynchronous helper methods for reading and deserializing file contents. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// All methods validate the provided path and throw <see cref="ArgumentException"/> for null/whitespace and <see cref=" |
| | | 10 | | /// Behavior matches <see cref="FileReader"/> exactly; only the I/O is asynchronous. |
| | | 11 | | /// </remarks> |
| | | 12 | | public static class FileReaderAsync |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Asynchronously reads the entire contents of a text file. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="path">Absolute or relative path to the file.</param> |
| | | 18 | | /// <param name="cancellationToken">Cancels the read.</param> |
| | | 19 | | /// <returns>File content as a string.</returns> |
| | | 20 | | /// <exception cref="ArgumentException">Path is null or whitespace.</exception> |
| | | 21 | | /// <exception cref="FileNotFoundException">File does not exist.</exception> |
| | | 22 | | /// <exception cref="OperationCanceledException"><paramref name="cancellationToken"/> was canceled.</exception> |
| | | 23 | | public static async Task<string> ReadAsync(string path, CancellationToken cancellationToken = default) |
| | | 24 | | { |
| | 7 | 25 | | FileReader.ValidatePath(path); |
| | | 26 | | |
| | 3 | 27 | | return await File.ReadAllTextAsync(path, cancellationToken).ConfigureAwait(false); |
| | 2 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Asynchronously reads a delimited text file (e.g. CSV) mapping each header to its column values. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="path">Path to the file.</param> |
| | | 34 | | /// <param name="separator">Delimiter separating fields (e.g. <c>','</c>).</param> |
| | | 35 | | /// <param name="cancellationToken">Cancels the read.</param> |
| | | 36 | | /// <returns>A dictionary keyed by column header mapping to arrays of its values.</returns> |
| | | 37 | | /// <exception cref="ArgumentException">Path is null or whitespace.</exception> |
| | | 38 | | /// <exception cref="FileNotFoundException">File does not exist.</exception> |
| | | 39 | | /// <exception cref="InvalidOperationException"> |
| | | 40 | | /// The file has fewer than two lines, or the header row repeats a name, which would silently drop a |
| | | 41 | | /// column from the result. |
| | | 42 | | /// </exception> |
| | | 43 | | /// <exception cref="OperationCanceledException"><paramref name="cancellationToken"/> was canceled.</exception> |
| | | 44 | | /// <remarks> |
| | | 45 | | /// This is a plain split on <paramref name="separator"/>, not an RFC 4180 parser: quoting is not |
| | | 46 | | /// interpreted, so a quoted field containing the separator or a line break is split like any other. |
| | | 47 | | /// Use a dedicated CSV library when the data can contain either. Rows shorter than the header are |
| | | 48 | | /// padded with empty strings, and values beyond the last header are discarded. |
| | | 49 | | /// </remarks> |
| | | 50 | | public static async Task<Dictionary<string, string[]>> ReadAsDictionaryAsync( |
| | | 51 | | string path, |
| | | 52 | | char separator, |
| | | 53 | | CancellationToken cancellationToken = default) |
| | | 54 | | { |
| | 16 | 55 | | FileReader.ValidatePath(path); |
| | | 56 | | |
| | 12 | 57 | | var lines = await File.ReadAllLinesAsync(path, cancellationToken).ConfigureAwait(false); |
| | | 58 | | |
| | 11 | 59 | | return DelimitedText.ToDictionary(lines, separator); |
| | 8 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Asynchronously reads all lines of a text file into an array. |
| | | 64 | | /// </summary> |
| | | 65 | | /// <param name="path">Path to the file.</param> |
| | | 66 | | /// <param name="cancellationToken">Cancels the read.</param> |
| | | 67 | | /// <returns>Array of lines.</returns> |
| | | 68 | | /// <exception cref="ArgumentException">Path is null or whitespace.</exception> |
| | | 69 | | /// <exception cref="FileNotFoundException">File does not exist.</exception> |
| | | 70 | | /// <exception cref="OperationCanceledException"><paramref name="cancellationToken"/> was canceled.</exception> |
| | | 71 | | public static async Task<string[]> ReadLinesAsync(string path, CancellationToken cancellationToken = default) |
| | | 72 | | { |
| | 7 | 73 | | FileReader.ValidatePath(path); |
| | | 74 | | |
| | 3 | 75 | | return await File.ReadAllLinesAsync(path, cancellationToken).ConfigureAwait(false); |
| | 2 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Asynchronously reads a JSON file and deserializes its content into the specified type. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <typeparam name="T">Target type.</typeparam> |
| | | 82 | | /// <param name="path">Path to the JSON file.</param> |
| | | 83 | | /// <param name="cancellationToken">Cancels the read and the deserialization.</param> |
| | | 84 | | /// <returns> |
| | | 85 | | /// The deserialized object, or <c>null</c> when the file contains the JSON literal <c>null</c>. |
| | | 86 | | /// </returns> |
| | | 87 | | /// <exception cref="ArgumentException">Path is null or whitespace.</exception> |
| | | 88 | | /// <exception cref="FileNotFoundException">File does not exist.</exception> |
| | | 89 | | /// <exception cref="JsonException"> |
| | | 90 | | /// The file is empty, holds only whitespace, or does not contain JSON matching |
| | | 91 | | /// <typeparamref name="T"/>. An unreadable file is reported rather than silently turned into |
| | | 92 | | /// <c>null</c>, which would be indistinguishable from a genuine null payload. |
| | | 93 | | /// </exception> |
| | | 94 | | /// <exception cref="OperationCanceledException"><paramref name="cancellationToken"/> was canceled.</exception> |
| | | 95 | | public static async Task<T?> ReadAndDeserializeAsync<T>(string path, CancellationToken cancellationToken = default) |
| | | 96 | | { |
| | 11 | 97 | | FileReader.ValidatePath(path); |
| | | 98 | | |
| | 7 | 99 | | await using var stream = File.OpenRead(path); |
| | | 100 | | |
| | 7 | 101 | | return await JsonSerializer.DeserializeAsync<T>(stream, cancellationToken: cancellationToken).ConfigureAwait(fal |
| | 2 | 102 | | } |
| | | 103 | | } |