From 7a3d700fd9d6dcdb559aaae9159af6725b28defb Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 25 Feb 2020 02:07:06 +1100 Subject: [PATCH] std: introduce json.WriteStream.stringify --- lib/std/json/write_stream.zig | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/std/json/write_stream.zig b/lib/std/json/write_stream.zig index ef53e5d5e0..60974a207e 100644 --- a/lib/std/json/write_stream.zig +++ b/lib/std/json/write_stream.zig @@ -138,13 +138,13 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type { pub fn emitNull(self: *Self) !void { assert(self.state[self.state_index] == State.Value); - try self.stream.writeAll("null"); + try self.stringify(null); self.popState(); } pub fn emitBool(self: *Self, value: bool) !void { assert(self.state[self.state_index] == State.Value); - try std.json.stringify(value, std.json.StringifyOptions{}, self.stream); + try self.stringify(value); self.popState(); } @@ -186,14 +186,12 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type { fn writeEscapedString(self: *Self, string: []const u8) !void { assert(std.unicode.utf8ValidateSlice(string)); - try std.json.stringify(string, std.json.StringifyOptions{}, self.stream); + try self.stringify(string); } /// Writes the complete json into the output stream pub fn emitJson(self: *Self, json: std.json.Value) Stream.Error!void { - try json.jsonStringify(std.json.StringifyOptions{ - .whitespace = self.whitespace, - }, self.stream); + try self.stringify(json); } fn indent(self: *Self) !void { @@ -210,6 +208,12 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type { fn popState(self: *Self) void { self.state_index -= 1; } + + fn stringify(self: *Self, value: var) !void { + try std.json.stringify(value, std.json.StringifyOptions{ + .whitespace = self.whitespace, + }, self.stream); + } }; }