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