encodeJsonStringChars
Write chars to writer as JSON encoded string characters.
Function parameters
Parameters
Type definitions in this namespace
Types
Functions in this namespace
Functions
- An alternative to calling `write` that formats a value with `std.fmt`.
- beginWriteRaw
- An alternative to calling `write` that allows you to write directly to the `.writer` field, e.g.
- endWriteRaw
- See `beginWriteRaw`.
- objectField
- See `Stringify` for when to call this method.
- objectFieldRaw
- See `Stringify` for when to call this method.
- beginObjectFieldRaw
- In the rare case that you need to write very long object field names,
- endObjectFieldRaw
- See `beginObjectFieldRaw`.
- write
- Renders the given Zig value as JSON.
- value
- Writes the given value to the `Writer` writer.
- valueAlloc
- Calls `value` and stores the result in dynamically allocated memory instead
- encodeJsonString
- Write `string` to `writer` as a JSON encoded string.
- encodeJsonStringChars
- Write `chars` to `writer` as JSON encoded string characters.
Error sets in this namespace
Error Sets
Source
Implementation
pub fn encodeJsonStringChars(chars: []const u8, options: Options, writer: *Writer) Error!void {
var write_cursor: usize = 0;
var i: usize = 0;
if (options.escape_unicode) {
while (i < chars.len) : (i += 1) {
switch (chars[i]) {
// normal ascii character
0x20...0x21, 0x23...0x5B, 0x5D...0x7E => {},
0x00...0x1F, '\\', '\"' => {
// Always must escape these.
try writer.writeAll(chars[write_cursor..i]);
try outputSpecialEscape(chars[i], writer);
write_cursor = i + 1;
},
0x7F...0xFF => {
try writer.writeAll(chars[write_cursor..i]);
const ulen = std.unicode.utf8ByteSequenceLength(chars[i]) catch unreachable;
const codepoint = std.unicode.utf8Decode(chars[i..][0..ulen]) catch unreachable;
try outputUnicodeEscape(codepoint, writer);
i += ulen - 1;
write_cursor = i + 1;
},
}
}
} else {
while (i < chars.len) : (i += 1) {
switch (chars[i]) {
// normal bytes
0x20...0x21, 0x23...0x5B, 0x5D...0xFF => {},
0x00...0x1F, '\\', '\"' => {
// Always must escape these.
try writer.writeAll(chars[write_cursor..i]);
try outputSpecialEscape(chars[i], writer);
write_cursor = i + 1;
},
}
}
}
try writer.writeAll(chars[write_cursor..chars.len]);
}