Skip to content

Commit

Permalink
std.json: Support disabling indent (#11823)
Browse files Browse the repository at this point in the history
Newline Delimited JSON (ndjson) expect compact json without newline inside its content
Add None to StringfyOptions.indent and move newline writeByte inside StringfyOptions.outputIndent
  • Loading branch information
ShuP1 authored and andrewrk committed Jul 19, 2022
1 parent dea7c34 commit 09ff086
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
26 changes: 20 additions & 6 deletions lib/std/json.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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?
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand All @@ -3154,7 +3154,6 @@ pub fn stringify(
}
if (field_output) {
if (options.whitespace) |whitespace| {
try out_stream.writeByte('\n');
try whitespace.outputIndent(out_stream);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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" {
Expand Down
1 change: 0 additions & 1 deletion lib/std/json/write_stream.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 09ff086

Please sign in to comment.