authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2024-04-06 03:52:20-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-04-06 03:52:20-04:00
log05b185811e7427296bc6b6231d0b7f882f118d38
tree1e519a65cbe5dfdef5b8d79780162827caa44ce8
parentb22ef55f362ca77d23a09c5d27065159fbc5f1f6
signaturebadge-check Signed by PGP key B5690EEEBB952194

json.WriteStream.objectFieldRaw() (#19553)


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

lib/std/json/stringify.zig+19-4
...@@ -28,7 +28,7 @@ pub const StringifyOptions = struct {...@@ -28,7 +28,7 @@ pub const StringifyOptions = struct {
2828
29 /// Arrays/slices of u8 are typically encoded as JSON strings.29 /// Arrays/slices of u8 are typically encoded as JSON strings.
30 /// This option emits them as arrays of numbers instead.30 /// This option emits them as arrays of numbers instead.
31 /// Does not affect calls to `objectField()`.31 /// Does not affect calls to `objectField*()`.
32 emit_strings_as_arrays: bool = false,32 emit_strings_as_arrays: bool = false,
3333
34 /// Should unicode characters be escaped in strings?34 /// Should unicode characters be escaped in strings?
...@@ -156,7 +156,8 @@ pub fn writeStreamArbitraryDepth(...@@ -156,7 +156,8 @@ pub fn writeStreamArbitraryDepth(
156/// | <array>156/// | <array>
157/// | write157/// | write
158/// | print158/// | print
159/// <object> = beginObject ( objectField <value> )* endObject159/// <object> = beginObject ( <field> <value> )* endObject
160/// <field> = objectField | objectFieldRaw
160/// <array> = beginArray ( <value> )* endArray161/// <array> = beginArray ( <value> )* endArray
161/// ```162/// ```
162///163///
...@@ -332,11 +333,11 @@ pub fn WriteStream(...@@ -332,11 +333,11 @@ pub fn WriteStream(
332 }333 }
333334
334 fn valueStart(self: *Self) !void {335 fn valueStart(self: *Self) !void {
335 if (self.isObjectKeyExpected()) |is_it| assert(!is_it); // Call objectField(), not write(), for object keys.336 if (self.isObjectKeyExpected()) |is_it| assert(!is_it); // Call objectField*(), not write(), for object keys.
336 return self.valueStartAssumeTypeOk();337 return self.valueStartAssumeTypeOk();
337 }338 }
338 fn objectFieldStart(self: *Self) !void {339 fn objectFieldStart(self: *Self) !void {
339 if (self.isObjectKeyExpected()) |is_it| assert(is_it); // Expected write(), not objectField().340 if (self.isObjectKeyExpected()) |is_it| assert(is_it); // Expected write(), not objectField*().
340 return self.valueStartAssumeTypeOk();341 return self.valueStartAssumeTypeOk();
341 }342 }
342 fn valueStartAssumeTypeOk(self: *Self) !void {343 fn valueStartAssumeTypeOk(self: *Self) !void {
...@@ -393,11 +394,25 @@ pub fn WriteStream(...@@ -393,11 +394,25 @@ pub fn WriteStream(
393 self.valueDone();394 self.valueDone();
394 }395 }
395396
397 /// See `WriteStream` for when to call this method.
398 /// `key` is the string content of the property name.
399 /// Surrounding quotes will be added and any special characters will be escaped.
400 /// See also `objectFieldRaw`.
396 pub fn objectField(self: *Self, key: []const u8) Error!void {401 pub fn objectField(self: *Self, key: []const u8) Error!void {
397 try self.objectFieldStart();402 try self.objectFieldStart();
398 try encodeJsonString(key, self.options, self.stream);403 try encodeJsonString(key, self.options, self.stream);
399 self.next_punctuation = .colon;404 self.next_punctuation = .colon;
400 }405 }
406 /// See `WriteStream` for when to call this method.
407 /// `quoted_key` is the complete bytes of the key including quotes and any necessary escape sequences.
408 /// A few assertions are performed on the given value to ensure that the caller of this function understands the API contract.
409 /// See also `objectField`.
410 pub fn objectFieldRaw(self: *Self, quoted_key: []const u8) Error!void {
411 assert(quoted_key.len >= 2 and quoted_key[0] == '"' and quoted_key[quoted_key.len - 1] == '"'); // quoted_key should be "quoted".
412 try self.objectFieldStart();
413 try self.stream.writeAll(quoted_key);
414 self.next_punctuation = .colon;
415 }
401416
402 /// See `WriteStream`.417 /// See `WriteStream`.
403 pub fn write(self: *Self, value: anytype) Error!void {418 pub fn write(self: *Self, value: anytype) Error!void {
lib/std/json/stringify_test.zig+1-1
...@@ -51,7 +51,7 @@ fn testBasicWriteStream(w: anytype, slice_stream: anytype) !void {...@@ -51,7 +51,7 @@ fn testBasicWriteStream(w: anytype, slice_stream: anytype) !void {
51 defer arena_allocator.deinit();51 defer arena_allocator.deinit();
52 try w.write(try getJsonObject(arena_allocator.allocator()));52 try w.write(try getJsonObject(arena_allocator.allocator()));
5353
54 try w.objectField("string");54 try w.objectFieldRaw("\"string\"");
55 try w.write("This is a string");55 try w.write("This is a string");
5656
57 try w.objectField("array");57 try w.objectField("array");