• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/03_stdlib/69_json_encode_utf8

  1 JSON encoding (`%J`) must produce valid UTF-8 output no matter which bytes
  2 the encoded strings contain: strings holding valid UTF-8 sequences are
  3 encoded verbatim, while bytes that do not form a valid UTF-8 sequence are
  4 replaced by the unicode replacement character `\ufffd`. This keeps module
  5 and command output containing binary data (e.g. file contents) parseable
  6 by consumers that require valid UTF-8.
  7 
  8 -- Testcase --
  9 {%
 10         // valid UTF-8 is preserved verbatim, including the NUL byte
 11         printf("%J\n", "plain text");
 12         printf("%J\n", "caf" + chr(0xc3, 0xa9));
 13         printf("%J\n", chr(0xe2, 0x82, 0xac));
 14         printf("%J\n", chr(0xf0, 0x9f, 0x98, 0x80));
 15         printf("%J\n", "a\0b");
 16         printf("%J\n", chr(0xc2, 0x80));
 17         printf("%J\n", chr(0xf4, 0x8f, 0xbf, 0xbf));
 18 %}
 19 -- End --
 20 
 21 -- Expect stdout --
 22 "plain text"
 23 "café"
 24 "€"
 25 "😀"
 26 "a\u0000b"
 27 "€"
 28 "􏿿"
 29 -- End --
 30 
 31 
 32 Bytes that do not form a valid UTF-8 sequence are replaced by the unicode
 33 replacement character, one `\ufffd` per invalid byte.
 34 
 35 -- Testcase --
 36 {%
 37         // lone high byte
 38         printf("%J\n", "a" + chr(0xff) + "b");
 39 
 40         // stray continuation byte
 41         printf("%J\n", "a" + chr(0x80) + "b");
 42 
 43         // truncated sequence at the end of the string
 44         printf("%J\n", "a" + chr(0xc3));
 45 
 46         // lead byte followed by an ASCII character
 47         printf("%J\n", chr(0xe2, 0x82) + "x");
 48 
 49         // overlong form
 50         printf("%J\n", chr(0xc0, 0xaf));
 51 
 52         // UTF-16 surrogate
 53         printf("%J\n", chr(0xed, 0xa0, 0x80));
 54 
 55         // codepoint above U+10FFFF
 56         printf("%J\n", chr(0xf4, 0x90, 0x80, 0x80));
 57 
 58         // only the invalid bytes are replaced, valid sequences around them
 59         // are kept
 60         printf("%J\n", chr(0xc3, 0xa9) + chr(0xff) + chr(0xc3, 0xa9));
 61 %}
 62 -- End --
 63 
 64 -- Expect stdout --
 65 "a\ufffdb"
 66 "a\ufffdb"
 67 "a\ufffd"
 68 "\ufffd\ufffdx"
 69 "\ufffd\ufffd"
 70 "\ufffd\ufffd\ufffd"
 71 "\ufffd\ufffd\ufffd\ufffd"
 72 "é\ufffdé"
 73 -- End --
 74 
 75 
 76 The replacement applies to strings nested in arrays and objects as well as
 77 to object keys.
 78 
 79 -- Testcase --
 80 {%
 81         let key = "k" + chr(0xfe);
 82         let o = {};
 83 
 84         o.nested = { list: [ "x" + chr(0xfd), 1, true, null ] };
 85         o.nested[key] = "v";
 86 
 87         printf("%J\n", o);
 88 %}
 89 -- End --
 90 
 91 -- Expect stdout --
 92 { "nested": { "list": [ "x\ufffd", 1, true, null ], "k\ufffd": "v" } }
 93 -- End --
 94 
 95 
 96 Strings without bytes above 0x7f are unaffected.
 97 
 98 -- Testcase --
 99 {%
100         printf("%J\n", sprintf("%c%c", 10, 255));
101         printf("%.1J\n", [ "a" + chr(0xc3, 0xa9) + chr(0xff) ]);
102 %}
103 -- End --
104 
105 -- Expect stdout --
106 "\n\ufffd"
107 [
108  "aé\ufffd"
109 ]
110 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt