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