Skip to main content

A small, resumable JSON decoder that catches complete JSON values from a stream.

Project description

jsonweir

Incremental JSON parsing for code that receives JSON in chunks.

jsonweir consumes text incrementally and emits parse events as soon as they are available. It is useful for streaming model responses, network payloads, or other producers where you want to react before the full JSON document has been received.

What it does

  • Parses JSON from one or more string chunks.
  • Emits structural events for objects and arrays.
  • Emits scalar events for numbers, booleans, and null.
  • Emits string values incrementally with StringDelta events.
  • Tracks a path for every event using object keys and array indexes.
  • Reports parse failures as JsonParseError events instead of raising for normal parse errors.

Installation

pip install jsonweir

Basic usage

Import the parser from the package root. Import events from jsonweir.events.

from jsonweir import IncrementalJsonParser
from jsonweir.events import EndString, JsonParseError, StringDelta

parser = IncrementalJsonParser()

chunks = [
    '{"message": "hel',
    'lo", "count": 2}',
]

for chunk in chunks:
    for event in parser.feed(chunk):
        if isinstance(event, StringDelta):
            print("string chunk:", event.path, event.text)
        elif isinstance(event, EndString):
            print("string value:", event.path, event.value)
        elif isinstance(event, JsonParseError):
            raise ValueError(event.message)

for event in parser.finish():
    if isinstance(event, JsonParseError):
        raise ValueError(event.message)

Events

Every event has a path except JsonParseError. Paths are tuples containing object keys and array indexes.

For this JSON:

{"items": [{"name": "book"}]}

the string "book" is reported at:

("items", 0, "name")

The event classes are:

  • StartObject
  • EndObject
  • StartArray
  • EndArray
  • Key
  • StartString
  • StringDelta
  • EndString
  • Scalar
  • JsonParseError

String streaming

Strings are split into StartString, zero or more StringDelta events, and EndString. Deltas are emitted at chunk boundaries and before escape sequences are decoded.

from jsonweir import IncrementalJsonParser
from jsonweir.events import StringDelta

parser = IncrementalJsonParser()

events = []
events.extend(parser.feed('["hel'))
events.extend(parser.feed('lo"]'))
events.extend(parser.finish())

assert StringDelta(path=(0,), text="hel") in events
assert StringDelta(path=(0,), text="lo") in events

Object keys are reported with Key events. They do not emit string delta events.

Errors

Invalid input produces JsonParseError(message, fatal=True) events. After a fatal error, callers should stop feeding input and discard the parser instance.

from jsonweir import IncrementalJsonParser
from jsonweir.events import JsonParseError

parser = IncrementalJsonParser()
events = list(parser.feed("[1,]")) + list(parser.finish())

errors = [event for event in events if isinstance(event, JsonParseError)]
assert errors

Development

Run the package tests:

uv sync
uv run pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonweir-0.1.0.tar.gz (30.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jsonweir-0.1.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file jsonweir-0.1.0.tar.gz.

File metadata

  • Download URL: jsonweir-0.1.0.tar.gz
  • Upload date:
  • Size: 30.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for jsonweir-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0823551700b06bf4b1605d52d3514e6581c6c5f72d0295ce8b47eb024e6420b2
MD5 d1675b64f9038d99b836e15b94fabde9
BLAKE2b-256 0e59140db46bc2f05dbc9fd09169b1aab52ffd9b9a8e24144bf295096c0e3d93

See more details on using hashes here.

Provenance

The following attestation bundles were made for jsonweir-0.1.0.tar.gz:

Publisher: publish.yml on nueruyu/jsonweir

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file jsonweir-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: jsonweir-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for jsonweir-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1b2615c89f34dd9878a23b826128746498536bdff05e389fb1056d3f8159ea5a
MD5 6a4e2c93a3d89c8d391af7e58a3071a3
BLAKE2b-256 60836043193396a9fc6dc9e6cf01cd5d85a329631c9b27969b75e112060022e2

See more details on using hashes here.

Provenance

The following attestation bundles were made for jsonweir-0.1.0-py3-none-any.whl:

Publisher: publish.yml on nueruyu/jsonweir

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page