add required Stream and AsyncEnumerable Extensions Methods ...
This commit is contained in:
@@ -14,6 +14,7 @@ using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using System.Xml;
|
||||
using AutoMapper;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
@@ -169,6 +170,7 @@ namespace xCommons.Extensions
|
||||
//
|
||||
var setting = new JsonSerializerSettings
|
||||
{
|
||||
Formatting = Newtonsoft.Json.Formatting.Indented,
|
||||
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.None
|
||||
};
|
||||
@@ -194,6 +196,7 @@ namespace xCommons.Extensions
|
||||
//
|
||||
var setting = new JsonSerializerSettings
|
||||
{
|
||||
Formatting = Newtonsoft.Json.Formatting.Indented,
|
||||
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.None,
|
||||
};
|
||||
@@ -670,12 +673,213 @@ namespace xCommons.Extensions
|
||||
foreach (var item in source)
|
||||
{
|
||||
//
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
//
|
||||
yield return item;
|
||||
await Task.Yield(); // keeps method truly async
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an Stream to Model AsyncEnumerator ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static async IAsyncEnumerable<T> ToModelAsyncEnumerable<T>(
|
||||
this Stream source,
|
||||
[System.Runtime.CompilerServices.EnumeratorCancellation]
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate Stream ...
|
||||
if (source.IsNull())
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
//
|
||||
while (source.CanRead)
|
||||
{
|
||||
//
|
||||
var model = await source
|
||||
.ReadModelFromStream<T>(cancellationToken);
|
||||
|
||||
//
|
||||
yield return model;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an Stream to String AsyncEnumerator ...
|
||||
/// using Read Line of Stream Reader ...S
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public static async IAsyncEnumerable<string> ToStringAsyncEnumerable(
|
||||
this Stream source,
|
||||
[System.Runtime.CompilerServices.EnumeratorCancellation]
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate Stream ...
|
||||
if (source.IsNull())
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
//
|
||||
while (source.CanRead)
|
||||
{
|
||||
//
|
||||
var json = await source
|
||||
.ReadJsonFromStream(cancellationToken);
|
||||
|
||||
//
|
||||
yield return json;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write a Model to Stream ...
|
||||
/// </summary>
|
||||
/// <param name="source">an Stream to Write ...</param>
|
||||
/// <param name="model">a Model instance to Write ...</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static async Task WriteModelToStream<T>(
|
||||
this Stream source,
|
||||
T model,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (source.IsNull() || model.IsNull())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Extract Json of Model ...
|
||||
var json = model.ToJSON(true);
|
||||
|
||||
//
|
||||
await source.WriteJsonToStream(
|
||||
json: json,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write a Json String to Stream ...
|
||||
/// </summary>
|
||||
/// <param name="source">an Stream to Write ...</param>
|
||||
/// <param name="json">json string to write ...</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task WriteJsonToStream(
|
||||
this Stream source,
|
||||
string json,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (source.IsNull() || json.IsNullOrEmpty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
using (var streamWriter = new StreamWriter(stream: source))
|
||||
{
|
||||
//
|
||||
using (var jsonWriter = new JsonTextWriter(streamWriter))
|
||||
{
|
||||
//
|
||||
await jsonWriter.WriteRawAsync(json, cancellationToken);
|
||||
await jsonWriter.FlushAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read a Json Model From Stream ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static async Task<T> ReadModelFromStream<T>(
|
||||
this Stream source,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
T result = default(T);
|
||||
|
||||
//
|
||||
var json = await source.ReadJsonFromStream(cancellationToken);
|
||||
if (!json.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
result = json.FromJSON<T>();
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reading Json String From Stream ...
|
||||
/// </summary>
|
||||
/// <param name="source">an Stream to Write ...</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<string> ReadJsonFromStream(
|
||||
this Stream source,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = string.Empty;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
if (source.IsNull())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
using (var streamReader = new StreamReader(source))
|
||||
{
|
||||
//
|
||||
result = await Task.Run(() =>
|
||||
{
|
||||
return streamReader.ReadLine();
|
||||
},
|
||||
cancellationToken);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update a Collection item ...
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user