|
4 | 4 | using System.Text; |
5 | 5 | using System.Text.Json; |
6 | 6 | using System.Text.Json.Nodes; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
7 | 9 | using ToonFormat; |
8 | 10 | using ToonFormat.Internal.Decode; |
9 | 11 |
|
@@ -194,4 +196,124 @@ public static class ToonDecoder |
194 | 196 | var text = reader.ReadToEnd(); |
195 | 197 | return Decode<T>(text, options ?? new ToonDecodeOptions()); |
196 | 198 | } |
| 199 | + |
| 200 | + #region Async Methods |
| 201 | + |
| 202 | + /// <summary> |
| 203 | + /// Asynchronously decodes a TOON-formatted string into a JsonNode with default options. |
| 204 | + /// </summary> |
| 205 | + /// <param name="toonString">The TOON-formatted string to decode.</param> |
| 206 | + /// <param name="cancellationToken">A token to cancel the operation.</param> |
| 207 | + /// <returns>A task that represents the asynchronous operation. The task result contains the decoded JsonNode.</returns> |
| 208 | + /// <exception cref="ArgumentNullException">Thrown when toonString is null.</exception> |
| 209 | + /// <exception cref="ToonFormatException">Thrown when the TOON format is invalid.</exception> |
| 210 | + public static Task<JsonNode?> DecodeAsync(string toonString, CancellationToken cancellationToken = default) |
| 211 | + { |
| 212 | + return DecodeAsync(toonString, new ToonDecodeOptions(), cancellationToken); |
| 213 | + } |
| 214 | + |
| 215 | + /// <summary> |
| 216 | + /// Asynchronously decodes a TOON-formatted string into a JsonNode with custom options. |
| 217 | + /// </summary> |
| 218 | + /// <param name="toonString">The TOON-formatted string to decode.</param> |
| 219 | + /// <param name="options">Decoding options to customize parsing behavior.</param> |
| 220 | + /// <param name="cancellationToken">A token to cancel the operation.</param> |
| 221 | + /// <returns>A task that represents the asynchronous operation. The task result contains the decoded JsonNode.</returns> |
| 222 | + /// <exception cref="ArgumentNullException">Thrown when toonString or options is null.</exception> |
| 223 | + /// <exception cref="ToonFormatException">Thrown when the TOON format is invalid.</exception> |
| 224 | + public static Task<JsonNode?> DecodeAsync(string toonString, ToonDecodeOptions? options, CancellationToken cancellationToken = default) |
| 225 | + { |
| 226 | + cancellationToken.ThrowIfCancellationRequested(); |
| 227 | + var result = Decode(toonString, options); |
| 228 | + return Task.FromResult(result); |
| 229 | + } |
| 230 | + |
| 231 | + /// <summary> |
| 232 | + /// Asynchronously decodes a TOON-formatted string into the specified type with default options. |
| 233 | + /// </summary> |
| 234 | + /// <typeparam name="T">Target type to deserialize into.</typeparam> |
| 235 | + /// <param name="toonString">The TOON-formatted string to decode.</param> |
| 236 | + /// <param name="cancellationToken">A token to cancel the operation.</param> |
| 237 | + /// <returns>A task that represents the asynchronous operation. The task result contains the deserialized value.</returns> |
| 238 | + public static Task<T?> DecodeAsync<T>(string toonString, CancellationToken cancellationToken = default) |
| 239 | + { |
| 240 | + return DecodeAsync<T>(toonString, new ToonDecodeOptions(), cancellationToken); |
| 241 | + } |
| 242 | + |
| 243 | + /// <summary> |
| 244 | + /// Asynchronously decodes a TOON-formatted string into the specified type with custom options. |
| 245 | + /// </summary> |
| 246 | + /// <typeparam name="T">Target type to deserialize into.</typeparam> |
| 247 | + /// <param name="toonString">The TOON-formatted string to decode.</param> |
| 248 | + /// <param name="options">Decoding options to customize parsing behavior.</param> |
| 249 | + /// <param name="cancellationToken">A token to cancel the operation.</param> |
| 250 | + /// <returns>A task that represents the asynchronous operation. The task result contains the deserialized value.</returns> |
| 251 | + public static Task<T?> DecodeAsync<T>(string toonString, ToonDecodeOptions? options, CancellationToken cancellationToken = default) |
| 252 | + { |
| 253 | + cancellationToken.ThrowIfCancellationRequested(); |
| 254 | + var result = Decode<T>(toonString, options); |
| 255 | + return Task.FromResult(result); |
| 256 | + } |
| 257 | + |
| 258 | + /// <summary> |
| 259 | + /// Asynchronously decodes TOON data from a stream (UTF-8) into a JsonNode with default options. |
| 260 | + /// </summary> |
| 261 | + /// <param name="stream">The input stream to read from.</param> |
| 262 | + /// <param name="cancellationToken">A token to cancel the operation.</param> |
| 263 | + /// <returns>A task that represents the asynchronous operation. The task result contains the decoded JsonNode.</returns> |
| 264 | + public static Task<JsonNode?> DecodeAsync(Stream stream, CancellationToken cancellationToken = default) |
| 265 | + { |
| 266 | + return DecodeAsync(stream, new ToonDecodeOptions(), cancellationToken); |
| 267 | + } |
| 268 | + |
| 269 | + /// <summary> |
| 270 | + /// Asynchronously decodes TOON data from a stream (UTF-8) into a JsonNode with custom options. |
| 271 | + /// </summary> |
| 272 | + /// <param name="stream">The input stream to read from.</param> |
| 273 | + /// <param name="options">Decoding options to customize parsing behavior.</param> |
| 274 | + /// <param name="cancellationToken">A token to cancel the operation.</param> |
| 275 | + /// <returns>A task that represents the asynchronous operation. The task result contains the decoded JsonNode.</returns> |
| 276 | + /// <exception cref="ArgumentNullException">Thrown when stream is null.</exception> |
| 277 | + public static async Task<JsonNode?> DecodeAsync(Stream stream, ToonDecodeOptions? options, CancellationToken cancellationToken = default) |
| 278 | + { |
| 279 | + if (stream == null) |
| 280 | + throw new ArgumentNullException(nameof(stream)); |
| 281 | + |
| 282 | + using var reader = new StreamReader(stream, Encoding.UTF8, detectEncodingFromByteOrderMarks: true, leaveOpen: true); |
| 283 | + var text = await reader.ReadToEndAsync(cancellationToken).ConfigureAwait(false); |
| 284 | + return Decode(text, options ?? new ToonDecodeOptions()); |
| 285 | + } |
| 286 | + |
| 287 | + /// <summary> |
| 288 | + /// Asynchronously decodes TOON data from a stream (UTF-8) into the specified type with default options. |
| 289 | + /// </summary> |
| 290 | + /// <typeparam name="T">Target type to deserialize into.</typeparam> |
| 291 | + /// <param name="stream">The input stream to read from.</param> |
| 292 | + /// <param name="cancellationToken">A token to cancel the operation.</param> |
| 293 | + /// <returns>A task that represents the asynchronous operation. The task result contains the deserialized value.</returns> |
| 294 | + public static Task<T?> DecodeAsync<T>(Stream stream, CancellationToken cancellationToken = default) |
| 295 | + { |
| 296 | + return DecodeAsync<T>(stream, new ToonDecodeOptions(), cancellationToken); |
| 297 | + } |
| 298 | + |
| 299 | + /// <summary> |
| 300 | + /// Asynchronously decodes TOON data from a stream (UTF-8) into the specified type with custom options. |
| 301 | + /// </summary> |
| 302 | + /// <typeparam name="T">Target type to deserialize into.</typeparam> |
| 303 | + /// <param name="stream">The input stream to read from.</param> |
| 304 | + /// <param name="options">Decoding options to customize parsing behavior.</param> |
| 305 | + /// <param name="cancellationToken">A token to cancel the operation.</param> |
| 306 | + /// <returns>A task that represents the asynchronous operation. The task result contains the deserialized value.</returns> |
| 307 | + /// <exception cref="ArgumentNullException">Thrown when stream is null.</exception> |
| 308 | + public static async Task<T?> DecodeAsync<T>(Stream stream, ToonDecodeOptions? options, CancellationToken cancellationToken = default) |
| 309 | + { |
| 310 | + if (stream == null) |
| 311 | + throw new ArgumentNullException(nameof(stream)); |
| 312 | + |
| 313 | + using var reader = new StreamReader(stream, Encoding.UTF8, detectEncodingFromByteOrderMarks: true, leaveOpen: true); |
| 314 | + var text = await reader.ReadToEndAsync(cancellationToken).ConfigureAwait(false); |
| 315 | + return Decode<T>(text, options ?? new ToonDecodeOptions()); |
| 316 | + } |
| 317 | + |
| 318 | + #endregion |
197 | 319 | } |
0 commit comments