Hello Zig community!
I’ve created jsmn_zig - a Zig port of the minimal JSMN JSON parser with significant improvements:
Key Features:
- Hybrid memory: stack for small JSON, heap for large (auto-switch)
- Compact tokens: 4 bytes vs 20+ in standard tokens (80% memory savings)
- Zero-copy streaming with proper state management
- SIMD-ready optimizations for x86/ARM
- Comptime-configurable for embedded use
Perfect for:
- Embedded systems where every byte matters
- High-performance servers needing low-latency parsing
- Network protocol implementations
- Configuration file parsing
Quick example:
const Parser = jsmn.Jsmn(.{.compact_tokens = true});
var tokens: [32]Parser.Token = undefined;
const count = try Parser.parseTokens(&tokens, &.{}, "{\"test\": 42}");
The library maintains JSMN’s philosophy of simplicity while leveraging Zig’s comptime power and memory safety.
GitHub: GitHub - Ferki-git-creator/jsmn_zig: Realization jsmn on a pure Zig
I’d love feedback on the API design and any performance optimization ideas!
2 Likes
The general philosphy of Zig is to let the user decide how they want to manage the memory by passing in an allocator. And the standard library has an allocator exactly for this purpose:
std.heap.stackFallback(comptime size: usize, fallback_allocator: Allocator)
So there is no need to offer a special hybrid mode, just take the allocator interface and let the user decide if they want to use stack memory and how much of it.
3 Likes
Fair point about allocator philosophy! You’re right that Zig prefers explicit allocator control.
But parseHybrid is just a convenience layer - the core API follows proper Zig patterns:
// The direct way - no allocators, just buffers
var tokens: [512]Token = undefined;
var parents: [512]IndexT = undefined;
const count = try Parser.parseTokens(&tokens, &parents, json);
The hybrid mode is sugar for when you don’t know JSON size upfront. But the real meat is elsewhere:
- 4-byte tokens vs 20+ in standard parsers (that’s 80% memory savings)
- Zero-copy streaming with proper state management
- SIMD optimizations that actually work
- Comptime config for embedded use
The hybrid thing is one small feature - the main value is making JSMN actually usable in Zig with proper memory safety and performance.
1 Like