|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Text; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +namespace Open.Serialization |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Default utiltiy methods for use with implementing interfaces/classes. |
| 12 | + /// </summary> |
| 13 | + // Note: not implemented as extensions as it would cause ambiguity collisions |
| 14 | + // But retained public for use externally. |
| 15 | + public static class DefaultMethods |
| 16 | + { |
| 17 | + /// <inheritdoc cref="IDeserializeObjectAsync.DeserializeAsync(Stream, Type, CancellationToken))" /> |
| 18 | + public static async ValueTask<object?> DeserializeAsync(IDeserializeObject deserializer, Stream source, Type type) |
| 19 | + { |
| 20 | + if (deserializer is null) throw new ArgumentNullException(nameof(deserializer)); |
| 21 | + if (source is null) throw new ArgumentNullException(nameof(source)); |
| 22 | + if (type is null) throw new ArgumentNullException(nameof(type)); |
| 23 | + string text; |
| 24 | + using (var reader = new StreamReader(source)) |
| 25 | + text = await reader.ReadToEndAsync().ConfigureAwait(false); |
| 26 | + return deserializer.Deserialize(text, type); |
| 27 | + } |
| 28 | + |
| 29 | + /// <inheritdoc cref="ISerializeObjectAsync.SerializeAsync(Stream, object, Type, CancellationToken)"/> |
| 30 | + public static async ValueTask SerializeAsync(ISerializeObject serializer, Stream target, object? item, Type type) |
| 31 | + { |
| 32 | + if (serializer is null) throw new ArgumentNullException(nameof(serializer)); |
| 33 | + if (target is null) throw new ArgumentNullException(nameof(target)); |
| 34 | + if (type is null) throw new ArgumentNullException(nameof(type)); |
| 35 | + var text = serializer.Serialize(item, type); |
| 36 | + using var writer = new StreamWriter(target); |
| 37 | + await writer.WriteAsync(text).ConfigureAwait(false); |
| 38 | + } |
| 39 | + |
| 40 | + /// <inheritdoc cref="IDeserializeAsync.DeserializeAsync{T}(Stream, CancellationToken)"/> |
| 41 | + public static async ValueTask<T> DeserializeAsync<T>(IDeserialize deserializer, Stream source) |
| 42 | + { |
| 43 | + if (deserializer is null) throw new ArgumentNullException(nameof(deserializer)); |
| 44 | + if (source is null) throw new ArgumentNullException(nameof(source)); |
| 45 | + string text; |
| 46 | + using (var reader = new StreamReader(source)) |
| 47 | + text = await reader.ReadToEndAsync().ConfigureAwait(false); |
| 48 | + return deserializer.Deserialize<T>(text); |
| 49 | + } |
| 50 | + |
| 51 | + /// <inheritdoc cref="ISerializeAsync.SerializeAsync{T}(Stream, T, CancellationToken)"/> |
| 52 | + public static async ValueTask SerializeAsync<T>(ISerialize serializer, Stream target, T item) |
| 53 | + { |
| 54 | + if (serializer is null) throw new ArgumentNullException(nameof(serializer)); |
| 55 | + if (target is null) throw new ArgumentNullException(nameof(target)); |
| 56 | + |
| 57 | + var text = serializer.Serialize(item); |
| 58 | + using var writer = new StreamWriter(target); |
| 59 | + await writer.WriteAsync(text).ConfigureAwait(false); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + /// <inheritdoc cref="IDeserializeAsync{T}.DeserializeAsync(Stream, CancellationToken)"/> |
| 64 | + public static async ValueTask<T> DeserializeAsync<T>(IDeserialize<T> deserializer, Stream source) |
| 65 | + { |
| 66 | + if (deserializer is null) throw new ArgumentNullException(nameof(deserializer)); |
| 67 | + if (source is null) throw new ArgumentNullException(nameof(source)); |
| 68 | + string text; |
| 69 | + using (var reader = new StreamReader(source)) |
| 70 | + text = await reader.ReadToEndAsync().ConfigureAwait(false); |
| 71 | + return deserializer.Deserialize(text); |
| 72 | + } |
| 73 | + |
| 74 | + /// <inheritdoc cref="ISerializeAsync{T}.SerializeAsync(Stream, T, CancellationToken)"/> |
| 75 | + public static async ValueTask SerializeAsync<T>(ISerialize<T> serializer, Stream target, T item) |
| 76 | + { |
| 77 | + if (serializer is null) throw new ArgumentNullException(nameof(serializer)); |
| 78 | + if (target is null) throw new ArgumentNullException(nameof(target)); |
| 79 | + |
| 80 | + var text = serializer.Serialize(item); |
| 81 | + using var writer = new StreamWriter(target); |
| 82 | + await writer.WriteAsync(text).ConfigureAwait(false); |
| 83 | + } |
| 84 | + |
| 85 | + } |
| 86 | +} |
0 commit comments