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
StringDeltaevents. - Tracks a path for every event using object keys and array indexes.
- Reports parse failures as
JsonParseErrorevents 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:
StartObjectEndObjectStartArrayEndArrayKeyStartStringStringDeltaEndStringScalarJsonParseError
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0823551700b06bf4b1605d52d3514e6581c6c5f72d0295ce8b47eb024e6420b2
|
|
| MD5 |
d1675b64f9038d99b836e15b94fabde9
|
|
| BLAKE2b-256 |
0e59140db46bc2f05dbc9fd09169b1aab52ffd9b9a8e24144bf295096c0e3d93
|
Provenance
The following attestation bundles were made for jsonweir-0.1.0.tar.gz:
Publisher:
publish.yml on nueruyu/jsonweir
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jsonweir-0.1.0.tar.gz -
Subject digest:
0823551700b06bf4b1605d52d3514e6581c6c5f72d0295ce8b47eb024e6420b2 - Sigstore transparency entry: 2075929124
- Sigstore integration time:
-
Permalink:
nueruyu/jsonweir@0b1c3025eddeabf9a7a41fbb8ed59834f39fa645 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/nueruyu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@0b1c3025eddeabf9a7a41fbb8ed59834f39fa645 -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1b2615c89f34dd9878a23b826128746498536bdff05e389fb1056d3f8159ea5a
|
|
| MD5 |
6a4e2c93a3d89c8d391af7e58a3071a3
|
|
| BLAKE2b-256 |
60836043193396a9fc6dc9e6cf01cd5d85a329631c9b27969b75e112060022e2
|
Provenance
The following attestation bundles were made for jsonweir-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on nueruyu/jsonweir
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jsonweir-0.1.0-py3-none-any.whl -
Subject digest:
1b2615c89f34dd9878a23b826128746498536bdff05e389fb1056d3f8159ea5a - Sigstore transparency entry: 2075929280
- Sigstore integration time:
-
Permalink:
nueruyu/jsonweir@0b1c3025eddeabf9a7a41fbb8ed59834f39fa645 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/nueruyu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@0b1c3025eddeabf9a7a41fbb8ed59834f39fa645 -
Trigger Event:
push
-
Statement type: