]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/json.c
Further cleanup of JSON-specific error messages.
[postgresql] / src / backend / utils / adt / json.c
1 /*-------------------------------------------------------------------------
2  *
3  * json.c
4  *              JSON data type support.
5  *
6  * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * IDENTIFICATION
10  *        src/backend/utils/adt/json.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres.h"
15
16 #include "access/htup_details.h"
17 #include "access/transam.h"
18 #include "catalog/pg_cast.h"
19 #include "catalog/pg_type.h"
20 #include "executor/spi.h"
21 #include "lib/stringinfo.h"
22 #include "libpq/pqformat.h"
23 #include "mb/pg_wchar.h"
24 #include "miscadmin.h"
25 #include "parser/parse_coerce.h"
26 #include "utils/array.h"
27 #include "utils/builtins.h"
28 #include "utils/datetime.h"
29 #include "utils/lsyscache.h"
30 #include "utils/json.h"
31 #include "utils/jsonapi.h"
32 #include "utils/typcache.h"
33 #include "utils/syscache.h"
34
35 /*
36  * The context of the parser is maintained by the recursive descent
37  * mechanism, but is passed explicitly to the error reporting routine
38  * for better diagnostics.
39  */
40 typedef enum                                    /* contexts of JSON parser */
41 {
42         JSON_PARSE_VALUE,                       /* expecting a value */
43         JSON_PARSE_STRING,                      /* expecting a string (for a field name) */
44         JSON_PARSE_ARRAY_START,         /* saw '[', expecting value or ']' */
45         JSON_PARSE_ARRAY_NEXT,          /* saw array element, expecting ',' or ']' */
46         JSON_PARSE_OBJECT_START,        /* saw '{', expecting label or '}' */
47         JSON_PARSE_OBJECT_LABEL,        /* saw object label, expecting ':' */
48         JSON_PARSE_OBJECT_NEXT,         /* saw object value, expecting ',' or '}' */
49         JSON_PARSE_OBJECT_COMMA,        /* saw object ',', expecting next label */
50         JSON_PARSE_END                          /* saw the end of a document, expect nothing */
51 } JsonParseContext;
52
53 typedef enum                                    /* type categories for datum_to_json */
54 {
55         JSONTYPE_NULL,                          /* null, so we didn't bother to identify */
56         JSONTYPE_BOOL,                          /* boolean (built-in types only) */
57         JSONTYPE_NUMERIC,                       /* numeric (ditto) */
58         JSONTYPE_TIMESTAMP,         /* we use special formatting for timestamp */
59         JSONTYPE_TIMESTAMPTZ,       /* ... and timestamptz */
60         JSONTYPE_JSON,                          /* JSON itself (and JSONB) */
61         JSONTYPE_ARRAY,                         /* array */
62         JSONTYPE_COMPOSITE,                     /* composite */
63         JSONTYPE_CAST,                          /* something with an explicit cast to JSON */
64         JSONTYPE_OTHER                          /* all else */
65 } JsonTypeCategory;
66
67 static inline void json_lex(JsonLexContext *lex);
68 static inline void json_lex_string(JsonLexContext *lex);
69 static inline void json_lex_number(JsonLexContext *lex, char *s, bool *num_err);
70 static inline void parse_scalar(JsonLexContext *lex, JsonSemAction *sem);
71 static void parse_object_field(JsonLexContext *lex, JsonSemAction *sem);
72 static void parse_object(JsonLexContext *lex, JsonSemAction *sem);
73 static void parse_array_element(JsonLexContext *lex, JsonSemAction *sem);
74 static void parse_array(JsonLexContext *lex, JsonSemAction *sem);
75 static void report_parse_error(JsonParseContext ctx, JsonLexContext *lex);
76 static void report_invalid_token(JsonLexContext *lex);
77 static int      report_json_context(JsonLexContext *lex);
78 static char *extract_mb_char(char *s);
79 static void composite_to_json(Datum composite, StringInfo result,
80                                   bool use_line_feeds);
81 static void array_dim_to_json(StringInfo result, int dim, int ndims, int *dims,
82                                   Datum *vals, bool *nulls, int *valcount,
83                                   JsonTypeCategory tcategory, Oid outfuncoid,
84                                   bool use_line_feeds);
85 static void array_to_json_internal(Datum array, StringInfo result,
86                                            bool use_line_feeds);
87 static void json_categorize_type(Oid typoid,
88                                          JsonTypeCategory *tcategory,
89                                          Oid *outfuncoid);
90 static void datum_to_json(Datum val, bool is_null, StringInfo result,
91                           JsonTypeCategory tcategory, Oid outfuncoid,
92                           bool key_scalar);
93 static void add_json(Datum val, bool is_null, StringInfo result,
94                  Oid val_type, bool key_scalar);
95
96 /* the null action object used for pure validation */
97 static JsonSemAction nullSemAction =
98 {
99         NULL, NULL, NULL, NULL, NULL,
100         NULL, NULL, NULL, NULL, NULL
101 };
102
103 /* Recursive Descent parser support routines */
104
105 /*
106  * lex_peek
107  *
108  * what is the current look_ahead token?
109 */
110 static inline JsonTokenType
111 lex_peek(JsonLexContext *lex)
112 {
113         return lex->token_type;
114 }
115
116 /*
117  * lex_accept
118  *
119  * accept the look_ahead token and move the lexer to the next token if the
120  * look_ahead token matches the token parameter. In that case, and if required,
121  * also hand back the de-escaped lexeme.
122  *
123  * returns true if the token matched, false otherwise.
124  */
125 static inline bool
126 lex_accept(JsonLexContext *lex, JsonTokenType token, char **lexeme)
127 {
128         if (lex->token_type == token)
129         {
130                 if (lexeme != NULL)
131                 {
132                         if (lex->token_type == JSON_TOKEN_STRING)
133                         {
134                                 if (lex->strval != NULL)
135                                         *lexeme = pstrdup(lex->strval->data);
136                         }
137                         else
138                         {
139                                 int                     len = (lex->token_terminator - lex->token_start);
140                                 char       *tokstr = palloc(len + 1);
141
142                                 memcpy(tokstr, lex->token_start, len);
143                                 tokstr[len] = '\0';
144                                 *lexeme = tokstr;
145                         }
146                 }
147                 json_lex(lex);
148                 return true;
149         }
150         return false;
151 }
152
153 /*
154  * lex_accept
155  *
156  * move the lexer to the next token if the current look_ahead token matches
157  * the parameter token. Otherwise, report an error.
158  */
159 static inline void
160 lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
161 {
162         if (!lex_accept(lex, token, NULL))
163                 report_parse_error(ctx, lex);;
164 }
165
166 /* chars to consider as part of an alphanumeric token */
167 #define JSON_ALPHANUMERIC_CHAR(c)  \
168         (((c) >= 'a' && (c) <= 'z') || \
169          ((c) >= 'A' && (c) <= 'Z') || \
170          ((c) >= '0' && (c) <= '9') || \
171          (c) == '_' || \
172          IS_HIGHBIT_SET(c))
173
174 /*
175  * Input.
176  */
177 Datum
178 json_in(PG_FUNCTION_ARGS)
179 {
180         char       *json = PG_GETARG_CSTRING(0);
181         text       *result = cstring_to_text(json);
182         JsonLexContext *lex;
183
184         /* validate it */
185         lex = makeJsonLexContext(result, false);
186         pg_parse_json(lex, &nullSemAction);
187
188         /* Internal representation is the same as text, for now */
189         PG_RETURN_TEXT_P(result);
190 }
191
192 /*
193  * Output.
194  */
195 Datum
196 json_out(PG_FUNCTION_ARGS)
197 {
198         /* we needn't detoast because text_to_cstring will handle that */
199         Datum           txt = PG_GETARG_DATUM(0);
200
201         PG_RETURN_CSTRING(TextDatumGetCString(txt));
202 }
203
204 /*
205  * Binary send.
206  */
207 Datum
208 json_send(PG_FUNCTION_ARGS)
209 {
210         text       *t = PG_GETARG_TEXT_PP(0);
211         StringInfoData buf;
212
213         pq_begintypsend(&buf);
214         pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
215         PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
216 }
217
218 /*
219  * Binary receive.
220  */
221 Datum
222 json_recv(PG_FUNCTION_ARGS)
223 {
224         StringInfo      buf = (StringInfo) PG_GETARG_POINTER(0);
225         char       *str;
226         int                     nbytes;
227         JsonLexContext *lex;
228
229         str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
230
231         /* Validate it. */
232         lex = makeJsonLexContextCstringLen(str, nbytes, false);
233         pg_parse_json(lex, &nullSemAction);
234
235         PG_RETURN_TEXT_P(cstring_to_text_with_len(str, nbytes));
236 }
237
238 /*
239  * makeJsonLexContext
240  *
241  * lex constructor, with or without StringInfo object
242  * for de-escaped lexemes.
243  *
244  * Without is better as it makes the processing faster, so only make one
245  * if really required.
246  *
247  * If you already have the json as a text* value, use the first of these
248  * functions, otherwise use  makeJsonLexContextCstringLen().
249  */
250 JsonLexContext *
251 makeJsonLexContext(text *json, bool need_escapes)
252 {
253         return makeJsonLexContextCstringLen(VARDATA(json),
254                                                                                 VARSIZE(json) - VARHDRSZ,
255                                                                                 need_escapes);
256 }
257
258 JsonLexContext *
259 makeJsonLexContextCstringLen(char *json, int len, bool need_escapes)
260 {
261         JsonLexContext *lex = palloc0(sizeof(JsonLexContext));
262
263         lex->input = lex->token_terminator = lex->line_start = json;
264         lex->line_number = 1;
265         lex->input_length = len;
266         if (need_escapes)
267                 lex->strval = makeStringInfo();
268         return lex;
269 }
270
271 /*
272  * pg_parse_json
273  *
274  * Publicly visible entry point for the JSON parser.
275  *
276  * lex is a lexing context, set up for the json to be processed by calling
277  * makeJsonLexContext(). sem is a strucure of function pointers to semantic
278  * action routines to be called at appropriate spots during parsing, and a
279  * pointer to a state object to be passed to those routines.
280  */
281 void
282 pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
283 {
284         JsonTokenType tok;
285
286         /* get the initial token */
287         json_lex(lex);
288
289         tok = lex_peek(lex);
290
291         /* parse by recursive descent */
292         switch (tok)
293         {
294                 case JSON_TOKEN_OBJECT_START:
295                         parse_object(lex, sem);
296                         break;
297                 case JSON_TOKEN_ARRAY_START:
298                         parse_array(lex, sem);
299                         break;
300                 default:
301                         parse_scalar(lex, sem);         /* json can be a bare scalar */
302         }
303
304         lex_expect(JSON_PARSE_END, lex, JSON_TOKEN_END);
305
306 }
307
308 /*
309  *      Recursive Descent parse routines. There is one for each structural
310  *      element in a json document:
311  *        - scalar (string, number, true, false, null)
312  *        - array  ( [ ] )
313  *        - array element
314  *        - object ( { } )
315  *        - object field
316  */
317 static inline void
318 parse_scalar(JsonLexContext *lex, JsonSemAction *sem)
319 {
320         char       *val = NULL;
321         json_scalar_action sfunc = sem->scalar;
322         char      **valaddr;
323         JsonTokenType tok = lex_peek(lex);
324
325         valaddr = sfunc == NULL ? NULL : &val;
326
327         /* a scalar must be a string, a number, true, false, or null */
328         switch (tok)
329         {
330                 case JSON_TOKEN_TRUE:
331                         lex_accept(lex, JSON_TOKEN_TRUE, valaddr);
332                         break;
333                 case JSON_TOKEN_FALSE:
334                         lex_accept(lex, JSON_TOKEN_FALSE, valaddr);
335                         break;
336                 case JSON_TOKEN_NULL:
337                         lex_accept(lex, JSON_TOKEN_NULL, valaddr);
338                         break;
339                 case JSON_TOKEN_NUMBER:
340                         lex_accept(lex, JSON_TOKEN_NUMBER, valaddr);
341                         break;
342                 case JSON_TOKEN_STRING:
343                         lex_accept(lex, JSON_TOKEN_STRING, valaddr);
344                         break;
345                 default:
346                         report_parse_error(JSON_PARSE_VALUE, lex);
347         }
348
349         if (sfunc != NULL)
350                 (*sfunc) (sem->semstate, val, tok);
351 }
352
353 static void
354 parse_object_field(JsonLexContext *lex, JsonSemAction *sem)
355 {
356         /*
357          * An object field is "fieldname" : value where value can be a scalar,
358          * object or array.  Note: in user-facing docs and error messages, we
359          * generally call a field name a "key".
360          */
361
362         char       *fname = NULL;       /* keep compiler quiet */
363         json_ofield_action ostart = sem->object_field_start;
364         json_ofield_action oend = sem->object_field_end;
365         bool            isnull;
366         char      **fnameaddr = NULL;
367         JsonTokenType tok;
368
369         if (ostart != NULL || oend != NULL)
370                 fnameaddr = &fname;
371
372         if (!lex_accept(lex, JSON_TOKEN_STRING, fnameaddr))
373                 report_parse_error(JSON_PARSE_STRING, lex);
374
375         lex_expect(JSON_PARSE_OBJECT_LABEL, lex, JSON_TOKEN_COLON);
376
377         tok = lex_peek(lex);
378         isnull = tok == JSON_TOKEN_NULL;
379
380         if (ostart != NULL)
381                 (*ostart) (sem->semstate, fname, isnull);
382
383         switch (tok)
384         {
385                 case JSON_TOKEN_OBJECT_START:
386                         parse_object(lex, sem);
387                         break;
388                 case JSON_TOKEN_ARRAY_START:
389                         parse_array(lex, sem);
390                         break;
391                 default:
392                         parse_scalar(lex, sem);
393         }
394
395         if (oend != NULL)
396                 (*oend) (sem->semstate, fname, isnull);
397 }
398
399 static void
400 parse_object(JsonLexContext *lex, JsonSemAction *sem)
401 {
402         /*
403          * an object is a possibly empty sequence of object fields, separated by
404          * commas and surrounded by curly braces.
405          */
406         json_struct_action ostart = sem->object_start;
407         json_struct_action oend = sem->object_end;
408         JsonTokenType tok;
409
410         if (ostart != NULL)
411                 (*ostart) (sem->semstate);
412
413         /*
414          * Data inside an object is at a higher nesting level than the object
415          * itself. Note that we increment this after we call the semantic routine
416          * for the object start and restore it before we call the routine for the
417          * object end.
418          */
419         lex->lex_level++;
420
421         /* we know this will succeeed, just clearing the token */
422         lex_expect(JSON_PARSE_OBJECT_START, lex, JSON_TOKEN_OBJECT_START);
423
424         tok = lex_peek(lex);
425         switch (tok)
426         {
427                 case JSON_TOKEN_STRING:
428                         parse_object_field(lex, sem);
429                         while (lex_accept(lex, JSON_TOKEN_COMMA, NULL))
430                                 parse_object_field(lex, sem);
431                         break;
432                 case JSON_TOKEN_OBJECT_END:
433                         break;
434                 default:
435                         /* case of an invalid initial token inside the object */
436                         report_parse_error(JSON_PARSE_OBJECT_START, lex);
437         }
438
439         lex_expect(JSON_PARSE_OBJECT_NEXT, lex, JSON_TOKEN_OBJECT_END);
440
441         lex->lex_level--;
442
443         if (oend != NULL)
444                 (*oend) (sem->semstate);
445 }
446
447 static void
448 parse_array_element(JsonLexContext *lex, JsonSemAction *sem)
449 {
450         json_aelem_action astart = sem->array_element_start;
451         json_aelem_action aend = sem->array_element_end;
452         JsonTokenType tok = lex_peek(lex);
453
454         bool            isnull;
455
456         isnull = tok == JSON_TOKEN_NULL;
457
458         if (astart != NULL)
459                 (*astart) (sem->semstate, isnull);
460
461         /* an array element is any object, array or scalar */
462         switch (tok)
463         {
464                 case JSON_TOKEN_OBJECT_START:
465                         parse_object(lex, sem);
466                         break;
467                 case JSON_TOKEN_ARRAY_START:
468                         parse_array(lex, sem);
469                         break;
470                 default:
471                         parse_scalar(lex, sem);
472         }
473
474         if (aend != NULL)
475                 (*aend) (sem->semstate, isnull);
476 }
477
478 static void
479 parse_array(JsonLexContext *lex, JsonSemAction *sem)
480 {
481         /*
482          * an array is a possibly empty sequence of array elements, separated by
483          * commas and surrounded by square brackets.
484          */
485         json_struct_action astart = sem->array_start;
486         json_struct_action aend = sem->array_end;
487
488         if (astart != NULL)
489                 (*astart) (sem->semstate);
490
491         /*
492          * Data inside an array is at a higher nesting level than the array
493          * itself. Note that we increment this after we call the semantic routine
494          * for the array start and restore it before we call the routine for the
495          * array end.
496          */
497         lex->lex_level++;
498
499         lex_expect(JSON_PARSE_ARRAY_START, lex, JSON_TOKEN_ARRAY_START);
500         if (lex_peek(lex) != JSON_TOKEN_ARRAY_END)
501         {
502
503                 parse_array_element(lex, sem);
504
505                 while (lex_accept(lex, JSON_TOKEN_COMMA, NULL))
506                         parse_array_element(lex, sem);
507         }
508
509         lex_expect(JSON_PARSE_ARRAY_NEXT, lex, JSON_TOKEN_ARRAY_END);
510
511         lex->lex_level--;
512
513         if (aend != NULL)
514                 (*aend) (sem->semstate);
515 }
516
517 /*
518  * Lex one token from the input stream.
519  */
520 static inline void
521 json_lex(JsonLexContext *lex)
522 {
523         char       *s;
524         int                     len;
525
526         /* Skip leading whitespace. */
527         s = lex->token_terminator;
528         len = s - lex->input;
529         while (len < lex->input_length &&
530                    (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'))
531         {
532                 if (*s == '\n')
533                         ++lex->line_number;
534                 ++s;
535                 ++len;
536         }
537         lex->token_start = s;
538
539         /* Determine token type. */
540         if (len >= lex->input_length)
541         {
542                 lex->token_start = NULL;
543                 lex->prev_token_terminator = lex->token_terminator;
544                 lex->token_terminator = s;
545                 lex->token_type = JSON_TOKEN_END;
546         }
547         else
548                 switch (*s)
549                 {
550                                 /* Single-character token, some kind of punctuation mark. */
551                         case '{':
552                                 lex->prev_token_terminator = lex->token_terminator;
553                                 lex->token_terminator = s + 1;
554                                 lex->token_type = JSON_TOKEN_OBJECT_START;
555                                 break;
556                         case '}':
557                                 lex->prev_token_terminator = lex->token_terminator;
558                                 lex->token_terminator = s + 1;
559                                 lex->token_type = JSON_TOKEN_OBJECT_END;
560                                 break;
561                         case '[':
562                                 lex->prev_token_terminator = lex->token_terminator;
563                                 lex->token_terminator = s + 1;
564                                 lex->token_type = JSON_TOKEN_ARRAY_START;
565                                 break;
566                         case ']':
567                                 lex->prev_token_terminator = lex->token_terminator;
568                                 lex->token_terminator = s + 1;
569                                 lex->token_type = JSON_TOKEN_ARRAY_END;
570                                 break;
571                         case ',':
572                                 lex->prev_token_terminator = lex->token_terminator;
573                                 lex->token_terminator = s + 1;
574                                 lex->token_type = JSON_TOKEN_COMMA;
575                                 break;
576                         case ':':
577                                 lex->prev_token_terminator = lex->token_terminator;
578                                 lex->token_terminator = s + 1;
579                                 lex->token_type = JSON_TOKEN_COLON;
580                                 break;
581                         case '"':
582                                 /* string */
583                                 json_lex_string(lex);
584                                 lex->token_type = JSON_TOKEN_STRING;
585                                 break;
586                         case '-':
587                                 /* Negative number. */
588                                 json_lex_number(lex, s + 1, NULL);
589                                 lex->token_type = JSON_TOKEN_NUMBER;
590                                 break;
591                         case '0':
592                         case '1':
593                         case '2':
594                         case '3':
595                         case '4':
596                         case '5':
597                         case '6':
598                         case '7':
599                         case '8':
600                         case '9':
601                                 /* Positive number. */
602                                 json_lex_number(lex, s, NULL);
603                                 lex->token_type = JSON_TOKEN_NUMBER;
604                                 break;
605                         default:
606                                 {
607                                         char       *p;
608
609                                         /*
610                                          * We're not dealing with a string, number, legal
611                                          * punctuation mark, or end of string.  The only legal
612                                          * tokens we might find here are true, false, and null,
613                                          * but for error reporting purposes we scan until we see a
614                                          * non-alphanumeric character.  That way, we can report
615                                          * the whole word as an unexpected token, rather than just
616                                          * some unintuitive prefix thereof.
617                                          */
618                                         for (p = s; p - s < lex->input_length - len && JSON_ALPHANUMERIC_CHAR(*p); p++)
619                                                  /* skip */ ;
620
621                                         /*
622                                          * We got some sort of unexpected punctuation or an
623                                          * otherwise unexpected character, so just complain about
624                                          * that one character.
625                                          */
626                                         if (p == s)
627                                         {
628                                                 lex->prev_token_terminator = lex->token_terminator;
629                                                 lex->token_terminator = s + 1;
630                                                 report_invalid_token(lex);
631                                         }
632
633                                         /*
634                                          * We've got a real alphanumeric token here.  If it
635                                          * happens to be true, false, or null, all is well.  If
636                                          * not, error out.
637                                          */
638                                         lex->prev_token_terminator = lex->token_terminator;
639                                         lex->token_terminator = p;
640                                         if (p - s == 4)
641                                         {
642                                                 if (memcmp(s, "true", 4) == 0)
643                                                         lex->token_type = JSON_TOKEN_TRUE;
644                                                 else if (memcmp(s, "null", 4) == 0)
645                                                         lex->token_type = JSON_TOKEN_NULL;
646                                                 else
647                                                         report_invalid_token(lex);
648                                         }
649                                         else if (p - s == 5 && memcmp(s, "false", 5) == 0)
650                                                 lex->token_type = JSON_TOKEN_FALSE;
651                                         else
652                                                 report_invalid_token(lex);
653
654                                 }
655                 }                                               /* end of switch */
656 }
657
658 /*
659  * The next token in the input stream is known to be a string; lex it.
660  */
661 static inline void
662 json_lex_string(JsonLexContext *lex)
663 {
664         char       *s;
665         int                     len;
666         int                     hi_surrogate = -1;
667
668         if (lex->strval != NULL)
669                 resetStringInfo(lex->strval);
670
671         Assert(lex->input_length > 0);
672         s = lex->token_start;
673         len = lex->token_start - lex->input;
674         for (;;)
675         {
676                 s++;
677                 len++;
678                 /* Premature end of the string. */
679                 if (len >= lex->input_length)
680                 {
681                         lex->token_terminator = s;
682                         report_invalid_token(lex);
683                 }
684                 else if (*s == '"')
685                         break;
686                 else if ((unsigned char) *s < 32)
687                 {
688                         /* Per RFC4627, these characters MUST be escaped. */
689                         /* Since *s isn't printable, exclude it from the context string */
690                         lex->token_terminator = s;
691                         ereport(ERROR,
692                                         (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
693                                          errmsg("invalid input syntax for type json"),
694                                          errdetail("Character with value 0x%02x must be escaped.",
695                                                            (unsigned char) *s),
696                                          report_json_context(lex)));
697                 }
698                 else if (*s == '\\')
699                 {
700                         /* OK, we have an escape character. */
701                         s++;
702                         len++;
703                         if (len >= lex->input_length)
704                         {
705                                 lex->token_terminator = s;
706                                 report_invalid_token(lex);
707                         }
708                         else if (*s == 'u')
709                         {
710                                 int                     i;
711                                 int                     ch = 0;
712
713                                 for (i = 1; i <= 4; i++)
714                                 {
715                                         s++;
716                                         len++;
717                                         if (len >= lex->input_length)
718                                         {
719                                                 lex->token_terminator = s;
720                                                 report_invalid_token(lex);
721                                         }
722                                         else if (*s >= '0' && *s <= '9')
723                                                 ch = (ch * 16) + (*s - '0');
724                                         else if (*s >= 'a' && *s <= 'f')
725                                                 ch = (ch * 16) + (*s - 'a') + 10;
726                                         else if (*s >= 'A' && *s <= 'F')
727                                                 ch = (ch * 16) + (*s - 'A') + 10;
728                                         else
729                                         {
730                                                 lex->token_terminator = s + pg_mblen(s);
731                                                 ereport(ERROR,
732                                                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
733                                                                  errmsg("invalid input syntax for type json"),
734                                                                  errdetail("\"\\u\" must be followed by four hexadecimal digits."),
735                                                                  report_json_context(lex)));
736                                         }
737                                 }
738                                 if (lex->strval != NULL)
739                                 {
740                                         char            utf8str[5];
741                                         int                     utf8len;
742
743                                         if (ch >= 0xd800 && ch <= 0xdbff)
744                                         {
745                                                 if (hi_surrogate != -1)
746                                                         ereport(ERROR,
747                                                            (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
748                                                                 errmsg("invalid input syntax for type json"),
749                                                                 errdetail("Unicode high surrogate must not follow a high surrogate."),
750                                                                 report_json_context(lex)));
751                                                 hi_surrogate = (ch & 0x3ff) << 10;
752                                                 continue;
753                                         }
754                                         else if (ch >= 0xdc00 && ch <= 0xdfff)
755                                         {
756                                                 if (hi_surrogate == -1)
757                                                         ereport(ERROR,
758                                                            (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
759                                                                 errmsg("invalid input syntax for type json"),
760                                                                 errdetail("Unicode low surrogate must follow a high surrogate."),
761                                                                 report_json_context(lex)));
762                                                 ch = 0x10000 + hi_surrogate + (ch & 0x3ff);
763                                                 hi_surrogate = -1;
764                                         }
765
766                                         if (hi_surrogate != -1)
767                                                 ereport(ERROR,
768                                                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
769                                                                  errmsg("invalid input syntax for type json"),
770                                                                  errdetail("Unicode low surrogate must follow a high surrogate."),
771                                                                  report_json_context(lex)));
772
773                                         /*
774                                          * For UTF8, replace the escape sequence by the actual
775                                          * utf8 character in lex->strval. Do this also for other
776                                          * encodings if the escape designates an ASCII character,
777                                          * otherwise raise an error. We don't ever unescape a
778                                          * \u0000, since that would result in an impermissible nul
779                                          * byte.
780                                          */
781
782                                         if (ch == 0)
783                                         {
784                                                 appendStringInfoString(lex->strval, "\\u0000");
785                                         }
786                                         else if (GetDatabaseEncoding() == PG_UTF8)
787                                         {
788                                                 unicode_to_utf8(ch, (unsigned char *) utf8str);
789                                                 utf8len = pg_utf_mblen((unsigned char *) utf8str);
790                                                 appendBinaryStringInfo(lex->strval, utf8str, utf8len);
791                                         }
792                                         else if (ch <= 0x007f)
793                                         {
794                                                 /*
795                                                  * This is the only way to designate things like a
796                                                  * form feed character in JSON, so it's useful in all
797                                                  * encodings.
798                                                  */
799                                                 appendStringInfoChar(lex->strval, (char) ch);
800                                         }
801                                         else
802                                         {
803                                                 ereport(ERROR,
804                                                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
805                                                                  errmsg("invalid input syntax for type json"),
806                                                                  errdetail("Unicode escape values cannot be used for code point values above 007F when the server encoding is not UTF8."),
807                                                                  report_json_context(lex)));
808                                         }
809
810                                 }
811                         }
812                         else if (lex->strval != NULL)
813                         {
814                                 if (hi_surrogate != -1)
815                                         ereport(ERROR,
816                                                         (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
817                                                          errmsg("invalid input syntax for type json"),
818                                                          errdetail("Unicode low surrogate must follow a high surrogate."),
819                                                          report_json_context(lex)));
820
821                                 switch (*s)
822                                 {
823                                         case '"':
824                                         case '\\':
825                                         case '/':
826                                                 appendStringInfoChar(lex->strval, *s);
827                                                 break;
828                                         case 'b':
829                                                 appendStringInfoChar(lex->strval, '\b');
830                                                 break;
831                                         case 'f':
832                                                 appendStringInfoChar(lex->strval, '\f');
833                                                 break;
834                                         case 'n':
835                                                 appendStringInfoChar(lex->strval, '\n');
836                                                 break;
837                                         case 'r':
838                                                 appendStringInfoChar(lex->strval, '\r');
839                                                 break;
840                                         case 't':
841                                                 appendStringInfoChar(lex->strval, '\t');
842                                                 break;
843                                         default:
844                                                 /* Not a valid string escape, so error out. */
845                                                 lex->token_terminator = s + pg_mblen(s);
846                                                 ereport(ERROR,
847                                                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
848                                                                  errmsg("invalid input syntax for type json"),
849                                                         errdetail("Escape sequence \"\\%s\" is invalid.",
850                                                                           extract_mb_char(s)),
851                                                                  report_json_context(lex)));
852                                 }
853                         }
854                         else if (strchr("\"\\/bfnrt", *s) == NULL)
855                         {
856                                 /*
857                                  * Simpler processing if we're not bothered about de-escaping
858                                  *
859                                  * It's very tempting to remove the strchr() call here and
860                                  * replace it with a switch statement, but testing so far has
861                                  * shown it's not a performance win.
862                                  */
863                                 lex->token_terminator = s + pg_mblen(s);
864                                 ereport(ERROR,
865                                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
866                                                  errmsg("invalid input syntax for type json"),
867                                                  errdetail("Escape sequence \"\\%s\" is invalid.",
868                                                                    extract_mb_char(s)),
869                                                  report_json_context(lex)));
870                         }
871
872                 }
873                 else if (lex->strval != NULL)
874                 {
875                         if (hi_surrogate != -1)
876                                 ereport(ERROR,
877                                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
878                                                  errmsg("invalid input syntax for type json"),
879                                                  errdetail("Unicode low surrogate must follow a high surrogate."),
880                                                  report_json_context(lex)));
881
882                         appendStringInfoChar(lex->strval, *s);
883                 }
884
885         }
886
887         if (hi_surrogate != -1)
888                 ereport(ERROR,
889                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
890                                  errmsg("invalid input syntax for type json"),
891                         errdetail("Unicode low surrogate must follow a high surrogate."),
892                                  report_json_context(lex)));
893
894         /* Hooray, we found the end of the string! */
895         lex->prev_token_terminator = lex->token_terminator;
896         lex->token_terminator = s + 1;
897 }
898
899 /*-------------------------------------------------------------------------
900  * The next token in the input stream is known to be a number; lex it.
901  *
902  * In JSON, a number consists of four parts:
903  *
904  * (1) An optional minus sign ('-').
905  *
906  * (2) Either a single '0', or a string of one or more digits that does not
907  *         begin with a '0'.
908  *
909  * (3) An optional decimal part, consisting of a period ('.') followed by
910  *         one or more digits.  (Note: While this part can be omitted
911  *         completely, it's not OK to have only the decimal point without
912  *         any digits afterwards.)
913  *
914  * (4) An optional exponent part, consisting of 'e' or 'E', optionally
915  *         followed by '+' or '-', followed by one or more digits.  (Note:
916  *         As with the decimal part, if 'e' or 'E' is present, it must be
917  *         followed by at least one digit.)
918  *
919  * The 's' argument to this function points to the ostensible beginning
920  * of part 2 - i.e. the character after any optional minus sign, and the
921  * first character of the string if there is none.
922  *
923  *-------------------------------------------------------------------------
924  */
925 static inline void
926 json_lex_number(JsonLexContext *lex, char *s, bool *num_err)
927 {
928         bool            error = false;
929         char       *p;
930         int                     len;
931
932         len = s - lex->input;
933         /* Part (1): leading sign indicator. */
934         /* Caller already did this for us; so do nothing. */
935
936         /* Part (2): parse main digit string. */
937         if (*s == '0')
938         {
939                 s++;
940                 len++;
941         }
942         else if (*s >= '1' && *s <= '9')
943         {
944                 do
945                 {
946                         s++;
947                         len++;
948                 } while (len < lex->input_length && *s >= '0' && *s <= '9');
949         }
950         else
951                 error = true;
952
953         /* Part (3): parse optional decimal portion. */
954         if (len < lex->input_length && *s == '.')
955         {
956                 s++;
957                 len++;
958                 if (len == lex->input_length || *s < '0' || *s > '9')
959                         error = true;
960                 else
961                 {
962                         do
963                         {
964                                 s++;
965                                 len++;
966                         } while (len < lex->input_length && *s >= '0' && *s <= '9');
967                 }
968         }
969
970         /* Part (4): parse optional exponent. */
971         if (len < lex->input_length && (*s == 'e' || *s == 'E'))
972         {
973                 s++;
974                 len++;
975                 if (len < lex->input_length && (*s == '+' || *s == '-'))
976                 {
977                         s++;
978                         len++;
979                 }
980                 if (len == lex->input_length || *s < '0' || *s > '9')
981                         error = true;
982                 else
983                 {
984                         do
985                         {
986                                 s++;
987                                 len++;
988                         } while (len < lex->input_length && *s >= '0' && *s <= '9');
989                 }
990         }
991
992         /*
993          * Check for trailing garbage.  As in json_lex(), any alphanumeric stuff
994          * here should be considered part of the token for error-reporting
995          * purposes.
996          */
997         for (p = s; len < lex->input_length && JSON_ALPHANUMERIC_CHAR(*p); p++, len++)
998                 error = true;
999
1000         if (num_err != NULL)
1001         {
1002                 /* let the caller handle the error */
1003                 *num_err = error;
1004         }
1005         else
1006         {
1007                 lex->prev_token_terminator = lex->token_terminator;
1008                 lex->token_terminator = p;
1009                 if (error)
1010                         report_invalid_token(lex);
1011         }
1012 }
1013
1014 /*
1015  * Report a parse error.
1016  *
1017  * lex->token_start and lex->token_terminator must identify the current token.
1018  */
1019 static void
1020 report_parse_error(JsonParseContext ctx, JsonLexContext *lex)
1021 {
1022         char       *token;
1023         int                     toklen;
1024
1025         /* Handle case where the input ended prematurely. */
1026         if (lex->token_start == NULL || lex->token_type == JSON_TOKEN_END)
1027                 ereport(ERROR,
1028                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1029                                  errmsg("invalid input syntax for type json"),
1030                                  errdetail("The input string ended unexpectedly."),
1031                                  report_json_context(lex)));
1032
1033         /* Separate out the current token. */
1034         toklen = lex->token_terminator - lex->token_start;
1035         token = palloc(toklen + 1);
1036         memcpy(token, lex->token_start, toklen);
1037         token[toklen] = '\0';
1038
1039         /* Complain, with the appropriate detail message. */
1040         if (ctx == JSON_PARSE_END)
1041                 ereport(ERROR,
1042                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1043                                  errmsg("invalid input syntax for type json"),
1044                                  errdetail("Expected end of input, but found \"%s\".",
1045                                                    token),
1046                                  report_json_context(lex)));
1047         else
1048         {
1049                 switch (ctx)
1050                 {
1051                         case JSON_PARSE_VALUE:
1052                                 ereport(ERROR,
1053                                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1054                                                  errmsg("invalid input syntax for type json"),
1055                                                  errdetail("Expected JSON value, but found \"%s\".",
1056                                                                    token),
1057                                                  report_json_context(lex)));
1058                                 break;
1059                         case JSON_PARSE_STRING:
1060                                 ereport(ERROR,
1061                                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1062                                                  errmsg("invalid input syntax for type json"),
1063                                                  errdetail("Expected string, but found \"%s\".",
1064                                                                    token),
1065                                                  report_json_context(lex)));
1066                                 break;
1067                         case JSON_PARSE_ARRAY_START:
1068                                 ereport(ERROR,
1069                                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1070                                                  errmsg("invalid input syntax for type json"),
1071                                                  errdetail("Expected array element or \"]\", but found \"%s\".",
1072                                                                    token),
1073                                                  report_json_context(lex)));
1074                                 break;
1075                         case JSON_PARSE_ARRAY_NEXT:
1076                                 ereport(ERROR,
1077                                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1078                                                  errmsg("invalid input syntax for type json"),
1079                                           errdetail("Expected \",\" or \"]\", but found \"%s\".",
1080                                                                 token),
1081                                                  report_json_context(lex)));
1082                                 break;
1083                         case JSON_PARSE_OBJECT_START:
1084                                 ereport(ERROR,
1085                                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1086                                                  errmsg("invalid input syntax for type json"),
1087                                          errdetail("Expected string or \"}\", but found \"%s\".",
1088                                                            token),
1089                                                  report_json_context(lex)));
1090                                 break;
1091                         case JSON_PARSE_OBJECT_LABEL:
1092                                 ereport(ERROR,
1093                                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1094                                                  errmsg("invalid input syntax for type json"),
1095                                                  errdetail("Expected \":\", but found \"%s\".",
1096                                                                    token),
1097                                                  report_json_context(lex)));
1098                                 break;
1099                         case JSON_PARSE_OBJECT_NEXT:
1100                                 ereport(ERROR,
1101                                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1102                                                  errmsg("invalid input syntax for type json"),
1103                                           errdetail("Expected \",\" or \"}\", but found \"%s\".",
1104                                                                 token),
1105                                                  report_json_context(lex)));
1106                                 break;
1107                         case JSON_PARSE_OBJECT_COMMA:
1108                                 ereport(ERROR,
1109                                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1110                                                  errmsg("invalid input syntax for type json"),
1111                                                  errdetail("Expected string, but found \"%s\".",
1112                                                                    token),
1113                                                  report_json_context(lex)));
1114                                 break;
1115                         default:
1116                                 elog(ERROR, "unexpected json parse state: %d", ctx);
1117                 }
1118         }
1119 }
1120
1121 /*
1122  * Report an invalid input token.
1123  *
1124  * lex->token_start and lex->token_terminator must identify the token.
1125  */
1126 static void
1127 report_invalid_token(JsonLexContext *lex)
1128 {
1129         char       *token;
1130         int                     toklen;
1131
1132         /* Separate out the offending token. */
1133         toklen = lex->token_terminator - lex->token_start;
1134         token = palloc(toklen + 1);
1135         memcpy(token, lex->token_start, toklen);
1136         token[toklen] = '\0';
1137
1138         ereport(ERROR,
1139                         (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1140                          errmsg("invalid input syntax for type json"),
1141                          errdetail("Token \"%s\" is invalid.", token),
1142                          report_json_context(lex)));
1143 }
1144
1145 /*
1146  * Report a CONTEXT line for bogus JSON input.
1147  *
1148  * lex->token_terminator must be set to identify the spot where we detected
1149  * the error.  Note that lex->token_start might be NULL, in case we recognized
1150  * error at EOF.
1151  *
1152  * The return value isn't meaningful, but we make it non-void so that this
1153  * can be invoked inside ereport().
1154  */
1155 static int
1156 report_json_context(JsonLexContext *lex)
1157 {
1158         const char *context_start;
1159         const char *context_end;
1160         const char *line_start;
1161         int                     line_number;
1162         char       *ctxt;
1163         int                     ctxtlen;
1164         const char *prefix;
1165         const char *suffix;
1166
1167         /* Choose boundaries for the part of the input we will display */
1168         context_start = lex->input;
1169         context_end = lex->token_terminator;
1170         line_start = context_start;
1171         line_number = 1;
1172         for (;;)
1173         {
1174                 /* Always advance over newlines */
1175                 if (context_start < context_end && *context_start == '\n')
1176                 {
1177                         context_start++;
1178                         line_start = context_start;
1179                         line_number++;
1180                         continue;
1181                 }
1182                 /* Otherwise, done as soon as we are close enough to context_end */
1183                 if (context_end - context_start < 50)
1184                         break;
1185                 /* Advance to next multibyte character */
1186                 if (IS_HIGHBIT_SET(*context_start))
1187                         context_start += pg_mblen(context_start);
1188                 else
1189                         context_start++;
1190         }
1191
1192         /*
1193          * We add "..." to indicate that the excerpt doesn't start at the
1194          * beginning of the line ... but if we're within 3 characters of the
1195          * beginning of the line, we might as well just show the whole line.
1196          */
1197         if (context_start - line_start <= 3)
1198                 context_start = line_start;
1199
1200         /* Get a null-terminated copy of the data to present */
1201         ctxtlen = context_end - context_start;
1202         ctxt = palloc(ctxtlen + 1);
1203         memcpy(ctxt, context_start, ctxtlen);
1204         ctxt[ctxtlen] = '\0';
1205
1206         /*
1207          * Show the context, prefixing "..." if not starting at start of line, and
1208          * suffixing "..." if not ending at end of line.
1209          */
1210         prefix = (context_start > line_start) ? "..." : "";
1211         suffix = (lex->token_type != JSON_TOKEN_END && context_end - lex->input < lex->input_length && *context_end != '\n' && *context_end != '\r') ? "..." : "";
1212
1213         return errcontext("JSON data, line %d: %s%s%s",
1214                                           line_number, prefix, ctxt, suffix);
1215 }
1216
1217 /*
1218  * Extract a single, possibly multi-byte char from the input string.
1219  */
1220 static char *
1221 extract_mb_char(char *s)
1222 {
1223         char       *res;
1224         int                     len;
1225
1226         len = pg_mblen(s);
1227         res = palloc(len + 1);
1228         memcpy(res, s, len);
1229         res[len] = '\0';
1230
1231         return res;
1232 }
1233
1234 /*
1235  * Determine how we want to print values of a given type in datum_to_json.
1236  *
1237  * Given the datatype OID, return its JsonTypeCategory, as well as the type's
1238  * output function OID.  If the returned category is JSONTYPE_CAST, we
1239  * return the OID of the type->JSON cast function instead.
1240  */
1241 static void
1242 json_categorize_type(Oid typoid,
1243                                          JsonTypeCategory *tcategory,
1244                                          Oid *outfuncoid)
1245 {
1246         bool            typisvarlena;
1247
1248         /* Look through any domain */
1249         typoid = getBaseType(typoid);
1250
1251         /* We'll usually need to return the type output function */
1252         getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
1253
1254         /* Check for known types */
1255         switch (typoid)
1256         {
1257                 case BOOLOID:
1258                         *tcategory = JSONTYPE_BOOL;
1259                         break;
1260
1261                 case INT2OID:
1262                 case INT4OID:
1263                 case INT8OID:
1264                 case FLOAT4OID:
1265                 case FLOAT8OID:
1266                 case NUMERICOID:
1267                         *tcategory = JSONTYPE_NUMERIC;
1268                         break;
1269
1270                 case TIMESTAMPOID:
1271                         *tcategory = JSONTYPE_TIMESTAMP;
1272                         break;
1273
1274                 case TIMESTAMPTZOID:
1275                         *tcategory = JSONTYPE_TIMESTAMPTZ;
1276                         break;
1277
1278                 case JSONOID:
1279                 case JSONBOID:
1280                         *tcategory = JSONTYPE_JSON;
1281                         break;
1282
1283                 default:
1284                         /* Check for arrays and composites */
1285                         if (OidIsValid(get_element_type(typoid)))
1286                                 *tcategory = JSONTYPE_ARRAY;
1287                         else if (type_is_rowtype(typoid))
1288                                 *tcategory = JSONTYPE_COMPOSITE;
1289                         else
1290                         {
1291                                 /* It's probably the general case ... */
1292                                 *tcategory = JSONTYPE_OTHER;
1293                                 /* but let's look for a cast to json, if it's not built-in */
1294                                 if (typoid >= FirstNormalObjectId)
1295                                 {
1296                                         HeapTuple       tuple;
1297
1298                                         tuple = SearchSysCache2(CASTSOURCETARGET,
1299                                                                                         ObjectIdGetDatum(typoid),
1300                                                                                         ObjectIdGetDatum(JSONOID));
1301                                         if (HeapTupleIsValid(tuple))
1302                                         {
1303                                                 Form_pg_cast castForm = (Form_pg_cast) GETSTRUCT(tuple);
1304
1305                                                 if (castForm->castmethod == COERCION_METHOD_FUNCTION)
1306                                                 {
1307                                                         *tcategory = JSONTYPE_CAST;
1308                                                         *outfuncoid = castForm->castfunc;
1309                                                 }
1310
1311                                                 ReleaseSysCache(tuple);
1312                                         }
1313                                 }
1314                         }
1315                         break;
1316         }
1317 }
1318
1319 /*
1320  * Turn a Datum into JSON text, appending the string to "result".
1321  *
1322  * tcategory and outfuncoid are from a previous call to json_categorize_type,
1323  * except that if is_null is true then they can be invalid.
1324  *
1325  * If key_scalar is true, the value is being printed as a key, so insist
1326  * it's of an acceptable type, and force it to be quoted.
1327  */
1328 static void
1329 datum_to_json(Datum val, bool is_null, StringInfo result,
1330                           JsonTypeCategory tcategory, Oid outfuncoid,
1331                           bool key_scalar)
1332 {
1333         char       *outputstr;
1334         text       *jsontext;
1335         bool            numeric_error;
1336         JsonLexContext dummy_lex;
1337
1338         if (is_null)
1339         {
1340                 appendStringInfoString(result, "null");
1341                 return;
1342         }
1343
1344         if (key_scalar &&
1345                 (tcategory == JSONTYPE_ARRAY ||
1346                  tcategory == JSONTYPE_COMPOSITE ||
1347                  tcategory == JSONTYPE_JSON ||
1348                  tcategory == JSONTYPE_CAST))
1349                 ereport(ERROR,
1350                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1351                   errmsg("key value must be scalar, not array, composite, or json")));
1352
1353         switch (tcategory)
1354         {
1355                 case JSONTYPE_ARRAY:
1356                         array_to_json_internal(val, result, false);
1357                         break;
1358                 case JSONTYPE_COMPOSITE:
1359                         composite_to_json(val, result, false);
1360                         break;
1361                 case JSONTYPE_BOOL:
1362                         outputstr = DatumGetBool(val) ? "true" : "false";
1363                         if (key_scalar)
1364                                 escape_json(result, outputstr);
1365                         else
1366                                 appendStringInfoString(result, outputstr);
1367                         break;
1368                 case JSONTYPE_NUMERIC:
1369                         outputstr = OidOutputFunctionCall(outfuncoid, val);
1370                         if (key_scalar)
1371                         {
1372                                 /* always quote keys */
1373                                 escape_json(result, outputstr);
1374                         }
1375                         else
1376                         {
1377                                 /*
1378                                  * Don't call escape_json for a non-key if it's a valid JSON
1379                                  * number.
1380                                  */
1381                                 dummy_lex.input = *outputstr == '-' ? outputstr + 1 : outputstr;
1382                                 dummy_lex.input_length = strlen(dummy_lex.input);
1383                                 json_lex_number(&dummy_lex, dummy_lex.input, &numeric_error);
1384                                 if (!numeric_error)
1385                                         appendStringInfoString(result, outputstr);
1386                                 else
1387                                         escape_json(result, outputstr);
1388                         }
1389                         pfree(outputstr);
1390                         break;
1391                 case JSONTYPE_TIMESTAMP:
1392                         {
1393                                 Timestamp       timestamp;
1394                                 struct pg_tm tm;
1395                                 fsec_t          fsec;
1396                                 char            buf[MAXDATELEN + 1];
1397
1398                                 timestamp = DatumGetTimestamp(val);
1399
1400                                 /* XSD doesn't support infinite values */
1401                                 if (TIMESTAMP_NOT_FINITE(timestamp))
1402                                         ereport(ERROR,
1403                                                         (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1404                                                          errmsg("timestamp out of range"),
1405                                                          errdetail("JSON does not support infinite timestamp values.")));
1406                                 else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
1407                                         EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
1408                                 else
1409                                         ereport(ERROR,
1410                                                         (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1411                                                          errmsg("timestamp out of range")));
1412
1413                                 appendStringInfo(result,"\"%s\"",buf);
1414                         }
1415                         break;
1416                 case JSONTYPE_TIMESTAMPTZ:
1417                         {
1418                                 TimestampTz timestamp;
1419                                 struct pg_tm tm;
1420                                 int                     tz;
1421                                 fsec_t          fsec;
1422                                 const char *tzn = NULL;
1423                                 char            buf[MAXDATELEN + 1];
1424
1425                                 timestamp = DatumGetTimestamp(val);
1426
1427                                 /* XSD doesn't support infinite values */
1428                                 if (TIMESTAMP_NOT_FINITE(timestamp))
1429                                         ereport(ERROR,
1430                                                         (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1431                                                          errmsg("timestamp out of range"),
1432                                                          errdetail("JSON does not support infinite timestamp values.")));
1433                                 else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
1434                                         EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
1435                                 else
1436                                         ereport(ERROR,
1437                                                         (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1438                                                          errmsg("timestamp out of range")));
1439
1440                                 appendStringInfo(result,"\"%s\"",buf);
1441                         }
1442                         break;
1443                 case JSONTYPE_JSON:
1444                         /* JSON and JSONB output will already be escaped */
1445                         outputstr = OidOutputFunctionCall(outfuncoid, val);
1446                         appendStringInfoString(result, outputstr);
1447                         pfree(outputstr);
1448                         break;
1449                 case JSONTYPE_CAST:
1450                         /* outfuncoid refers to a cast function, not an output function */
1451                         jsontext = DatumGetTextP(OidFunctionCall1(outfuncoid, val));
1452                         outputstr = text_to_cstring(jsontext);
1453                         appendStringInfoString(result, outputstr);
1454                         pfree(outputstr);
1455                         pfree(jsontext);
1456                         break;
1457                 default:
1458                         outputstr = OidOutputFunctionCall(outfuncoid, val);
1459                         if (key_scalar && *outputstr == '\0')
1460                                 ereport(ERROR,
1461                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1462                                                  errmsg("key value must not be empty")));
1463                         escape_json(result, outputstr);
1464                         pfree(outputstr);
1465                         break;
1466         }
1467 }
1468
1469 /*
1470  * Process a single dimension of an array.
1471  * If it's the innermost dimension, output the values, otherwise call
1472  * ourselves recursively to process the next dimension.
1473  */
1474 static void
1475 array_dim_to_json(StringInfo result, int dim, int ndims, int *dims, Datum *vals,
1476                                   bool *nulls, int *valcount, JsonTypeCategory tcategory,
1477                                   Oid outfuncoid, bool use_line_feeds)
1478 {
1479         int                     i;
1480         const char *sep;
1481
1482         Assert(dim < ndims);
1483
1484         sep = use_line_feeds ? ",\n " : ",";
1485
1486         appendStringInfoChar(result, '[');
1487
1488         for (i = 1; i <= dims[dim]; i++)
1489         {
1490                 if (i > 1)
1491                         appendStringInfoString(result, sep);
1492
1493                 if (dim + 1 == ndims)
1494                 {
1495                         datum_to_json(vals[*valcount], nulls[*valcount], result, tcategory,
1496                                                   outfuncoid, false);
1497                         (*valcount)++;
1498                 }
1499                 else
1500                 {
1501                         /*
1502                          * Do we want line feeds on inner dimensions of arrays? For now
1503                          * we'll say no.
1504                          */
1505                         array_dim_to_json(result, dim + 1, ndims, dims, vals, nulls,
1506                                                           valcount, tcategory, outfuncoid, false);
1507                 }
1508         }
1509
1510         appendStringInfoChar(result, ']');
1511 }
1512
1513 /*
1514  * Turn an array into JSON.
1515  */
1516 static void
1517 array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
1518 {
1519         ArrayType  *v = DatumGetArrayTypeP(array);
1520         Oid                     element_type = ARR_ELEMTYPE(v);
1521         int                *dim;
1522         int                     ndim;
1523         int                     nitems;
1524         int                     count = 0;
1525         Datum      *elements;
1526         bool       *nulls;
1527         int16           typlen;
1528         bool            typbyval;
1529         char            typalign;
1530         JsonTypeCategory tcategory;
1531         Oid                     outfuncoid;
1532
1533         ndim = ARR_NDIM(v);
1534         dim = ARR_DIMS(v);
1535         nitems = ArrayGetNItems(ndim, dim);
1536
1537         if (nitems <= 0)
1538         {
1539                 appendStringInfoString(result, "[]");
1540                 return;
1541         }
1542
1543         get_typlenbyvalalign(element_type,
1544                                                  &typlen, &typbyval, &typalign);
1545
1546         json_categorize_type(element_type,
1547                                                  &tcategory, &outfuncoid);
1548
1549         deconstruct_array(v, element_type, typlen, typbyval,
1550                                           typalign, &elements, &nulls,
1551                                           &nitems);
1552
1553         array_dim_to_json(result, 0, ndim, dim, elements, nulls, &count, tcategory,
1554                                           outfuncoid, use_line_feeds);
1555
1556         pfree(elements);
1557         pfree(nulls);
1558 }
1559
1560 /*
1561  * Turn a composite / record into JSON.
1562  */
1563 static void
1564 composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
1565 {
1566         HeapTupleHeader td;
1567         Oid                     tupType;
1568         int32           tupTypmod;
1569         TupleDesc       tupdesc;
1570         HeapTupleData tmptup,
1571                            *tuple;
1572         int                     i;
1573         bool            needsep = false;
1574         const char *sep;
1575
1576         sep = use_line_feeds ? ",\n " : ",";
1577
1578         td = DatumGetHeapTupleHeader(composite);
1579
1580         /* Extract rowtype info and find a tupdesc */
1581         tupType = HeapTupleHeaderGetTypeId(td);
1582         tupTypmod = HeapTupleHeaderGetTypMod(td);
1583         tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
1584
1585         /* Build a temporary HeapTuple control structure */
1586         tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
1587         tmptup.t_data = td;
1588         tuple = &tmptup;
1589
1590         appendStringInfoChar(result, '{');
1591
1592         for (i = 0; i < tupdesc->natts; i++)
1593         {
1594                 Datum           val;
1595                 bool            isnull;
1596                 char       *attname;
1597                 JsonTypeCategory tcategory;
1598                 Oid                     outfuncoid;
1599
1600                 if (tupdesc->attrs[i]->attisdropped)
1601                         continue;
1602
1603                 if (needsep)
1604                         appendStringInfoString(result, sep);
1605                 needsep = true;
1606
1607                 attname = NameStr(tupdesc->attrs[i]->attname);
1608                 escape_json(result, attname);
1609                 appendStringInfoChar(result, ':');
1610
1611                 val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
1612
1613                 if (isnull)
1614                 {
1615                         tcategory = JSONTYPE_NULL;
1616                         outfuncoid = InvalidOid;
1617                 }
1618                 else
1619                         json_categorize_type(tupdesc->attrs[i]->atttypid,
1620                                                                  &tcategory, &outfuncoid);
1621
1622                 datum_to_json(val, isnull, result, tcategory, outfuncoid, false);
1623         }
1624
1625         appendStringInfoChar(result, '}');
1626         ReleaseTupleDesc(tupdesc);
1627 }
1628
1629 /*
1630  * Append JSON text for "val" to "result".
1631  *
1632  * This is just a thin wrapper around datum_to_json.  If the same type will be
1633  * printed many times, avoid using this; better to do the json_categorize_type
1634  * lookups only once.
1635  */
1636 static void
1637 add_json(Datum val, bool is_null, StringInfo result,
1638                  Oid val_type, bool key_scalar)
1639 {
1640         JsonTypeCategory tcategory;
1641         Oid                     outfuncoid;
1642
1643         if (val_type == InvalidOid)
1644                 ereport(ERROR,
1645                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1646                                  errmsg("could not determine input data type")));
1647
1648         if (is_null)
1649         {
1650                 tcategory = JSONTYPE_NULL;
1651                 outfuncoid = InvalidOid;
1652         }
1653         else
1654                 json_categorize_type(val_type,
1655                                                          &tcategory, &outfuncoid);
1656
1657         datum_to_json(val, is_null, result, tcategory, outfuncoid, key_scalar);
1658 }
1659
1660 /*
1661  * SQL function array_to_json(row)
1662  */
1663 extern Datum
1664 array_to_json(PG_FUNCTION_ARGS)
1665 {
1666         Datum           array = PG_GETARG_DATUM(0);
1667         StringInfo      result;
1668
1669         result = makeStringInfo();
1670
1671         array_to_json_internal(array, result, false);
1672
1673         PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1674 }
1675
1676 /*
1677  * SQL function array_to_json(row, prettybool)
1678  */
1679 extern Datum
1680 array_to_json_pretty(PG_FUNCTION_ARGS)
1681 {
1682         Datum           array = PG_GETARG_DATUM(0);
1683         bool            use_line_feeds = PG_GETARG_BOOL(1);
1684         StringInfo      result;
1685
1686         result = makeStringInfo();
1687
1688         array_to_json_internal(array, result, use_line_feeds);
1689
1690         PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1691 }
1692
1693 /*
1694  * SQL function row_to_json(row)
1695  */
1696 extern Datum
1697 row_to_json(PG_FUNCTION_ARGS)
1698 {
1699         Datum           array = PG_GETARG_DATUM(0);
1700         StringInfo      result;
1701
1702         result = makeStringInfo();
1703
1704         composite_to_json(array, result, false);
1705
1706         PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1707 }
1708
1709 /*
1710  * SQL function row_to_json(row, prettybool)
1711  */
1712 extern Datum
1713 row_to_json_pretty(PG_FUNCTION_ARGS)
1714 {
1715         Datum           array = PG_GETARG_DATUM(0);
1716         bool            use_line_feeds = PG_GETARG_BOOL(1);
1717         StringInfo      result;
1718
1719         result = makeStringInfo();
1720
1721         composite_to_json(array, result, use_line_feeds);
1722
1723         PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1724 }
1725
1726 /*
1727  * SQL function to_json(anyvalue)
1728  */
1729 Datum
1730 to_json(PG_FUNCTION_ARGS)
1731 {
1732         Datum           val = PG_GETARG_DATUM(0);
1733         Oid                     val_type = get_fn_expr_argtype(fcinfo->flinfo, 0);
1734         StringInfo      result;
1735         JsonTypeCategory tcategory;
1736         Oid                     outfuncoid;
1737
1738         if (val_type == InvalidOid)
1739                 ereport(ERROR,
1740                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1741                                  errmsg("could not determine input data type")));
1742
1743         json_categorize_type(val_type,
1744                                                  &tcategory, &outfuncoid);
1745
1746         result = makeStringInfo();
1747
1748         datum_to_json(val, false, result, tcategory, outfuncoid, false);
1749
1750         PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1751 }
1752
1753 /*
1754  * json_agg transition function
1755  */
1756 Datum
1757 json_agg_transfn(PG_FUNCTION_ARGS)
1758 {
1759         Oid                     val_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
1760         MemoryContext aggcontext,
1761                                 oldcontext;
1762         StringInfo      state;
1763         Datum           val;
1764         JsonTypeCategory tcategory;
1765         Oid                     outfuncoid;
1766
1767         if (val_type == InvalidOid)
1768                 ereport(ERROR,
1769                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1770                                  errmsg("could not determine input data type")));
1771
1772         if (!AggCheckCallContext(fcinfo, &aggcontext))
1773         {
1774                 /* cannot be called directly because of internal-type argument */
1775                 elog(ERROR, "json_agg_transfn called in non-aggregate context");
1776         }
1777
1778         if (PG_ARGISNULL(0))
1779         {
1780                 /*
1781                  * Make this StringInfo in a context where it will persist for the
1782                  * duration of the aggregate call.  MemoryContextSwitchTo is only
1783                  * needed the first time, as the StringInfo routines make sure they
1784                  * use the right context to enlarge the object if necessary.
1785                  */
1786                 oldcontext = MemoryContextSwitchTo(aggcontext);
1787                 state = makeStringInfo();
1788                 MemoryContextSwitchTo(oldcontext);
1789
1790                 appendStringInfoChar(state, '[');
1791         }
1792         else
1793         {
1794                 state = (StringInfo) PG_GETARG_POINTER(0);
1795                 appendStringInfoString(state, ", ");
1796         }
1797
1798         /* fast path for NULLs */
1799         if (PG_ARGISNULL(1))
1800         {
1801                 datum_to_json((Datum) 0, true, state, JSONTYPE_NULL, InvalidOid, false);
1802                 PG_RETURN_POINTER(state);
1803         }
1804
1805         val = PG_GETARG_DATUM(1);
1806
1807         /* XXX we do this every time?? */
1808         json_categorize_type(val_type,
1809                                                  &tcategory, &outfuncoid);
1810
1811         /* add some whitespace if structured type and not first item */
1812         if (!PG_ARGISNULL(0) &&
1813                 (tcategory == JSONTYPE_ARRAY || tcategory == JSONTYPE_COMPOSITE))
1814         {
1815                 appendStringInfoString(state, "\n ");
1816         }
1817
1818         datum_to_json(val, false, state, tcategory, outfuncoid, false);
1819
1820         /*
1821          * The transition type for array_agg() is declared to be "internal", which
1822          * is a pass-by-value type the same size as a pointer.  So we can safely
1823          * pass the ArrayBuildState pointer through nodeAgg.c's machinations.
1824          */
1825         PG_RETURN_POINTER(state);
1826 }
1827
1828 /*
1829  * json_agg final function
1830  */
1831 Datum
1832 json_agg_finalfn(PG_FUNCTION_ARGS)
1833 {
1834         StringInfo      state;
1835
1836         /* cannot be called directly because of internal-type argument */
1837         Assert(AggCheckCallContext(fcinfo, NULL));
1838
1839         state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
1840
1841         if (state == NULL)
1842                 PG_RETURN_NULL();
1843
1844         appendStringInfoChar(state, ']');
1845
1846         PG_RETURN_TEXT_P(cstring_to_text_with_len(state->data, state->len));
1847 }
1848
1849 /*
1850  * json_object_agg transition function.
1851  *
1852  * aggregate two input columns as a single json value.
1853  */
1854 Datum
1855 json_object_agg_transfn(PG_FUNCTION_ARGS)
1856 {
1857         Oid                     val_type;
1858         MemoryContext aggcontext,
1859                                 oldcontext;
1860         StringInfo      state;
1861         Datum           arg;
1862
1863         if (!AggCheckCallContext(fcinfo, &aggcontext))
1864         {
1865                 /* cannot be called directly because of internal-type argument */
1866                 elog(ERROR, "json_agg_transfn called in non-aggregate context");
1867         }
1868
1869         if (PG_ARGISNULL(0))
1870         {
1871                 /*
1872                  * Make this StringInfo in a context where it will persist for the
1873                  * duration off the aggregate call. It's only needed for this initial
1874                  * piece, as the StringInfo routines make sure they use the right
1875                  * context to enlarge the object if necessary.
1876                  */
1877                 oldcontext = MemoryContextSwitchTo(aggcontext);
1878                 state = makeStringInfo();
1879                 MemoryContextSwitchTo(oldcontext);
1880
1881                 appendStringInfoString(state, "{ ");
1882         }
1883         else
1884         {
1885                 state = (StringInfo) PG_GETARG_POINTER(0);
1886                 appendStringInfoString(state, ", ");
1887         }
1888
1889         if (PG_ARGISNULL(1))
1890                 ereport(ERROR,
1891                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1892                                  errmsg("field name must not be null")));
1893
1894         val_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
1895
1896         /*
1897          * turn a constant (more or less literal) value that's of unknown type
1898          * into text. Unknowns come in as a cstring pointer.
1899          */
1900         if (val_type == UNKNOWNOID && get_fn_expr_arg_stable(fcinfo->flinfo, 1))
1901         {
1902                 val_type = TEXTOID;
1903                 arg = CStringGetTextDatum(PG_GETARG_POINTER(1));
1904         }
1905         else
1906         {
1907                 arg = PG_GETARG_DATUM(1);
1908         }
1909
1910         if (val_type == InvalidOid || val_type == UNKNOWNOID)
1911                 ereport(ERROR,
1912                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1913                                  errmsg("could not determine data type for argument %d", 1)));
1914
1915         add_json(arg, false, state, val_type, true);
1916
1917         appendStringInfoString(state, " : ");
1918
1919         val_type = get_fn_expr_argtype(fcinfo->flinfo, 2);
1920         /* see comments above */
1921         if (val_type == UNKNOWNOID && get_fn_expr_arg_stable(fcinfo->flinfo, 2))
1922         {
1923                 val_type = TEXTOID;
1924                 if (PG_ARGISNULL(2))
1925                         arg = (Datum) 0;
1926                 else
1927                         arg = CStringGetTextDatum(PG_GETARG_POINTER(2));
1928         }
1929         else
1930         {
1931                 arg = PG_GETARG_DATUM(2);
1932         }
1933
1934         if (val_type == InvalidOid || val_type == UNKNOWNOID)
1935                 ereport(ERROR,
1936                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1937                                  errmsg("could not determine data type for argument %d", 2)));
1938
1939         add_json(arg, PG_ARGISNULL(2), state, val_type, false);
1940
1941         PG_RETURN_POINTER(state);
1942 }
1943
1944 /*
1945  * json_object_agg final function.
1946  *
1947  */
1948 Datum
1949 json_object_agg_finalfn(PG_FUNCTION_ARGS)
1950 {
1951         StringInfo      state;
1952
1953         /* cannot be called directly because of internal-type argument */
1954         Assert(AggCheckCallContext(fcinfo, NULL));
1955
1956         state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
1957
1958         if (state == NULL)
1959                 PG_RETURN_TEXT_P(cstring_to_text("{}"));
1960
1961         appendStringInfoString(state, " }");
1962
1963         PG_RETURN_TEXT_P(cstring_to_text_with_len(state->data, state->len));
1964 }
1965
1966 /*
1967  * SQL function json_build_object(variadic "any")
1968  */
1969 Datum
1970 json_build_object(PG_FUNCTION_ARGS)
1971 {
1972         int                     nargs = PG_NARGS();
1973         int                     i;
1974         Datum           arg;
1975         char       *sep = "";
1976         StringInfo      result;
1977         Oid                     val_type;
1978
1979         if (nargs % 2 != 0)
1980                 ereport(ERROR,
1981                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1982                                  errmsg("argument list must have even number of elements"),
1983                                  errhint("The arguments of json_build_object() must consist of alternating keys and values.")));
1984
1985         result = makeStringInfo();
1986
1987         appendStringInfoChar(result, '{');
1988
1989         for (i = 0; i < nargs; i += 2)
1990         {
1991                 /* process key */
1992
1993                 if (PG_ARGISNULL(i))
1994                         ereport(ERROR,
1995                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1996                                          errmsg("argument %d cannot be null", i + 1),
1997                                          errhint("Object keys should be text.")));
1998                 val_type = get_fn_expr_argtype(fcinfo->flinfo, i);
1999
2000                 /*
2001                  * turn a constant (more or less literal) value that's of unknown type
2002                  * into text. Unknowns come in as a cstring pointer.
2003                  */
2004                 if (val_type == UNKNOWNOID && get_fn_expr_arg_stable(fcinfo->flinfo, i))
2005                 {
2006                         val_type = TEXTOID;
2007                         arg = CStringGetTextDatum(PG_GETARG_POINTER(i));
2008                 }
2009                 else
2010                 {
2011                         arg = PG_GETARG_DATUM(i);
2012                 }
2013                 if (val_type == InvalidOid || val_type == UNKNOWNOID)
2014                         ereport(ERROR,
2015                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2016                                          errmsg("could not determine data type for argument %d",
2017                                                         i + 1)));
2018                 appendStringInfoString(result, sep);
2019                 sep = ", ";
2020                 add_json(arg, false, result, val_type, true);
2021
2022                 appendStringInfoString(result, " : ");
2023
2024                 /* process value */
2025
2026                 val_type = get_fn_expr_argtype(fcinfo->flinfo, i + 1);
2027                 /* see comments above */
2028                 if (val_type == UNKNOWNOID && get_fn_expr_arg_stable(fcinfo->flinfo, i + 1))
2029                 {
2030                         val_type = TEXTOID;
2031                         if (PG_ARGISNULL(i + 1))
2032                                 arg = (Datum) 0;
2033                         else
2034                                 arg = CStringGetTextDatum(PG_GETARG_POINTER(i + 1));
2035                 }
2036                 else
2037                 {
2038                         arg = PG_GETARG_DATUM(i + 1);
2039                 }
2040                 if (val_type == InvalidOid || val_type == UNKNOWNOID)
2041                         ereport(ERROR,
2042                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2043                                          errmsg("could not determine data type for argument %d",
2044                                                         i + 2)));
2045                 add_json(arg, PG_ARGISNULL(i + 1), result, val_type, false);
2046         }
2047
2048         appendStringInfoChar(result, '}');
2049
2050         PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
2051 }
2052
2053 /*
2054  * degenerate case of json_build_object where it gets 0 arguments.
2055  */
2056 Datum
2057 json_build_object_noargs(PG_FUNCTION_ARGS)
2058 {
2059         PG_RETURN_TEXT_P(cstring_to_text_with_len("{}", 2));
2060 }
2061
2062 /*
2063  * SQL function json_build_array(variadic "any")
2064  */
2065 Datum
2066 json_build_array(PG_FUNCTION_ARGS)
2067 {
2068         int                     nargs = PG_NARGS();
2069         int                     i;
2070         Datum           arg;
2071         char       *sep = "";
2072         StringInfo      result;
2073         Oid                     val_type;
2074
2075
2076         result = makeStringInfo();
2077
2078         appendStringInfoChar(result, '[');
2079
2080         for (i = 0; i < nargs; i++)
2081         {
2082                 val_type = get_fn_expr_argtype(fcinfo->flinfo, i);
2083                 arg = PG_GETARG_DATUM(i + 1);
2084                 /* see comments in json_build_object above */
2085                 if (val_type == UNKNOWNOID && get_fn_expr_arg_stable(fcinfo->flinfo, i))
2086                 {
2087                         val_type = TEXTOID;
2088                         if (PG_ARGISNULL(i))
2089                                 arg = (Datum) 0;
2090                         else
2091                                 arg = CStringGetTextDatum(PG_GETARG_POINTER(i));
2092                 }
2093                 else
2094                 {
2095                         arg = PG_GETARG_DATUM(i);
2096                 }
2097                 if (val_type == InvalidOid || val_type == UNKNOWNOID)
2098                         ereport(ERROR,
2099                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2100                                          errmsg("could not determine data type for argument %d",
2101                                                         i + 1)));
2102                 appendStringInfoString(result, sep);
2103                 sep = ", ";
2104                 add_json(arg, PG_ARGISNULL(i), result, val_type, false);
2105         }
2106         appendStringInfoChar(result, ']');
2107
2108         PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
2109
2110 }
2111
2112 /*
2113  * degenerate case of json_build_array where it gets 0 arguments.
2114  */
2115 Datum
2116 json_build_array_noargs(PG_FUNCTION_ARGS)
2117 {
2118         PG_RETURN_TEXT_P(cstring_to_text_with_len("[]", 2));
2119 }
2120
2121 /*
2122  * SQL function json_object(text[])
2123  *
2124  * take a one or two dimensional array of text as key/value pairs
2125  * for a json object.
2126  */
2127 Datum
2128 json_object(PG_FUNCTION_ARGS)
2129 {
2130         ArrayType  *in_array = PG_GETARG_ARRAYTYPE_P(0);
2131         int                     ndims = ARR_NDIM(in_array);
2132         StringInfoData result;
2133         Datum      *in_datums;
2134         bool       *in_nulls;
2135         int                     in_count,
2136                                 count,
2137                                 i;
2138         text       *rval;
2139         char       *v;
2140
2141         switch (ndims)
2142         {
2143                 case 0:
2144                         PG_RETURN_DATUM(CStringGetTextDatum("{}"));
2145                         break;
2146
2147                 case 1:
2148                         if ((ARR_DIMS(in_array)[0]) % 2)
2149                                 ereport(ERROR,
2150                                                 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2151                                                  errmsg("array must have even number of elements")));
2152                         break;
2153
2154                 case 2:
2155                         if ((ARR_DIMS(in_array)[1]) != 2)
2156                                 ereport(ERROR,
2157                                                 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2158                                                  errmsg("array must have two columns")));
2159                         break;
2160
2161                 default:
2162                         ereport(ERROR,
2163                                         (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2164                                          errmsg("wrong number of array subscripts")));
2165         }
2166
2167         deconstruct_array(in_array,
2168                                           TEXTOID, -1, false, 'i',
2169                                           &in_datums, &in_nulls, &in_count);
2170
2171         count = in_count / 2;
2172
2173         initStringInfo(&result);
2174
2175         appendStringInfoChar(&result, '{');
2176
2177         for (i = 0; i < count; ++i)
2178         {
2179                 if (in_nulls[i * 2])
2180                         ereport(ERROR,
2181                                         (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2182                                          errmsg("null value not allowed for object key")));
2183
2184                 v = TextDatumGetCString(in_datums[i * 2]);
2185                 if (i > 0)
2186                         appendStringInfoString(&result, ", ");
2187                 escape_json(&result, v);
2188                 appendStringInfoString(&result, " : ");
2189                 pfree(v);
2190                 if (in_nulls[i * 2 + 1])
2191                         appendStringInfoString(&result, "null");
2192                 else
2193                 {
2194                         v = TextDatumGetCString(in_datums[i * 2 + 1]);
2195                         escape_json(&result, v);
2196                         pfree(v);
2197                 }
2198         }
2199
2200         appendStringInfoChar(&result, '}');
2201
2202         pfree(in_datums);
2203         pfree(in_nulls);
2204
2205         rval = cstring_to_text_with_len(result.data, result.len);
2206         pfree(result.data);
2207
2208         PG_RETURN_TEXT_P(rval);
2209
2210 }
2211
2212 /*
2213  * SQL function json_object(text[], text[])
2214  *
2215  * take separate key and value arrays of text to construct a json object
2216  * pairwise.
2217  */
2218 Datum
2219 json_object_two_arg(PG_FUNCTION_ARGS)
2220 {
2221         ArrayType  *key_array = PG_GETARG_ARRAYTYPE_P(0);
2222         ArrayType  *val_array = PG_GETARG_ARRAYTYPE_P(1);
2223         int                     nkdims = ARR_NDIM(key_array);
2224         int                     nvdims = ARR_NDIM(val_array);
2225         StringInfoData result;
2226         Datum      *key_datums,
2227                            *val_datums;
2228         bool       *key_nulls,
2229                            *val_nulls;
2230         int                     key_count,
2231                                 val_count,
2232                                 i;
2233         text       *rval;
2234         char       *v;
2235
2236         if (nkdims > 1 || nkdims != nvdims)
2237                 ereport(ERROR,
2238                                 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2239                                  errmsg("wrong number of array subscripts")));
2240
2241         if (nkdims == 0)
2242                 PG_RETURN_DATUM(CStringGetTextDatum("{}"));
2243
2244         deconstruct_array(key_array,
2245                                           TEXTOID, -1, false, 'i',
2246                                           &key_datums, &key_nulls, &key_count);
2247
2248         deconstruct_array(val_array,
2249                                           TEXTOID, -1, false, 'i',
2250                                           &val_datums, &val_nulls, &val_count);
2251
2252         if (key_count != val_count)
2253                 ereport(ERROR,
2254                                 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2255                                  errmsg("mismatched array dimensions")));
2256
2257         initStringInfo(&result);
2258
2259         appendStringInfoChar(&result, '{');
2260
2261         for (i = 0; i < key_count; ++i)
2262         {
2263                 if (key_nulls[i])
2264                         ereport(ERROR,
2265                                         (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2266                                          errmsg("null value not allowed for object key")));
2267
2268                 v = TextDatumGetCString(key_datums[i]);
2269                 if (i > 0)
2270                         appendStringInfoString(&result, ", ");
2271                 escape_json(&result, v);
2272                 appendStringInfoString(&result, " : ");
2273                 pfree(v);
2274                 if (val_nulls[i])
2275                         appendStringInfoString(&result, "null");
2276                 else
2277                 {
2278                         v = TextDatumGetCString(val_datums[i]);
2279                         escape_json(&result, v);
2280                         pfree(v);
2281                 }
2282         }
2283
2284         appendStringInfoChar(&result, '}');
2285
2286         pfree(key_datums);
2287         pfree(key_nulls);
2288         pfree(val_datums);
2289         pfree(val_nulls);
2290
2291         rval = cstring_to_text_with_len(result.data, result.len);
2292         pfree(result.data);
2293
2294         PG_RETURN_TEXT_P(rval);
2295 }
2296
2297
2298 /*
2299  * Produce a JSON string literal, properly escaping characters in the text.
2300  */
2301 void
2302 escape_json(StringInfo buf, const char *str)
2303 {
2304         const char *p;
2305
2306         appendStringInfoCharMacro(buf, '\"');
2307         for (p = str; *p; p++)
2308         {
2309                 switch (*p)
2310                 {
2311                         case '\b':
2312                                 appendStringInfoString(buf, "\\b");
2313                                 break;
2314                         case '\f':
2315                                 appendStringInfoString(buf, "\\f");
2316                                 break;
2317                         case '\n':
2318                                 appendStringInfoString(buf, "\\n");
2319                                 break;
2320                         case '\r':
2321                                 appendStringInfoString(buf, "\\r");
2322                                 break;
2323                         case '\t':
2324                                 appendStringInfoString(buf, "\\t");
2325                                 break;
2326                         case '"':
2327                                 appendStringInfoString(buf, "\\\"");
2328                                 break;
2329                         case '\\':
2330                                 /*
2331                                  * Unicode escapes are passed through as is. There is no
2332                                  * requirement that they denote a valid character in the
2333                                  * server encoding - indeed that is a big part of their
2334                                  * usefulness.
2335                                  *
2336                                  * All we require is that they consist of \uXXXX where
2337                                  * the Xs are hexadecimal digits. It is the responsibility
2338                                  * of the caller of, say, to_json() to make sure that the
2339                                  * unicode escape is valid.
2340                                  *
2341                                  * In the case of a jsonb string value being escaped, the
2342                                  * only unicode escape that should be present is \u0000,
2343                                  * all the other unicode escapes will have been resolved.
2344                                  */
2345                                 if (p[1] == 'u' &&
2346                                         isxdigit((unsigned char) p[2]) &&
2347                                         isxdigit((unsigned char) p[3]) &&
2348                                         isxdigit((unsigned char) p[4]) &&
2349                                         isxdigit((unsigned char) p[5]))
2350                                         appendStringInfoCharMacro(buf, *p);
2351                                 else
2352                                         appendStringInfoString(buf, "\\\\");
2353                                 break;
2354                         default:
2355                                 if ((unsigned char) *p < ' ')
2356                                         appendStringInfo(buf, "\\u%04x", (int) *p);
2357                                 else
2358                                         appendStringInfoCharMacro(buf, *p);
2359                                 break;
2360                 }
2361         }
2362         appendStringInfoCharMacro(buf, '\"');
2363 }
2364
2365 /*
2366  * SQL function json_typeof(json) -> text
2367  *
2368  * Returns the type of the outermost JSON value as TEXT.  Possible types are
2369  * "object", "array", "string", "number", "boolean", and "null".
2370  *
2371  * Performs a single call to json_lex() to get the first token of the supplied
2372  * value.  This initial token uniquely determines the value's type.  As our
2373  * input must already have been validated by json_in() or json_recv(), the
2374  * initial token should never be JSON_TOKEN_OBJECT_END, JSON_TOKEN_ARRAY_END,
2375  * JSON_TOKEN_COLON, JSON_TOKEN_COMMA, or JSON_TOKEN_END.
2376  */
2377 Datum
2378 json_typeof(PG_FUNCTION_ARGS)
2379 {
2380         text       *json;
2381
2382         JsonLexContext *lex;
2383         JsonTokenType tok;
2384         char       *type;
2385
2386         json = PG_GETARG_TEXT_P(0);
2387         lex = makeJsonLexContext(json, false);
2388
2389         /* Lex exactly one token from the input and check its type. */
2390         json_lex(lex);
2391         tok = lex_peek(lex);
2392         switch (tok)
2393         {
2394                 case JSON_TOKEN_OBJECT_START:
2395                         type = "object";
2396                         break;
2397                 case JSON_TOKEN_ARRAY_START:
2398                         type = "array";
2399                         break;
2400                 case JSON_TOKEN_STRING:
2401                         type = "string";
2402                         break;
2403                 case JSON_TOKEN_NUMBER:
2404                         type = "number";
2405                         break;
2406                 case JSON_TOKEN_TRUE:
2407                 case JSON_TOKEN_FALSE:
2408                         type = "boolean";
2409                         break;
2410                 case JSON_TOKEN_NULL:
2411                         type = "null";
2412                         break;
2413                 default:
2414                         elog(ERROR, "unexpected json token: %d", tok);
2415         }
2416
2417         PG_RETURN_TEXT_P(cstring_to_text(type));
2418 }