Skip to content

Commit d166bcf

Browse files
authored
Use mkCharLenCE with UTF-8 tag in JSON string decode (#34)
* Use mkCharLenCE with UTF-8 tag in JSON string decode * Add NEWS item
1 parent c040469 commit d166bcf

3 files changed

Lines changed: 5 additions & 2 deletions

File tree

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# secretbase (development version)
22

33
* Speeds up hex string conversion for hash output (`convert = TRUE`).
4+
* `jsondec()` now tags decoded strings as UTF-8 per RFC 8259.
45

56
# secretbase 1.2.1
67

src/json.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,8 @@ static SEXP json_parse_string(const char **p, int *err) {
146146
*d++ = *s++;
147147
}
148148
}
149-
*d = '\0';
150149
(*p)++; // skip closing "
151-
return Rf_mkString(buf);
150+
return Rf_ScalarString(Rf_mkCharLenCE(buf, (int) (d - buf), CE_UTF8));
152151
}
153152

154153
static SEXP json_parse_number(const char **p, int *err) {

tests/tests.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,9 @@ test_equal(jsondec('{"a":"\\u00eF"}')[["a"]], "\u00ef") # Mixed case hex
418418
test_equal(jsondec('{"a":"Hello \\u4e16\\u754c!"}')[["a"]], "Hello \u4e16\u754c!") # Mixed content
419419
test_equal(jsondec('{"a":"line1\\u000aline2"}')[["a"]], "line1\nline2") # Newline via Unicode
420420
test_equal(charToRaw(jsondec('{"a":"\\u0001"}')[["a"]])[1], as.raw(1)) # Control char U+0001
421+
# UTF-8 encoding tag tests (RFC 8259 Section 8):
422+
test_equal(Encoding(jsondec('{"a":"\\u00e9"}')[["a"]]), "UTF-8") # decoded value tagged UTF-8
423+
test_equal(Encoding(names(jsondec('{"\\u00e9":"v"}'))), "UTF-8") # decoded key tagged UTF-8
421424
# UTF-16 surrogate pair tests (characters outside BMP):
422425
test_equal(jsondec('{"a":"\\uD83D\\uDE00"}')[["a"]], "\U0001F600")
423426
test_equal(jsondec('{"a":"\\uD83D\\uDCA9"}')[["a"]], "\U0001F4A9")

0 commit comments

Comments
 (0)