Add struct field tag support for JSON customization
Summary
Support struct field tags/attributes for customizing JSON field names and behavior, similar to Go's struct tags.
Background
Go uses struct tags like `json:"fieldName,omitempty,string"` to customize JSON behavior per-field.
In Zig, we can use doc comments or a convention-based approach since Zig doesn't have runtime tags.
Proposed Approaches
Option 1: Comptime Field Configuration
const Person = struct {
first_name: []const u8,
age: ?u32,
_private: u32, // Fields starting with _ are skipped
pub const json_config = .{
.first_name = .{ .name = "firstName" },
.age = .{ .omitempty = true },
};
};
Option 2: Wrapper Types
const Person = struct {
first_name: json.Field([]const u8, .{ .name = "firstName" }),
age: json.Optional(u32, .{ .omitempty = true }),
secret: json.Skip([]const u8),
};
Features Required
-
Custom field names (
name) -
Omit empty/zero values (
omitempty,omitzero) -
Skip field entirely (
-) -
Stringify numbers as strings (
string) -
Inline/flatten nested structs (
inline) -
Format specifiers (
format:base64,format:unix) -
Case sensitivity options (
case:ignore,case:strict)
Reference
Go struct tags: json:"name,omitempty,string,inline,format:base64"
Priority
High - Essential for API compatibility