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) {...@@ -1321,7 +1321,6 @@ pub const Value = union(enum) {
1321 try out_stream.writeByte(',');1321 try out_stream.writeByte(',');
1322 }1322 }
1323 if (child_options.whitespace) |child_whitespace| {1323 if (child_options.whitespace) |child_whitespace| {
1324 try out_stream.writeByte('\n');
1325 try child_whitespace.outputIndent(out_stream);1324 try child_whitespace.outputIndent(out_stream);
1326 }1325 }
13271326
...@@ -1336,7 +1335,6 @@ pub const Value = union(enum) {...@@ -1336,7 +1335,6 @@ pub const Value = union(enum) {
1336 }1335 }
1337 if (field_output) {1336 if (field_output) {
1338 if (options.whitespace) |whitespace| {1337 if (options.whitespace) |whitespace| {
1339 try out_stream.writeByte('\n');
1340 try whitespace.outputIndent(out_stream);1338 try whitespace.outputIndent(out_stream);
1341 }1339 }
1342 }1340 }
...@@ -2943,6 +2941,7 @@ pub const StringifyOptions = struct {...@@ -2943,6 +2941,7 @@ pub const StringifyOptions = struct {
2943 indent: union(enum) {2941 indent: union(enum) {
2944 Space: u8,2942 Space: u8,
2945 Tab: void,2943 Tab: void,
2944 None: void,
2946 } = .{ .Space = 4 },2945 } = .{ .Space = 4 },
29472946
2948 /// After a colon, should whitespace be inserted?2947 /// After a colon, should whitespace be inserted?
...@@ -2963,7 +2962,9 @@ pub const StringifyOptions = struct {...@@ -2963,7 +2962,9 @@ pub const StringifyOptions = struct {
2963 char = '\t';2962 char = '\t';
2964 n_chars = 1;2963 n_chars = 1;
2965 },2964 },
2965 .None => return,
2966 }2966 }
2967 try out_stream.writeByte('\n');
2967 n_chars *= whitespace.indent_level;2968 n_chars *= whitespace.indent_level;
2968 try out_stream.writeByteNTimes(char, n_chars);2969 try out_stream.writeByteNTimes(char, n_chars);
2969 }2970 }
...@@ -3139,7 +3140,6 @@ pub fn stringify(...@@ -3139,7 +3140,6 @@ pub fn stringify(
3139 try out_stream.writeByte(',');3140 try out_stream.writeByte(',');
3140 }3141 }
3141 if (child_options.whitespace) |child_whitespace| {3142 if (child_options.whitespace) |child_whitespace| {
3142 try out_stream.writeByte('\n');
3143 try child_whitespace.outputIndent(out_stream);3143 try child_whitespace.outputIndent(out_stream);
3144 }3144 }
3145 try outputJsonString(Field.name, options, out_stream);3145 try outputJsonString(Field.name, options, out_stream);
...@@ -3154,7 +3154,6 @@ pub fn stringify(...@@ -3154,7 +3154,6 @@ pub fn stringify(
3154 }3154 }
3155 if (field_output) {3155 if (field_output) {
3156 if (options.whitespace) |whitespace| {3156 if (options.whitespace) |whitespace| {
3157 try out_stream.writeByte('\n');
3158 try whitespace.outputIndent(out_stream);3157 try whitespace.outputIndent(out_stream);
3159 }3158 }
3160 }3159 }
...@@ -3190,14 +3189,12 @@ pub fn stringify(...@@ -3190,14 +3189,12 @@ pub fn stringify(
3190 try out_stream.writeByte(',');3189 try out_stream.writeByte(',');
3191 }3190 }
3192 if (child_options.whitespace) |child_whitespace| {3191 if (child_options.whitespace) |child_whitespace| {
3193 try out_stream.writeByte('\n');
3194 try child_whitespace.outputIndent(out_stream);3192 try child_whitespace.outputIndent(out_stream);
3195 }3193 }
3196 try stringify(x, child_options, out_stream);3194 try stringify(x, child_options, out_stream);
3197 }3195 }
3198 if (value.len != 0) {3196 if (value.len != 0) {
3199 if (options.whitespace) |whitespace| {3197 if (options.whitespace) |whitespace| {
3200 try out_stream.writeByte('\n');
3201 try whitespace.outputIndent(out_stream);3198 try whitespace.outputIndent(out_stream);
3202 }3199 }
3203 }3200 }
...@@ -3368,6 +3365,23 @@ test "stringify struct with indentation" {...@@ -3368,6 +3365,23 @@ test "stringify struct with indentation" {
3368 },3365 },
3369 },3366 },
3370 );3367 );
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 );
3371}3385}
33723386
3373test "stringify struct with void field" {3387test "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 {...@@ -202,7 +202,6 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type {
202202
203 fn indent(self: *Self) !void {203 fn indent(self: *Self) !void {
204 assert(self.state_index >= 1);204 assert(self.state_index >= 1);
205 try self.stream.writeByte('\n');
206 try self.whitespace.outputIndent(self.stream);205 try self.whitespace.outputIndent(self.stream);
207 }206 }
208207