Parsing ZON with optional fields

Hello, i would like to write zon in such a way that, i could omit struct fields and their value would default to whatever code i write for them

example

// when i parse this, i check if the optional bool is present in the zon.
// if not, default it to "false" 
const Foo = struct {
  required_int: u32 = 0,
  optional_bool: bool = false,
};

is it possible to do this with ZON? i have tried it briefly with std.zon.parse.fromSlice() but it fails with error.WrongType error, and the options parameter does not specify the ability to notify the parser that missing fields is OK and it should just report them instead of erroring out.

i have a ton of ZON to write where many fields are unnecessary to write and optionality would perfect for that.

Hmmm. I think at present, the best way forward is unfortunately a bit more complex: you need something like

// has all required fields and no optional ones
const MinFoo = struct {
    required_int: u32,
};

// one struct with matching name per field,
// although surely if you're willing to go lower level with the parser
// this is overkill
const FooOptionals: []const type = &.{ struct { optional_bool: bool = false } };

// to be honest, the above could be comptime-generated from Foo
// based strictly on whether there is or is not a default value provided
pub const Foo = struct {
    required_int: u32,
    optional_bool: bool = false,

    pub fn fromZonSlice(
        allocator: std.mem.Allocator, 
        source: [:0]const u8, 
        diag: ?*Diagnostics, 
        options: Options,
    ) error{ OutOfMemory, ParseZon }!Foo {
        const min = try std.zon.parse.fromSlice(MinFoo, allocator, source, diag, options);
        var ret: Foo = undefined;
        inline for (fields) |name| { // pseudocode, forgive me, easy to fill in tho
            @field(ret, name) = @field(min, name);
        }
        inline for (FooOptionals) |T| {
            const optional: T = std.zon.parse.fromSlice(T, allocator, source, diag, .{
                .ignore_unknown_fields = true,
            }) catch .{};
            inline for (fields) |name| { // pseudocode again
                @field(ret, name) = @field(optional, name);
            }
        }
        return ret;
    }
};

Ah, so there is no builtin solution in the std for my specific use case?
That’s fine i suppose, i will write my own ZON field parser. thank you.