authorgravatar for shu@3hg.frMay B <shu@3hg.fr> 2022-06-29 11:53:01+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-06-29 11:53:01+02:00
logea13437ac575329f1198f8422b856ebfcadff4c8
tree458d7bd292406beebafd6b46dc25d340ba538020
parent4a6b70fbd1e8f6de25ae00c16051aa9613d8a7bc
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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

2 files changed, 20 insertions(+), 7 deletions(-)

lib/std/json.zig+20-6
......@@ -1321,7 +1321,6 @@ pub const Value = union(enum) {
13211321 try out_stream.writeByte(',');
13221322 }
13231323 if (child_options.whitespace) |child_whitespace| {
1324 try out_stream.writeByte('\n');
13251324 try child_whitespace.outputIndent(out_stream);
13261325 }
13271326
......@@ -1336,7 +1335,6 @@ pub const Value = union(enum) {
13361335 }
13371336 if (field_output) {
13381337 if (options.whitespace) |whitespace| {
1339 try out_stream.writeByte('\n');
13401338 try whitespace.outputIndent(out_stream);
13411339 }
13421340 }
......@@ -2943,6 +2941,7 @@ pub const StringifyOptions = struct {
29432941 indent: union(enum) {
29442942 Space: u8,
29452943 Tab: void,
2944 None: void,
29462945 } = .{ .Space = 4 },
29472946
29482947 /// After a colon, should whitespace be inserted?
......@@ -2963,7 +2962,9 @@ pub const StringifyOptions = struct {
29632962 char = '\t';
29642963 n_chars = 1;
29652964 },
2965 .None => return,
29662966 }
2967 try out_stream.writeByte('\n');
29672968 n_chars *= whitespace.indent_level;
29682969 try out_stream.writeByteNTimes(char, n_chars);
29692970 }
......@@ -3139,7 +3140,6 @@ pub fn stringify(
31393140 try out_stream.writeByte(',');
31403141 }
31413142 if (child_options.whitespace) |child_whitespace| {
3142 try out_stream.writeByte('\n');
31433143 try child_whitespace.outputIndent(out_stream);
31443144 }
31453145 try outputJsonString(Field.name, options, out_stream);
......@@ -3154,7 +3154,6 @@ pub fn stringify(
31543154 }
31553155 if (field_output) {
31563156 if (options.whitespace) |whitespace| {
3157 try out_stream.writeByte('\n');
31583157 try whitespace.outputIndent(out_stream);
31593158 }
31603159 }
......@@ -3190,14 +3189,12 @@ pub fn stringify(
31903189 try out_stream.writeByte(',');
31913190 }
31923191 if (child_options.whitespace) |child_whitespace| {
3193 try out_stream.writeByte('\n');
31943192 try child_whitespace.outputIndent(out_stream);
31953193 }
31963194 try stringify(x, child_options, out_stream);
31973195 }
31983196 if (value.len != 0) {
31993197 if (options.whitespace) |whitespace| {
3200 try out_stream.writeByte('\n');
32013198 try whitespace.outputIndent(out_stream);
32023199 }
32033200 }
......@@ -3368,6 +3365,23 @@ test "stringify struct with indentation" {
33683365 },
33693366 },
33703367 );
3368 try teststringify(
3369 \\{"foo":42,"bar":[1,2,3]}
3370 ,
3371 struct {
3372 foo: u32,
3373 bar: [3]u32,
3374 }{
3375 .foo = 42,
3376 .bar = .{ 1, 2, 3 },
3377 },
3378 StringifyOptions{
3379 .whitespace = .{
3380 .indent = .None,
3381 .separator = false,
3382 },
3383 },
3384 );
33713385}
33723386
33733387test "stringify struct with void field" {
lib/std/json/write_stream.zig-1
......@@ -202,7 +202,6 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
202202
203203 fn indent(self: *Self) !void {
204204 assert(self.state_index >= 1);
205 try self.stream.writeByte('\n');
206205 try self.whitespace.outputIndent(self.stream);
207206 }
208207