I'm learning Zig and I'm trying to write a generic function that automatically serializes any struct into JSON. In languages like Rust or Go, there are libraries that can iterate over the fields of a struct at compile time. I was hoping to do something similar in Zig using @typeInfo.
Here is a simplified example of what I'm trying to do:
const std = @import("std");
const Example = struct {
a: i32,
b: []const u8,
c: bool,
};
pub fn toJson(comptime T: type, value: T) []const u8 {
// iterate over T's fields here
// return a JSON string like {"a":1,"b":"hello","c":true}
}
I tried inspecting @typeInfo(T) and matching on TypeInfo.Struct to get the fields array, but I can't figure out how to loop over it in a way that works at compile time. If I write a normal for loop inside the function, Zig complains that it cannot evaluate it at compile time. If I try to use inline for, I get "expected comptime expression" errors. I've also looked at examples in the standard library but they use hard-coded field names.
What I've tried:
- Using
@typeInfo(T).Struct.fieldsinside aninline forloop. The compiler errors with "indexis not comptime-known". - Attempting to build a
[]const u8by concatenating the field names and values, but I run into lifetime and allocation issues since I can't allocate memory at comptime. - Searching the docs and examples for "reflection" or "struct fields" but most examples are about enums.
Is there a supported way in Zig (0.11 or 0.12) to iterate over the fields of a struct at compile time so I can generate code based on them? Or is this not possible and I need to manually write serialization functions for each struct?
Any pointers or examples would be greatly appreciated!