JSON Text Sequences (RFC 7464)

1.0.0 · active · verified Thu Apr 16

jsonseq is a Python library that provides support for encoding and decoding JSON text sequences as defined by RFC 7464. This format is designed for streaming multiple JSON objects, each prefixed by an ASCII Record Separator (0x1E) and terminated by a Line Feed (0x0A). It's particularly useful for handling large or indefinite streams of JSON data incrementally. The current version is 1.0.0, with releases historically following an as-needed cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to encode a list of Python dictionaries into a JSON text sequence using `JSONSeqEncoder` and then decode it back into Python objects using `JSONSeqDecoder`. It uses `io.BytesIO` to simulate file or network streams.

import io
from jsonseq.encode import JSONSeqEncoder
from jsonseq.decode import JSONSeqDecoder

# --- Encoding ---
data_to_encode = [
    {'id': 1, 'name': 'Alice'},
    {'id': 2, 'name': 'Bob', 'details': {'age': 30}},
    {'id': 3, 'name': 'Charlie'}
]

# Encode to a BytesIO stream
output_buffer = io.BytesIO()
encoder = JSONSeqEncoder(output_buffer)
for obj in data_to_encode:
    encoder.encode(obj)

encoded_bytes = output_buffer.getvalue()
print("Encoded JSONSeq:")
print(encoded_bytes.decode('utf-8'))

# --- Decoding ---
# Simulate reading from the stream
input_buffer = io.BytesIO(encoded_bytes)
decoder = JSONSeqDecoder(input_buffer)

decoded_objects = []
for obj in decoder:
    decoded_objects.append(obj)

print("\nDecoded objects:")
for obj in decoded_objects:
    print(obj)

view raw JSON →