joechung2008/json-zig
No description provided.
A JSON parser implementation in Zig 0.15.1.
MIT
# Clone the repository
git clone https://github.com/joechung2008/json-zig/json-zig.git
cd json-zig
# Build the project
zig build
# The CLI executable will be available at: ./zig-out/bin/cli
# The library will be available at: ./zig-out/lib/libshared.a or ./zig-out/lib/shared.lib
# Run all tests (silent on success)
zig build test
# Run tests with detailed summary
zig build test --summary all
# Run with verbose build output
zig build test --verbose
# Run specific test by name
zig test lib/src/main.zig --test-filter "parse number"
Example output:
$ zig build test
# No output = all tests passed
$ zig build test --summary all
Build Summary: 3/3 steps succeeded; 68/68 tests passed
test success
└─ run test 68 passed 1ms MaxRSS:1M
└─ compile test Debug native success 431ms MaxRSS:128M
$ zig test lib/src/main.zig
All 68 tests passed.
The CLI tool reads JSON from standard input and displays the parsed structure:
# Parse a simple number
echo '42' | ./zig-out/bin/cli
# Parse a string
echo '"hello world"' | ./zig-out/bin/cli
# Parse a complex object
echo '{"name": "Alice", "age": 30, "hobbies": ["reading", "coding"]}' | ./zig-out/bin/cli
# Parse from a file
cat data.json | ./zig-out/bin/cli
# Test error handling
echo '{"invalid": json}' | ./zig-out/bin/cli
$ echo '{"name": "Alice", "age": 30, "hobbies": ["reading", "coding"]}' | ./zig-out/bin/cli
Successfully parsed JSON:
Object (3 members):
"name":
String: "Alice"
"age":
Number: 30
"hobbies":
Array (2 elements):
[0]:
String: "reading"
[1]:
String: "coding"
const std = @import("std");
const json = @import("json-zig");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Parse JSON string
var result = try json.parse(allocator, "{\"hello\": \"world\"}");
defer result.deinit(allocator);
// Access parsed data
if (result.token) |token| {
switch (token) {
.object => |obj| {
std.debug.print("Parsed object with {} members\n", .{obj.members.items.len});
},
else => {},
}
}
}
If you use the Zig Language Server (zls) for editor integration, be aware:
This does not affect building or running the project itself, only editor diagnostics and completions.