json.marshal

This commit is contained in:
gingerBill
2019-01-06 23:32:50 +00:00
parent 08598b9425
commit 6c21e99832
3 changed files with 302 additions and 3 deletions
+13 -1
View File
@@ -117,7 +117,7 @@ write_encoded_rune :: proc(b: ^Builder, r: rune, write_quote := true) {
}
write_escaped_rune :: proc(b: ^Builder, r: rune, quote: byte) {
write_escaped_rune :: proc(b: ^Builder, r: rune, quote: byte, html_safe := false) {
is_printable :: proc(r: rune) -> bool {
if r <= 0xff {
switch r {
@@ -132,6 +132,18 @@ write_escaped_rune :: proc(b: ^Builder, r: rune, quote: byte) {
return false;
}
if html_safe {
switch r {
case '<', '>', '&':
write_byte(b, '\\');
write_byte(b, 'u');
for s := 12; s >= 0; s -= 4 {
write_byte(b, DIGITS_LOWER[r>>uint(s) & 0xf]);
}
return;
}
}
if r == rune(quote) || r == '\\' {
write_byte(b, '\\');
write_byte(b, byte(r));