std: use json.stringify logic in some json.WriteStream code paths

This commit is contained in:
daurnimator 2020-02-25 01:37:08 +11:00
parent 5a053247e2
commit 17f5d04bed
No known key found for this signature in database
GPG Key ID: 45B429A8F9D9D22A

View File

@ -140,11 +140,7 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
pub fn emitBool(self: *Self, value: bool) !void {
assert(self.state[self.state_index] == State.Value);
if (value) {
try self.stream.writeAll("true");
} else {
try self.stream.writeAll("false");
}
try std.json.stringify(value, std.json.StringifyOptions{}, self.stream);
self.popState();
}
@ -185,20 +181,8 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
}
fn writeEscapedString(self: *Self, string: []const u8) !void {
try self.stream.writeByte('"');
for (string) |s| {
switch (s) {
'"' => try self.stream.writeAll("\\\""),
'\t' => try self.stream.writeAll("\\t"),
'\r' => try self.stream.writeAll("\\r"),
'\n' => try self.stream.writeAll("\\n"),
8 => try self.stream.writeAll("\\b"),
12 => try self.stream.writeAll("\\f"),
'\\' => try self.stream.writeAll("\\\\"),
else => try self.stream.writeByte(s),
}
}
try self.stream.writeByte('"');
assert(std.unicode.utf8ValidateSlice(string));
try std.json.stringify(string, std.json.StringifyOptions{}, self.stream);
}
/// Writes the complete json into the output stream