| ... | ... | @@ -28,7 +28,7 @@ pub const StringifyOptions = struct { |
| 28 | 28 | |
| 29 | 29 | /// Arrays/slices of u8 are typically encoded as JSON strings. |
| 30 | 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 | 32 | emit_strings_as_arrays: bool = false, |
| 33 | 33 | |
| 34 | 34 | /// Should unicode characters be escaped in strings? |
| ... | ... | @@ -156,7 +156,8 @@ pub fn writeStreamArbitraryDepth( |
| 156 | 156 | /// | <array> |
| 157 | 157 | /// | write |
| 158 | 158 | /// | print |
| 159 | | /// <object> = beginObject ( objectField <value> )* endObject |
| 159 | /// <object> = beginObject ( <field> <value> )* endObject |
| 160 | /// <field> = objectField | objectFieldRaw |
| 160 | 161 | /// <array> = beginArray ( <value> )* endArray |
| 161 | 162 | /// ``` |
| 162 | 163 | /// |
| ... | ... | @@ -332,11 +333,11 @@ pub fn WriteStream( |
| 332 | 333 | } |
| 333 | 334 | |
| 334 | 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 | 337 | return self.valueStartAssumeTypeOk(); |
| 337 | 338 | } |
| 338 | 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 | 341 | return self.valueStartAssumeTypeOk(); |
| 341 | 342 | } |
| 342 | 343 | fn valueStartAssumeTypeOk(self: *Self) !void { |
| ... | ... | @@ -393,11 +394,25 @@ pub fn WriteStream( |
| 393 | 394 | self.valueDone(); |
| 394 | 395 | } |
| 395 | 396 | |
| 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 | 401 | pub fn objectField(self: *Self, key: []const u8) Error!void { |
| 397 | 402 | try self.objectFieldStart(); |
| 398 | 403 | try encodeJsonString(key, self.options, self.stream); |
| 399 | 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 | } |
| 401 | 416 | |
| 402 | 417 | /// See `WriteStream`. |
| 403 | 418 | pub fn write(self: *Self, value: anytype) Error!void { |