]> granicus.if.org Git - postgresql/blob - src/test/regress/expected/json.out
Fix bogus handling of control characters in json_lex_string().
[postgresql] / src / test / regress / expected / json.out
1 -- Strings.
2 SELECT '""'::json;                              -- OK.
3  json 
4 ------
5  ""
6 (1 row)
7
8 SELECT $$''$$::json;                    -- ERROR, single quotes are not allowed
9 ERROR:  invalid input syntax for type json
10 LINE 1: SELECT $$''$$::json;
11                ^
12 DETAIL:  line 1: Token "'" is invalid.
13 SELECT '"abc"'::json;                   -- OK
14  json  
15 -------
16  "abc"
17 (1 row)
18
19 SELECT '"abc'::json;                    -- ERROR, quotes not closed
20 ERROR:  invalid input syntax for type json
21 LINE 1: SELECT '"abc'::json;
22                ^
23 DETAIL:  line 1: Token ""abc" is invalid.
24 SELECT '"abc
25 def"'::json;                                    -- ERROR, unescaped newline in string constant
26 ERROR:  invalid input syntax for type json
27 LINE 1: SELECT '"abc
28                ^
29 DETAIL:  line 1: Character with value "0x0a" must be escaped.
30 SELECT '"\n\"\\"'::json;                -- OK, legal escapes
31    json   
32 ----------
33  "\n\"\\"
34 (1 row)
35
36 SELECT '"\v"'::json;                    -- ERROR, not a valid JSON escape
37 ERROR:  invalid input syntax for type json
38 LINE 1: SELECT '"\v"'::json;
39                ^
40 DETAIL:  line 1: Invalid escape "\v".
41 SELECT '"\u"'::json;                    -- ERROR, incomplete escape
42 ERROR:  invalid input syntax for type json
43 LINE 1: SELECT '"\u"'::json;
44                ^
45 DETAIL:  line 1: "\u" must be followed by four hexadecimal digits.
46 SELECT '"\u00"'::json;                  -- ERROR, incomplete escape
47 ERROR:  invalid input syntax for type json
48 LINE 1: SELECT '"\u00"'::json;
49                ^
50 DETAIL:  line 1: "\u" must be followed by four hexadecimal digits.
51 SELECT '"\u000g"'::json;                -- ERROR, g is not a hex digit
52 ERROR:  invalid input syntax for type json
53 LINE 1: SELECT '"\u000g"'::json;
54                ^
55 DETAIL:  line 1: "\u" must be followed by four hexadecimal digits.
56 SELECT '"\u0000"'::json;                -- OK, legal escape
57    json   
58 ----------
59  "\u0000"
60 (1 row)
61
62 SELECT '"\uaBcD"'::json;                -- OK, uppercase and lower case both OK
63    json   
64 ----------
65  "\uaBcD"
66 (1 row)
67
68 -- Numbers.
69 SELECT '1'::json;                               -- OK
70  json 
71 ------
72  1
73 (1 row)
74
75 SELECT '0'::json;                               -- OK
76  json 
77 ------
78  0
79 (1 row)
80
81 SELECT '01'::json;                              -- ERROR, not valid according to JSON spec
82 ERROR:  invalid input syntax for type json
83 LINE 1: SELECT '01'::json;
84                ^
85 DETAIL:  line 1: Token "01" is invalid.
86 SELECT '0.1'::json;                             -- OK
87  json 
88 ------
89  0.1
90 (1 row)
91
92 SELECT '9223372036854775808'::json;     -- OK, even though it's too large for int8
93         json         
94 ---------------------
95  9223372036854775808
96 (1 row)
97
98 SELECT '1e100'::json;                   -- OK
99  json  
100 -------
101  1e100
102 (1 row)
103
104 SELECT '1.3e100'::json;                 -- OK
105   json   
106 ---------
107  1.3e100
108 (1 row)
109
110 SELECT '1f2'::json;                             -- ERROR
111 ERROR:  invalid input syntax for type json
112 LINE 1: SELECT '1f2'::json;
113                ^
114 DETAIL:  line 1: Token "1f2" is invalid.
115 SELECT '0.x1'::json;                    -- ERROR
116 ERROR:  invalid input syntax for type json
117 LINE 1: SELECT '0.x1'::json;
118                ^
119 DETAIL:  line 1: Token "0.x1" is invalid.
120 SELECT '1.3ex100'::json;                -- ERROR
121 ERROR:  invalid input syntax for type json
122 LINE 1: SELECT '1.3ex100'::json;
123                ^
124 DETAIL:  line 1: Token "1.3ex100" is invalid.
125 -- Arrays.
126 SELECT '[]'::json;                              -- OK
127  json 
128 ------
129  []
130 (1 row)
131
132 SELECT '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'::json;  -- OK
133                                                                                                    json                                                                                                   
134 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
135  [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
136 (1 row)
137
138 SELECT '[1,2]'::json;                   -- OK
139  json  
140 -------
141  [1,2]
142 (1 row)
143
144 SELECT '[1,2,]'::json;                  -- ERROR, trailing comma
145 ERROR:  invalid input syntax for type json: "[1,2,]"
146 LINE 1: SELECT '[1,2,]'::json;
147                ^
148 DETAIL:  line 1: Expected string, number, object, array, true, false, or null, but found "]".
149 SELECT '[1,2'::json;                    -- ERROR, no closing bracket
150 ERROR:  invalid input syntax for type json: "[1,2"
151 LINE 1: SELECT '[1,2'::json;
152                ^
153 DETAIL:  The input string ended unexpectedly.
154 SELECT '[1,[2]'::json;                  -- ERROR, no closing bracket
155 ERROR:  invalid input syntax for type json: "[1,[2]"
156 LINE 1: SELECT '[1,[2]'::json;
157                ^
158 DETAIL:  The input string ended unexpectedly.
159 -- Objects.
160 SELECT '{}'::json;                              -- OK
161  json 
162 ------
163  {}
164 (1 row)
165
166 SELECT '{"abc"}'::json;                 -- ERROR, no value
167 ERROR:  invalid input syntax for type json: "{"abc"}"
168 LINE 1: SELECT '{"abc"}'::json;
169                ^
170 DETAIL:  line 1: Expected ":", but found "}".
171 SELECT '{"abc":1}'::json;               -- OK
172    json    
173 -----------
174  {"abc":1}
175 (1 row)
176
177 SELECT '{1:"abc"}'::json;               -- ERROR, keys must be strings
178 ERROR:  invalid input syntax for type json: "{1:"abc"}"
179 LINE 1: SELECT '{1:"abc"}'::json;
180                ^
181 DETAIL:  line 1: Expected string or "}", but found "1".
182 SELECT '{"abc",1}'::json;               -- ERROR, wrong separator
183 ERROR:  invalid input syntax for type json: "{"abc",1}"
184 LINE 1: SELECT '{"abc",1}'::json;
185                ^
186 DETAIL:  line 1: Expected ":", but found ",".
187 SELECT '{"abc"=1}'::json;               -- ERROR, totally wrong separator
188 ERROR:  invalid input syntax for type json
189 LINE 1: SELECT '{"abc"=1}'::json;
190                ^
191 DETAIL:  line 1: Token "=" is invalid.
192 SELECT '{"abc"::1}'::json;              -- ERROR, another wrong separator
193 ERROR:  invalid input syntax for type json: "{"abc"::1}"
194 LINE 1: SELECT '{"abc"::1}'::json;
195                ^
196 DETAIL:  line 1: Expected string, number, object, array, true, false, or null, but found ":".
197 SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::json; -- OK
198                           json                           
199 ---------------------------------------------------------
200  {"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}
201 (1 row)
202
203 SELECT '{"abc":1:2}'::json;             -- ERROR, colon in wrong spot
204 ERROR:  invalid input syntax for type json: "{"abc":1:2}"
205 LINE 1: SELECT '{"abc":1:2}'::json;
206                ^
207 DETAIL:  line 1: Expected "," or "}", but found ":".
208 SELECT '{"abc":1,3}'::json;             -- ERROR, no value
209 ERROR:  invalid input syntax for type json: "{"abc":1,3}"
210 LINE 1: SELECT '{"abc":1,3}'::json;
211                ^
212 DETAIL:  line 1: Expected string, but found "3".
213 -- Miscellaneous stuff.
214 SELECT 'true'::json;                    -- OK
215  json 
216 ------
217  true
218 (1 row)
219
220 SELECT 'false'::json;                   -- OK
221  json  
222 -------
223  false
224 (1 row)
225
226 SELECT 'null'::json;                    -- OK
227  json 
228 ------
229  null
230 (1 row)
231
232 SELECT ' true '::json;                  -- OK, even with extra whitespace
233   json  
234 --------
235   true 
236 (1 row)
237
238 SELECT 'true false'::json;              -- ERROR, too many values
239 ERROR:  invalid input syntax for type json: "true false"
240 LINE 1: SELECT 'true false'::json;
241                ^
242 DETAIL:  line 1: Expected end of input, but found "false".
243 SELECT 'true, false'::json;             -- ERROR, too many values
244 ERROR:  invalid input syntax for type json: "true, false"
245 LINE 1: SELECT 'true, false'::json;
246                ^
247 DETAIL:  line 1: Expected end of input, but found ",".
248 SELECT 'truf'::json;                    -- ERROR, not a keyword
249 ERROR:  invalid input syntax for type json
250 LINE 1: SELECT 'truf'::json;
251                ^
252 DETAIL:  line 1: Token "truf" is invalid.
253 SELECT 'trues'::json;                   -- ERROR, not a keyword
254 ERROR:  invalid input syntax for type json
255 LINE 1: SELECT 'trues'::json;
256                ^
257 DETAIL:  line 1: Token "trues" is invalid.
258 SELECT ''::json;                                -- ERROR, no value
259 ERROR:  invalid input syntax for type json: ""
260 LINE 1: SELECT ''::json;
261                ^
262 DETAIL:  The input string ended unexpectedly.
263 SELECT '    '::json;                    -- ERROR, no value
264 ERROR:  invalid input syntax for type json: "    "
265 LINE 1: SELECT '    '::json;
266                ^
267 DETAIL:  The input string ended unexpectedly.
268 --constructors
269 -- array_to_json
270 SELECT array_to_json(array(select 1 as a));
271  array_to_json 
272 ---------------
273  [1]
274 (1 row)
275
276 SELECT array_to_json(array_agg(q),false) from (select x as b, x * 2 as c from generate_series(1,3) x) q;
277                 array_to_json                
278 ---------------------------------------------
279  [{"b":1,"c":2},{"b":2,"c":4},{"b":3,"c":6}]
280 (1 row)
281
282 SELECT array_to_json(array_agg(q),true) from (select x as b, x * 2 as c from generate_series(1,3) x) q;
283   array_to_json  
284 -----------------
285  [{"b":1,"c":2},+
286   {"b":2,"c":4},+
287   {"b":3,"c":6}]
288 (1 row)
289
290 SELECT array_to_json(array_agg(q),false)
291   FROM ( SELECT $$a$$ || x AS b, y AS c,
292                ARRAY[ROW(x.*,ARRAY[1,2,3]),
293                ROW(y.*,ARRAY[4,5,6])] AS z
294          FROM generate_series(1,2) x,
295               generate_series(4,5) y) q;
296                                                                                                                                  array_to_json                                                                                                                                 
297 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
298  [{"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]},{"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]},{"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]},{"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}]
299 (1 row)
300
301 SELECT array_to_json(array_agg(x),false) from generate_series(5,10) x;
302  array_to_json  
303 ----------------
304  [5,6,7,8,9,10]
305 (1 row)
306
307 SELECT array_to_json('{{1,5},{99,100}}'::int[]);
308   array_to_json   
309 ------------------
310  [[1,5],[99,100]]
311 (1 row)
312
313 -- row_to_json
314 SELECT row_to_json(row(1,'foo'));
315      row_to_json     
316 ---------------------
317  {"f1":1,"f2":"foo"}
318 (1 row)
319
320 SELECT row_to_json(q)
321 FROM (SELECT $$a$$ || x AS b,
322          y AS c,
323          ARRAY[ROW(x.*,ARRAY[1,2,3]),
324                ROW(y.*,ARRAY[4,5,6])] AS z
325       FROM generate_series(1,2) x,
326            generate_series(4,5) y) q;
327                             row_to_json                             
328 --------------------------------------------------------------------
329  {"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
330  {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
331  {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
332  {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
333 (4 rows)
334
335 SELECT row_to_json(q,true)
336 FROM (SELECT $$a$$ || x AS b,
337          y AS c,
338          ARRAY[ROW(x.*,ARRAY[1,2,3]),
339                ROW(y.*,ARRAY[4,5,6])] AS z
340       FROM generate_series(1,2) x,
341            generate_series(4,5) y) q;
342                      row_to_json                     
343 -----------------------------------------------------
344  {"b":"a1",                                         +
345   "c":4,                                            +
346   "z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
347  {"b":"a1",                                         +
348   "c":5,                                            +
349   "z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
350  {"b":"a2",                                         +
351   "c":4,                                            +
352   "z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
353  {"b":"a2",                                         +
354   "c":5,                                            +
355   "z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
356 (4 rows)
357
358 CREATE TEMP TABLE rows AS
359 SELECT x, 'txt' || x as y
360 FROM generate_series(1,3) AS x;
361 SELECT row_to_json(q,true)
362 FROM rows q;
363  row_to_json  
364 --------------
365  {"x":1,     +
366   "y":"txt1"}
367  {"x":2,     +
368   "y":"txt2"}
369  {"x":3,     +
370   "y":"txt3"}
371 (3 rows)
372
373 SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),false);
374       row_to_json      
375 -----------------------
376  {"f1":[5,6,7,8,9,10]}
377 (1 row)
378
379 -- non-numeric output
380 SELECT row_to_json(q)
381 FROM (SELECT 'NaN'::float8 AS "float8field") q;
382       row_to_json      
383 -----------------------
384  {"float8field":"NaN"}
385 (1 row)
386
387 SELECT row_to_json(q)
388 FROM (SELECT 'Infinity'::float8 AS "float8field") q;
389         row_to_json         
390 ----------------------------
391  {"float8field":"Infinity"}
392 (1 row)
393
394 SELECT row_to_json(q)
395 FROM (SELECT '-Infinity'::float8 AS "float8field") q;
396          row_to_json         
397 -----------------------------
398  {"float8field":"-Infinity"}
399 (1 row)
400
401 -- json input
402 SELECT row_to_json(q)
403 FROM (SELECT '{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}'::json AS "jsonfield") q;
404                            row_to_json                            
405 ------------------------------------------------------------------
406  {"jsonfield":{"a":1,"b": [2,3,4,"d","e","f"],"c":{"p":1,"q":2}}}
407 (1 row)
408