mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 23:24:09 +00:00
117 lines
3.2 KiB
Zig
117 lines
3.2 KiB
Zig
const std = @import("std");
|
|
|
|
//
|
|
// 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,
|
|
value: anytype,
|
|
options: std.json.StringifyOptions,
|
|
) ?[]const u8 {
|
|
var fba = std.heap.FixedBufferAllocator.init(buffer);
|
|
var string = std.ArrayList(u8).init(fba.allocator());
|
|
if (std.json.stringify(value, options, string.writer())) {
|
|
return string.items;
|
|
} else |_| { // error
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// provide your own buf, NOT mutex-protected
|
|
pub fn stringifyArrayListBuf(
|
|
buffer: []u8,
|
|
comptime T: anytype,
|
|
list: *std.ArrayList(T),
|
|
options: std.json.StringifyOptions,
|
|
) !?[]const u8 {
|
|
var fba = std.heap.FixedBufferAllocator.init(buffer);
|
|
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 allocator, NOT mutex-protected
|
|
pub fn stringifyAlloc(
|
|
a: std.mem.Allocator,
|
|
value: anytype,
|
|
options: std.json.StringifyOptions,
|
|
) ?[]const u8 {
|
|
var string = std.ArrayList(u8).init(a);
|
|
if (std.json.stringify(value, options, string.writer())) {
|
|
return string.items;
|
|
} 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,
|
|
) !?[]const u8 {
|
|
var string = std.ArrayList(u8).init(a);
|
|
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;
|
|
}
|