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