authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-02-27 15:49:59 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-02-27 15:49:59 -08:00
logb16f583d5af5c86c92237bf56fa8063a5e9961c3
tree522683a3ff53716f17d7f52f6d3505b98c95b5c0
parentaa4f77ea988ad23fee6af78afbb1daafbd7193a2

add jsonStringify mixins


1 files changed, 23 insertions(+), 0 deletions(-)

src/lib.zig+23
...@@ -207,3 +207,26 @@ pub fn pipe(reader_from: anytype, writer_to: anytype) !void {...@@ -207,3 +207,26 @@ pub fn pipe(reader_from: anytype, writer_to: anytype) !void {
207 defer fifo.deinit();207 defer fifo.deinit();
208 try fifo.pump(reader_from, writer_to);208 try fifo.pump(reader_from, writer_to);
209}209}
210
211pub fn StringerJsonStringifyMixin(comptime S: type) type {
212 return struct {
213 pub fn jsonStringify(self: S, options: std.json.StringifyOptions, out_stream: anytype) !void {
214 var buf: [1024]u8 = undefined;
215 var fba = std.heap.FixedBufferAllocator.init(&buf);
216 const alloc = fba.allocator();
217 var list = std.ArrayList(u8).init(alloc);
218 errdefer list.deinit();
219 const writer = list.writer();
220 try writer.writeAll(try self.toString(alloc));
221 try std.json.stringify(list.toOwnedSlice(), options, out_stream);
222 }
223 };
224}
225
226pub fn TagNameJsonStringifyMixin(comptime S: type) type {
227 return struct {
228 pub fn jsonStringify(self: S, options: std.json.StringifyOptions, out_stream: anytype) !void {
229 try std.json.stringify(@tagName(self), options, out_stream);
230 }
231 };
232}