Zig JSON parse errors with line and column
Zig
const std = @import("std");
const User = struct { id: u32, name: []const u8 };
fn parseUser(a: std.mem.Allocator, text: []const u8) !User {
var diag: std.json.Diagnostics = .{};
const user = std.json.parseFromSliceLeaky(User, a, text, .{
.allocate = .alloc_always,
.diagnostics = &diag,
.ignore_unknown_fields = true,
}) catch |err| {
std.log.err("invalid JSON at {d}:{d}: {s}", .{ diag.getLine(), diag.getColumn(), @errorName(err) });
return err;
};
if (user.id == 0) return error.InvalidId;
if (user.name.len == 0) return error.MissingName;
return user;
}