mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 15:14:08 +00:00
API-improved json stuff in util.zig
This commit is contained in:
parent
27f3cb8628
commit
532d5e9d3e
1 changed files with 4 additions and 18 deletions
22
src/util.zig
22
src/util.zig
|
@ -82,36 +82,22 @@ pub fn stringifyArrayListBuf(
|
|||
return string.items;
|
||||
}
|
||||
|
||||
/// provide your own allocator, NOT mutex-protected
|
||||
pub fn stringifyAlloc(
|
||||
a: std.mem.Allocator,
|
||||
value: anytype,
|
||||
options: std.json.StringifyOptions,
|
||||
) ?std.ArrayList(u8) {
|
||||
var string = std.ArrayList(u8).init(a);
|
||||
if (std.json.stringify(value, options, string.writer())) {
|
||||
return string;
|
||||
} else |_| { // error
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// provide your own allocator, NOT mutex-protected
|
||||
pub fn stringifyArrayListAlloc(
|
||||
a: std.mem.Allocator,
|
||||
comptime T: anytype,
|
||||
list: *std.ArrayList(T),
|
||||
options: std.json.StringifyOptions,
|
||||
) !?std.ArrayList(u8) {
|
||||
) ![]const u8 {
|
||||
var string = std.ArrayList(u8).init(a);
|
||||
var writer = string.writer();
|
||||
try writer.writeByte('[');
|
||||
var first: bool = true;
|
||||
for (list.items) |user| {
|
||||
for (list.items) |item| {
|
||||
if (!first) try writer.writeByte(',');
|
||||
first = false;
|
||||
try std.json.stringify(user, options, string.writer());
|
||||
try std.json.stringify(item, options, string.writer());
|
||||
}
|
||||
try writer.writeByte(']');
|
||||
return string;
|
||||
return string.toOwnedSlice();
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue