diff --git a/vlib/x/json2/encoder.v b/vlib/x/json2/encoder.v index 9faeb70327..b1ca0e49e7 100644 --- a/vlib/x/json2/encoder.v +++ b/vlib/x/json2/encoder.v @@ -156,7 +156,10 @@ fn json_string(s string) string { } else { slice := s[i..i + char_len] 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') } else { // TODO: still figuring out what diff --git a/vlib/x/json2/encoder_test.v b/vlib/x/json2/encoder_test.v index bb506752a8..813517216c 100644 --- a/vlib/x/json2/encoder_test.v +++ b/vlib/x/json2/encoder_test.v @@ -19,3 +19,11 @@ fn test_json_string_non_ascii() { text := json2.Any('ひらがな') 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 +}