x.json2: fix `json_string` for utf8 codepoints, add regression test

pull/10257/head
Delyan Angelov 2021-06-29 11:40:44 +03:00
parent 04221475d1
commit 58df35b453
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 12 additions and 1 deletions

View File

@ -156,7 +156,10 @@ fn json_string(s string) string {
} else { } else {
slice := s[i..i + char_len] slice := s[i..i + char_len]
hex_code := slice.utf32_code().hex() hex_code := slice.utf32_code().hex()
if hex_code.len == 4 { if hex_code.len < 4 {
// an utf8 codepoint
sb.write_string(slice)
} else if hex_code.len == 4 {
sb.write_string('\\u$hex_code') sb.write_string('\\u$hex_code')
} else { } else {
// TODO: still figuring out what // TODO: still figuring out what

View File

@ -19,3 +19,11 @@ fn test_json_string_non_ascii() {
text := json2.Any('') text := json2.Any('')
assert text.json_str() == r'\u3072\u3089\u304c\u306a' assert text.json_str() == r'\u3072\u3089\u304c\u306a'
} }
fn test_utf8_strings_are_not_modified() ? {
original := '{"s":"Schilddrüsenerkrankungen"}'
// dump(original)
deresult := json2.raw_decode(original) ?
// dump(deresult)
assert deresult.str() == original
}