| Crates.io | pjson-rs |
| lib.rs | pjson-rs |
| version | 0.4.7 |
| created_at | 2025-08-11 14:25:38.601189+00 |
| updated_at | 2026-01-25 00:21:51.756966+00 |
| description | Priority JSON Streaming Protocol - high-performance priority-based JSON streaming (requires nightly Rust) |
| homepage | |
| repository | https://github.com/bug-ops/pjs |
| max_upload_size | |
| id | 1790240 |
| size | 1,232,071 |
Priority-based streaming | Progressive loading | Zero-copy operations | WebAssembly ready
High-performance Rust library for priority-based JSON streaming with SIMD acceleration. Stream large JSON responses progressively, delivering critical data first while background data loads asynchronously.
[!IMPORTANT] v0.4.7: GAT migration (1.82x faster), HTTP adapter with CQRS, decompression algorithms with security hardening. 2,158 tests passing. Requires nightly Rust for zero-cost async abstractions.
| Benchmark | Performance Gain | Notes |
|---|---|---|
| GAT Async | 1.82x faster | Static dispatch eliminates virtual calls |
| Small JSON | Competitive | Comparable to industry standards |
| Medium JSON | ~3x faster | vs traditional parsers |
| Large JSON | ~6x faster | vs traditional parsers |
| Progressive Loading | ~5x faster | vs batch processing |
cargo add pjson-rs
Or add to Cargo.toml:
[dependencies]
pjson-rs = "0.4"
[!NOTE] Requires Rust 1.89+ (nightly). See MSRV policy for details.
use pjson_rs::infrastructure::http::axum_adapter::create_pjs_router;
#[tokio::main]
async fn main() {
let app = create_pjs_router().with_state(app_state);
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await?;
axum::serve(listener, app).await?;
}
Start server and test:
# Run example server
cargo run --example axum_server
# Create session
curl -X POST http://localhost:3000/pjs/sessions \
-H "Content-Type: application/json" \
-d '{"max_concurrent_streams": 5}'
# Stream data with priority
curl http://localhost:3000/pjs/stream/SESSION_ID/sse
npm install pjs-wasm
<script type="module">
import init, { PriorityStream, PriorityConstants } from './pkg/pjs_wasm.js';
async function main() {
await init();
const stream = new PriorityStream();
stream.setMinPriority(PriorityConstants.MEDIUM());
// Register callbacks
stream.onFrame((frame) => {
console.log(`${frame.type} [${frame.priority}]: ${frame.payload}`);
if (frame.priority >= 80) {
updateUI(JSON.parse(frame.payload)); // High priority first
}
});
stream.onComplete((stats) => {
console.log(`Completed: ${stats.totalFrames} frames in ${stats.durationMs}ms`);
});
// Start streaming
stream.start(JSON.stringify({ id: 123, name: "Alice", bio: "..." }));
}
main();
</script>
[!TIP] Use the PriorityStream API for automatic frame handling and built-in security limits. Ideal for real-time dashboards and progressive loading.
<script type="module">
import init, { PjsParser, PriorityConstants } from './pkg/pjs_wasm.js';
async function main() {
await init();
const parser = new PjsParser();
const frames = parser.generateFrames(
JSON.stringify({ user_id: 123, name: "Alice" }),
PriorityConstants.MEDIUM()
);
frames.forEach(frame => {
if (frame.priority >= 90) {
updateUI(frame.data); // Critical data first
}
});
}
main();
</script>
Try the Browser Demo with transport switching, performance benchmarks, and real-time metrics.
import init, { PjsParser } from 'pjs-wasm';
import { readFile } from 'fs/promises';
const wasmBuffer = await readFile('./node_modules/pjs-wasm/pkg/pjs_wasm_bg.wasm');
await init(wasmBuffer);
const parser = new PjsParser();
const frames = parser.generateFrames(JSON.stringify(data), 50);
frames.forEach(frame => {
console.log(`Priority ${frame.priority}: ${frame.frame_type}`);
});
# Install wasm-pack
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
# Build for different targets
wasm-pack build crates/pjs-wasm --target web --release # Browsers
wasm-pack build crates/pjs-wasm --target nodejs --release # Node.js
wasm-pack build crates/pjs-wasm --target bundler --release # Webpack/Rollup
[!WARNING] This project requires nightly Rust for Generic Associated Types (GAT) features. Stable Rust is not supported.
rustup install nightly
rustup override set nightly
# Standard build
cargo build --release
# Run tests with nextest
cargo nextest run --workspace
# Run benchmarks
cargo bench -p pjs-bench
# Run demo servers
cargo run --bin websocket_streaming --manifest-path crates/pjs-demo/Cargo.toml
cargo run --bin interactive_demo --manifest-path crates/pjs-demo/Cargo.toml
[!TIP] Start with default features. Add extras only when needed to keep compile times fast.
| Feature | Description | Default |
|---|---|---|
simd-auto |
Auto-detect SIMD support | ✅ Yes |
simd-avx2 |
Force AVX2 SIMD | No |
simd-neon |
Force ARM NEON | No |
schema-validation |
Schema validation engine | ✅ Yes |
compression |
Schema-based compression | No |
http-server |
Axum HTTP server | No |
websocket-server |
WebSocket streaming | No |
prometheus-metrics |
Prometheus integration | No |
jemalloc |
Use jemalloc allocator | No |
mimalloc |
Use mimalloc allocator | No |
PJS includes built-in security features to prevent DoS attacks:
import { PriorityStream, SecurityConfig } from 'pjs-wasm';
const security = new SecurityConfig()
.setMaxJsonSize(5 * 1024 * 1024) // 5 MB limit
.setMaxDepth(32); // 32 levels max
const stream = PriorityStream.withSecurityConfig(security);
Default Limits:
[!IMPORTANT] v0.4.7 Security: Delta and RLE decompression now include 4-layer defense-in-depth protection against decompression bombs (CVSS 7.5 vulnerabilities fixed).
Decompression Security:
PJS follows Clean Architecture with Domain-Driven Design:
v0.4.7 Improvements:
Contributions welcome! Please ensure:
rustup override set nightly
cargo clippy --workspace -- -D warnings
cargo nextest run --workspace --all-features
cargo +nightly fmt --check
See CONTRIBUTING.md for guidelines.
Licensed under either of:
at your option.
PJS: Priority-based JSON streaming for instant user experiences.