Exemplo n.º 1
0
 /// <inheritdoc cref="IDeserializeObjectAsync.DeserializeAsync(Stream, Type, CancellationToken)" />
 public static ValueTask <object?> DeserializeAsync(this IDeserializeObject deserializer, Stream source, Type type, CancellationToken cancellationToken = default)
 {
     if (deserializer is null)
     {
         throw new ArgumentNullException(nameof(deserializer));
     }
     return(deserializer is IDeserializeObjectAsync d
                         ? d.DeserializeAsync(source, type, cancellationToken)
                         : DefaultMethods.DeserializeAsync(deserializer, source, type));
 }
Exemplo n.º 2
0
 /// <inheritdoc cref="IDeserialize.Deserialize{T}(string)" />
 public static T Deserialize <T>(this IDeserializeObject deserializer, string?value)
 {
     if (deserializer is null)
     {
         throw new ArgumentNullException(nameof(deserializer));
     }
     return(deserializer is IDeserialize d
                         ? d.Deserialize <T>(value)
                         : (T)deserializer.Deserialize(value, typeof(T)) !);
 }
Exemplo n.º 3
0
 /// <inheritdoc cref="IDeserializeAsync.DeserializeAsync{T}(Stream, CancellationToken)" />
 public static async ValueTask <T> DeserializeAsync <T>(this IDeserializeObject deserializer, Stream source, CancellationToken cancellationToken = default)
 {
     if (deserializer is null)
     {
         throw new ArgumentNullException(nameof(deserializer));
     }
     return(deserializer is IDeserializeAsync d
                         ? await d.DeserializeAsync <T>(source, cancellationToken).ConfigureAwait(false)
                         : (T)(await DeserializeAsync(deserializer, source, typeof(T), cancellationToken).ConfigureAwait(false)) !);
 }
        /// <summary>
        /// Deserializes a stream to the specified type.
        /// </summary>
        public static async ValueTask <object?> DeserializeAsync(IDeserializeObject deserializer, Stream source, Type type)
        {
            if (deserializer is null)
            {
                throw new ArgumentNullException(nameof(deserializer));
            }
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            string text;

            using (var reader = new StreamReader(source))
                text = await reader.ReadToEndAsync().ConfigureAwait(false);
            return(deserializer.Deserialize(text, type));
        }