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 {
2828
2929 /// Arrays/slices of u8 are typically encoded as JSON strings.
3030 /// This option emits them as arrays of numbers instead.
31 /// Does not affect calls to `objectField()`.
31 /// Does not affect calls to `objectField*()`.
3232 emit_strings_as_arrays: bool = false,
3333
3434 /// Should unicode characters be escaped in strings?
......@@ -156,7 +156,8 @@ pub fn writeStreamArbitraryDepth(
156156/// | <array>
157157/// | write
158158/// | print
159/// <object> = beginObject ( objectField <value> )* endObject
159/// <object> = beginObject ( <field> <value> )* endObject
160/// <field> = objectField | objectFieldRaw
160161/// <array> = beginArray ( <value> )* endArray
161162/// ```
162163///
......@@ -332,11 +333,11 @@ pub fn WriteStream(
332333 }
333334
334335 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.
336337 return self.valueStartAssumeTypeOk();
337338 }
338339 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*().
340341 return self.valueStartAssumeTypeOk();
341342 }
342343 fn valueStartAssumeTypeOk(self: *Self) !void {
......@@ -393,11 +394,25 @@ pub fn WriteStream(
393394 self.valueDone();
394395 }
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`.
396401 pub fn objectField(self: *Self, key: []const u8) Error!void {
397402 try self.objectFieldStart();
398403 try encodeJsonString(key, self.options, self.stream);
399404 self.next_punctuation = .colon;
400405 }
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
402417 /// See `WriteStream`.
403418 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 {
5151 defer arena_allocator.deinit();
5252 try w.write(try getJsonObject(arena_allocator.allocator()));
5353
54 try w.objectField("string");
54 try w.objectFieldRaw("\"string\"");
5555 try w.write("This is a string");
5656
5757 try w.objectField("array");