2 -- encoding-sensitive tests for json and jsonb
4 -- We provide expected-results files for UTF8 (json_encoding.out)
5 -- and for SQL_ASCII (json_encoding_1.out). Skip otherwise.
6 SELECT getdatabaseencoding() NOT IN ('UTF8', 'SQL_ASCII')
11 SELECT getdatabaseencoding(); -- just to label the results files
18 -- basic unicode input
19 SELECT '"\u"'::json; -- ERROR, incomplete escape
20 ERROR: invalid input syntax for type json
21 LINE 1: SELECT '"\u"'::json;
23 DETAIL: "\u" must be followed by four hexadecimal digits.
24 CONTEXT: JSON data, line 1: "\u"
25 SELECT '"\u00"'::json; -- ERROR, incomplete escape
26 ERROR: invalid input syntax for type json
27 LINE 1: SELECT '"\u00"'::json;
29 DETAIL: "\u" must be followed by four hexadecimal digits.
30 CONTEXT: JSON data, line 1: "\u00"
31 SELECT '"\u000g"'::json; -- ERROR, g is not a hex digit
32 ERROR: invalid input syntax for type json
33 LINE 1: SELECT '"\u000g"'::json;
35 DETAIL: "\u" must be followed by four hexadecimal digits.
36 CONTEXT: JSON data, line 1: "\u000g...
37 SELECT '"\u0000"'::json; -- OK, legal escape
43 SELECT '"\uaBcD"'::json; -- OK, uppercase and lower case both OK
49 -- handling of unicode surrogate pairs
50 select json '{ "a": "\ud83d\ude04\ud83d\udc36" }' -> 'a' as correct_in_utf8;
51 ERROR: unsupported Unicode escape sequence
52 DETAIL: Unicode escape value could not be translated to the server's encoding SQL_ASCII.
53 CONTEXT: JSON data, line 1: { "a": "\ud83d\ude04...
54 select json '{ "a": "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row
55 ERROR: invalid input syntax for type json
56 DETAIL: Unicode high surrogate must not follow a high surrogate.
57 CONTEXT: JSON data, line 1: { "a": "\ud83d\ud83d...
58 select json '{ "a": "\ude04\ud83d" }' -> 'a'; -- surrogates in wrong order
59 ERROR: invalid input syntax for type json
60 DETAIL: Unicode low surrogate must follow a high surrogate.
61 CONTEXT: JSON data, line 1: { "a": "\ude04...
62 select json '{ "a": "\ud83dX" }' -> 'a'; -- orphan high surrogate
63 ERROR: invalid input syntax for type json
64 DETAIL: Unicode low surrogate must follow a high surrogate.
65 CONTEXT: JSON data, line 1: { "a": "\ud83dX...
66 select json '{ "a": "\ude04X" }' -> 'a'; -- orphan low surrogate
67 ERROR: invalid input syntax for type json
68 DETAIL: Unicode low surrogate must follow a high surrogate.
69 CONTEXT: JSON data, line 1: { "a": "\ude04...
70 --handling of simple unicode escapes
71 select json '{ "a": "the Copyright \u00a9 sign" }' as correct_in_utf8;
73 ---------------------------------------
74 { "a": "the Copyright \u00a9 sign" }
77 select json '{ "a": "dollar \u0024 character" }' as correct_everywhere;
79 -------------------------------------
80 { "a": "dollar \u0024 character" }
83 select json '{ "a": "dollar \\u0024 character" }' as not_an_escape;
85 --------------------------------------
86 { "a": "dollar \\u0024 character" }
89 select json '{ "a": "null \u0000 escape" }' as not_unescaped;
91 --------------------------------
92 { "a": "null \u0000 escape" }
95 select json '{ "a": "null \\u0000 escape" }' as not_an_escape;
97 ---------------------------------
98 { "a": "null \\u0000 escape" }
101 select json '{ "a": "the Copyright \u00a9 sign" }' ->> 'a' as correct_in_utf8;
102 ERROR: unsupported Unicode escape sequence
103 DETAIL: Unicode escape value could not be translated to the server's encoding SQL_ASCII.
104 CONTEXT: JSON data, line 1: { "a": "the Copyright \u00a9...
105 select json '{ "a": "dollar \u0024 character" }' ->> 'a' as correct_everywhere;
111 select json '{ "a": "dollar \\u0024 character" }' ->> 'a' as not_an_escape;
113 -------------------------
114 dollar \u0024 character
117 select json '{ "a": "null \u0000 escape" }' ->> 'a' as fails;
118 ERROR: unsupported Unicode escape sequence
119 DETAIL: \u0000 cannot be converted to text.
120 CONTEXT: JSON data, line 1: { "a": "null \u0000...
121 select json '{ "a": "null \\u0000 escape" }' ->> 'a' as not_an_escape;
128 -- basic unicode input
129 SELECT '"\u"'::jsonb; -- ERROR, incomplete escape
130 ERROR: invalid input syntax for type json
131 LINE 1: SELECT '"\u"'::jsonb;
133 DETAIL: "\u" must be followed by four hexadecimal digits.
134 CONTEXT: JSON data, line 1: "\u"
135 SELECT '"\u00"'::jsonb; -- ERROR, incomplete escape
136 ERROR: invalid input syntax for type json
137 LINE 1: SELECT '"\u00"'::jsonb;
139 DETAIL: "\u" must be followed by four hexadecimal digits.
140 CONTEXT: JSON data, line 1: "\u00"
141 SELECT '"\u000g"'::jsonb; -- ERROR, g is not a hex digit
142 ERROR: invalid input syntax for type json
143 LINE 1: SELECT '"\u000g"'::jsonb;
145 DETAIL: "\u" must be followed by four hexadecimal digits.
146 CONTEXT: JSON data, line 1: "\u000g...
147 SELECT '"\u0045"'::jsonb; -- OK, legal escape
153 SELECT '"\u0000"'::jsonb; -- ERROR, we don't support U+0000
154 ERROR: unsupported Unicode escape sequence
155 LINE 1: SELECT '"\u0000"'::jsonb;
157 DETAIL: \u0000 cannot be converted to text.
158 CONTEXT: JSON data, line 1: "\u0000...
159 -- use octet_length here so we don't get an odd unicode char in the
161 SELECT octet_length('"\uaBcD"'::jsonb::text); -- OK, uppercase and lower case both OK
162 ERROR: unsupported Unicode escape sequence
163 LINE 1: SELECT octet_length('"\uaBcD"'::jsonb::text);
165 DETAIL: Unicode escape value could not be translated to the server's encoding SQL_ASCII.
166 CONTEXT: JSON data, line 1: "\uaBcD...
167 -- handling of unicode surrogate pairs
168 SELECT octet_length((jsonb '{ "a": "\ud83d\ude04\ud83d\udc36" }' -> 'a')::text) AS correct_in_utf8;
169 ERROR: unsupported Unicode escape sequence
170 LINE 1: SELECT octet_length((jsonb '{ "a": "\ud83d\ude04\ud83d\udc3...
172 DETAIL: Unicode escape value could not be translated to the server's encoding SQL_ASCII.
173 CONTEXT: JSON data, line 1: { "a": "\ud83d\ude04...
174 SELECT jsonb '{ "a": "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row
175 ERROR: invalid input syntax for type json
176 LINE 1: SELECT jsonb '{ "a": "\ud83d\ud83d" }' -> 'a';
178 DETAIL: Unicode high surrogate must not follow a high surrogate.
179 CONTEXT: JSON data, line 1: { "a": "\ud83d\ud83d...
180 SELECT jsonb '{ "a": "\ude04\ud83d" }' -> 'a'; -- surrogates in wrong order
181 ERROR: invalid input syntax for type json
182 LINE 1: SELECT jsonb '{ "a": "\ude04\ud83d" }' -> 'a';
184 DETAIL: Unicode low surrogate must follow a high surrogate.
185 CONTEXT: JSON data, line 1: { "a": "\ude04...
186 SELECT jsonb '{ "a": "\ud83dX" }' -> 'a'; -- orphan high surrogate
187 ERROR: invalid input syntax for type json
188 LINE 1: SELECT jsonb '{ "a": "\ud83dX" }' -> 'a';
190 DETAIL: Unicode low surrogate must follow a high surrogate.
191 CONTEXT: JSON data, line 1: { "a": "\ud83dX...
192 SELECT jsonb '{ "a": "\ude04X" }' -> 'a'; -- orphan low surrogate
193 ERROR: invalid input syntax for type json
194 LINE 1: SELECT jsonb '{ "a": "\ude04X" }' -> 'a';
196 DETAIL: Unicode low surrogate must follow a high surrogate.
197 CONTEXT: JSON data, line 1: { "a": "\ude04...
198 -- handling of simple unicode escapes
199 SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' as correct_in_utf8;
200 ERROR: unsupported Unicode escape sequence
201 LINE 1: SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' as corr...
203 DETAIL: Unicode escape value could not be translated to the server's encoding SQL_ASCII.
204 CONTEXT: JSON data, line 1: { "a": "the Copyright \u00a9...
205 SELECT jsonb '{ "a": "dollar \u0024 character" }' as correct_everywhere;
207 -----------------------------
208 {"a": "dollar $ character"}
211 SELECT jsonb '{ "a": "dollar \\u0024 character" }' as not_an_escape;
213 -----------------------------------
214 {"a": "dollar \\u0024 character"}
217 SELECT jsonb '{ "a": "null \u0000 escape" }' as fails;
218 ERROR: unsupported Unicode escape sequence
219 LINE 1: SELECT jsonb '{ "a": "null \u0000 escape" }' as fails;
221 DETAIL: \u0000 cannot be converted to text.
222 CONTEXT: JSON data, line 1: { "a": "null \u0000...
223 SELECT jsonb '{ "a": "null \\u0000 escape" }' as not_an_escape;
225 ------------------------------
226 {"a": "null \\u0000 escape"}
229 SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' ->> 'a' as correct_in_utf8;
230 ERROR: unsupported Unicode escape sequence
231 LINE 1: SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' ->> 'a'...
233 DETAIL: Unicode escape value could not be translated to the server's encoding SQL_ASCII.
234 CONTEXT: JSON data, line 1: { "a": "the Copyright \u00a9...
235 SELECT jsonb '{ "a": "dollar \u0024 character" }' ->> 'a' as correct_everywhere;
241 SELECT jsonb '{ "a": "dollar \\u0024 character" }' ->> 'a' as not_an_escape;
243 -------------------------
244 dollar \u0024 character
247 SELECT jsonb '{ "a": "null \u0000 escape" }' ->> 'a' as fails;
248 ERROR: unsupported Unicode escape sequence
249 LINE 1: SELECT jsonb '{ "a": "null \u0000 escape" }' ->> 'a' as fai...
251 DETAIL: \u0000 cannot be converted to text.
252 CONTEXT: JSON data, line 1: { "a": "null \u0000...
253 SELECT jsonb '{ "a": "null \\u0000 escape" }' ->> 'a' as not_an_escape;
259 -- soft error for input-time failure
260 select * from pg_input_error_info('{ "a": "\ud83d\ude04\ud83d\udc36" }', 'jsonb');
261 message | detail | hint | sql_error_code
262 -------------------------------------+----------------------------------------------------------------------------------+------+----------------
263 unsupported Unicode escape sequence | Unicode escape value could not be translated to the server's encoding SQL_ASCII. | | 22P05