diff --git a/README.md b/README.md index aa4a149..ad0e334 100644 --- a/README.md +++ b/README.md @@ -326,7 +326,7 @@ pub fn main() !void { ### [endpoints](examples/endpoints/) -Only showing `main.zig` here: +[`main.zig`](examples/endpoints/main.zig): ```zig const std = @import("std"); @@ -369,3 +369,6 @@ pub fn main() !void { }); } ``` + + +[`endpoints.zig`](examples/endpoints/endpoints.zig): diff --git a/examples/endpoints/endpoints.zig b/examples/endpoints/endpoints.zig index e3e990b..a2d1c70 100644 --- a/examples/endpoints/endpoints.zig +++ b/examples/endpoints/endpoints.zig @@ -57,23 +57,12 @@ fn userIdFromPath(path: []const u8) ?usize { return null; } -var jsonbuf: [100 * 1024]u8 = undefined; -fn stringify(value: anytype, options: std.json.StringifyOptions) ?[]const u8 { - 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; - } -} - pub fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { _ = e; if (r.path) |path| { if (userIdFromPath(path)) |id| { if (users.get(id)) |user| { - if (stringify(user, .{})) |json| { + if (Users.stringify(user, .{})) |json| { _ = r.sendJson(json); } } @@ -81,31 +70,13 @@ pub fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { } } -fn stringifyUserList( - userlist: *std.ArrayList(Users.User), - options: std.json.StringifyOptions, -) !?[]const u8 { - 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 (userlist.items) |user| { - if (!first) try writer.writeByte(','); - first = false; - try std.json.stringify(user, options, string.writer()); - } - try writer.writeByte(']'); - return string.items; -} - pub fn listUsers(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { _ = e; var l: std.ArrayList(Users.User) = std.ArrayList(Users.User).init(alloc); if (users.list(&l)) {} else |_| { return; } - if (stringifyUserList(&l, .{})) |maybe_json| { + if (Users.stringifyUserList(&l, .{})) |maybe_json| { if (maybe_json) |json| { _ = r.sendJson(json); } diff --git a/examples/endpoints/users.zig b/examples/endpoints/users.zig index 788a0b5..2646659 100644 --- a/examples/endpoints/users.zig +++ b/examples/endpoints/users.zig @@ -111,3 +111,36 @@ const JsonUserIterator = struct { return null; } }; + +// +// JSON helpers +// +var jsonbuf: [100 * 1024]u8 = undefined; + +pub fn stringify(value: anytype, options: std.json.StringifyOptions) ?[]const u8 { + 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; + } +} + +pub fn stringifyUserList( + userlist: *std.ArrayList(User), + options: std.json.StringifyOptions, +) !?[]const u8 { + 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 (userlist.items) |user| { + if (!first) try writer.writeByte(','); + first = false; + try std.json.stringify(user, options, string.writer()); + } + try writer.writeByte(']'); + return string.items; +}