1 /*-------------------------------------------------------------------------
4 * JSON data type support.
6 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/backend/utils/adt/json.c
12 *-------------------------------------------------------------------------
16 #include "access/htup_details.h"
17 #include "access/transam.h"
18 #include "catalog/pg_type.h"
19 #include "executor/spi.h"
20 #include "lib/stringinfo.h"
21 #include "libpq/pqformat.h"
22 #include "mb/pg_wchar.h"
23 #include "miscadmin.h"
24 #include "parser/parse_coerce.h"
25 #include "utils/array.h"
26 #include "utils/builtins.h"
27 #include "utils/date.h"
28 #include "utils/datetime.h"
29 #include "utils/lsyscache.h"
30 #include "utils/json.h"
31 #include "utils/jsonapi.h"
32 #include "utils/typcache.h"
33 #include "utils/syscache.h"
36 * The context of the parser is maintained by the recursive descent
37 * mechanism, but is passed explicitly to the error reporting routine
38 * for better diagnostics.
40 typedef enum /* contexts of JSON parser */
42 JSON_PARSE_VALUE, /* expecting a value */
43 JSON_PARSE_STRING, /* expecting a string (for a field name) */
44 JSON_PARSE_ARRAY_START, /* saw '[', expecting value or ']' */
45 JSON_PARSE_ARRAY_NEXT, /* saw array element, expecting ',' or ']' */
46 JSON_PARSE_OBJECT_START, /* saw '{', expecting label or '}' */
47 JSON_PARSE_OBJECT_LABEL, /* saw object label, expecting ':' */
48 JSON_PARSE_OBJECT_NEXT, /* saw object value, expecting ',' or '}' */
49 JSON_PARSE_OBJECT_COMMA, /* saw object ',', expecting next label */
50 JSON_PARSE_END /* saw the end of a document, expect nothing */
53 typedef enum /* type categories for datum_to_json */
55 JSONTYPE_NULL, /* null, so we didn't bother to identify */
56 JSONTYPE_BOOL, /* boolean (built-in types only) */
57 JSONTYPE_NUMERIC, /* numeric (ditto) */
58 JSONTYPE_DATE, /* we use special formatting for datetimes */
61 JSONTYPE_JSON, /* JSON itself (and JSONB) */
62 JSONTYPE_ARRAY, /* array */
63 JSONTYPE_COMPOSITE, /* composite */
64 JSONTYPE_CAST, /* something with an explicit cast to JSON */
65 JSONTYPE_OTHER /* all else */
68 typedef struct JsonAggState
71 JsonTypeCategory key_category;
73 JsonTypeCategory val_category;
77 static inline void json_lex(JsonLexContext *lex);
78 static inline void json_lex_string(JsonLexContext *lex);
79 static inline void json_lex_number(JsonLexContext *lex, char *s,
80 bool *num_err, int *total_len);
81 static inline void parse_scalar(JsonLexContext *lex, JsonSemAction *sem);
82 static void parse_object_field(JsonLexContext *lex, JsonSemAction *sem);
83 static void parse_object(JsonLexContext *lex, JsonSemAction *sem);
84 static void parse_array_element(JsonLexContext *lex, JsonSemAction *sem);
85 static void parse_array(JsonLexContext *lex, JsonSemAction *sem);
86 static void report_parse_error(JsonParseContext ctx, JsonLexContext *lex);
87 static void report_invalid_token(JsonLexContext *lex);
88 static int report_json_context(JsonLexContext *lex);
89 static char *extract_mb_char(char *s);
90 static void composite_to_json(Datum composite, StringInfo result,
92 static void array_dim_to_json(StringInfo result, int dim, int ndims, int *dims,
93 Datum *vals, bool *nulls, int *valcount,
94 JsonTypeCategory tcategory, Oid outfuncoid,
96 static void array_to_json_internal(Datum array, StringInfo result,
98 static void json_categorize_type(Oid typoid,
99 JsonTypeCategory *tcategory,
101 static void datum_to_json(Datum val, bool is_null, StringInfo result,
102 JsonTypeCategory tcategory, Oid outfuncoid,
104 static void add_json(Datum val, bool is_null, StringInfo result,
105 Oid val_type, bool key_scalar);
106 static text *catenate_stringinfo_string(StringInfo buffer, const char *addon);
108 /* the null action object used for pure validation */
109 static JsonSemAction nullSemAction =
111 NULL, NULL, NULL, NULL, NULL,
112 NULL, NULL, NULL, NULL, NULL
115 /* Recursive Descent parser support routines */
120 * what is the current look_ahead token?
122 static inline JsonTokenType
123 lex_peek(JsonLexContext *lex)
125 return lex->token_type;
131 * accept the look_ahead token and move the lexer to the next token if the
132 * look_ahead token matches the token parameter. In that case, and if required,
133 * also hand back the de-escaped lexeme.
135 * returns true if the token matched, false otherwise.
138 lex_accept(JsonLexContext *lex, JsonTokenType token, char **lexeme)
140 if (lex->token_type == token)
144 if (lex->token_type == JSON_TOKEN_STRING)
146 if (lex->strval != NULL)
147 *lexeme = pstrdup(lex->strval->data);
151 int len = (lex->token_terminator - lex->token_start);
152 char *tokstr = palloc(len + 1);
154 memcpy(tokstr, lex->token_start, len);
168 * move the lexer to the next token if the current look_ahead token matches
169 * the parameter token. Otherwise, report an error.
172 lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
174 if (!lex_accept(lex, token, NULL))
175 report_parse_error(ctx, lex);
178 /* chars to consider as part of an alphanumeric token */
179 #define JSON_ALPHANUMERIC_CHAR(c) \
180 (((c) >= 'a' && (c) <= 'z') || \
181 ((c) >= 'A' && (c) <= 'Z') || \
182 ((c) >= '0' && (c) <= '9') || \
187 * Utility function to check if a string is a valid JSON number.
189 * str is of length len, and need not be null-terminated.
192 IsValidJsonNumber(const char *str, int len)
196 JsonLexContext dummy_lex;
202 * json_lex_number expects a leading '-' to have been eaten already.
204 * having to cast away the constness of str is ugly, but there's not much
209 dummy_lex.input = (char *) str + 1;
210 dummy_lex.input_length = len - 1;
214 dummy_lex.input = (char *) str;
215 dummy_lex.input_length = len;
218 json_lex_number(&dummy_lex, dummy_lex.input, &numeric_error, &total_len);
220 return (!numeric_error) && (total_len == dummy_lex.input_length);
227 json_in(PG_FUNCTION_ARGS)
229 char *json = PG_GETARG_CSTRING(0);
230 text *result = cstring_to_text(json);
234 lex = makeJsonLexContext(result, false);
235 pg_parse_json(lex, &nullSemAction);
237 /* Internal representation is the same as text, for now */
238 PG_RETURN_TEXT_P(result);
245 json_out(PG_FUNCTION_ARGS)
247 /* we needn't detoast because text_to_cstring will handle that */
248 Datum txt = PG_GETARG_DATUM(0);
250 PG_RETURN_CSTRING(TextDatumGetCString(txt));
257 json_send(PG_FUNCTION_ARGS)
259 text *t = PG_GETARG_TEXT_PP(0);
262 pq_begintypsend(&buf);
263 pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
264 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
271 json_recv(PG_FUNCTION_ARGS)
273 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
278 str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
281 lex = makeJsonLexContextCstringLen(str, nbytes, false);
282 pg_parse_json(lex, &nullSemAction);
284 PG_RETURN_TEXT_P(cstring_to_text_with_len(str, nbytes));
290 * lex constructor, with or without StringInfo object
291 * for de-escaped lexemes.
293 * Without is better as it makes the processing faster, so only make one
294 * if really required.
296 * If you already have the json as a text* value, use the first of these
297 * functions, otherwise use makeJsonLexContextCstringLen().
300 makeJsonLexContext(text *json, bool need_escapes)
302 return makeJsonLexContextCstringLen(VARDATA_ANY(json),
303 VARSIZE_ANY_EXHDR(json),
308 makeJsonLexContextCstringLen(char *json, int len, bool need_escapes)
310 JsonLexContext *lex = palloc0(sizeof(JsonLexContext));
312 lex->input = lex->token_terminator = lex->line_start = json;
313 lex->line_number = 1;
314 lex->input_length = len;
316 lex->strval = makeStringInfo();
323 * Publicly visible entry point for the JSON parser.
325 * lex is a lexing context, set up for the json to be processed by calling
326 * makeJsonLexContext(). sem is a structure of function pointers to semantic
327 * action routines to be called at appropriate spots during parsing, and a
328 * pointer to a state object to be passed to those routines.
331 pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
335 /* get the initial token */
340 /* parse by recursive descent */
343 case JSON_TOKEN_OBJECT_START:
344 parse_object(lex, sem);
346 case JSON_TOKEN_ARRAY_START:
347 parse_array(lex, sem);
350 parse_scalar(lex, sem); /* json can be a bare scalar */
353 lex_expect(JSON_PARSE_END, lex, JSON_TOKEN_END);
358 * json_count_array_elements
360 * Returns number of array elements in lex context at start of array token
361 * until end of array token at same nesting level.
363 * Designed to be called from array_start routines.
366 json_count_array_elements(JsonLexContext *lex)
368 JsonLexContext copylex;
372 * It's safe to do this with a shallow copy because the lexical routines
373 * don't scribble on the input. They do scribble on the other pointers
374 * etc, so doing this with a copy makes that safe.
376 memcpy(©lex, lex, sizeof(JsonLexContext));
377 copylex.strval = NULL; /* not interested in values here */
381 lex_expect(JSON_PARSE_ARRAY_START, ©lex, JSON_TOKEN_ARRAY_START);
382 if (lex_peek(©lex) != JSON_TOKEN_ARRAY_END)
387 parse_array_element(©lex, &nullSemAction);
389 while (lex_accept(©lex, JSON_TOKEN_COMMA, NULL));
391 lex_expect(JSON_PARSE_ARRAY_NEXT, ©lex, JSON_TOKEN_ARRAY_END);
397 * Recursive Descent parse routines. There is one for each structural
398 * element in a json document:
399 * - scalar (string, number, true, false, null)
406 parse_scalar(JsonLexContext *lex, JsonSemAction *sem)
409 json_scalar_action sfunc = sem->scalar;
411 JsonTokenType tok = lex_peek(lex);
413 valaddr = sfunc == NULL ? NULL : &val;
415 /* a scalar must be a string, a number, true, false, or null */
418 case JSON_TOKEN_TRUE:
419 lex_accept(lex, JSON_TOKEN_TRUE, valaddr);
421 case JSON_TOKEN_FALSE:
422 lex_accept(lex, JSON_TOKEN_FALSE, valaddr);
424 case JSON_TOKEN_NULL:
425 lex_accept(lex, JSON_TOKEN_NULL, valaddr);
427 case JSON_TOKEN_NUMBER:
428 lex_accept(lex, JSON_TOKEN_NUMBER, valaddr);
430 case JSON_TOKEN_STRING:
431 lex_accept(lex, JSON_TOKEN_STRING, valaddr);
434 report_parse_error(JSON_PARSE_VALUE, lex);
438 (*sfunc) (sem->semstate, val, tok);
442 parse_object_field(JsonLexContext *lex, JsonSemAction *sem)
445 * An object field is "fieldname" : value where value can be a scalar,
446 * object or array. Note: in user-facing docs and error messages, we
447 * generally call a field name a "key".
450 char *fname = NULL; /* keep compiler quiet */
451 json_ofield_action ostart = sem->object_field_start;
452 json_ofield_action oend = sem->object_field_end;
454 char **fnameaddr = NULL;
457 if (ostart != NULL || oend != NULL)
460 if (!lex_accept(lex, JSON_TOKEN_STRING, fnameaddr))
461 report_parse_error(JSON_PARSE_STRING, lex);
463 lex_expect(JSON_PARSE_OBJECT_LABEL, lex, JSON_TOKEN_COLON);
466 isnull = tok == JSON_TOKEN_NULL;
469 (*ostart) (sem->semstate, fname, isnull);
473 case JSON_TOKEN_OBJECT_START:
474 parse_object(lex, sem);
476 case JSON_TOKEN_ARRAY_START:
477 parse_array(lex, sem);
480 parse_scalar(lex, sem);
484 (*oend) (sem->semstate, fname, isnull);
488 parse_object(JsonLexContext *lex, JsonSemAction *sem)
491 * an object is a possibly empty sequence of object fields, separated by
492 * commas and surrounded by curly braces.
494 json_struct_action ostart = sem->object_start;
495 json_struct_action oend = sem->object_end;
501 (*ostart) (sem->semstate);
504 * Data inside an object is at a higher nesting level than the object
505 * itself. Note that we increment this after we call the semantic routine
506 * for the object start and restore it before we call the routine for the
511 /* we know this will succeed, just clearing the token */
512 lex_expect(JSON_PARSE_OBJECT_START, lex, JSON_TOKEN_OBJECT_START);
517 case JSON_TOKEN_STRING:
518 parse_object_field(lex, sem);
519 while (lex_accept(lex, JSON_TOKEN_COMMA, NULL))
520 parse_object_field(lex, sem);
522 case JSON_TOKEN_OBJECT_END:
525 /* case of an invalid initial token inside the object */
526 report_parse_error(JSON_PARSE_OBJECT_START, lex);
529 lex_expect(JSON_PARSE_OBJECT_NEXT, lex, JSON_TOKEN_OBJECT_END);
534 (*oend) (sem->semstate);
538 parse_array_element(JsonLexContext *lex, JsonSemAction *sem)
540 json_aelem_action astart = sem->array_element_start;
541 json_aelem_action aend = sem->array_element_end;
542 JsonTokenType tok = lex_peek(lex);
546 isnull = tok == JSON_TOKEN_NULL;
549 (*astart) (sem->semstate, isnull);
551 /* an array element is any object, array or scalar */
554 case JSON_TOKEN_OBJECT_START:
555 parse_object(lex, sem);
557 case JSON_TOKEN_ARRAY_START:
558 parse_array(lex, sem);
561 parse_scalar(lex, sem);
565 (*aend) (sem->semstate, isnull);
569 parse_array(JsonLexContext *lex, JsonSemAction *sem)
572 * an array is a possibly empty sequence of array elements, separated by
573 * commas and surrounded by square brackets.
575 json_struct_action astart = sem->array_start;
576 json_struct_action aend = sem->array_end;
581 (*astart) (sem->semstate);
584 * Data inside an array is at a higher nesting level than the array
585 * itself. Note that we increment this after we call the semantic routine
586 * for the array start and restore it before we call the routine for the
591 lex_expect(JSON_PARSE_ARRAY_START, lex, JSON_TOKEN_ARRAY_START);
592 if (lex_peek(lex) != JSON_TOKEN_ARRAY_END)
595 parse_array_element(lex, sem);
597 while (lex_accept(lex, JSON_TOKEN_COMMA, NULL))
598 parse_array_element(lex, sem);
601 lex_expect(JSON_PARSE_ARRAY_NEXT, lex, JSON_TOKEN_ARRAY_END);
606 (*aend) (sem->semstate);
610 * Lex one token from the input stream.
613 json_lex(JsonLexContext *lex)
618 /* Skip leading whitespace. */
619 s = lex->token_terminator;
620 len = s - lex->input;
621 while (len < lex->input_length &&
622 (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'))
629 lex->token_start = s;
631 /* Determine token type. */
632 if (len >= lex->input_length)
634 lex->token_start = NULL;
635 lex->prev_token_terminator = lex->token_terminator;
636 lex->token_terminator = s;
637 lex->token_type = JSON_TOKEN_END;
642 /* Single-character token, some kind of punctuation mark. */
644 lex->prev_token_terminator = lex->token_terminator;
645 lex->token_terminator = s + 1;
646 lex->token_type = JSON_TOKEN_OBJECT_START;
649 lex->prev_token_terminator = lex->token_terminator;
650 lex->token_terminator = s + 1;
651 lex->token_type = JSON_TOKEN_OBJECT_END;
654 lex->prev_token_terminator = lex->token_terminator;
655 lex->token_terminator = s + 1;
656 lex->token_type = JSON_TOKEN_ARRAY_START;
659 lex->prev_token_terminator = lex->token_terminator;
660 lex->token_terminator = s + 1;
661 lex->token_type = JSON_TOKEN_ARRAY_END;
664 lex->prev_token_terminator = lex->token_terminator;
665 lex->token_terminator = s + 1;
666 lex->token_type = JSON_TOKEN_COMMA;
669 lex->prev_token_terminator = lex->token_terminator;
670 lex->token_terminator = s + 1;
671 lex->token_type = JSON_TOKEN_COLON;
675 json_lex_string(lex);
676 lex->token_type = JSON_TOKEN_STRING;
679 /* Negative number. */
680 json_lex_number(lex, s + 1, NULL, NULL);
681 lex->token_type = JSON_TOKEN_NUMBER;
693 /* Positive number. */
694 json_lex_number(lex, s, NULL, NULL);
695 lex->token_type = JSON_TOKEN_NUMBER;
702 * We're not dealing with a string, number, legal
703 * punctuation mark, or end of string. The only legal
704 * tokens we might find here are true, false, and null,
705 * but for error reporting purposes we scan until we see a
706 * non-alphanumeric character. That way, we can report
707 * the whole word as an unexpected token, rather than just
708 * some unintuitive prefix thereof.
710 for (p = s; p - s < lex->input_length - len && JSON_ALPHANUMERIC_CHAR(*p); p++)
714 * We got some sort of unexpected punctuation or an
715 * otherwise unexpected character, so just complain about
716 * that one character.
720 lex->prev_token_terminator = lex->token_terminator;
721 lex->token_terminator = s + 1;
722 report_invalid_token(lex);
726 * We've got a real alphanumeric token here. If it
727 * happens to be true, false, or null, all is well. If
730 lex->prev_token_terminator = lex->token_terminator;
731 lex->token_terminator = p;
734 if (memcmp(s, "true", 4) == 0)
735 lex->token_type = JSON_TOKEN_TRUE;
736 else if (memcmp(s, "null", 4) == 0)
737 lex->token_type = JSON_TOKEN_NULL;
739 report_invalid_token(lex);
741 else if (p - s == 5 && memcmp(s, "false", 5) == 0)
742 lex->token_type = JSON_TOKEN_FALSE;
744 report_invalid_token(lex);
747 } /* end of switch */
751 * The next token in the input stream is known to be a string; lex it.
754 json_lex_string(JsonLexContext *lex)
758 int hi_surrogate = -1;
760 if (lex->strval != NULL)
761 resetStringInfo(lex->strval);
763 Assert(lex->input_length > 0);
764 s = lex->token_start;
765 len = lex->token_start - lex->input;
770 /* Premature end of the string. */
771 if (len >= lex->input_length)
773 lex->token_terminator = s;
774 report_invalid_token(lex);
778 else if ((unsigned char) *s < 32)
780 /* Per RFC4627, these characters MUST be escaped. */
781 /* Since *s isn't printable, exclude it from the context string */
782 lex->token_terminator = s;
784 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
785 errmsg("invalid input syntax for type %s", "json"),
786 errdetail("Character with value 0x%02x must be escaped.",
788 report_json_context(lex)));
792 /* OK, we have an escape character. */
795 if (len >= lex->input_length)
797 lex->token_terminator = s;
798 report_invalid_token(lex);
805 for (i = 1; i <= 4; i++)
809 if (len >= lex->input_length)
811 lex->token_terminator = s;
812 report_invalid_token(lex);
814 else if (*s >= '0' && *s <= '9')
815 ch = (ch * 16) + (*s - '0');
816 else if (*s >= 'a' && *s <= 'f')
817 ch = (ch * 16) + (*s - 'a') + 10;
818 else if (*s >= 'A' && *s <= 'F')
819 ch = (ch * 16) + (*s - 'A') + 10;
822 lex->token_terminator = s + pg_mblen(s);
824 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
825 errmsg("invalid input syntax for type %s",
827 errdetail("\"\\u\" must be followed by four hexadecimal digits."),
828 report_json_context(lex)));
831 if (lex->strval != NULL)
836 if (ch >= 0xd800 && ch <= 0xdbff)
838 if (hi_surrogate != -1)
840 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
841 errmsg("invalid input syntax for type %s",
843 errdetail("Unicode high surrogate must not follow a high surrogate."),
844 report_json_context(lex)));
845 hi_surrogate = (ch & 0x3ff) << 10;
848 else if (ch >= 0xdc00 && ch <= 0xdfff)
850 if (hi_surrogate == -1)
852 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
853 errmsg("invalid input syntax for type %s", "json"),
854 errdetail("Unicode low surrogate must follow a high surrogate."),
855 report_json_context(lex)));
856 ch = 0x10000 + hi_surrogate + (ch & 0x3ff);
860 if (hi_surrogate != -1)
862 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
863 errmsg("invalid input syntax for type %s", "json"),
864 errdetail("Unicode low surrogate must follow a high surrogate."),
865 report_json_context(lex)));
868 * For UTF8, replace the escape sequence by the actual
869 * utf8 character in lex->strval. Do this also for other
870 * encodings if the escape designates an ASCII character,
871 * otherwise raise an error.
876 /* We can't allow this, since our TEXT type doesn't */
878 (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
879 errmsg("unsupported Unicode escape sequence"),
880 errdetail("\\u0000 cannot be converted to text."),
881 report_json_context(lex)));
883 else if (GetDatabaseEncoding() == PG_UTF8)
885 unicode_to_utf8(ch, (unsigned char *) utf8str);
886 utf8len = pg_utf_mblen((unsigned char *) utf8str);
887 appendBinaryStringInfo(lex->strval, utf8str, utf8len);
889 else if (ch <= 0x007f)
892 * This is the only way to designate things like a
893 * form feed character in JSON, so it's useful in all
896 appendStringInfoChar(lex->strval, (char) ch);
901 (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
902 errmsg("unsupported Unicode escape sequence"),
903 errdetail("Unicode escape values cannot be used for code point values above 007F when the server encoding is not UTF8."),
904 report_json_context(lex)));
909 else if (lex->strval != NULL)
911 if (hi_surrogate != -1)
913 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
914 errmsg("invalid input syntax for type %s",
916 errdetail("Unicode low surrogate must follow a high surrogate."),
917 report_json_context(lex)));
924 appendStringInfoChar(lex->strval, *s);
927 appendStringInfoChar(lex->strval, '\b');
930 appendStringInfoChar(lex->strval, '\f');
933 appendStringInfoChar(lex->strval, '\n');
936 appendStringInfoChar(lex->strval, '\r');
939 appendStringInfoChar(lex->strval, '\t');
942 /* Not a valid string escape, so error out. */
943 lex->token_terminator = s + pg_mblen(s);
945 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
946 errmsg("invalid input syntax for type %s",
948 errdetail("Escape sequence \"\\%s\" is invalid.",
950 report_json_context(lex)));
953 else if (strchr("\"\\/bfnrt", *s) == NULL)
956 * Simpler processing if we're not bothered about de-escaping
958 * It's very tempting to remove the strchr() call here and
959 * replace it with a switch statement, but testing so far has
960 * shown it's not a performance win.
962 lex->token_terminator = s + pg_mblen(s);
964 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
965 errmsg("invalid input syntax for type %s", "json"),
966 errdetail("Escape sequence \"\\%s\" is invalid.",
968 report_json_context(lex)));
972 else if (lex->strval != NULL)
974 if (hi_surrogate != -1)
976 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
977 errmsg("invalid input syntax for type %s", "json"),
978 errdetail("Unicode low surrogate must follow a high surrogate."),
979 report_json_context(lex)));
981 appendStringInfoChar(lex->strval, *s);
986 if (hi_surrogate != -1)
988 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
989 errmsg("invalid input syntax for type %s", "json"),
990 errdetail("Unicode low surrogate must follow a high surrogate."),
991 report_json_context(lex)));
993 /* Hooray, we found the end of the string! */
994 lex->prev_token_terminator = lex->token_terminator;
995 lex->token_terminator = s + 1;
999 * The next token in the input stream is known to be a number; lex it.
1001 * In JSON, a number consists of four parts:
1003 * (1) An optional minus sign ('-').
1005 * (2) Either a single '0', or a string of one or more digits that does not
1008 * (3) An optional decimal part, consisting of a period ('.') followed by
1009 * one or more digits. (Note: While this part can be omitted
1010 * completely, it's not OK to have only the decimal point without
1011 * any digits afterwards.)
1013 * (4) An optional exponent part, consisting of 'e' or 'E', optionally
1014 * followed by '+' or '-', followed by one or more digits. (Note:
1015 * As with the decimal part, if 'e' or 'E' is present, it must be
1016 * followed by at least one digit.)
1018 * The 's' argument to this function points to the ostensible beginning
1019 * of part 2 - i.e. the character after any optional minus sign, or the
1020 * first character of the string if there is none.
1022 * If num_err is not NULL, we return an error flag to *num_err rather than
1023 * raising an error for a badly-formed number. Also, if total_len is not NULL
1024 * the distance from lex->input to the token end+1 is returned to *total_len.
1027 json_lex_number(JsonLexContext *lex, char *s,
1028 bool *num_err, int *total_len)
1031 int len = s - lex->input;
1033 /* Part (1): leading sign indicator. */
1034 /* Caller already did this for us; so do nothing. */
1036 /* Part (2): parse main digit string. */
1037 if (len < lex->input_length && *s == '0')
1042 else if (len < lex->input_length && *s >= '1' && *s <= '9')
1048 } while (len < lex->input_length && *s >= '0' && *s <= '9');
1053 /* Part (3): parse optional decimal portion. */
1054 if (len < lex->input_length && *s == '.')
1058 if (len == lex->input_length || *s < '0' || *s > '9')
1066 } while (len < lex->input_length && *s >= '0' && *s <= '9');
1070 /* Part (4): parse optional exponent. */
1071 if (len < lex->input_length && (*s == 'e' || *s == 'E'))
1075 if (len < lex->input_length && (*s == '+' || *s == '-'))
1080 if (len == lex->input_length || *s < '0' || *s > '9')
1088 } while (len < lex->input_length && *s >= '0' && *s <= '9');
1093 * Check for trailing garbage. As in json_lex(), any alphanumeric stuff
1094 * here should be considered part of the token for error-reporting
1097 for (; len < lex->input_length && JSON_ALPHANUMERIC_CHAR(*s); s++, len++)
1100 if (total_len != NULL)
1103 if (num_err != NULL)
1105 /* let the caller handle any error */
1110 /* return token endpoint */
1111 lex->prev_token_terminator = lex->token_terminator;
1112 lex->token_terminator = s;
1113 /* handle error if any */
1115 report_invalid_token(lex);
1120 * Report a parse error.
1122 * lex->token_start and lex->token_terminator must identify the current token.
1125 report_parse_error(JsonParseContext ctx, JsonLexContext *lex)
1130 /* Handle case where the input ended prematurely. */
1131 if (lex->token_start == NULL || lex->token_type == JSON_TOKEN_END)
1133 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1134 errmsg("invalid input syntax for type %s", "json"),
1135 errdetail("The input string ended unexpectedly."),
1136 report_json_context(lex)));
1138 /* Separate out the current token. */
1139 toklen = lex->token_terminator - lex->token_start;
1140 token = palloc(toklen + 1);
1141 memcpy(token, lex->token_start, toklen);
1142 token[toklen] = '\0';
1144 /* Complain, with the appropriate detail message. */
1145 if (ctx == JSON_PARSE_END)
1147 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1148 errmsg("invalid input syntax for type %s", "json"),
1149 errdetail("Expected end of input, but found \"%s\".",
1151 report_json_context(lex)));
1156 case JSON_PARSE_VALUE:
1158 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1159 errmsg("invalid input syntax for type %s", "json"),
1160 errdetail("Expected JSON value, but found \"%s\".",
1162 report_json_context(lex)));
1164 case JSON_PARSE_STRING:
1166 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1167 errmsg("invalid input syntax for type %s", "json"),
1168 errdetail("Expected string, but found \"%s\".",
1170 report_json_context(lex)));
1172 case JSON_PARSE_ARRAY_START:
1174 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1175 errmsg("invalid input syntax for type %s", "json"),
1176 errdetail("Expected array element or \"]\", but found \"%s\".",
1178 report_json_context(lex)));
1180 case JSON_PARSE_ARRAY_NEXT:
1182 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1183 errmsg("invalid input syntax for type %s", "json"),
1184 errdetail("Expected \",\" or \"]\", but found \"%s\".",
1186 report_json_context(lex)));
1188 case JSON_PARSE_OBJECT_START:
1190 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1191 errmsg("invalid input syntax for type %s", "json"),
1192 errdetail("Expected string or \"}\", but found \"%s\".",
1194 report_json_context(lex)));
1196 case JSON_PARSE_OBJECT_LABEL:
1198 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1199 errmsg("invalid input syntax for type %s", "json"),
1200 errdetail("Expected \":\", but found \"%s\".",
1202 report_json_context(lex)));
1204 case JSON_PARSE_OBJECT_NEXT:
1206 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1207 errmsg("invalid input syntax for type %s", "json"),
1208 errdetail("Expected \",\" or \"}\", but found \"%s\".",
1210 report_json_context(lex)));
1212 case JSON_PARSE_OBJECT_COMMA:
1214 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1215 errmsg("invalid input syntax for type %s", "json"),
1216 errdetail("Expected string, but found \"%s\".",
1218 report_json_context(lex)));
1221 elog(ERROR, "unexpected json parse state: %d", ctx);
1227 * Report an invalid input token.
1229 * lex->token_start and lex->token_terminator must identify the token.
1232 report_invalid_token(JsonLexContext *lex)
1237 /* Separate out the offending token. */
1238 toklen = lex->token_terminator - lex->token_start;
1239 token = palloc(toklen + 1);
1240 memcpy(token, lex->token_start, toklen);
1241 token[toklen] = '\0';
1244 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1245 errmsg("invalid input syntax for type %s", "json"),
1246 errdetail("Token \"%s\" is invalid.", token),
1247 report_json_context(lex)));
1251 * Report a CONTEXT line for bogus JSON input.
1253 * lex->token_terminator must be set to identify the spot where we detected
1254 * the error. Note that lex->token_start might be NULL, in case we recognized
1257 * The return value isn't meaningful, but we make it non-void so that this
1258 * can be invoked inside ereport().
1261 report_json_context(JsonLexContext *lex)
1263 const char *context_start;
1264 const char *context_end;
1265 const char *line_start;
1272 /* Choose boundaries for the part of the input we will display */
1273 context_start = lex->input;
1274 context_end = lex->token_terminator;
1275 line_start = context_start;
1279 /* Always advance over newlines */
1280 if (context_start < context_end && *context_start == '\n')
1283 line_start = context_start;
1287 /* Otherwise, done as soon as we are close enough to context_end */
1288 if (context_end - context_start < 50)
1290 /* Advance to next multibyte character */
1291 if (IS_HIGHBIT_SET(*context_start))
1292 context_start += pg_mblen(context_start);
1298 * We add "..." to indicate that the excerpt doesn't start at the
1299 * beginning of the line ... but if we're within 3 characters of the
1300 * beginning of the line, we might as well just show the whole line.
1302 if (context_start - line_start <= 3)
1303 context_start = line_start;
1305 /* Get a null-terminated copy of the data to present */
1306 ctxtlen = context_end - context_start;
1307 ctxt = palloc(ctxtlen + 1);
1308 memcpy(ctxt, context_start, ctxtlen);
1309 ctxt[ctxtlen] = '\0';
1312 * Show the context, prefixing "..." if not starting at start of line, and
1313 * suffixing "..." if not ending at end of line.
1315 prefix = (context_start > line_start) ? "..." : "";
1316 suffix = (lex->token_type != JSON_TOKEN_END && context_end - lex->input < lex->input_length && *context_end != '\n' && *context_end != '\r') ? "..." : "";
1318 return errcontext("JSON data, line %d: %s%s%s",
1319 line_number, prefix, ctxt, suffix);
1323 * Extract a single, possibly multi-byte char from the input string.
1326 extract_mb_char(char *s)
1332 res = palloc(len + 1);
1333 memcpy(res, s, len);
1340 * Determine how we want to print values of a given type in datum_to_json.
1342 * Given the datatype OID, return its JsonTypeCategory, as well as the type's
1343 * output function OID. If the returned category is JSONTYPE_CAST, we
1344 * return the OID of the type->JSON cast function instead.
1347 json_categorize_type(Oid typoid,
1348 JsonTypeCategory *tcategory,
1353 /* Look through any domain */
1354 typoid = getBaseType(typoid);
1356 *outfuncoid = InvalidOid;
1359 * We need to get the output function for everything except date and
1360 * timestamp types, array and composite types, booleans, and non-builtin
1361 * types where there's a cast to json.
1367 *tcategory = JSONTYPE_BOOL;
1376 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
1377 *tcategory = JSONTYPE_NUMERIC;
1381 *tcategory = JSONTYPE_DATE;
1385 *tcategory = JSONTYPE_TIMESTAMP;
1388 case TIMESTAMPTZOID:
1389 *tcategory = JSONTYPE_TIMESTAMPTZ;
1394 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
1395 *tcategory = JSONTYPE_JSON;
1399 /* Check for arrays and composites */
1400 if (OidIsValid(get_element_type(typoid)) || typoid == ANYARRAYOID
1401 || typoid == RECORDARRAYOID)
1402 *tcategory = JSONTYPE_ARRAY;
1403 else if (type_is_rowtype(typoid)) /* includes RECORDOID */
1404 *tcategory = JSONTYPE_COMPOSITE;
1407 /* It's probably the general case ... */
1408 *tcategory = JSONTYPE_OTHER;
1409 /* but let's look for a cast to json, if it's not built-in */
1410 if (typoid >= FirstNormalObjectId)
1413 CoercionPathType ctype;
1415 ctype = find_coercion_pathway(JSONOID, typoid,
1418 if (ctype == COERCION_PATH_FUNC && OidIsValid(castfunc))
1420 *tcategory = JSONTYPE_CAST;
1421 *outfuncoid = castfunc;
1425 /* non builtin type with no cast */
1426 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
1431 /* any other builtin type */
1432 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
1440 * Turn a Datum into JSON text, appending the string to "result".
1442 * tcategory and outfuncoid are from a previous call to json_categorize_type,
1443 * except that if is_null is true then they can be invalid.
1445 * If key_scalar is true, the value is being printed as a key, so insist
1446 * it's of an acceptable type, and force it to be quoted.
1449 datum_to_json(Datum val, bool is_null, StringInfo result,
1450 JsonTypeCategory tcategory, Oid outfuncoid,
1456 check_stack_depth();
1458 /* callers are expected to ensure that null keys are not passed in */
1459 Assert(!(key_scalar && is_null));
1463 appendStringInfoString(result, "null");
1468 (tcategory == JSONTYPE_ARRAY ||
1469 tcategory == JSONTYPE_COMPOSITE ||
1470 tcategory == JSONTYPE_JSON ||
1471 tcategory == JSONTYPE_CAST))
1473 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1474 errmsg("key value must be scalar, not array, composite, or json")));
1478 case JSONTYPE_ARRAY:
1479 array_to_json_internal(val, result, false);
1481 case JSONTYPE_COMPOSITE:
1482 composite_to_json(val, result, false);
1485 outputstr = DatumGetBool(val) ? "true" : "false";
1487 escape_json(result, outputstr);
1489 appendStringInfoString(result, outputstr);
1491 case JSONTYPE_NUMERIC:
1492 outputstr = OidOutputFunctionCall(outfuncoid, val);
1495 * Don't call escape_json for a non-key if it's a valid JSON
1498 if (!key_scalar && IsValidJsonNumber(outputstr, strlen(outputstr)))
1499 appendStringInfoString(result, outputstr);
1501 escape_json(result, outputstr);
1508 char buf[MAXDATELEN + 1];
1510 date = DatumGetDateADT(val);
1511 /* Same as date_out(), but forcing DateStyle */
1512 if (DATE_NOT_FINITE(date))
1513 EncodeSpecialDate(date, buf);
1516 j2date(date + POSTGRES_EPOCH_JDATE,
1517 &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
1518 EncodeDateOnly(&tm, USE_XSD_DATES, buf);
1520 appendStringInfo(result, "\"%s\"", buf);
1523 case JSONTYPE_TIMESTAMP:
1525 Timestamp timestamp;
1528 char buf[MAXDATELEN + 1];
1530 timestamp = DatumGetTimestamp(val);
1531 /* Same as timestamp_out(), but forcing DateStyle */
1532 if (TIMESTAMP_NOT_FINITE(timestamp))
1533 EncodeSpecialTimestamp(timestamp, buf);
1534 else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
1535 EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
1538 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1539 errmsg("timestamp out of range")));
1540 appendStringInfo(result, "\"%s\"", buf);
1543 case JSONTYPE_TIMESTAMPTZ:
1545 TimestampTz timestamp;
1549 const char *tzn = NULL;
1550 char buf[MAXDATELEN + 1];
1552 timestamp = DatumGetTimestampTz(val);
1553 /* Same as timestamptz_out(), but forcing DateStyle */
1554 if (TIMESTAMP_NOT_FINITE(timestamp))
1555 EncodeSpecialTimestamp(timestamp, buf);
1556 else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
1557 EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
1560 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1561 errmsg("timestamp out of range")));
1562 appendStringInfo(result, "\"%s\"", buf);
1566 /* JSON and JSONB output will already be escaped */
1567 outputstr = OidOutputFunctionCall(outfuncoid, val);
1568 appendStringInfoString(result, outputstr);
1572 /* outfuncoid refers to a cast function, not an output function */
1573 jsontext = DatumGetTextPP(OidFunctionCall1(outfuncoid, val));
1574 outputstr = text_to_cstring(jsontext);
1575 appendStringInfoString(result, outputstr);
1580 outputstr = OidOutputFunctionCall(outfuncoid, val);
1581 escape_json(result, outputstr);
1588 * Process a single dimension of an array.
1589 * If it's the innermost dimension, output the values, otherwise call
1590 * ourselves recursively to process the next dimension.
1593 array_dim_to_json(StringInfo result, int dim, int ndims, int *dims, Datum *vals,
1594 bool *nulls, int *valcount, JsonTypeCategory tcategory,
1595 Oid outfuncoid, bool use_line_feeds)
1600 Assert(dim < ndims);
1602 sep = use_line_feeds ? ",\n " : ",";
1604 appendStringInfoChar(result, '[');
1606 for (i = 1; i <= dims[dim]; i++)
1609 appendStringInfoString(result, sep);
1611 if (dim + 1 == ndims)
1613 datum_to_json(vals[*valcount], nulls[*valcount], result, tcategory,
1620 * Do we want line feeds on inner dimensions of arrays? For now
1623 array_dim_to_json(result, dim + 1, ndims, dims, vals, nulls,
1624 valcount, tcategory, outfuncoid, false);
1628 appendStringInfoChar(result, ']');
1632 * Turn an array into JSON.
1635 array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
1637 ArrayType *v = DatumGetArrayTypeP(array);
1638 Oid element_type = ARR_ELEMTYPE(v);
1648 JsonTypeCategory tcategory;
1653 nitems = ArrayGetNItems(ndim, dim);
1657 appendStringInfoString(result, "[]");
1661 get_typlenbyvalalign(element_type,
1662 &typlen, &typbyval, &typalign);
1664 json_categorize_type(element_type,
1665 &tcategory, &outfuncoid);
1667 deconstruct_array(v, element_type, typlen, typbyval,
1668 typalign, &elements, &nulls,
1671 array_dim_to_json(result, 0, ndim, dim, elements, nulls, &count, tcategory,
1672 outfuncoid, use_line_feeds);
1679 * Turn a composite / record into JSON.
1682 composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
1688 HeapTupleData tmptup,
1691 bool needsep = false;
1694 sep = use_line_feeds ? ",\n " : ",";
1696 td = DatumGetHeapTupleHeader(composite);
1698 /* Extract rowtype info and find a tupdesc */
1699 tupType = HeapTupleHeaderGetTypeId(td);
1700 tupTypmod = HeapTupleHeaderGetTypMod(td);
1701 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
1703 /* Build a temporary HeapTuple control structure */
1704 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
1708 appendStringInfoChar(result, '{');
1710 for (i = 0; i < tupdesc->natts; i++)
1715 JsonTypeCategory tcategory;
1718 if (tupdesc->attrs[i]->attisdropped)
1722 appendStringInfoString(result, sep);
1725 attname = NameStr(tupdesc->attrs[i]->attname);
1726 escape_json(result, attname);
1727 appendStringInfoChar(result, ':');
1729 val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
1733 tcategory = JSONTYPE_NULL;
1734 outfuncoid = InvalidOid;
1737 json_categorize_type(tupdesc->attrs[i]->atttypid,
1738 &tcategory, &outfuncoid);
1740 datum_to_json(val, isnull, result, tcategory, outfuncoid, false);
1743 appendStringInfoChar(result, '}');
1744 ReleaseTupleDesc(tupdesc);
1748 * Append JSON text for "val" to "result".
1750 * This is just a thin wrapper around datum_to_json. If the same type will be
1751 * printed many times, avoid using this; better to do the json_categorize_type
1752 * lookups only once.
1755 add_json(Datum val, bool is_null, StringInfo result,
1756 Oid val_type, bool key_scalar)
1758 JsonTypeCategory tcategory;
1761 if (val_type == InvalidOid)
1763 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1764 errmsg("could not determine input data type")));
1768 tcategory = JSONTYPE_NULL;
1769 outfuncoid = InvalidOid;
1772 json_categorize_type(val_type,
1773 &tcategory, &outfuncoid);
1775 datum_to_json(val, is_null, result, tcategory, outfuncoid, key_scalar);
1779 * SQL function array_to_json(row)
1782 array_to_json(PG_FUNCTION_ARGS)
1784 Datum array = PG_GETARG_DATUM(0);
1787 result = makeStringInfo();
1789 array_to_json_internal(array, result, false);
1791 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1795 * SQL function array_to_json(row, prettybool)
1798 array_to_json_pretty(PG_FUNCTION_ARGS)
1800 Datum array = PG_GETARG_DATUM(0);
1801 bool use_line_feeds = PG_GETARG_BOOL(1);
1804 result = makeStringInfo();
1806 array_to_json_internal(array, result, use_line_feeds);
1808 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1812 * SQL function row_to_json(row)
1815 row_to_json(PG_FUNCTION_ARGS)
1817 Datum array = PG_GETARG_DATUM(0);
1820 result = makeStringInfo();
1822 composite_to_json(array, result, false);
1824 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1828 * SQL function row_to_json(row, prettybool)
1831 row_to_json_pretty(PG_FUNCTION_ARGS)
1833 Datum array = PG_GETARG_DATUM(0);
1834 bool use_line_feeds = PG_GETARG_BOOL(1);
1837 result = makeStringInfo();
1839 composite_to_json(array, result, use_line_feeds);
1841 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1845 * SQL function to_json(anyvalue)
1848 to_json(PG_FUNCTION_ARGS)
1850 Datum val = PG_GETARG_DATUM(0);
1851 Oid val_type = get_fn_expr_argtype(fcinfo->flinfo, 0);
1853 JsonTypeCategory tcategory;
1856 if (val_type == InvalidOid)
1858 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1859 errmsg("could not determine input data type")));
1861 json_categorize_type(val_type,
1862 &tcategory, &outfuncoid);
1864 result = makeStringInfo();
1866 datum_to_json(val, false, result, tcategory, outfuncoid, false);
1868 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1872 * json_agg transition function
1874 * aggregate input column as a json array value.
1877 json_agg_transfn(PG_FUNCTION_ARGS)
1879 MemoryContext aggcontext,
1881 JsonAggState *state;
1884 if (!AggCheckCallContext(fcinfo, &aggcontext))
1886 /* cannot be called directly because of internal-type argument */
1887 elog(ERROR, "json_agg_transfn called in non-aggregate context");
1890 if (PG_ARGISNULL(0))
1892 Oid arg_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
1894 if (arg_type == InvalidOid)
1896 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1897 errmsg("could not determine input data type")));
1900 * Make this state object in a context where it will persist for the
1901 * duration of the aggregate call. MemoryContextSwitchTo is only
1902 * needed the first time, as the StringInfo routines make sure they
1903 * use the right context to enlarge the object if necessary.
1905 oldcontext = MemoryContextSwitchTo(aggcontext);
1906 state = (JsonAggState *) palloc(sizeof(JsonAggState));
1907 state->str = makeStringInfo();
1908 MemoryContextSwitchTo(oldcontext);
1910 appendStringInfoChar(state->str, '[');
1911 json_categorize_type(arg_type, &state->val_category,
1912 &state->val_output_func);
1916 state = (JsonAggState *) PG_GETARG_POINTER(0);
1917 appendStringInfoString(state->str, ", ");
1920 /* fast path for NULLs */
1921 if (PG_ARGISNULL(1))
1923 datum_to_json((Datum) 0, true, state->str, JSONTYPE_NULL,
1925 PG_RETURN_POINTER(state);
1928 val = PG_GETARG_DATUM(1);
1930 /* add some whitespace if structured type and not first item */
1931 if (!PG_ARGISNULL(0) &&
1932 (state->val_category == JSONTYPE_ARRAY ||
1933 state->val_category == JSONTYPE_COMPOSITE))
1935 appendStringInfoString(state->str, "\n ");
1938 datum_to_json(val, false, state->str, state->val_category,
1939 state->val_output_func, false);
1942 * The transition type for array_agg() is declared to be "internal", which
1943 * is a pass-by-value type the same size as a pointer. So we can safely
1944 * pass the JsonAggState pointer through nodeAgg.c's machinations.
1946 PG_RETURN_POINTER(state);
1950 * json_agg final function
1953 json_agg_finalfn(PG_FUNCTION_ARGS)
1955 JsonAggState *state;
1957 /* cannot be called directly because of internal-type argument */
1958 Assert(AggCheckCallContext(fcinfo, NULL));
1960 state = PG_ARGISNULL(0) ?
1962 (JsonAggState *) PG_GETARG_POINTER(0);
1964 /* NULL result for no rows in, as is standard with aggregates */
1968 /* Else return state with appropriate array terminator added */
1969 PG_RETURN_TEXT_P(catenate_stringinfo_string(state->str, "]"));
1973 * json_object_agg transition function.
1975 * aggregate two input columns as a single json object value.
1978 json_object_agg_transfn(PG_FUNCTION_ARGS)
1980 MemoryContext aggcontext,
1982 JsonAggState *state;
1985 if (!AggCheckCallContext(fcinfo, &aggcontext))
1987 /* cannot be called directly because of internal-type argument */
1988 elog(ERROR, "json_object_agg_transfn called in non-aggregate context");
1991 if (PG_ARGISNULL(0))
1996 * Make the StringInfo in a context where it will persist for the
1997 * duration of the aggregate call. Switching context is only needed
1998 * for this initial step, as the StringInfo routines make sure they
1999 * use the right context to enlarge the object if necessary.
2001 oldcontext = MemoryContextSwitchTo(aggcontext);
2002 state = (JsonAggState *) palloc(sizeof(JsonAggState));
2003 state->str = makeStringInfo();
2004 MemoryContextSwitchTo(oldcontext);
2006 arg_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
2008 if (arg_type == InvalidOid)
2010 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2011 errmsg("could not determine data type for argument 1")));
2013 json_categorize_type(arg_type, &state->key_category,
2014 &state->key_output_func);
2016 arg_type = get_fn_expr_argtype(fcinfo->flinfo, 2);
2018 if (arg_type == InvalidOid)
2020 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2021 errmsg("could not determine data type for argument 2")));
2023 json_categorize_type(arg_type, &state->val_category,
2024 &state->val_output_func);
2026 appendStringInfoString(state->str, "{ ");
2030 state = (JsonAggState *) PG_GETARG_POINTER(0);
2031 appendStringInfoString(state->str, ", ");
2035 * Note: since json_object_agg() is declared as taking type "any", the
2036 * parser will not do any type conversion on unknown-type literals (that
2037 * is, undecorated strings or NULLs). Such values will arrive here as
2038 * type UNKNOWN, which fortunately does not matter to us, since
2039 * unknownout() works fine.
2042 if (PG_ARGISNULL(1))
2044 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2045 errmsg("field name must not be null")));
2047 arg = PG_GETARG_DATUM(1);
2049 datum_to_json(arg, false, state->str, state->key_category,
2050 state->key_output_func, true);
2052 appendStringInfoString(state->str, " : ");
2054 if (PG_ARGISNULL(2))
2057 arg = PG_GETARG_DATUM(2);
2059 datum_to_json(arg, PG_ARGISNULL(2), state->str, state->val_category,
2060 state->val_output_func, false);
2062 PG_RETURN_POINTER(state);
2066 * json_object_agg final function.
2069 json_object_agg_finalfn(PG_FUNCTION_ARGS)
2071 JsonAggState *state;
2073 /* cannot be called directly because of internal-type argument */
2074 Assert(AggCheckCallContext(fcinfo, NULL));
2076 state = PG_ARGISNULL(0) ? NULL : (JsonAggState *) PG_GETARG_POINTER(0);
2078 /* NULL result for no rows in, as is standard with aggregates */
2082 /* Else return state with appropriate object terminator added */
2083 PG_RETURN_TEXT_P(catenate_stringinfo_string(state->str, " }"));
2087 * Helper function for aggregates: return given StringInfo's contents plus
2088 * specified trailing string, as a text datum. We need this because aggregate
2089 * final functions are not allowed to modify the aggregate state.
2092 catenate_stringinfo_string(StringInfo buffer, const char *addon)
2094 /* custom version of cstring_to_text_with_len */
2095 int buflen = buffer->len;
2096 int addlen = strlen(addon);
2097 text *result = (text *) palloc(buflen + addlen + VARHDRSZ);
2099 SET_VARSIZE(result, buflen + addlen + VARHDRSZ);
2100 memcpy(VARDATA(result), buffer->data, buflen);
2101 memcpy(VARDATA(result) + buflen, addon, addlen);
2107 * SQL function json_build_object(variadic "any")
2110 json_build_object(PG_FUNCTION_ARGS)
2112 int nargs = PG_NARGS();
2115 const char *sep = "";
2121 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2122 errmsg("argument list must have even number of elements"),
2123 errhint("The arguments of json_build_object() must consist of alternating keys and values.")));
2125 result = makeStringInfo();
2127 appendStringInfoChar(result, '{');
2129 for (i = 0; i < nargs; i += 2)
2132 * Note: since json_build_object() is declared as taking type "any",
2133 * the parser will not do any type conversion on unknown-type literals
2134 * (that is, undecorated strings or NULLs). Such values will arrive
2135 * here as type UNKNOWN, which fortunately does not matter to us,
2136 * since unknownout() works fine.
2138 appendStringInfoString(result, sep);
2142 val_type = get_fn_expr_argtype(fcinfo->flinfo, i);
2144 if (val_type == InvalidOid)
2146 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2147 errmsg("could not determine data type for argument %d",
2150 if (PG_ARGISNULL(i))
2152 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2153 errmsg("argument %d cannot be null", i + 1),
2154 errhint("Object keys should be text.")));
2156 arg = PG_GETARG_DATUM(i);
2158 add_json(arg, false, result, val_type, true);
2160 appendStringInfoString(result, " : ");
2163 val_type = get_fn_expr_argtype(fcinfo->flinfo, i + 1);
2165 if (val_type == InvalidOid)
2167 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2168 errmsg("could not determine data type for argument %d",
2171 if (PG_ARGISNULL(i + 1))
2174 arg = PG_GETARG_DATUM(i + 1);
2176 add_json(arg, PG_ARGISNULL(i + 1), result, val_type, false);
2179 appendStringInfoChar(result, '}');
2181 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
2185 * degenerate case of json_build_object where it gets 0 arguments.
2188 json_build_object_noargs(PG_FUNCTION_ARGS)
2190 PG_RETURN_TEXT_P(cstring_to_text_with_len("{}", 2));
2194 * SQL function json_build_array(variadic "any")
2197 json_build_array(PG_FUNCTION_ARGS)
2199 int nargs = PG_NARGS();
2202 const char *sep = "";
2206 result = makeStringInfo();
2208 appendStringInfoChar(result, '[');
2210 for (i = 0; i < nargs; i++)
2213 * Note: since json_build_array() is declared as taking type "any",
2214 * the parser will not do any type conversion on unknown-type literals
2215 * (that is, undecorated strings or NULLs). Such values will arrive
2216 * here as type UNKNOWN, which fortunately does not matter to us,
2217 * since unknownout() works fine.
2219 appendStringInfoString(result, sep);
2222 val_type = get_fn_expr_argtype(fcinfo->flinfo, i);
2224 if (val_type == InvalidOid)
2226 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2227 errmsg("could not determine data type for argument %d",
2230 if (PG_ARGISNULL(i))
2233 arg = PG_GETARG_DATUM(i);
2235 add_json(arg, PG_ARGISNULL(i), result, val_type, false);
2238 appendStringInfoChar(result, ']');
2240 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
2244 * degenerate case of json_build_array where it gets 0 arguments.
2247 json_build_array_noargs(PG_FUNCTION_ARGS)
2249 PG_RETURN_TEXT_P(cstring_to_text_with_len("[]", 2));
2253 * SQL function json_object(text[])
2255 * take a one or two dimensional array of text as key/value pairs
2256 * for a json object.
2259 json_object(PG_FUNCTION_ARGS)
2261 ArrayType *in_array = PG_GETARG_ARRAYTYPE_P(0);
2262 int ndims = ARR_NDIM(in_array);
2263 StringInfoData result;
2275 PG_RETURN_DATUM(CStringGetTextDatum("{}"));
2279 if ((ARR_DIMS(in_array)[0]) % 2)
2281 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2282 errmsg("array must have even number of elements")));
2286 if ((ARR_DIMS(in_array)[1]) != 2)
2288 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2289 errmsg("array must have two columns")));
2294 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2295 errmsg("wrong number of array subscripts")));
2298 deconstruct_array(in_array,
2299 TEXTOID, -1, false, 'i',
2300 &in_datums, &in_nulls, &in_count);
2302 count = in_count / 2;
2304 initStringInfo(&result);
2306 appendStringInfoChar(&result, '{');
2308 for (i = 0; i < count; ++i)
2310 if (in_nulls[i * 2])
2312 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2313 errmsg("null value not allowed for object key")));
2315 v = TextDatumGetCString(in_datums[i * 2]);
2317 appendStringInfoString(&result, ", ");
2318 escape_json(&result, v);
2319 appendStringInfoString(&result, " : ");
2321 if (in_nulls[i * 2 + 1])
2322 appendStringInfoString(&result, "null");
2325 v = TextDatumGetCString(in_datums[i * 2 + 1]);
2326 escape_json(&result, v);
2331 appendStringInfoChar(&result, '}');
2336 rval = cstring_to_text_with_len(result.data, result.len);
2339 PG_RETURN_TEXT_P(rval);
2344 * SQL function json_object(text[], text[])
2346 * take separate key and value arrays of text to construct a json object
2350 json_object_two_arg(PG_FUNCTION_ARGS)
2352 ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(0);
2353 ArrayType *val_array = PG_GETARG_ARRAYTYPE_P(1);
2354 int nkdims = ARR_NDIM(key_array);
2355 int nvdims = ARR_NDIM(val_array);
2356 StringInfoData result;
2367 if (nkdims > 1 || nkdims != nvdims)
2369 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2370 errmsg("wrong number of array subscripts")));
2373 PG_RETURN_DATUM(CStringGetTextDatum("{}"));
2375 deconstruct_array(key_array,
2376 TEXTOID, -1, false, 'i',
2377 &key_datums, &key_nulls, &key_count);
2379 deconstruct_array(val_array,
2380 TEXTOID, -1, false, 'i',
2381 &val_datums, &val_nulls, &val_count);
2383 if (key_count != val_count)
2385 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2386 errmsg("mismatched array dimensions")));
2388 initStringInfo(&result);
2390 appendStringInfoChar(&result, '{');
2392 for (i = 0; i < key_count; ++i)
2396 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2397 errmsg("null value not allowed for object key")));
2399 v = TextDatumGetCString(key_datums[i]);
2401 appendStringInfoString(&result, ", ");
2402 escape_json(&result, v);
2403 appendStringInfoString(&result, " : ");
2406 appendStringInfoString(&result, "null");
2409 v = TextDatumGetCString(val_datums[i]);
2410 escape_json(&result, v);
2415 appendStringInfoChar(&result, '}');
2422 rval = cstring_to_text_with_len(result.data, result.len);
2425 PG_RETURN_TEXT_P(rval);
2430 * Produce a JSON string literal, properly escaping characters in the text.
2433 escape_json(StringInfo buf, const char *str)
2437 appendStringInfoCharMacro(buf, '"');
2438 for (p = str; *p; p++)
2443 appendStringInfoString(buf, "\\b");
2446 appendStringInfoString(buf, "\\f");
2449 appendStringInfoString(buf, "\\n");
2452 appendStringInfoString(buf, "\\r");
2455 appendStringInfoString(buf, "\\t");
2458 appendStringInfoString(buf, "\\\"");
2461 appendStringInfoString(buf, "\\\\");
2464 if ((unsigned char) *p < ' ')
2465 appendStringInfo(buf, "\\u%04x", (int) *p);
2467 appendStringInfoCharMacro(buf, *p);
2471 appendStringInfoCharMacro(buf, '"');
2475 * SQL function json_typeof(json) -> text
2477 * Returns the type of the outermost JSON value as TEXT. Possible types are
2478 * "object", "array", "string", "number", "boolean", and "null".
2480 * Performs a single call to json_lex() to get the first token of the supplied
2481 * value. This initial token uniquely determines the value's type. As our
2482 * input must already have been validated by json_in() or json_recv(), the
2483 * initial token should never be JSON_TOKEN_OBJECT_END, JSON_TOKEN_ARRAY_END,
2484 * JSON_TOKEN_COLON, JSON_TOKEN_COMMA, or JSON_TOKEN_END.
2487 json_typeof(PG_FUNCTION_ARGS)
2491 JsonLexContext *lex;
2495 json = PG_GETARG_TEXT_PP(0);
2496 lex = makeJsonLexContext(json, false);
2498 /* Lex exactly one token from the input and check its type. */
2500 tok = lex_peek(lex);
2503 case JSON_TOKEN_OBJECT_START:
2506 case JSON_TOKEN_ARRAY_START:
2509 case JSON_TOKEN_STRING:
2512 case JSON_TOKEN_NUMBER:
2515 case JSON_TOKEN_TRUE:
2516 case JSON_TOKEN_FALSE:
2519 case JSON_TOKEN_NULL:
2523 elog(ERROR, "unexpected json token: %d", tok);
2526 PG_RETURN_TEXT_P(cstring_to_text(type));