From 58df35b453f83b6832acdc23d1e6f8398f5fcb02 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 29 Jun 2021 11:40:44 +0300 Subject: [PATCH] x.json2: fix `json_string` for utf8 codepoints, add regression test --- vlib/x/json2/encoder.v | 5 ++++- vlib/x/json2/encoder_test.v | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) 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 +}