From ea13437ac575329f1198f8422b856ebfcadff4c8 Mon Sep 17 00:00:00 2001 From: May B Date: Wed, 29 Jun 2022 11:53:01 +0200 Subject: [PATCH] std.json: Support disabling indent (#11823) Newline Delimited JSON (ndjson) expect compact json without newline inside its content Add None to StringfyOptions.indent and move newline writeByte inside StringfyOptions.outputIndent --- lib/std/json.zig | 26 ++++++++++++++++++++------ lib/std/json/write_stream.zig | 1 - 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/std/json.zig b/lib/std/json.zig index cc82c1d0a5..d20797e131 100644 --- a/lib/std/json.zig +++ b/lib/std/json.zig @@ -1321,7 +1321,6 @@ pub const Value = union(enum) { try out_stream.writeByte(','); } if (child_options.whitespace) |child_whitespace| { - try out_stream.writeByte('\n'); try child_whitespace.outputIndent(out_stream); } @@ -1336,7 +1335,6 @@ pub const Value = union(enum) { } if (field_output) { if (options.whitespace) |whitespace| { - try out_stream.writeByte('\n'); try whitespace.outputIndent(out_stream); } } @@ -2943,6 +2941,7 @@ pub const StringifyOptions = struct { indent: union(enum) { Space: u8, Tab: void, + None: void, } = .{ .Space = 4 }, /// After a colon, should whitespace be inserted? @@ -2963,7 +2962,9 @@ pub const StringifyOptions = struct { char = '\t'; n_chars = 1; }, + .None => return, } + try out_stream.writeByte('\n'); n_chars *= whitespace.indent_level; try out_stream.writeByteNTimes(char, n_chars); } @@ -3139,7 +3140,6 @@ pub fn stringify( try out_stream.writeByte(','); } if (child_options.whitespace) |child_whitespace| { - try out_stream.writeByte('\n'); try child_whitespace.outputIndent(out_stream); } try outputJsonString(Field.name, options, out_stream); @@ -3154,7 +3154,6 @@ pub fn stringify( } if (field_output) { if (options.whitespace) |whitespace| { - try out_stream.writeByte('\n'); try whitespace.outputIndent(out_stream); } } @@ -3190,14 +3189,12 @@ pub fn stringify( try out_stream.writeByte(','); } if (child_options.whitespace) |child_whitespace| { - try out_stream.writeByte('\n'); try child_whitespace.outputIndent(out_stream); } try stringify(x, child_options, out_stream); } if (value.len != 0) { if (options.whitespace) |whitespace| { - try out_stream.writeByte('\n'); try whitespace.outputIndent(out_stream); } } @@ -3368,6 +3365,23 @@ test "stringify struct with indentation" { }, }, ); + try teststringify( + \\{"foo":42,"bar":[1,2,3]} + , + struct { + foo: u32, + bar: [3]u32, + }{ + .foo = 42, + .bar = .{ 1, 2, 3 }, + }, + StringifyOptions{ + .whitespace = .{ + .indent = .None, + .separator = false, + }, + }, + ); } test "stringify struct with void field" { diff --git a/lib/std/json/write_stream.zig b/lib/std/json/write_stream.zig index 3393f8a6ee..298f640856 100644 --- a/lib/std/json/write_stream.zig +++ b/lib/std/json/write_stream.zig @@ -202,7 +202,6 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type { fn indent(self: *Self) !void { assert(self.state_index >= 1); - try self.stream.writeByte('\n'); try self.whitespace.outputIndent(self.stream); }