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