authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-02-25 02:07:06+11:00
committergravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-04-01 00:13:00+11:00
log7a3d700fd9d6dcdb559aaae9159af6725b28defb
treea7d97784d89a34c91292246b7543117c442f1612
parent42cabe436675dd20d67de533e534df4f5cf46cd2
signaturelock-open Commit is signed but in an unrecognized format.

std: introduce json.WriteStream.stringify


1 files changed, 10 insertions(+), 6 deletions(-)

lib/std/json/write_stream.zig+10-6
......@@ -138,13 +138,13 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
138138
139139 pub fn emitNull(self: *Self) !void {
140140 assert(self.state[self.state_index] == State.Value);
141 try self.stream.writeAll("null");
141 try self.stringify(null);
142142 self.popState();
143143 }
144144
145145 pub fn emitBool(self: *Self, value: bool) !void {
146146 assert(self.state[self.state_index] == State.Value);
147 try std.json.stringify(value, std.json.StringifyOptions{}, self.stream);
147 try self.stringify(value);
148148 self.popState();
149149 }
150150
......@@ -186,14 +186,12 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
186186
187187 fn writeEscapedString(self: *Self, string: []const u8) !void {
188188 assert(std.unicode.utf8ValidateSlice(string));
189 try std.json.stringify(string, std.json.StringifyOptions{}, self.stream);
189 try self.stringify(string);
190190 }
191191
192192 /// Writes the complete json into the output stream
193193 pub fn emitJson(self: *Self, json: std.json.Value) Stream.Error!void {
194 try json.jsonStringify(std.json.StringifyOptions{
195 .whitespace = self.whitespace,
196 }, self.stream);
194 try self.stringify(json);
197195 }
198196
199197 fn indent(self: *Self) !void {
......@@ -210,6 +208,12 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
210208 fn popState(self: *Self) void {
211209 self.state_index -= 1;
212210 }
211
212 fn stringify(self: *Self, value: var) !void {
213 try std.json.stringify(value, std.json.StringifyOptions{
214 .whitespace = self.whitespace,
215 }, self.stream);
216 }
213217 };
214218}
215219