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