Class JsonLineLoader<TRecord>
Loads items of type TRecord into a JSONL (JSON Lines / NDJSON) stream.
public sealed class JsonLineLoader<TRecord> : LoaderBase<TRecord, JsonReport>, ILoadWithProgressAndCancellationAsync<TRecord, JsonReport>, ILoadWithProgressAsync<TRecord, JsonReport>, ILoadWithCancellationAsync<TRecord>, ILoadAsync<TRecord> where TRecord : notnull
Type Parameters
TRecordThe type of items to load. Must be
notnull.
- Inheritance
-
LoaderBase<TRecord, JsonReport>JsonLineLoader<TRecord>
- Implements
-
ILoadWithProgressAndCancellationAsync<TRecord, JsonReport>ILoadWithProgressAsync<TRecord, JsonReport>ILoadWithCancellationAsync<TRecord>ILoadAsync<TRecord>
- Inherited Members
-
LoaderBase<TRecord, JsonReport>.ReportingIntervalLoaderBase<TRecord, JsonReport>.CurrentItemCountLoaderBase<TRecord, JsonReport>.CurrentSkippedItemCountLoaderBase<TRecord, JsonReport>.MaximumItemCountLoaderBase<TRecord, JsonReport>.SkipItemCount
Examples
using var stream = File.Create("output.jsonl");
var loader = new JsonLineLoader<Person>(stream);
await loader.LoadAsync(items, cancellationToken);
Remarks
Writes each item as a single line of JSON to the output stream, with each line separated by a newline character. Compatible with both JSONL and NDJSON consumers.
Constructors
JsonLineLoader(Stream)
Initializes a new instance of the JsonLineLoader<TRecord> class.
public JsonLineLoader(Stream stream)
Parameters
streamStreamThe stream to write JSONL data to.
Exceptions
- ArgumentNullException
Thrown when
streamisnull.
JsonLineLoader(Stream, ILogger<JsonLineLoader<TRecord>>)
Initializes a new instance of the JsonLineLoader<TRecord> class with diagnostic logging.
public JsonLineLoader(Stream stream, ILogger<JsonLineLoader<TRecord>> logger)
Parameters
streamStreamThe stream to write JSONL data to.
loggerILogger<JsonLineLoader<TRecord>>The logger instance for diagnostic output.
Exceptions
- ArgumentNullException
Thrown when
streamorloggerisnull.
JsonLineLoader(Stream, JsonSerializerOptions, ILogger<JsonLineLoader<TRecord>>?)
Initializes a new instance of the JsonLineLoader<TRecord> class with custom serialization options.
public JsonLineLoader(Stream stream, JsonSerializerOptions options, ILogger<JsonLineLoader<TRecord>>? logger = null)
Parameters
streamStreamThe stream to write JSONL data to.
optionsJsonSerializerOptionsThe JSON serializer options to use for serialization.
loggerILogger<JsonLineLoader<TRecord>>An optional logger instance for diagnostic output.
Exceptions
- ArgumentNullException
Thrown when
streamoroptionsisnull.
JsonLineLoader(Stream, JsonTypeInfo<TRecord>, ILogger<JsonLineLoader<TRecord>>?)
Initializes a new instance of the JsonLineLoader<TRecord> class with source-generated type metadata for AOT-friendly, reflection-free serialization.
public JsonLineLoader(Stream stream, JsonTypeInfo<TRecord> typeInfo, ILogger<JsonLineLoader<TRecord>>? logger = null)
Parameters
streamStreamThe stream to write JSONL data to.
typeInfoJsonTypeInfo<TRecord>The source-generated type metadata for
TRecord.loggerILogger<JsonLineLoader<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 loading 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.
LoadWorkerAsync(IAsyncEnumerable<TRecord>, CancellationToken)
This method is the core implementation of the loading logic and should be overridden by derived classes.
protected override Task LoadWorkerAsync(IAsyncEnumerable<TRecord> items, CancellationToken token)
Parameters
itemsIAsyncEnumerable<TRecord>The items to be loaded to the destination.
tokenCancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task
A task representing the asynchronous operation.
Remarks
Items may be an empty sequence if no data is available or if the loading fails.
Exceptions
- ArgumentNullException
Argument items is null