#How to parse json into ArrayList of structs

1 messages · Page 1 of 1 (latest)

fair rover
#

Tried this and I don't quite undesrstand the compile error:

fn queryAll(comptime T: type, arena: std.mem.Allocator) !std.ArrayList(T) {
    const args = switch (T) {
        Window => &[_][]const u8{ "query", "--windows" },
        Display => &[_][]const u8{ "query", "--displays" },
        Space => &[_][]const u8{ "query", "--spaces" },
        else => @compileError("Invalid query type"),
    };
    const stream = try submitAndGetStream(args);
    defer stream.close();

    var reader = std.json.reader(arena, stream.reader());
    return try std.json.parseFromTokenSourceLeaky(std.ArrayList(T), arena, &reader, .{ .ignore_unknown_fields = true });
}
Build Summary: 0/3 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
└─ install y3-zig transitive failure
   └─ zig build-exe y3-zig Debug native 8 errors
/Users/xxx/zig/lib/std/json/static.zig:211:33: error: error union with payload of opaque type 'anyopaque' not allowed
) ParseError(@TypeOf(source.*))!T {
                                ^
sand geode
#

arraylist.allocator.ptr is a *anyopaque, std.json cannot parse a *anyopaque

normal hinge
#

you can do []T then turn that into an arraylist later if its needed

fair rover
#

oh lol

#

so you can just use []T like that?

normal hinge
#

as the first argument for the json parsing yeah

fair rover
#

amazing