Compare commits

...

2 Commits

Author SHA1 Message Date
259c22999c
example: output JSON
this works a lot better than the very bad dump function I had before.
The output isn't pretty printed but can easily be made so by piping it
to jq, for example.
2024-03-08 10:50:21 -08:00
ab0202a5f7
yaml.Value: add basic JSON emitting
This is quite useful for checking yaml document structure.
2024-03-08 10:49:29 -08:00
2 changed files with 17 additions and 23 deletions

View File

@ -27,27 +27,5 @@ pub fn main() !void {
};
defer doc.deinit();
std.debug.print("\n-----\n\n", .{});
dump(doc.root);
}
fn dump(val: yaml.Value) void {
switch (val) {
.scalar => |str| std.debug.print("scalar: {s}\n", .{str}),
.list => |list| {
std.debug.print("list: \n", .{});
for (list) |item| dump(item);
std.debug.print("end list\n", .{});
},
.map => |map| {
std.debug.print("map: \n", .{});
var iter = map.iterator();
while (iter.next()) |entry| {
std.debug.print("key: {s}\n", .{entry.key_ptr.*});
dump(entry.value_ptr.*);
}
std.debug.print("end map\n", .{});
},
}
try std.json.stringify(doc.root, .{}, std.io.getStdOut().writer());
}

View File

@ -297,4 +297,20 @@ pub const Value = union(enum) {
}
}
};
pub fn jsonStringify(value: @This(), jws: anytype) !void {
switch (value) {
.scalar => |scalar| try jws.write(scalar),
.list => |list| try jws.write(list),
.map => |map| {
try jws.beginObject();
var it = map.iterator();
while (it.next()) |entry| {
try jws.objectField(entry.key_ptr.*);
try jws.write(entry.value_ptr.*);
}
try jws.endObject();
},
}
}
};