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