Xpandables.AsyncPaged.Json 10.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Xpandables.AsyncPaged.Json --version 10.0.1
                    
NuGet\Install-Package Xpandables.AsyncPaged.Json -Version 10.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Xpandables.AsyncPaged.Json" Version="10.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Xpandables.AsyncPaged.Json" Version="10.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Xpandables.AsyncPaged.Json" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Xpandables.AsyncPaged.Json --version 10.0.1
                    
#r "nuget: Xpandables.AsyncPaged.Json, 10.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Xpandables.AsyncPaged.Json@10.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Xpandables.AsyncPaged.Json&version=10.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Xpandables.AsyncPaged.Json&version=10.0.1
                    
Install as a Cake Tool

ðŸ“Ą System.AsyncPaged.Json

NuGet Version NuGet Downloads .NET License

High-Performance JSON Serialization for Async Pagination — Efficient, streaming JSON serialization and deserialization for IAsyncPagedEnumerable<T> with source generation, adaptive flushing, and minimal allocations.


ðŸŽŊ Overview

Xpandables.AsyncPaged.Json provides native System.Text.Json integration for serializing and deserializing IAsyncPagedEnumerable<T> sequences with pagination metadata. It enables efficient handling of large JSON payloads through streaming serialization, memory-aware buffer management, and full cancellation support.

Built for .NET 10 with AOT compatibility, this library seamlessly integrates pagination metadata into JSON output while maintaining high throughput and minimal memory overhead.

âœĻ Key Features

  • Streaming Serialization — Asynchronously serialize large IAsyncPagedEnumerable<T> to JSON with adaptive flushing
  • Streaming Deserialization — Efficiently deserialize JSON into IAsyncPagedEnumerable<T> with pagination metadata
  • Pagination Metadata Preservation — Automatically includes pagination information in serialized output
  • Adaptive Buffer Management — Memory-aware flushing strategy based on dataset size and buffer pressure
  • Source Generation Ready — Compatible with source-generated JSON serialization for AOT
  • PipeWriter & Stream Support — Works with both System.IO.Pipelines and Stream-based I/O
  • Full Cancellation Support — All operations support CancellationToken for graceful shutdown
  • High Performance — Optimized for minimal allocations and maximum throughput
  • Type-Safe — Generic and non-generic overloads for maximum flexibility

ðŸ“Ķ Installation

dotnet add package Xpandables.AsyncPaged.Json

Or via NuGet Package Manager:

Install-Package Xpandables.AsyncPaged.Json

Prerequisites

  • Xpandables.AsyncPaged (automatically installed as a dependency)
  • .NET 10.0 or later

🚀 Quick Start

ðŸ“Ī Serializing Paged Data to JSON

using System.Text.Json;
using System.IO.Pipelines;

// Get paged data
IAsyncPagedEnumerable<Product> products = GetProductsAsync();

// Serialize to stream
using var stream = new FileStream("products.json", FileMode.Create);
var options = new JsonSerializerOptions { WriteIndented = true };

await JsonSerializer.SerializeAsyncPaged(
    stream,
    products,
    options,
    cancellationToken: CancellationToken.None);

ðŸ“Ĩ Deserializing JSON to Paged Enumerable

using System.Text.Json;

// Deserialize from stream
using var stream = new FileStream("products.json", FileMode.Open);
var options = new JsonSerializerOptions();

IAsyncPagedEnumerable<Product> products = JsonSerializer.DeserializeAsyncPagedEnumerable<Product>(
    stream,
    options,
    PaginationStrategy.None);

// Enumerate with pagination
await foreach (var product in products)
{
    Console.WriteLine(product.Name);
}

var pagination = await products.GetPaginationAsync();
Console.WriteLine($"Total: {pagination.TotalCount}");

🔄 Round-Trip Serialization

// Original paged enumerable
var original = _context.Products
    .Where(p => p.IsActive)
    .ToAsyncPagedEnumerable();

// Serialize to JSON
using var stream = new MemoryStream();
var options = JsonSerializerOptions.Default;

await JsonSerializer.SerializeAsyncPaged(stream, original, options);

// Deserialize back
stream.Seek(0, SeekOrigin.Begin);
var restored = JsonSerializer.DeserializeAsyncPagedEnumerable<Product>(
    stream,
    options,
    PaginationStrategy.None);

// Both have the same pagination metadata
var originalPagination = await original.GetPaginationAsync();
var restoredPagination = await restored.GetPaginationAsync();

📚 Core Concepts

📋 JSON Structure

When serialized, IAsyncPagedEnumerable<T> produces the following JSON structure:

{
  "pagination": {
    "totalCount": 500,
    "pageSize": 20,
    "currentPage": 1,
    "continuationToken": null
  },
  "items": [
    { "id": 1, "name": "Product A" },
    { "id": 2, "name": "Product B" },
    ....
  ]
}

The pagination object contains all metadata needed to reconstruct page navigation, while items contains the serialized enumerable elements.

🔄 Serialization Modes

Stream-Based:

// Serialize to Stream
await JsonSerializer.SerializeAsyncPaged(
    utf8Stream,
    pagedEnumerable,
    options);

PipeWriter-Based (System.IO.Pipelines):

// Serialize to PipeWriter for high-performance scenarios
PipeWriter writer = PipeWriter.Create(stream);
await JsonSerializer.SerializeAsyncPaged(
    writer,
    pagedEnumerable,
    options);

ðŸ“Ĩ Deserialization Modes

From Stream:

IAsyncPagedEnumerable<T> result = JsonSerializer.DeserializeAsyncPagedEnumerable<T>(
    stream,
    options,
    PaginationStrategy.None);

From PipeReader:

PipeReader reader = PipeReader.Create(stream);
IAsyncPagedEnumerable<T> result = JsonSerializer.DeserializeAsyncPagedEnumerable<T>(
    reader,
    options,
    PaginationStrategy.None);

🧠 Adaptive Flushing Strategy

The serializer uses an intelligent flushing strategy that adapts based on dataset size:

Dataset Size Batch Size Behavior
Unknown 100 items Default balanced approach
< 1,000 items 200 items Less frequent flushes for small datasets
1,000 - 10,000 items 100 items Standard balanced flushing
10,000 - 100,000 items 50 items More frequent for memory management
> 100,000 items 25 items Aggressive flushing for very large datasets

Additionally, flushing occurs whenever the pending bytes exceed 32 KB, ensuring memory pressure is managed.


ðŸ’Ą Common Patterns

📊 REST API Response with Pagination

[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
    [HttpGet]
    public async Task GetProductsAsync(int pageNumber = 1, int pageSize = 20)
    {
        var products = _context.Products
            .Where(p => p.IsActive)
            .Skip((pageNumber - 1) * pageSize)
            .Take(pageSize)
            .ToAsyncPagedEnumerable();

        Response.ContentType = "application/json";
        await JsonSerializer.SerializeAsyncPaged(
            Response.Body,
            products,
            _jsonOptions);
    }
}

ðŸ’ū Large Dataset Export

public async Task ExportProductsAsync(string filePath, CancellationToken cancellationToken)
{
    var products = GetAllProductsAsync();

    using var stream = new FileStream(
        filePath,
        FileMode.Create,
        FileAccess.Write,
        FileShare.None,
        bufferSize: 65536,
        useAsync: true);

    var options = new JsonSerializerOptions { WriteIndented = false };

    await JsonSerializer.SerializeAsyncPaged(
        stream,
        products,
        options,
        cancellationToken);

    Console.WriteLine($"Exported {products.GetPaginationAsync().Result.TotalCount} products");
}

ðŸ“Ķ Import with Validation

public async Task ImportProductsAsync(string filePath, CancellationToken cancellationToken)
{
    using var stream = new FileStream(filePath, FileMode.Open);
    var options = new JsonSerializerOptions();

    var products = JsonSerializer.DeserializeAsyncPagedEnumerable<Product>(
        stream,
        options,
        PaginationStrategy.None,
        cancellationToken);

    await foreach (var product in products.WithCancellation(cancellationToken))
    {
        if (product is not null)
        {
            // Validate each product
            if (await ValidateProductAsync(product, cancellationToken))
            {
                await _repository.AddAsync(product, cancellationToken);
            }
        }
    }

    var pagination = await products.GetPaginationAsync(cancellationToken);
    Console.WriteLine($"Imported {pagination.TotalCount} products");
}

🔄 Data Transformation Pipeline

public async Task TransformAndExportAsync(
    string inputPath,
    string outputPath,
    CancellationToken cancellationToken)
{
    // Deserialize
    using var inputStream = new FileStream(inputPath, FileMode.Open);
    var inputOptions = new JsonSerializerOptions();
    var input = JsonSerializer.DeserializeAsyncPagedEnumerable<ProductDto>(
        inputStream,
        inputOptions,
        PaginationStrategy.None,
        cancellationToken);

    // Transform using LINQ
    var transformed = input.SelectPagedAsync(async product =>
    {
        if (product is not null)
        {
            product.Price *= 1.1m; // 10% markup
            product.LastUpdated = DateTime.UtcNow;
        }
        return product;
    });

    // Serialize
    using var outputStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write, FileShare.None, 65536, useAsync: true);
    var outputOptions = new JsonSerializerOptions { WriteIndented = false };

    await JsonSerializer.SerializeAsyncPaged(
        outputStream,
        transformed,
        outputOptions,
        cancellationToken);
}

🌐 HTTP Response Streaming

public async Task<IAsyncResult> GetProductsStreamAsync(int pageNumber = 1, int pageSize = 20)
{
    var products = _context.Products
        .Skip((pageNumber - 1) * pageSize)
        .Take(pageSize)
        .ToAsyncPagedEnumerable();

    return new StreamingResult(async (outputStream, cancellationToken) =>
    {
        var options = JsonSerializerOptions.Default;
        await JsonSerializer.SerializeAsyncPaged(
            outputStream,
            products,
            options,
            cancellationToken);
    });
}

⚙ïļ Performance Benchmarks

The following benchmarks compare System.AsyncPaged.Json serialization/deserialization with standard IAsyncEnumerable<T> approaches:

Serialization Performance (Write to JSON)

Scenario Dataset Custom AsyncPaged Framework IAsyncEnumerable Improvement
Small 100 109 Ξs 121 Ξs +10%
Medium 1,000 1,290 Ξs 1,445 Ξs +11%
Large 10,000 8,732 Ξs 9,975 Ξs +12%

Deserialization Performance (Read from JSON)

Scenario Dataset Custom AsyncPaged Framework IAsyncEnumerable Improvement
Small 100 80 Ξs 122 Ξs +34%
Medium 1,000 460 Ξs 1,174 Ξs +61%
Large 10,000 4,310 Ξs 11,759 Ξs +63%

Memory Allocation

Scenario Dataset Custom AsyncPaged Framework IAsyncEnumerable Reduction
Small 100 24.23 KB 60.80 KB −60%
Medium 1,000 25.45 KB 574.08 KB −96%
Large 10,000 37.70 KB 5,706.89 KB −99%

Memory figures are for deserialization (read from JSON), where the difference is most significant.

Raw BenchmarkDotNet Results

<details> <summary>Click to expand full benchmark results</summary>

Method ItemCount Mean Error StdDev Ratio RatioSD Rank Gen0 Gen1 Gen2 Allocated Alloc Ratio
'Framework IAsyncEnumerable Serialization' 100 121.26 us 0.616 us 0.576 us 1.00 0.01 3 6.8359 0.4883 - 74.01 KB 1.00
'Custom IAsyncPagedEnumerable Serialization' 100 108.53 us 1.581 us 1.479 us 0.90 0.01 2 5.9814 0.6104 - 72.12 KB 0.97
'Framework IAsyncEnumerable Deserialization (HttpContent)' 100 121.96 us 1.218 us 1.080 us 1.01 0.01 3 4.8828 0.2441 - 60.8 KB 0.82
'Custom IAsyncPagedEnumerable Deserialization (HttpContent)' 100 80.07 us 0.405 us 0.379 us 0.66 0.00 1 1.9531 - - 24.23 KB 0.33
'Framework IAsyncEnumerable Serialization' 1000 1,444.90 us 27.481 us 30.545 us 1.00 0.03 4 117.1875 117.1875 117.1875 599.92 KB 1.00
'Custom IAsyncPagedEnumerable Serialization' 1000 1,290.02 us 21.372 us 19.991 us 0.89 0.02 3 117.1875 117.1875 117.1875 616.86 KB 1.03
'Framework IAsyncEnumerable Deserialization (HttpContent)' 1000 1,173.81 us 7.631 us 7.138 us 0.81 0.02 2 45.8984 1.9531 - 574.08 KB 0.96
'Custom IAsyncPagedEnumerable Deserialization (HttpContent)' 1000 460.10 us 2.103 us 1.967 us 0.32 0.01 1 1.9531 - - 25.45 KB 0.04
'Framework IAsyncEnumerable Serialization' 10000 9,975.05 us 55.857 us 52.249 us 1.00 0.01 3 718.7500 640.6250 640.6250 5026.28 KB 1.000
'Custom IAsyncPagedEnumerable Serialization' 10000 8,731.93 us 36.779 us 32.604 us 0.88 0.01 2 601.5625 500.0000 492.1875 5192.73 KB 1.033
'Framework IAsyncEnumerable Deserialization (HttpContent)' 10000 11,759.42 us 59.528 us 55.683 us 1.18 0.01 4 460.9375 23.4375 - 5706.89 KB 1.135
'Custom IAsyncPagedEnumerable Deserialization (HttpContent)' 10000 4,309.54 us 17.159 us 16.051 us 0.43 0.00 1 - - - 37.7 KB 0.007

</details>

Key Findings:

  • ⚡ Serialization: 10–12% faster across all dataset sizes with comparable memory usage
  • ⚡ Deserialization: 34–63% faster, with the advantage growing for larger datasets
  • ðŸ’ū Memory Allocation: Up to 99% reduction in deserialization memory (37.7 KB vs 5,707 KB at 10K items) — zero Gen0/Gen1/Gen2 GC pressure at scale
  • 📈 Scalability: Performance and memory advantages increase with dataset size due to streaming architecture and adaptive flushing

📖 API Reference

ðŸ”Ī Generic Serialization Extensions

// Serialize to Stream with JsonTypeInfo
Task SerializeAsyncPaged<TValue>(
    Stream utf8Json,
    IAsyncPagedEnumerable<TValue> pagedEnumerable,
    JsonTypeInfo<TValue> jsonTypeInfo,
    CancellationToken cancellationToken = default);

// Serialize to Stream with JsonSerializerOptions
Task SerializeAsyncPaged<TValue>(
    Stream utf8Json,
    IAsyncPagedEnumerable<TValue> pagedEnumerable,
    JsonSerializerOptions options,
    CancellationToken cancellationToken = default);

// Serialize to PipeWriter with JsonTypeInfo
Task SerializeAsyncPaged<TValue>(
    PipeWriter utf8Json,
    IAsyncPagedEnumerable<TValue> pagedEnumerable,
    JsonTypeInfo<TValue> jsonTypeInfo,
    CancellationToken cancellationToken = default);

// Serialize to PipeWriter with JsonSerializerOptions
Task SerializeAsyncPaged<TValue>(
    PipeWriter utf8Json,
    IAsyncPagedEnumerable<TValue> pagedEnumerable,
    JsonSerializerOptions options,
    CancellationToken cancellationToken = default);

ðŸ”Ī Non-Generic Serialization Extensions

// Serialize (non-generic) to Stream with JsonTypeInfo
Task SerializeAsyncPaged(
    Stream utf8Json,
    IAsyncPagedEnumerable pagedEnumerable,
    JsonTypeInfo jsonTypeInfo,
    CancellationToken cancellationToken = default);

// Serialize to Stream with JsonSerializerContext
Task SerializeAsyncPaged(
    Stream utf8Json,
    IAsyncPagedEnumerable pagedEnumerable,
    JsonSerializerContext context,
    CancellationToken cancellationToken = default);

// Serialize to Stream with JsonSerializerOptions
Task SerializeAsyncPaged(
    Stream utf8Json,
    IAsyncPagedEnumerable pagedEnumerable,
    JsonSerializerOptions options,
    CancellationToken cancellationToken = default);

// Serialize to PipeWriter with JsonTypeInfo
Task SerializeAsyncPaged(
    PipeWriter utf8Json,
    IAsyncPagedEnumerable pagedEnumerable,
    JsonTypeInfo jsonTypeInfo,
    CancellationToken cancellationToken = default);

// Serialize to PipeWriter with JsonSerializerContext
Task SerializeAsyncPaged(
    PipeWriter utf8Json,
    IAsyncPagedEnumerable pagedEnumerable,
    JsonSerializerContext context,
    CancellationToken cancellationToken = default);

// Serialize to PipeWriter with JsonSerializerOptions
Task SerializeAsyncPaged(
    PipeWriter utf8Json,
    IAsyncPagedEnumerable pagedEnumerable,
    JsonSerializerOptions options,
    CancellationToken cancellationToken = default);

ðŸ”Ī Generic Deserialization Extensions

// Deserialize from Stream with JsonTypeInfo
IAsyncPagedEnumerable<TValue> DeserializeAsyncPagedEnumerable<TValue>(
    Stream utf8Json,
    JsonTypeInfo<TValue> jsonTypeInfo,
    PaginationStrategy strategy = PaginationStrategy.None,
    CancellationToken cancellationToken = default);

// Deserialize from Stream with JsonSerializerOptions (returns nullable)
IAsyncPagedEnumerable<TValue> DeserializeAsyncPagedEnumerable<TValue>(
    Stream utf8Json,
    JsonSerializerOptions options,
    PaginationStrategy strategy = PaginationStrategy.None,
    CancellationToken cancellationToken = default);

// Deserialize from PipeReader with JsonTypeInfo
IAsyncPagedEnumerable<TValue> DeserializeAsyncPagedEnumerable<TValue>(
    PipeReader utf8Json,
    JsonTypeInfo<TValue> jsonTypeInfo,
    PaginationStrategy strategy = PaginationStrategy.None,
    CancellationToken cancellationToken = default);

// Deserialize from PipeReader with JsonSerializerOptions (returns nullable)
IAsyncPagedEnumerable<TValue?> DeserializeAsyncPagedEnumerable<TValue>(
    PipeReader utf8Json,
    JsonSerializerOptions options,
    PaginationStrategy strategy = PaginationStrategy.None,
    CancellationToken cancellationToken = default);

✅ Best Practices

✅ Do

  • Use source-generated JSON — Leverage JsonTypeInfo<T> and JsonSerializerContext for AOT compatibility
  • Apply streaming for large datasets — Don't materialize when serializing/deserializing
  • Handle cancellation — Always pass CancellationToken through the pipeline
  • Use PipeWriter for high-throughput scenarios — Better for server-side streaming
  • Monitor pagination metadata — Use pagination info to handle multi-page results correctly
  • Validate after deserialization — Check items and pagination before processing
  • Specify PaginationStrategy — Use appropriate strategy (None, PerPage, PerItem) for deserialization based on your needs
  • Handle nullable values — When using JsonSerializerOptions overload, items may be nullable

❌ Don't

  • Block on async operations — Never use .Result or .Wait() on serialization tasks
  • Ignore cancellation tokens — Support graceful shutdown and timeouts
  • Materialize entire streams into memory — Process items as they're deserialized
  • Use default JsonSerializerOptions — Provide explicit options for consistent behavior
  • Assume pagination is always present — Handle cases where pagination metadata may be unknown
  • Reuse streams without resetting — Always reset position or create new streams for new operations
  • Forget to check for null — When using options overload, returned items can be null

🔧 Advanced Configuration

Custom Serialization Options

var options = new JsonSerializerOptions
{
    WriteIndented = false,
    PropertyNameCaseInsensitive = true,
    DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
    Converters = { new JsonStringEnumConverter() }
};

await JsonSerializer.SerializeAsyncPaged(stream, products, options);

Source-Generated JSON

[JsonSourceGenerationOptions(
    WriteIndented = false,
    PropertyNameCaseInsensitive = true)]
[JsonSerializable(typeof(Product))]
[JsonSerializable(typeof(Pagination))]
public partial class AppJsonContext : JsonSerializerContext { }

// Use for serialization
var context = new AppJsonContext();
await JsonSerializer.SerializeAsyncPaged(
    stream,
    products,
    context);

With Compression

using var fileStream = new FileStream("products.json.gz", FileMode.Create);
using var gzipStream = new GZipStream(fileStream, CompressionMode.Compress);

var options = JsonSerializerOptions.Default;
await JsonSerializer.SerializeAsyncPaged(
    gzipStream,
    products,
    options);

🔗 Dependency

This library depends on:

  • System.AsyncPaged — Core pagination types and interfaces


📄 License & Contributing

Licensed under the Apache License 2.0. Copyright ÂĐ Kamersoft 2025.

Contributions are welcome! Please visit Xpandables.Net on GitHub to contribute, report issues, or request features.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
10.0.2 188 3/15/2026
10.0.1 158 2/20/2026
10.0.0 163 1/9/2026