Sorry in advance for the lack of snippets, im writing this from my phone on the bus.
I am trying to parse a JSON object with two fields: a number (which is an enum) and another JSON object. The issue is that the inner object’s fields are dependent on the number, with almost no overlapping fields. My initial guess was to use a tagged union on the number, but json.parseFromSlice returns error.UnexpectedToken.
The repo is gitlab.com/g1rly-c0d3r/obs-ws.git if it will be helpful. (apologies if the visibility is private, I will update once i get home)
a custom jsonParse function is a way to do it I guess?
const Foo = struct { a: u32 };
const Bar = struct { b: []const u8 };
const Payload = union(enum) {
foo: Foo,
bar: Bar,
pub fn jsonParse(allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) !Payload {
if (try source.next() != .object_begin) return error.UnexpectedToken;
_ = try source.next();
const num = switch (try source.next()) {
.number => |s| try std.fmt.parseInt(u8, s, 10),
else => return error.UnexpectedToken,
};
_ = try source.next();
const result: Payload = switch (num) {
0 => .{ .foo = try std.json.innerParse(Foo, allocator, source, options) },
1 => .{ .bar = try std.json.innerParse(Bar, allocator, source, options) },
else => return error.UnexpectedToken,
};
if (try source.next() != .object_end) return error.UnexpectedToken;
return result;
}
};