Metadata-Version: 2.4
Name: pjson-rs
Version: 0.1.0
Classifier: Programming Language :: Rust
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
License-File: LICENSE
Summary: SIMD-accelerated streaming partial JSON parser for Python — drop-in partial-json-parser replacement + O(N) StreamParser for vLLM, SGLang, and LLM agents
License: MIT
Requires-Python: >=3.9
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Homepage, https://github.com/gatewaytofredom/pjson-rs
Project-URL: Issues, https://github.com/gatewaytofredom/pjson-rs/issues
Project-URL: Repository, https://github.com/gatewaytofredom/pjson-rs

# pjson-rs

SIMD-accelerated (AVX2 / SSE2 / ARM64 NEON) streaming partial JSON parser for Python — a zero-dependency Rust/PyO3 drop-in replacement for `partial-json-parser` plus a stateful $O(N)$ `StreamParser`, built for LLM tool-call streaming (`vLLM`, `SGLang`, `LangChain`, and AI agents).

- **14×–16× faster** single-shot `loads` / `raw_decode` vs `partial-json-parser`
- **100×–330× faster** stateful $O(N)$ streaming (`StreamParser.complete()`) on 1 KB–8 KB chunked streams
- **Drop-in `Allow` bitflags** (1:1 with `partial_json_parser.core.options.Allow`)
- **Single-pass `raw_decode(s, allow) -> (obj, end_char_idx)`** — handles trailing data & Unicode char offsets without catching `JSONDecodeError`
- **Pythonic `cp39-abi3` wheel** (~270 KB, works on Python 3.9–3.14+, free-threaded ready with `gil_used = false`)
- **Accepts `str | bytes | bytearray`** (including multi-byte UTF-8 split across `bytes` chunks in `StreamParser.feed`)
- **Uniform `json.JSONDecodeError`** — never leaks `AssertionError`, `IndexError`, or lone UTF-16 surrogates

## Install

```bash
pip install pjson-rs
```

## Quick Start

### Drop-in `partial-json-parser` & vLLM/SGLang `raw_decode`

```python
import pjson_rs
from pjson_rs import Allow

# Drop-in replacement for partial_json_parser.loads
obj = pjson_rs.loads('{"tool": "write_file", "path": "src/main.rs", "content": "fn main', Allow.ALL)
# {'tool': 'write_file', 'path': 'src/main.rs', 'content': 'fn main'}

# Single-pass raw_decode with trailing text & exact Python char offset
text = '{"emoji": "🚀", "n": 42}  trailing text'
obj, end_idx = pjson_rs.raw_decode(text, Allow.ALL)
assert text[end_idx:] == "  trailing text"
```

### Stateful $O(N)$ `StreamParser`

```python
import pjson_rs

sp = pjson_rs.StreamParser()
for chunk in [b'{"cmd": "echo ', b'\xf0\x9f', b'\x9a\x80", "ok": tr', b'ue}']:
    sp.feed(chunk)
    healed_json = sp.complete()   # O(1) close, always valid UTF-8 JSON

assert sp.done()
assert sp.loads() == {"cmd": "echo 🚀", "ok": True}
sp.reset()  # reuse internal buffers across tool calls
```

## Benchmarks

8-byte chunk streaming and 75%-truncated single-shot loads (`benchmarks/bench.py`):

| Workload | `partial-json-parser` | `pjson_rs` (Intel i5-12400 AVX2) | `pjson_rs` (Apple M4 NEON) |
|---|---:|---:|---:|
| Single-shot `1 KB` | `30.0 µs` (x86) / `26.5 µs` (M4) | **`2.1 µs`** (**14.4×**) | **`1.7 µs`** (**15.3×**) |
| Single-shot `8 KB` | `200.8 µs` (x86) / `129.9 µs` (M4) | **`14.5 µs`** (**13.9×**) | **`10.9 µs`** (**11.9×**) |
| Single-shot `32 KB` | `817.2 µs` (x86) / `605.1 µs` (M4) | **`49.5 µs`** (**16.5×**) | **`43.3 µs`** (**14.0×**) |
| Streaming `1 KB` (139 × 8B) | `3.56 ms` (x86) / `2.16 ms` (M4) | `0.25 ms` (`raw_decode`, **14.2×**) · **`0.02 ms`** (`StreamParser`, **162×**) | `0.18 ms` (**12.0×**) · **`0.02 ms`** (**108×**) |
| Streaming `8 KB` (1,041 × 8B) | `130.5 ms` (x86) / `91.8 ms` (M4) | `9.46 ms` (`raw_decode`, **13.8×**) · **`0.39 ms`** (`StreamParser`, **333×**) | `8.25 ms` (**11.1×**) · **`0.31 ms`** (**298×**) |

## ABI & Verification

- `cp39-abi3` Limited API verified with `abi3audit --strict`
- PEP 561 typed (`py.typed` + `__init__.pyi`) verified with `mypy --strict` and `stubtest`
- Rust SIMD alignment tests (`cargo test --lib`) across AVX2, SSE2, and ARM64 NEON
- Differential property-based testing (`hypothesis`), split-UTF-8, 8-thread concurrency, and `tracemalloc` leak tests

## License

MIT

