1 -- encoding-sensitive tests for json and jsonb
4 SELECT '"\u"'::json; -- ERROR, incomplete escape
5 ERROR: invalid input syntax for type json
6 LINE 1: SELECT '"\u"'::json;
8 DETAIL: "\u" must be followed by four hexadecimal digits.
9 CONTEXT: JSON data, line 1: "\u"
10 SELECT '"\u00"'::json; -- ERROR, incomplete escape
11 ERROR: invalid input syntax for type json
12 LINE 1: SELECT '"\u00"'::json;
14 DETAIL: "\u" must be followed by four hexadecimal digits.
15 CONTEXT: JSON data, line 1: "\u00"
16 SELECT '"\u000g"'::json; -- ERROR, g is not a hex digit
17 ERROR: invalid input syntax for type json
18 LINE 1: SELECT '"\u000g"'::json;
20 DETAIL: "\u" must be followed by four hexadecimal digits.
21 CONTEXT: JSON data, line 1: "\u000g...
22 SELECT '"\u0000"'::json; -- OK, legal escape
28 SELECT '"\uaBcD"'::json; -- OK, uppercase and lower case both OK
34 -- handling of unicode surrogate pairs
35 select json '{ "a": "\ud83d\ude04\ud83d\udc36" }' -> 'a' as correct_in_utf8;
37 ----------------------------
38 "\ud83d\ude04\ud83d\udc36"
41 select json '{ "a": "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row
42 ERROR: invalid input syntax for type json
43 DETAIL: Unicode high surrogate must not follow a high surrogate.
44 CONTEXT: JSON data, line 1: { "a":...
45 select json '{ "a": "\ude04\ud83d" }' -> 'a'; -- surrogates in wrong order
46 ERROR: invalid input syntax for type json
47 DETAIL: Unicode low surrogate must follow a high surrogate.
48 CONTEXT: JSON data, line 1: { "a":...
49 select json '{ "a": "\ud83dX" }' -> 'a'; -- orphan high surrogate
50 ERROR: invalid input syntax for type json
51 DETAIL: Unicode low surrogate must follow a high surrogate.
52 CONTEXT: JSON data, line 1: { "a":...
53 select json '{ "a": "\ude04X" }' -> 'a'; -- orphan low surrogate
54 ERROR: invalid input syntax for type json
55 DETAIL: Unicode low surrogate must follow a high surrogate.
56 CONTEXT: JSON data, line 1: { "a":...
57 --handling of simple unicode escapes
58 select json '{ "a": "the Copyright \u00a9 sign" }' as correct_in_utf8;
60 ---------------------------------------
61 { "a": "the Copyright \u00a9 sign" }
64 select json '{ "a": "dollar \u0024 character" }' as correct_everywhere;
66 -------------------------------------
67 { "a": "dollar \u0024 character" }
70 select json '{ "a": "dollar \\u0024 character" }' as not_an_escape;
72 --------------------------------------
73 { "a": "dollar \\u0024 character" }
76 select json '{ "a": "null \u0000 escape" }' as not_unescaped;
78 --------------------------------
79 { "a": "null \u0000 escape" }
82 select json '{ "a": "null \\u0000 escape" }' as not_an_escape;
84 ---------------------------------
85 { "a": "null \\u0000 escape" }
88 select json '{ "a": "the Copyright \u00a9 sign" }' ->> 'a' as correct_in_utf8;
90 ----------------------
94 select json '{ "a": "dollar \u0024 character" }' ->> 'a' as correct_everywhere;
100 select json '{ "a": "dollar \\u0024 character" }' ->> 'a' as not_an_escape;
102 -------------------------
103 dollar \u0024 character
106 select json '{ "a": "null \u0000 escape" }' ->> 'a' as fails;
107 ERROR: unsupported Unicode escape sequence
108 DETAIL: \u0000 cannot be converted to text.
109 CONTEXT: JSON data, line 1: { "a":...
110 select json '{ "a": "null \\u0000 escape" }' ->> 'a' as not_an_escape;
117 -- basic unicode input
118 SELECT '"\u"'::jsonb; -- ERROR, incomplete escape
119 ERROR: invalid input syntax for type json
120 LINE 1: SELECT '"\u"'::jsonb;
122 DETAIL: "\u" must be followed by four hexadecimal digits.
123 CONTEXT: JSON data, line 1: "\u"
124 SELECT '"\u00"'::jsonb; -- ERROR, incomplete escape
125 ERROR: invalid input syntax for type json
126 LINE 1: SELECT '"\u00"'::jsonb;
128 DETAIL: "\u" must be followed by four hexadecimal digits.
129 CONTEXT: JSON data, line 1: "\u00"
130 SELECT '"\u000g"'::jsonb; -- ERROR, g is not a hex digit
131 ERROR: invalid input syntax for type json
132 LINE 1: SELECT '"\u000g"'::jsonb;
134 DETAIL: "\u" must be followed by four hexadecimal digits.
135 CONTEXT: JSON data, line 1: "\u000g...
136 SELECT '"\u0045"'::jsonb; -- OK, legal escape
142 SELECT '"\u0000"'::jsonb; -- ERROR, we don't support U+0000
143 ERROR: unsupported Unicode escape sequence
144 LINE 1: SELECT '"\u0000"'::jsonb;
146 DETAIL: \u0000 cannot be converted to text.
147 CONTEXT: JSON data, line 1: ...
148 -- use octet_length here so we don't get an odd unicode char in the
150 SELECT octet_length('"\uaBcD"'::jsonb::text); -- OK, uppercase and lower case both OK
156 -- handling of unicode surrogate pairs
157 SELECT octet_length((jsonb '{ "a": "\ud83d\ude04\ud83d\udc36" }' -> 'a')::text) AS correct_in_utf8;
163 SELECT jsonb '{ "a": "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row
164 ERROR: invalid input syntax for type json
165 LINE 1: SELECT jsonb '{ "a": "\ud83d\ud83d" }' -> 'a';
167 DETAIL: Unicode high surrogate must not follow a high surrogate.
168 CONTEXT: JSON data, line 1: { "a":...
169 SELECT jsonb '{ "a": "\ude04\ud83d" }' -> 'a'; -- surrogates in wrong order
170 ERROR: invalid input syntax for type json
171 LINE 1: SELECT jsonb '{ "a": "\ude04\ud83d" }' -> 'a';
173 DETAIL: Unicode low surrogate must follow a high surrogate.
174 CONTEXT: JSON data, line 1: { "a":...
175 SELECT jsonb '{ "a": "\ud83dX" }' -> 'a'; -- orphan high surrogate
176 ERROR: invalid input syntax for type json
177 LINE 1: SELECT jsonb '{ "a": "\ud83dX" }' -> 'a';
179 DETAIL: Unicode low surrogate must follow a high surrogate.
180 CONTEXT: JSON data, line 1: { "a":...
181 SELECT jsonb '{ "a": "\ude04X" }' -> 'a'; -- orphan low surrogate
182 ERROR: invalid input syntax for type json
183 LINE 1: SELECT jsonb '{ "a": "\ude04X" }' -> 'a';
185 DETAIL: Unicode low surrogate must follow a high surrogate.
186 CONTEXT: JSON data, line 1: { "a":...
187 -- handling of simple unicode escapes
188 SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' as correct_in_utf8;
190 -------------------------------
191 {"a": "the Copyright © sign"}
194 SELECT jsonb '{ "a": "dollar \u0024 character" }' as correct_everywhere;
196 -----------------------------
197 {"a": "dollar $ character"}
200 SELECT jsonb '{ "a": "dollar \\u0024 character" }' as not_an_escape;
202 -----------------------------------
203 {"a": "dollar \\u0024 character"}
206 SELECT jsonb '{ "a": "null \u0000 escape" }' as fails;
207 ERROR: unsupported Unicode escape sequence
208 LINE 1: SELECT jsonb '{ "a": "null \u0000 escape" }' as fails;
210 DETAIL: \u0000 cannot be converted to text.
211 CONTEXT: JSON data, line 1: { "a":...
212 SELECT jsonb '{ "a": "null \\u0000 escape" }' as not_an_escape;
214 ------------------------------
215 {"a": "null \\u0000 escape"}
218 SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' ->> 'a' as correct_in_utf8;
220 ----------------------
224 SELECT jsonb '{ "a": "dollar \u0024 character" }' ->> 'a' as correct_everywhere;
230 SELECT jsonb '{ "a": "dollar \\u0024 character" }' ->> 'a' as not_an_escape;
232 -------------------------
233 dollar \u0024 character
236 SELECT jsonb '{ "a": "null \u0000 escape" }' ->> 'a' as fails;
237 ERROR: unsupported Unicode escape sequence
238 LINE 1: SELECT jsonb '{ "a": "null \u0000 escape" }' ->> 'a' as fai...
240 DETAIL: \u0000 cannot be converted to text.
241 CONTEXT: JSON data, line 1: { "a":...
242 SELECT jsonb '{ "a": "null \\u0000 escape" }' ->> 'a' as not_an_escape;