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: conversion between UTF8 and SQL_ASCII is not supported
52 select json '{ "a": "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row
53 ERROR: invalid input syntax for type json
54 DETAIL: Unicode high surrogate must not follow a high surrogate.
55 CONTEXT: JSON data, line 1: { "a":...
56 select json '{ "a": "\ude04\ud83d" }' -> 'a'; -- surrogates in wrong order
57 ERROR: invalid input syntax for type json
58 DETAIL: Unicode low surrogate must follow a high surrogate.
59 CONTEXT: JSON data, line 1: { "a":...
60 select json '{ "a": "\ud83dX" }' -> 'a'; -- orphan high surrogate
61 ERROR: invalid input syntax for type json
62 DETAIL: Unicode low surrogate must follow a high surrogate.
63 CONTEXT: JSON data, line 1: { "a":...
64 select json '{ "a": "\ude04X" }' -> 'a'; -- orphan low surrogate
65 ERROR: invalid input syntax for type json
66 DETAIL: Unicode low surrogate must follow a high surrogate.
67 CONTEXT: JSON data, line 1: { "a":...
68 --handling of simple unicode escapes
69 select json '{ "a": "the Copyright \u00a9 sign" }' as correct_in_utf8;
71 ---------------------------------------
72 { "a": "the Copyright \u00a9 sign" }
75 select json '{ "a": "dollar \u0024 character" }' as correct_everywhere;
77 -------------------------------------
78 { "a": "dollar \u0024 character" }
81 select json '{ "a": "dollar \\u0024 character" }' as not_an_escape;
83 --------------------------------------
84 { "a": "dollar \\u0024 character" }
87 select json '{ "a": "null \u0000 escape" }' as not_unescaped;
89 --------------------------------
90 { "a": "null \u0000 escape" }
93 select json '{ "a": "null \\u0000 escape" }' as not_an_escape;
95 ---------------------------------
96 { "a": "null \\u0000 escape" }
99 select json '{ "a": "the Copyright \u00a9 sign" }' ->> 'a' as correct_in_utf8;
100 ERROR: conversion between UTF8 and SQL_ASCII is not supported
101 select json '{ "a": "dollar \u0024 character" }' ->> 'a' as correct_everywhere;
107 select json '{ "a": "dollar \\u0024 character" }' ->> 'a' as not_an_escape;
109 -------------------------
110 dollar \u0024 character
113 select json '{ "a": "null \u0000 escape" }' ->> 'a' as fails;
114 ERROR: unsupported Unicode escape sequence
115 DETAIL: \u0000 cannot be converted to text.
116 CONTEXT: JSON data, line 1: { "a":...
117 select json '{ "a": "null \\u0000 escape" }' ->> 'a' as not_an_escape;
124 -- basic unicode input
125 SELECT '"\u"'::jsonb; -- ERROR, incomplete escape
126 ERROR: invalid input syntax for type json
127 LINE 1: SELECT '"\u"'::jsonb;
129 DETAIL: "\u" must be followed by four hexadecimal digits.
130 CONTEXT: JSON data, line 1: "\u"
131 SELECT '"\u00"'::jsonb; -- ERROR, incomplete escape
132 ERROR: invalid input syntax for type json
133 LINE 1: SELECT '"\u00"'::jsonb;
135 DETAIL: "\u" must be followed by four hexadecimal digits.
136 CONTEXT: JSON data, line 1: "\u00"
137 SELECT '"\u000g"'::jsonb; -- ERROR, g is not a hex digit
138 ERROR: invalid input syntax for type json
139 LINE 1: SELECT '"\u000g"'::jsonb;
141 DETAIL: "\u" must be followed by four hexadecimal digits.
142 CONTEXT: JSON data, line 1: "\u000g...
143 SELECT '"\u0045"'::jsonb; -- OK, legal escape
149 SELECT '"\u0000"'::jsonb; -- ERROR, we don't support U+0000
150 ERROR: unsupported Unicode escape sequence
151 LINE 1: SELECT '"\u0000"'::jsonb;
153 DETAIL: \u0000 cannot be converted to text.
154 CONTEXT: JSON data, line 1: ...
155 -- use octet_length here so we don't get an odd unicode char in the
157 SELECT octet_length('"\uaBcD"'::jsonb::text); -- OK, uppercase and lower case both OK
158 ERROR: conversion between UTF8 and SQL_ASCII is not supported
159 LINE 1: SELECT octet_length('"\uaBcD"'::jsonb::text);
161 -- handling of unicode surrogate pairs
162 SELECT octet_length((jsonb '{ "a": "\ud83d\ude04\ud83d\udc36" }' -> 'a')::text) AS correct_in_utf8;
163 ERROR: conversion between UTF8 and SQL_ASCII is not supported
164 LINE 1: SELECT octet_length((jsonb '{ "a": "\ud83d\ude04\ud83d\udc3...
166 SELECT jsonb '{ "a": "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row
167 ERROR: invalid input syntax for type json
168 LINE 1: SELECT jsonb '{ "a": "\ud83d\ud83d" }' -> 'a';
170 DETAIL: Unicode high surrogate must not follow a high surrogate.
171 CONTEXT: JSON data, line 1: { "a":...
172 SELECT jsonb '{ "a": "\ude04\ud83d" }' -> 'a'; -- surrogates in wrong order
173 ERROR: invalid input syntax for type json
174 LINE 1: SELECT jsonb '{ "a": "\ude04\ud83d" }' -> 'a';
176 DETAIL: Unicode low surrogate must follow a high surrogate.
177 CONTEXT: JSON data, line 1: { "a":...
178 SELECT jsonb '{ "a": "\ud83dX" }' -> 'a'; -- orphan high surrogate
179 ERROR: invalid input syntax for type json
180 LINE 1: SELECT jsonb '{ "a": "\ud83dX" }' -> 'a';
182 DETAIL: Unicode low surrogate must follow a high surrogate.
183 CONTEXT: JSON data, line 1: { "a":...
184 SELECT jsonb '{ "a": "\ude04X" }' -> 'a'; -- orphan low surrogate
185 ERROR: invalid input syntax for type json
186 LINE 1: SELECT jsonb '{ "a": "\ude04X" }' -> 'a';
188 DETAIL: Unicode low surrogate must follow a high surrogate.
189 CONTEXT: JSON data, line 1: { "a":...
190 -- handling of simple unicode escapes
191 SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' as correct_in_utf8;
192 ERROR: conversion between UTF8 and SQL_ASCII is not supported
193 LINE 1: SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' as corr...
195 SELECT jsonb '{ "a": "dollar \u0024 character" }' as correct_everywhere;
197 -----------------------------
198 {"a": "dollar $ character"}
201 SELECT jsonb '{ "a": "dollar \\u0024 character" }' as not_an_escape;
203 -----------------------------------
204 {"a": "dollar \\u0024 character"}
207 SELECT jsonb '{ "a": "null \u0000 escape" }' as fails;
208 ERROR: unsupported Unicode escape sequence
209 LINE 1: SELECT jsonb '{ "a": "null \u0000 escape" }' as fails;
211 DETAIL: \u0000 cannot be converted to text.
212 CONTEXT: JSON data, line 1: { "a":...
213 SELECT jsonb '{ "a": "null \\u0000 escape" }' as not_an_escape;
215 ------------------------------
216 {"a": "null \\u0000 escape"}
219 SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' ->> 'a' as correct_in_utf8;
220 ERROR: conversion between UTF8 and SQL_ASCII is not supported
221 LINE 1: SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' ->> 'a'...
223 SELECT jsonb '{ "a": "dollar \u0024 character" }' ->> 'a' as correct_everywhere;
229 SELECT jsonb '{ "a": "dollar \\u0024 character" }' ->> 'a' as not_an_escape;
231 -------------------------
232 dollar \u0024 character
235 SELECT jsonb '{ "a": "null \u0000 escape" }' ->> 'a' as fails;
236 ERROR: unsupported Unicode escape sequence
237 LINE 1: SELECT jsonb '{ "a": "null \u0000 escape" }' ->> 'a' as fai...
239 DETAIL: \u0000 cannot be converted to text.
240 CONTEXT: JSON data, line 1: { "a":...
241 SELECT jsonb '{ "a": "null \\u0000 escape" }' ->> 'a' as not_an_escape;