DoxigAlpha

jsonParse

Function parameters

Parameters

#
source:anytype

A thin wrapper around `std.StringArrayHashMapUnmanaged` that implements

Functions

#
ArrayHashMap
A thin wrapper around `std.StringArrayHashMapUnmanaged` that implements

Source

Implementation

#
pub fn jsonParse(allocator: Allocator, source: anytype, options: ParseOptions) !@This() {
    var map: std.StringArrayHashMapUnmanaged(T) = .empty;
    errdefer map.deinit(allocator);

    if (.object_begin != try source.next()) return error.UnexpectedToken;
    while (true) {
        const token = try source.nextAlloc(allocator, options.allocate.?);
        switch (token) {
            inline .string, .allocated_string => |k| {
                const gop = try map.getOrPut(allocator, k);
                if (gop.found_existing) {
                    switch (options.duplicate_field_behavior) {
                        .use_first => {
                            // Parse and ignore the redundant value.
                            // We don't want to skip the value, because we want type checking.
                            _ = try innerParse(T, allocator, source, options);
                            continue;
                        },
                        .@"error" => return error.DuplicateField,
                        .use_last => {},
                    }
                }
                gop.value_ptr.* = try innerParse(T, allocator, source, options);
            },
            .object_end => break,
            else => unreachable,
        }
    }
    return .{ .map = map };
}