mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
escape json if needed
This commit is contained in:
@@ -210,8 +210,30 @@ pub const Entry = struct {
|
||||
return bun.strings.percentEncodeWrite(utf8_input, writer);
|
||||
}
|
||||
|
||||
// On the server, we don't need to do anything
|
||||
try writer.appendSlice(utf8_input);
|
||||
// On the server, escape special characters for JSON
|
||||
var remaining = utf8_input;
|
||||
while (remaining.len > 0) {
|
||||
if (std.mem.indexOfAny(u8, remaining, "\"\\\n\r\t")) |index| {
|
||||
// Write everything before the special character
|
||||
if (index > 0) {
|
||||
try writer.appendSlice(remaining[0..index]);
|
||||
}
|
||||
// Write the escaped character
|
||||
switch (remaining[index]) {
|
||||
'"' => try writer.appendSlice("\\\""),
|
||||
'\\' => try writer.appendSlice("\\\\"),
|
||||
'\n' => try writer.appendSlice("\\n"),
|
||||
'\r' => try writer.appendSlice("\\r"),
|
||||
'\t' => try writer.appendSlice("\\t"),
|
||||
else => unreachable,
|
||||
}
|
||||
remaining = remaining[index + 1 ..];
|
||||
} else {
|
||||
// No special characters found, write the rest
|
||||
try writer.appendSlice(remaining);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn joinVLQ(map: *const Entry, kind: ChunkKind, j: *StringJoiner, arena: Allocator, side: bake.Side) !void {
|
||||
|
||||
Reference in New Issue
Block a user