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