1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 15:14:08 +00:00

new release after std.json chanes

This commit is contained in:
Rene Schallner 2023-05-19 14:47:55 +02:00
parent 25fc7cf809
commit 4349f6e25d
2 changed files with 45 additions and 43 deletions

View file

@ -1,6 +1,6 @@
.{
.name = "zap",
.version = "0.0.21",
.version = "0.0.22",
.dependencies = .{
.@"facil.io" = .{

View file

@ -54,48 +54,6 @@ pub fn toCharPtr(s: []const u8) [*c]u8 {
// JSON helpers
//
// 1MB JSON buffer
var jsonbuf: [1024 * 1024]u8 = undefined;
var mutex: std.Thread.Mutex = .{};
/// use default 1MB buffer, mutex-protected
pub fn stringify(
value: anytype,
options: std.json.StringifyOptions,
) ?[]const u8 {
mutex.lock();
defer mutex.unlock();
var fba = std.heap.FixedBufferAllocator.init(&jsonbuf);
var string = std.ArrayList(u8).init(fba.allocator());
if (std.json.stringify(value, options, string.writer())) {
return string.items;
} else |_| { // error
return null;
}
}
/// use default 1MB buffer, mutex-protected
pub fn stringifyArrayList(
comptime T: anytype,
list: *std.ArrayList(T),
options: std.json.StringifyOptions,
) !?[]const u8 {
mutex.lock();
defer mutex.unlock();
var fba = std.heap.FixedBufferAllocator.init(&jsonbuf);
var string = std.ArrayList(u8).init(fba.allocator());
var writer = string.writer();
try writer.writeByte('[');
var first: bool = true;
for (list.items) |user| {
if (!first) try writer.writeByte(',');
first = false;
try std.json.stringify(user, options, string.writer());
}
try writer.writeByte(']');
return string.items;
}
/// provide your own buf, NOT mutex-protected!
pub fn stringifyBuf(
buffer: []u8,
@ -110,3 +68,47 @@ pub fn stringifyBuf(
return null;
}
}
// deprecated:
// 1MB JSON buffer
// var jsonbuf: [1024 * 1024]u8 = undefined;
// var mutex: std.Thread.Mutex = .{};
// use default 1MB buffer, mutex-protected
// pub fn stringify(
// value: anytype,
// options: std.json.StringifyOptions,
// ) ?[]const u8 {
// mutex.lock();
// defer mutex.unlock();
// var fba = std.heap.FixedBufferAllocator.init(&jsonbuf);
// var string = std.ArrayList(u8).init(fba.allocator());
// if (std.json.stringify(value, options, string.writer())) {
// return string.items;
// } else |_| { // error
// return null;
// }
// }
// use default 1MB buffer, mutex-protected
// pub fn stringifyArrayList(
// comptime T: anytype,
// list: *std.ArrayList(T),
// options: std.json.StringifyOptions,
// ) !?[]const u8 {
// mutex.lock();
// defer mutex.unlock();
// var fba = std.heap.FixedBufferAllocator.init(&jsonbuf);
// var string = std.ArrayList(u8).init(fba.allocator());
// var writer = string.writer();
// try writer.writeByte('[');
// var first: bool = true;
// for (list.items) |user| {
// if (!first) try writer.writeByte(',');
// first = false;
// try std.json.stringify(user, options, string.writer());
// }
// try writer.writeByte(']');
// return string.items;
// }