Skip to main content

Module ndjson

Module ndjson 

Source
Expand description

Newline-Delimited JSON (NDJSON) streaming support.

This module provides types for streaming large datasets as NDJSON, where each JSON object is on its own line. This format is ideal for:

  • Streaming database query results
  • Real-time log output
  • Incremental data export
  • Large dataset downloads

§Overview

NDJSON (also known as JSON Lines) is a convenient format for streaming JSON data. Each line is a valid JSON value, typically an object, followed by a newline character.

§Example

use fastapi_core::ndjson::{NdjsonResponse, ndjson_response};
use fastapi_core::Response;
use asupersync::stream;
use serde::Serialize;

#[derive(Serialize)]
struct LogEntry {
    timestamp: u64,
    level: String,
    message: String,
}

async fn stream_logs() -> Response {
    let logs = stream::iter(vec![
        LogEntry { timestamp: 1, level: "INFO".into(), message: "Started".into() },
        LogEntry { timestamp: 2, level: "DEBUG".into(), message: "Processing".into() },
    ]);

    NdjsonResponse::new(logs).into_response()
}

§Wire Format

{"id":1,"name":"Alice"}
{"id":2,"name":"Bob"}
{"id":3,"name":"Charlie"}

Each line is a complete, valid JSON value followed by \n.

§Content Type

The standard content type for NDJSON is application/x-ndjson, though application/jsonlines and application/json-lines are also recognized.

§Memory Efficiency

NDJSON streaming does not buffer the entire response. Each item is serialized and sent immediately, making it suitable for datasets of any size.

§Error Handling

If serialization fails for an item, the stream will include an error object on that line and continue with subsequent items. Clients should be prepared to handle {"error": "..."} entries in the stream.

Structs§

NdjsonConfig
Configuration for NDJSON responses.
NdjsonResponse
Builder for creating NDJSON streaming responses.
NdjsonStream
A wrapper that converts an async stream of serializable items into NDJSON format.

Constants§

NDJSON_CONTENT_TYPE
The standard NDJSON content type.
NDJSON_CONTENT_TYPE_ALT
Alternative content types that are sometimes used for NDJSON.

Functions§

ndjson_iter
Create an NDJSON response from an iterator.
ndjson_response
Convenience function to create an NDJSON response from a stream.