authorgravatar for johnnymarler@gmail.comJonathan Marler <johnnymarler@gmail.com> 2022-06-30 15:02:09-06:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-24 11:51:59+03:00
logf598234ee820ca0d57946542a852f193b21d847f
tree5ab1dda8719c06cd4b52e2b7ce7c8f0c4b8f417a
parenta2ab9e36faded9755ecc1fe809c49140120c3c61

std.json: expose encodeJsonString and encodeJsonStringChars

Expose 2 functions from std.json. These functions take a slice of bytes and forward them to a given writer as a JSON encoded string. The use case I have for this is in a custom JsonStringWriter. This writer takes data and automatically encodes it as JSON string characters and forwards it to an underlying writer. I use this JsonStringWriter in combination with std.fmt.format to go directly from a format string/arg pair to JSON. This way I don't have to format my string into a separate buffer first and encode it afterwards, which avoids the need to create a temporary buffer to hold the unencoded but formatted string.

1 files changed, 27 insertions(+), 21 deletions(-)

lib/std/json.zig+27-21
......@@ -2163,45 +2163,51 @@ fn outputUnicodeEscape(
21632163 }
21642164}
21652165
2166fn outputJsonString(value: []const u8, options: StringifyOptions, out_stream: anytype) !void {
2167 try out_stream.writeByte('\"');
2166/// Write `string` to `writer` as a JSON encoded string.
2167pub fn encodeJsonString(string: []const u8, options: StringifyOptions, writer: anytype) !void {
2168 try writer.writeByte('\"');
2169 try encodeJsonStringChars(string, options, writer);
2170 try writer.writeByte('\"');
2171}
2172
2173/// Write `chars` to `writer` as JSON encoded string characters.
2174pub fn encodeJsonStringChars(chars: []const u8, options: StringifyOptions, writer: anytype) !void {
21682175 var i: usize = 0;
2169 while (i < value.len) : (i += 1) {
2170 switch (value[i]) {
2176 while (i < chars.len) : (i += 1) {
2177 switch (chars[i]) {
21712178 // normal ascii character
2172 0x20...0x21, 0x23...0x2E, 0x30...0x5B, 0x5D...0x7F => |c| try out_stream.writeByte(c),
2179 0x20...0x21, 0x23...0x2E, 0x30...0x5B, 0x5D...0x7F => |c| try writer.writeByte(c),
21732180 // only 2 characters that *must* be escaped
2174 '\\' => try out_stream.writeAll("\\\\"),
2175 '\"' => try out_stream.writeAll("\\\""),
2181 '\\' => try writer.writeAll("\\\\"),
2182 '\"' => try writer.writeAll("\\\""),
21762183 // solidus is optional to escape
21772184 '/' => {
21782185 if (options.string.String.escape_solidus) {
2179 try out_stream.writeAll("\\/");
2186 try writer.writeAll("\\/");
21802187 } else {
2181 try out_stream.writeByte('/');
2188 try writer.writeByte('/');
21822189 }
21832190 },
21842191 // control characters with short escapes
21852192 // TODO: option to switch between unicode and 'short' forms?
2186 0x8 => try out_stream.writeAll("\\b"),
2187 0xC => try out_stream.writeAll("\\f"),
2188 '\n' => try out_stream.writeAll("\\n"),
2189 '\r' => try out_stream.writeAll("\\r"),
2190 '\t' => try out_stream.writeAll("\\t"),
2193 0x8 => try writer.writeAll("\\b"),
2194 0xC => try writer.writeAll("\\f"),
2195 '\n' => try writer.writeAll("\\n"),
2196 '\r' => try writer.writeAll("\\r"),
2197 '\t' => try writer.writeAll("\\t"),
21912198 else => {
2192 const ulen = std.unicode.utf8ByteSequenceLength(value[i]) catch unreachable;
2199 const ulen = std.unicode.utf8ByteSequenceLength(chars[i]) catch unreachable;
21932200 // control characters (only things left with 1 byte length) should always be printed as unicode escapes
21942201 if (ulen == 1 or options.string.String.escape_unicode) {
2195 const codepoint = std.unicode.utf8Decode(value[i .. i + ulen]) catch unreachable;
2196 try outputUnicodeEscape(codepoint, out_stream);
2202 const codepoint = std.unicode.utf8Decode(chars[i .. i + ulen]) catch unreachable;
2203 try outputUnicodeEscape(codepoint, writer);
21972204 } else {
2198 try out_stream.writeAll(value[i .. i + ulen]);
2205 try writer.writeAll(chars[i .. i + ulen]);
21992206 }
22002207 i += ulen - 1;
22012208 },
22022209 }
22032210 }
2204 try out_stream.writeByte('\"');
22052211}
22062212
22072213pub fn stringify(
......@@ -2288,7 +2294,7 @@ pub fn stringify(
22882294 if (child_options.whitespace) |child_whitespace| {
22892295 try child_whitespace.outputIndent(out_stream);
22902296 }
2291 try outputJsonString(Field.name, options, out_stream);
2297 try encodeJsonString(Field.name, options, out_stream);
22922298 try out_stream.writeByte(':');
22932299 if (child_options.whitespace) |child_whitespace| {
22942300 if (child_whitespace.separator) {
......@@ -2321,7 +2327,7 @@ pub fn stringify(
23212327 // TODO: .Many when there is a sentinel (waiting for https://github.com/ziglang/zig/pull/3972)
23222328 .Slice => {
23232329 if (ptr_info.child == u8 and options.string == .String and std.unicode.utf8ValidateSlice(value)) {
2324 try outputJsonString(value, options, out_stream);
2330 try encodeJsonString(value, options, out_stream);
23252331 return;
23262332 }
23272333