Class JsonSingleStreamExtractor<TRecord>
Extracts items of type TRecord from a single JSON array stream.
public sealed class JsonSingleStreamExtractor<TRecord> : ExtractorBase<TRecord, JsonReport>, IExtractWithProgressAndCancellationAsync<TRecord, JsonReport>, IExtractWithCancellationAsync<TRecord>, IExtractWithProgressAsync<TRecord, JsonReport>, IExtractAsync<TRecord> where TRecord : notnull
Type Parameters
TRecordThe type of items to extract. Must be
notnull.
- Inheritance
-
ExtractorBase<TRecord, JsonReport>JsonSingleStreamExtractor<TRecord>
- Implements
-
IExtractWithProgressAndCancellationAsync<TRecord, JsonReport>IExtractWithCancellationAsync<TRecord>IExtractWithProgressAsync<TRecord, JsonReport>IExtractAsync<TRecord>
- Inherited Members
-
ExtractorBase<TRecord, JsonReport>.ExtractAsync()ExtractorBase<TRecord, JsonReport>.ReportingIntervalExtractorBase<TRecord, JsonReport>.CurrentItemCountExtractorBase<TRecord, JsonReport>.CurrentSkippedItemCountExtractorBase<TRecord, JsonReport>.MaximumItemCountExtractorBase<TRecord, JsonReport>.SkipItemCount
Examples
using var stream = File.OpenRead("data.json");
var extractor = new JsonSingleStreamExtractor<Person>(stream);
await foreach (var person in extractor.ExtractAsync(cancellationToken))
{
Console.WriteLine(person.Name);
}
Remarks
Reads a JSON array (e.g. [{"name":"Alice"},{"name":"Bob"}]) from a Stream
and yields each deserialized object as an item in the async enumerable sequence.
Uses DeserializeAsyncEnumerable<TValue>(Stream, JsonSerializerOptions, CancellationToken)
for true streaming deserialization.
Constructors
JsonSingleStreamExtractor(Stream)
Initializes a new instance of the JsonSingleStreamExtractor<TRecord> class.
public JsonSingleStreamExtractor(Stream stream)
Parameters
streamStreamThe stream containing a JSON array to read from.
Exceptions
- ArgumentNullException
Thrown when
streamisnull.
JsonSingleStreamExtractor(Stream, ILogger<JsonSingleStreamExtractor<TRecord>>)
Initializes a new instance of the JsonSingleStreamExtractor<TRecord> class with diagnostic logging.
public JsonSingleStreamExtractor(Stream stream, ILogger<JsonSingleStreamExtractor<TRecord>> logger)
Parameters
streamStreamThe stream containing a JSON array to read from.
loggerILogger<JsonSingleStreamExtractor<TRecord>>The logger instance for diagnostic output.
Exceptions
- ArgumentNullException
Thrown when
streamorloggerisnull.
JsonSingleStreamExtractor(Stream, JsonSerializerOptions, ILogger<JsonSingleStreamExtractor<TRecord>>?)
Initializes a new instance of the JsonSingleStreamExtractor<TRecord> class with custom serialization options.
public JsonSingleStreamExtractor(Stream stream, JsonSerializerOptions options, ILogger<JsonSingleStreamExtractor<TRecord>>? logger = null)
Parameters
streamStreamThe stream containing a JSON array to read from.
optionsJsonSerializerOptionsThe JSON serializer options to use for deserialization.
loggerILogger<JsonSingleStreamExtractor<TRecord>>An optional logger instance for diagnostic output.
Exceptions
- ArgumentNullException
Thrown when
streamoroptionsisnull.
JsonSingleStreamExtractor(Stream, JsonTypeInfo<TRecord>, ILogger<JsonSingleStreamExtractor<TRecord>>?)
Initializes a new instance of the JsonSingleStreamExtractor<TRecord> class with source-generated type metadata for AOT-friendly, reflection-free deserialization.
public JsonSingleStreamExtractor(Stream stream, JsonTypeInfo<TRecord> typeInfo, ILogger<JsonSingleStreamExtractor<TRecord>>? logger = null)
Parameters
streamStreamThe stream containing a JSON array to read from.
typeInfoJsonTypeInfo<TRecord>The source-generated type metadata for
TRecord.loggerILogger<JsonSingleStreamExtractor<TRecord>>An optional logger instance for diagnostic output.
Exceptions
- ArgumentNullException
Thrown when
streamortypeInfoisnull.
Methods
CreateProgressReport()
Creates a progress report of type TProgress. This gives the derived class the opportunity to implement a custom progress report that is specific to the extraction process.
protected override JsonReport CreateProgressReport()
Returns
- JsonReport
Progress of type TProgress
CreateProgressTimer(IProgress<JsonReport>)
Creates the Wolfgang.Etl.Abstractions.IProgressTimer used to drive progress callbacks. Override this method in a derived class to inject a custom timer (for example, a custom implementation that allows manual control in unit tests).
protected override IProgressTimer CreateProgressTimer(IProgress<JsonReport> progress)
Parameters
progressIProgress<JsonReport>The progress sink that will receive callbacks.
Returns
- IProgressTimer
A started Wolfgang.Etl.Abstractions.IProgressTimer instance.
ExtractWorkerAsync(CancellationToken)
This method is the core implementation of the extraction logic and should be overridden by derived classes.
protected override IAsyncEnumerable<TRecord> ExtractWorkerAsync(CancellationToken token)
Parameters
tokenCancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- IAsyncEnumerable<TRecord>
IAsyncEnumerable<TSource> The result may be an empty sequence if no data is available or if the extraction fails.