deserialiser

This commit is contained in:
Moritz Gmeiner 2025-03-30 15:38:38 +02:00
commit f454526c5a
3 changed files with 827 additions and 3 deletions

View file

@ -1,21 +1,67 @@
const std = @import("std");
const Raw = union(enum) {
pub const deserialise = @import("deserialise.zig");
pub const Raw = union(enum) {
string: []u8,
binary: []u8,
pub fn deinit(self: Raw, alloc: std.mem.Allocator) void {
switch (self) {
.string => |s| alloc.free(s),
.binary => |b| alloc.free(b),
}
}
};
const Object = union(enum) {
pub const MapEntry = struct {
key: Object,
value: Object,
pub fn deinit(self: MapEntry, alloc: std.mem.Allocator) void {
self.key.deinit(alloc);
self.value.deinit(alloc);
}
};
pub const Object = union(enum) {
nil,
bool: bool,
integer: i64,
float: f64,
raw: Raw,
array: []Object,
map: []struct { key: Object, value: Object },
map: []MapEntry,
extension: struct { type: u8, bytes: []u8 },
pub fn deinit(self: Object, alloc: std.mem.Allocator) void {
switch (self) {
.raw => |raw| raw.deinit(alloc),
.array => |array| {
for (array) |x| {
x.deinit(alloc);
}
alloc.free(array);
},
.map => |map| {
for (map) |elem| {
elem.key.deinit(alloc);
elem.value.deinit(alloc);
}
alloc.free(map);
},
.extension => |ext| alloc.free(ext.bytes),
else => {},
}
}
};
test {
std.testing.refAllDecls(@This());
}
test {
const o: Object = .nil;