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 {...@@ -138,13 +138,13 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
138138
139 pub fn emitNull(self: *Self) !void {139 pub fn emitNull(self: *Self) !void {
140 assert(self.state[self.state_index] == State.Value);140 assert(self.state[self.state_index] == State.Value);
141 try self.stream.writeAll("null");141 try self.stringify(null);
142 self.popState();142 self.popState();
143 }143 }
144144
145 pub fn emitBool(self: *Self, value: bool) !void {145 pub fn emitBool(self: *Self, value: bool) !void {
146 assert(self.state[self.state_index] == State.Value);146 assert(self.state[self.state_index] == State.Value);
147 try std.json.stringify(value, std.json.StringifyOptions{}, self.stream);147 try self.stringify(value);
148 self.popState();148 self.popState();
149 }149 }
150150
...@@ -186,14 +186,12 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {...@@ -186,14 +186,12 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
186186
187 fn writeEscapedString(self: *Self, string: []const u8) !void {187 fn writeEscapedString(self: *Self, string: []const u8) !void {
188 assert(std.unicode.utf8ValidateSlice(string));188 assert(std.unicode.utf8ValidateSlice(string));
189 try std.json.stringify(string, std.json.StringifyOptions{}, self.stream);189 try self.stringify(string);
190 }190 }
191191
192 /// Writes the complete json into the output stream192 /// Writes the complete json into the output stream
193 pub fn emitJson(self: *Self, json: std.json.Value) Stream.Error!void {193 pub fn emitJson(self: *Self, json: std.json.Value) Stream.Error!void {
194 try json.jsonStringify(std.json.StringifyOptions{194 try self.stringify(json);
195 .whitespace = self.whitespace,
196 }, self.stream);
197 }195 }
198196
199 fn indent(self: *Self) !void {197 fn indent(self: *Self) !void {
...@@ -210,6 +208,12 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {...@@ -210,6 +208,12 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
210 fn popState(self: *Self) void {208 fn popState(self: *Self) void {
211 self.state_index -= 1;209 self.state_index -= 1;
212 }210 }
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 }
213 };217 };
214}218}
215219