1 /*-------------------------------------------------------------------------
4 * JSON data type support.
6 * Portions Copyright (c) 1996-2015, 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"
35 /* String to output for infinite dates and timestamps */
36 #define DT_INFINITY "\"infinity\""
39 * The context of the parser is maintained by the recursive descent
40 * mechanism, but is passed explicitly to the error reporting routine
41 * for better diagnostics.
43 typedef enum /* contexts of JSON parser */
45 JSON_PARSE_VALUE, /* expecting a value */
46 JSON_PARSE_STRING, /* expecting a string (for a field name) */
47 JSON_PARSE_ARRAY_START, /* saw '[', expecting value or ']' */
48 JSON_PARSE_ARRAY_NEXT, /* saw array element, expecting ',' or ']' */
49 JSON_PARSE_OBJECT_START, /* saw '{', expecting label or '}' */
50 JSON_PARSE_OBJECT_LABEL, /* saw object label, expecting ':' */
51 JSON_PARSE_OBJECT_NEXT, /* saw object value, expecting ',' or '}' */
52 JSON_PARSE_OBJECT_COMMA, /* saw object ',', expecting next label */
53 JSON_PARSE_END /* saw the end of a document, expect nothing */
56 typedef enum /* type categories for datum_to_json */
58 JSONTYPE_NULL, /* null, so we didn't bother to identify */
59 JSONTYPE_BOOL, /* boolean (built-in types only) */
60 JSONTYPE_NUMERIC, /* numeric (ditto) */
61 JSONTYPE_DATE, /* we use special formatting for datetimes */
64 JSONTYPE_JSON, /* JSON itself (and JSONB) */
65 JSONTYPE_ARRAY, /* array */
66 JSONTYPE_COMPOSITE, /* composite */
67 JSONTYPE_CAST, /* something with an explicit cast to JSON */
68 JSONTYPE_OTHER /* all else */
71 typedef struct JsonAggState
74 JsonTypeCategory key_category;
76 JsonTypeCategory val_category;
80 static inline void json_lex(JsonLexContext *lex);
81 static inline void json_lex_string(JsonLexContext *lex);
82 static inline void json_lex_number(JsonLexContext *lex, char *s, bool *num_err);
83 static inline void parse_scalar(JsonLexContext *lex, JsonSemAction *sem);
84 static void parse_object_field(JsonLexContext *lex, JsonSemAction *sem);
85 static void parse_object(JsonLexContext *lex, JsonSemAction *sem);
86 static void parse_array_element(JsonLexContext *lex, JsonSemAction *sem);
87 static void parse_array(JsonLexContext *lex, JsonSemAction *sem);
88 static void report_parse_error(JsonParseContext ctx, JsonLexContext *lex);
89 static void report_invalid_token(JsonLexContext *lex);
90 static int report_json_context(JsonLexContext *lex);
91 static char *extract_mb_char(char *s);
92 static void composite_to_json(Datum composite, StringInfo result,
94 static void array_dim_to_json(StringInfo result, int dim, int ndims, int *dims,
95 Datum *vals, bool *nulls, int *valcount,
96 JsonTypeCategory tcategory, Oid outfuncoid,
98 static void array_to_json_internal(Datum array, StringInfo result,
100 static void json_categorize_type(Oid typoid,
101 JsonTypeCategory *tcategory,
103 static void datum_to_json(Datum val, bool is_null, StringInfo result,
104 JsonTypeCategory tcategory, Oid outfuncoid,
106 static void add_json(Datum val, bool is_null, StringInfo result,
107 Oid val_type, bool key_scalar);
108 static text *catenate_stringinfo_string(StringInfo buffer, const char *addon);
110 /* the null action object used for pure validation */
111 static JsonSemAction nullSemAction =
113 NULL, NULL, NULL, NULL, NULL,
114 NULL, NULL, NULL, NULL, NULL
117 /* Recursive Descent parser support routines */
122 * what is the current look_ahead token?
124 static inline JsonTokenType
125 lex_peek(JsonLexContext *lex)
127 return lex->token_type;
133 * accept the look_ahead token and move the lexer to the next token if the
134 * look_ahead token matches the token parameter. In that case, and if required,
135 * also hand back the de-escaped lexeme.
137 * returns true if the token matched, false otherwise.
140 lex_accept(JsonLexContext *lex, JsonTokenType token, char **lexeme)
142 if (lex->token_type == token)
146 if (lex->token_type == JSON_TOKEN_STRING)
148 if (lex->strval != NULL)
149 *lexeme = pstrdup(lex->strval->data);
153 int len = (lex->token_terminator - lex->token_start);
154 char *tokstr = palloc(len + 1);
156 memcpy(tokstr, lex->token_start, len);
170 * move the lexer to the next token if the current look_ahead token matches
171 * the parameter token. Otherwise, report an error.
174 lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
176 if (!lex_accept(lex, token, NULL))
177 report_parse_error(ctx, lex);
180 /* chars to consider as part of an alphanumeric token */
181 #define JSON_ALPHANUMERIC_CHAR(c) \
182 (((c) >= 'a' && (c) <= 'z') || \
183 ((c) >= 'A' && (c) <= 'Z') || \
184 ((c) >= '0' && (c) <= '9') || \
188 /* utility function to check if a string is a valid JSON number */
190 IsValidJsonNumber(const char *str, int len)
193 JsonLexContext dummy_lex;
197 * json_lex_number expects a leading '-' to have been eaten already.
199 * having to cast away the constness of str is ugly, but there's not much
204 dummy_lex.input = (char *) str + 1;
205 dummy_lex.input_length = len - 1;
209 dummy_lex.input = (char *) str;
210 dummy_lex.input_length = len;
213 json_lex_number(&dummy_lex, dummy_lex.input, &numeric_error);
215 return !numeric_error;
222 json_in(PG_FUNCTION_ARGS)
224 char *json = PG_GETARG_CSTRING(0);
225 text *result = cstring_to_text(json);
229 lex = makeJsonLexContext(result, false);
230 pg_parse_json(lex, &nullSemAction);
232 /* Internal representation is the same as text, for now */
233 PG_RETURN_TEXT_P(result);
240 json_out(PG_FUNCTION_ARGS)
242 /* we needn't detoast because text_to_cstring will handle that */
243 Datum txt = PG_GETARG_DATUM(0);
245 PG_RETURN_CSTRING(TextDatumGetCString(txt));
252 json_send(PG_FUNCTION_ARGS)
254 text *t = PG_GETARG_TEXT_PP(0);
257 pq_begintypsend(&buf);
258 pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
259 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
266 json_recv(PG_FUNCTION_ARGS)
268 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
273 str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
276 lex = makeJsonLexContextCstringLen(str, nbytes, false);
277 pg_parse_json(lex, &nullSemAction);
279 PG_RETURN_TEXT_P(cstring_to_text_with_len(str, nbytes));
285 * lex constructor, with or without StringInfo object
286 * for de-escaped lexemes.
288 * Without is better as it makes the processing faster, so only make one
289 * if really required.
291 * If you already have the json as a text* value, use the first of these
292 * functions, otherwise use makeJsonLexContextCstringLen().
295 makeJsonLexContext(text *json, bool need_escapes)
297 return makeJsonLexContextCstringLen(VARDATA(json),
298 VARSIZE(json) - VARHDRSZ,
303 makeJsonLexContextCstringLen(char *json, int len, bool need_escapes)
305 JsonLexContext *lex = palloc0(sizeof(JsonLexContext));
307 lex->input = lex->token_terminator = lex->line_start = json;
308 lex->line_number = 1;
309 lex->input_length = len;
311 lex->strval = makeStringInfo();
318 * Publicly visible entry point for the JSON parser.
320 * lex is a lexing context, set up for the json to be processed by calling
321 * makeJsonLexContext(). sem is a strucure of function pointers to semantic
322 * action routines to be called at appropriate spots during parsing, and a
323 * pointer to a state object to be passed to those routines.
326 pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
330 /* get the initial token */
335 /* parse by recursive descent */
338 case JSON_TOKEN_OBJECT_START:
339 parse_object(lex, sem);
341 case JSON_TOKEN_ARRAY_START:
342 parse_array(lex, sem);
345 parse_scalar(lex, sem); /* json can be a bare scalar */
348 lex_expect(JSON_PARSE_END, lex, JSON_TOKEN_END);
353 * json_count_array_elements
355 * Returns number of array elements in lex context at start of array token
356 * until end of array token at same nesting level.
358 * Designed to be called from array_start routines.
361 json_count_array_elements(JsonLexContext *lex)
363 JsonLexContext copylex;
367 * It's safe to do this with a shallow copy because the lexical routines
368 * don't scribble on the input. They do scribble on the other pointers etc,
369 * so doing this with a copy makes that safe.
371 memcpy(©lex, lex, sizeof(JsonLexContext));
372 copylex.strval = NULL; /* not interested in values here */
376 lex_expect(JSON_PARSE_ARRAY_START, ©lex, JSON_TOKEN_ARRAY_START);
377 if (lex_peek(©lex) != JSON_TOKEN_ARRAY_END)
382 parse_array_element(©lex, &nullSemAction);
384 while (lex_accept(©lex, JSON_TOKEN_COMMA, NULL));
386 lex_expect(JSON_PARSE_ARRAY_NEXT, ©lex, JSON_TOKEN_ARRAY_END);
392 * Recursive Descent parse routines. There is one for each structural
393 * element in a json document:
394 * - scalar (string, number, true, false, null)
401 parse_scalar(JsonLexContext *lex, JsonSemAction *sem)
404 json_scalar_action sfunc = sem->scalar;
406 JsonTokenType tok = lex_peek(lex);
408 valaddr = sfunc == NULL ? NULL : &val;
410 /* a scalar must be a string, a number, true, false, or null */
413 case JSON_TOKEN_TRUE:
414 lex_accept(lex, JSON_TOKEN_TRUE, valaddr);
416 case JSON_TOKEN_FALSE:
417 lex_accept(lex, JSON_TOKEN_FALSE, valaddr);
419 case JSON_TOKEN_NULL:
420 lex_accept(lex, JSON_TOKEN_NULL, valaddr);
422 case JSON_TOKEN_NUMBER:
423 lex_accept(lex, JSON_TOKEN_NUMBER, valaddr);
425 case JSON_TOKEN_STRING:
426 lex_accept(lex, JSON_TOKEN_STRING, valaddr);
429 report_parse_error(JSON_PARSE_VALUE, lex);
433 (*sfunc) (sem->semstate, val, tok);
437 parse_object_field(JsonLexContext *lex, JsonSemAction *sem)
440 * An object field is "fieldname" : value where value can be a scalar,
441 * object or array. Note: in user-facing docs and error messages, we
442 * generally call a field name a "key".
445 char *fname = NULL; /* keep compiler quiet */
446 json_ofield_action ostart = sem->object_field_start;
447 json_ofield_action oend = sem->object_field_end;
449 char **fnameaddr = NULL;
452 if (ostart != NULL || oend != NULL)
455 if (!lex_accept(lex, JSON_TOKEN_STRING, fnameaddr))
456 report_parse_error(JSON_PARSE_STRING, lex);
458 lex_expect(JSON_PARSE_OBJECT_LABEL, lex, JSON_TOKEN_COLON);
461 isnull = tok == JSON_TOKEN_NULL;
464 (*ostart) (sem->semstate, fname, isnull);
468 case JSON_TOKEN_OBJECT_START:
469 parse_object(lex, sem);
471 case JSON_TOKEN_ARRAY_START:
472 parse_array(lex, sem);
475 parse_scalar(lex, sem);
479 (*oend) (sem->semstate, fname, isnull);
483 parse_object(JsonLexContext *lex, JsonSemAction *sem)
486 * an object is a possibly empty sequence of object fields, separated by
487 * commas and surrounded by curly braces.
489 json_struct_action ostart = sem->object_start;
490 json_struct_action oend = sem->object_end;
494 (*ostart) (sem->semstate);
497 * Data inside an object is at a higher nesting level than the object
498 * itself. Note that we increment this after we call the semantic routine
499 * for the object start and restore it before we call the routine for the
504 /* we know this will succeeed, just clearing the token */
505 lex_expect(JSON_PARSE_OBJECT_START, lex, JSON_TOKEN_OBJECT_START);
510 case JSON_TOKEN_STRING:
511 parse_object_field(lex, sem);
512 while (lex_accept(lex, JSON_TOKEN_COMMA, NULL))
513 parse_object_field(lex, sem);
515 case JSON_TOKEN_OBJECT_END:
518 /* case of an invalid initial token inside the object */
519 report_parse_error(JSON_PARSE_OBJECT_START, lex);
522 lex_expect(JSON_PARSE_OBJECT_NEXT, lex, JSON_TOKEN_OBJECT_END);
527 (*oend) (sem->semstate);
531 parse_array_element(JsonLexContext *lex, JsonSemAction *sem)
533 json_aelem_action astart = sem->array_element_start;
534 json_aelem_action aend = sem->array_element_end;
535 JsonTokenType tok = lex_peek(lex);
539 isnull = tok == JSON_TOKEN_NULL;
542 (*astart) (sem->semstate, isnull);
544 /* an array element is any object, array or scalar */
547 case JSON_TOKEN_OBJECT_START:
548 parse_object(lex, sem);
550 case JSON_TOKEN_ARRAY_START:
551 parse_array(lex, sem);
554 parse_scalar(lex, sem);
558 (*aend) (sem->semstate, isnull);
562 parse_array(JsonLexContext *lex, JsonSemAction *sem)
565 * an array is a possibly empty sequence of array elements, separated by
566 * commas and surrounded by square brackets.
568 json_struct_action astart = sem->array_start;
569 json_struct_action aend = sem->array_end;
572 (*astart) (sem->semstate);
575 * Data inside an array is at a higher nesting level than the array
576 * itself. Note that we increment this after we call the semantic routine
577 * for the array start and restore it before we call the routine for the
582 lex_expect(JSON_PARSE_ARRAY_START, lex, JSON_TOKEN_ARRAY_START);
583 if (lex_peek(lex) != JSON_TOKEN_ARRAY_END)
586 parse_array_element(lex, sem);
588 while (lex_accept(lex, JSON_TOKEN_COMMA, NULL))
589 parse_array_element(lex, sem);
592 lex_expect(JSON_PARSE_ARRAY_NEXT, lex, JSON_TOKEN_ARRAY_END);
597 (*aend) (sem->semstate);
601 * Lex one token from the input stream.
604 json_lex(JsonLexContext *lex)
609 /* Skip leading whitespace. */
610 s = lex->token_terminator;
611 len = s - lex->input;
612 while (len < lex->input_length &&
613 (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'))
620 lex->token_start = s;
622 /* Determine token type. */
623 if (len >= lex->input_length)
625 lex->token_start = NULL;
626 lex->prev_token_terminator = lex->token_terminator;
627 lex->token_terminator = s;
628 lex->token_type = JSON_TOKEN_END;
633 /* Single-character token, some kind of punctuation mark. */
635 lex->prev_token_terminator = lex->token_terminator;
636 lex->token_terminator = s + 1;
637 lex->token_type = JSON_TOKEN_OBJECT_START;
640 lex->prev_token_terminator = lex->token_terminator;
641 lex->token_terminator = s + 1;
642 lex->token_type = JSON_TOKEN_OBJECT_END;
645 lex->prev_token_terminator = lex->token_terminator;
646 lex->token_terminator = s + 1;
647 lex->token_type = JSON_TOKEN_ARRAY_START;
650 lex->prev_token_terminator = lex->token_terminator;
651 lex->token_terminator = s + 1;
652 lex->token_type = JSON_TOKEN_ARRAY_END;
655 lex->prev_token_terminator = lex->token_terminator;
656 lex->token_terminator = s + 1;
657 lex->token_type = JSON_TOKEN_COMMA;
660 lex->prev_token_terminator = lex->token_terminator;
661 lex->token_terminator = s + 1;
662 lex->token_type = JSON_TOKEN_COLON;
666 json_lex_string(lex);
667 lex->token_type = JSON_TOKEN_STRING;
670 /* Negative number. */
671 json_lex_number(lex, s + 1, NULL);
672 lex->token_type = JSON_TOKEN_NUMBER;
684 /* Positive number. */
685 json_lex_number(lex, s, NULL);
686 lex->token_type = JSON_TOKEN_NUMBER;
693 * We're not dealing with a string, number, legal
694 * punctuation mark, or end of string. The only legal
695 * tokens we might find here are true, false, and null,
696 * but for error reporting purposes we scan until we see a
697 * non-alphanumeric character. That way, we can report
698 * the whole word as an unexpected token, rather than just
699 * some unintuitive prefix thereof.
701 for (p = s; p - s < lex->input_length - len && JSON_ALPHANUMERIC_CHAR(*p); p++)
705 * We got some sort of unexpected punctuation or an
706 * otherwise unexpected character, so just complain about
707 * that one character.
711 lex->prev_token_terminator = lex->token_terminator;
712 lex->token_terminator = s + 1;
713 report_invalid_token(lex);
717 * We've got a real alphanumeric token here. If it
718 * happens to be true, false, or null, all is well. If
721 lex->prev_token_terminator = lex->token_terminator;
722 lex->token_terminator = p;
725 if (memcmp(s, "true", 4) == 0)
726 lex->token_type = JSON_TOKEN_TRUE;
727 else if (memcmp(s, "null", 4) == 0)
728 lex->token_type = JSON_TOKEN_NULL;
730 report_invalid_token(lex);
732 else if (p - s == 5 && memcmp(s, "false", 5) == 0)
733 lex->token_type = JSON_TOKEN_FALSE;
735 report_invalid_token(lex);
738 } /* end of switch */
742 * The next token in the input stream is known to be a string; lex it.
745 json_lex_string(JsonLexContext *lex)
749 int hi_surrogate = -1;
751 if (lex->strval != NULL)
752 resetStringInfo(lex->strval);
754 Assert(lex->input_length > 0);
755 s = lex->token_start;
756 len = lex->token_start - lex->input;
761 /* Premature end of the string. */
762 if (len >= lex->input_length)
764 lex->token_terminator = s;
765 report_invalid_token(lex);
769 else if ((unsigned char) *s < 32)
771 /* Per RFC4627, these characters MUST be escaped. */
772 /* Since *s isn't printable, exclude it from the context string */
773 lex->token_terminator = s;
775 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
776 errmsg("invalid input syntax for type json"),
777 errdetail("Character with value 0x%02x must be escaped.",
779 report_json_context(lex)));
783 /* OK, we have an escape character. */
786 if (len >= lex->input_length)
788 lex->token_terminator = s;
789 report_invalid_token(lex);
796 for (i = 1; i <= 4; i++)
800 if (len >= lex->input_length)
802 lex->token_terminator = s;
803 report_invalid_token(lex);
805 else if (*s >= '0' && *s <= '9')
806 ch = (ch * 16) + (*s - '0');
807 else if (*s >= 'a' && *s <= 'f')
808 ch = (ch * 16) + (*s - 'a') + 10;
809 else if (*s >= 'A' && *s <= 'F')
810 ch = (ch * 16) + (*s - 'A') + 10;
813 lex->token_terminator = s + pg_mblen(s);
815 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
816 errmsg("invalid input syntax for type json"),
817 errdetail("\"\\u\" must be followed by four hexadecimal digits."),
818 report_json_context(lex)));
821 if (lex->strval != NULL)
826 if (ch >= 0xd800 && ch <= 0xdbff)
828 if (hi_surrogate != -1)
830 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
831 errmsg("invalid input syntax for type json"),
832 errdetail("Unicode high surrogate must not follow a high surrogate."),
833 report_json_context(lex)));
834 hi_surrogate = (ch & 0x3ff) << 10;
837 else if (ch >= 0xdc00 && ch <= 0xdfff)
839 if (hi_surrogate == -1)
841 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
842 errmsg("invalid input syntax for type json"),
843 errdetail("Unicode low surrogate must follow a high surrogate."),
844 report_json_context(lex)));
845 ch = 0x10000 + hi_surrogate + (ch & 0x3ff);
849 if (hi_surrogate != -1)
851 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
852 errmsg("invalid input syntax for type json"),
853 errdetail("Unicode low surrogate must follow a high surrogate."),
854 report_json_context(lex)));
857 * For UTF8, replace the escape sequence by the actual
858 * utf8 character in lex->strval. Do this also for other
859 * encodings if the escape designates an ASCII character,
860 * otherwise raise an error.
865 /* We can't allow this, since our TEXT type doesn't */
867 (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
868 errmsg("unsupported Unicode escape sequence"),
869 errdetail("\\u0000 cannot be converted to text."),
870 report_json_context(lex)));
872 else if (GetDatabaseEncoding() == PG_UTF8)
874 unicode_to_utf8(ch, (unsigned char *) utf8str);
875 utf8len = pg_utf_mblen((unsigned char *) utf8str);
876 appendBinaryStringInfo(lex->strval, utf8str, utf8len);
878 else if (ch <= 0x007f)
881 * This is the only way to designate things like a
882 * form feed character in JSON, so it's useful in all
885 appendStringInfoChar(lex->strval, (char) ch);
890 (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
891 errmsg("unsupported Unicode escape sequence"),
892 errdetail("Unicode escape values cannot be used for code point values above 007F when the server encoding is not UTF8."),
893 report_json_context(lex)));
898 else if (lex->strval != NULL)
900 if (hi_surrogate != -1)
902 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
903 errmsg("invalid input syntax for type json"),
904 errdetail("Unicode low surrogate must follow a high surrogate."),
905 report_json_context(lex)));
912 appendStringInfoChar(lex->strval, *s);
915 appendStringInfoChar(lex->strval, '\b');
918 appendStringInfoChar(lex->strval, '\f');
921 appendStringInfoChar(lex->strval, '\n');
924 appendStringInfoChar(lex->strval, '\r');
927 appendStringInfoChar(lex->strval, '\t');
930 /* Not a valid string escape, so error out. */
931 lex->token_terminator = s + pg_mblen(s);
933 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
934 errmsg("invalid input syntax for type json"),
935 errdetail("Escape sequence \"\\%s\" is invalid.",
937 report_json_context(lex)));
940 else if (strchr("\"\\/bfnrt", *s) == NULL)
943 * Simpler processing if we're not bothered about de-escaping
945 * It's very tempting to remove the strchr() call here and
946 * replace it with a switch statement, but testing so far has
947 * shown it's not a performance win.
949 lex->token_terminator = s + pg_mblen(s);
951 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
952 errmsg("invalid input syntax for type json"),
953 errdetail("Escape sequence \"\\%s\" is invalid.",
955 report_json_context(lex)));
959 else if (lex->strval != NULL)
961 if (hi_surrogate != -1)
963 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
964 errmsg("invalid input syntax for type json"),
965 errdetail("Unicode low surrogate must follow a high surrogate."),
966 report_json_context(lex)));
968 appendStringInfoChar(lex->strval, *s);
973 if (hi_surrogate != -1)
975 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
976 errmsg("invalid input syntax for type json"),
977 errdetail("Unicode low surrogate must follow a high surrogate."),
978 report_json_context(lex)));
980 /* Hooray, we found the end of the string! */
981 lex->prev_token_terminator = lex->token_terminator;
982 lex->token_terminator = s + 1;
985 /*-------------------------------------------------------------------------
986 * The next token in the input stream is known to be a number; lex it.
988 * In JSON, a number consists of four parts:
990 * (1) An optional minus sign ('-').
992 * (2) Either a single '0', or a string of one or more digits that does not
995 * (3) An optional decimal part, consisting of a period ('.') followed by
996 * one or more digits. (Note: While this part can be omitted
997 * completely, it's not OK to have only the decimal point without
998 * any digits afterwards.)
1000 * (4) An optional exponent part, consisting of 'e' or 'E', optionally
1001 * followed by '+' or '-', followed by one or more digits. (Note:
1002 * As with the decimal part, if 'e' or 'E' is present, it must be
1003 * followed by at least one digit.)
1005 * The 's' argument to this function points to the ostensible beginning
1006 * of part 2 - i.e. the character after any optional minus sign, and the
1007 * first character of the string if there is none.
1009 *-------------------------------------------------------------------------
1012 json_lex_number(JsonLexContext *lex, char *s, bool *num_err)
1018 len = s - lex->input;
1019 /* Part (1): leading sign indicator. */
1020 /* Caller already did this for us; so do nothing. */
1022 /* Part (2): parse main digit string. */
1028 else if (*s >= '1' && *s <= '9')
1034 } while (len < lex->input_length && *s >= '0' && *s <= '9');
1039 /* Part (3): parse optional decimal portion. */
1040 if (len < lex->input_length && *s == '.')
1044 if (len == lex->input_length || *s < '0' || *s > '9')
1052 } while (len < lex->input_length && *s >= '0' && *s <= '9');
1056 /* Part (4): parse optional exponent. */
1057 if (len < lex->input_length && (*s == 'e' || *s == 'E'))
1061 if (len < lex->input_length && (*s == '+' || *s == '-'))
1066 if (len == lex->input_length || *s < '0' || *s > '9')
1074 } while (len < lex->input_length && *s >= '0' && *s <= '9');
1079 * Check for trailing garbage. As in json_lex(), any alphanumeric stuff
1080 * here should be considered part of the token for error-reporting
1083 for (p = s; len < lex->input_length && JSON_ALPHANUMERIC_CHAR(*p); p++, len++)
1086 if (num_err != NULL)
1088 /* let the caller handle the error */
1093 lex->prev_token_terminator = lex->token_terminator;
1094 lex->token_terminator = p;
1096 report_invalid_token(lex);
1101 * Report a parse error.
1103 * lex->token_start and lex->token_terminator must identify the current token.
1106 report_parse_error(JsonParseContext ctx, JsonLexContext *lex)
1111 /* Handle case where the input ended prematurely. */
1112 if (lex->token_start == NULL || lex->token_type == JSON_TOKEN_END)
1114 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1115 errmsg("invalid input syntax for type json"),
1116 errdetail("The input string ended unexpectedly."),
1117 report_json_context(lex)));
1119 /* Separate out the current token. */
1120 toklen = lex->token_terminator - lex->token_start;
1121 token = palloc(toklen + 1);
1122 memcpy(token, lex->token_start, toklen);
1123 token[toklen] = '\0';
1125 /* Complain, with the appropriate detail message. */
1126 if (ctx == JSON_PARSE_END)
1128 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1129 errmsg("invalid input syntax for type json"),
1130 errdetail("Expected end of input, but found \"%s\".",
1132 report_json_context(lex)));
1137 case JSON_PARSE_VALUE:
1139 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1140 errmsg("invalid input syntax for type json"),
1141 errdetail("Expected JSON value, but found \"%s\".",
1143 report_json_context(lex)));
1145 case JSON_PARSE_STRING:
1147 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1148 errmsg("invalid input syntax for type json"),
1149 errdetail("Expected string, but found \"%s\".",
1151 report_json_context(lex)));
1153 case JSON_PARSE_ARRAY_START:
1155 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1156 errmsg("invalid input syntax for type json"),
1157 errdetail("Expected array element or \"]\", but found \"%s\".",
1159 report_json_context(lex)));
1161 case JSON_PARSE_ARRAY_NEXT:
1163 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1164 errmsg("invalid input syntax for type json"),
1165 errdetail("Expected \",\" or \"]\", but found \"%s\".",
1167 report_json_context(lex)));
1169 case JSON_PARSE_OBJECT_START:
1171 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1172 errmsg("invalid input syntax for type json"),
1173 errdetail("Expected string or \"}\", but found \"%s\".",
1175 report_json_context(lex)));
1177 case JSON_PARSE_OBJECT_LABEL:
1179 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1180 errmsg("invalid input syntax for type json"),
1181 errdetail("Expected \":\", but found \"%s\".",
1183 report_json_context(lex)));
1185 case JSON_PARSE_OBJECT_NEXT:
1187 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1188 errmsg("invalid input syntax for type json"),
1189 errdetail("Expected \",\" or \"}\", but found \"%s\".",
1191 report_json_context(lex)));
1193 case JSON_PARSE_OBJECT_COMMA:
1195 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1196 errmsg("invalid input syntax for type json"),
1197 errdetail("Expected string, but found \"%s\".",
1199 report_json_context(lex)));
1202 elog(ERROR, "unexpected json parse state: %d", ctx);
1208 * Report an invalid input token.
1210 * lex->token_start and lex->token_terminator must identify the token.
1213 report_invalid_token(JsonLexContext *lex)
1218 /* Separate out the offending token. */
1219 toklen = lex->token_terminator - lex->token_start;
1220 token = palloc(toklen + 1);
1221 memcpy(token, lex->token_start, toklen);
1222 token[toklen] = '\0';
1225 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1226 errmsg("invalid input syntax for type json"),
1227 errdetail("Token \"%s\" is invalid.", token),
1228 report_json_context(lex)));
1232 * Report a CONTEXT line for bogus JSON input.
1234 * lex->token_terminator must be set to identify the spot where we detected
1235 * the error. Note that lex->token_start might be NULL, in case we recognized
1238 * The return value isn't meaningful, but we make it non-void so that this
1239 * can be invoked inside ereport().
1242 report_json_context(JsonLexContext *lex)
1244 const char *context_start;
1245 const char *context_end;
1246 const char *line_start;
1253 /* Choose boundaries for the part of the input we will display */
1254 context_start = lex->input;
1255 context_end = lex->token_terminator;
1256 line_start = context_start;
1260 /* Always advance over newlines */
1261 if (context_start < context_end && *context_start == '\n')
1264 line_start = context_start;
1268 /* Otherwise, done as soon as we are close enough to context_end */
1269 if (context_end - context_start < 50)
1271 /* Advance to next multibyte character */
1272 if (IS_HIGHBIT_SET(*context_start))
1273 context_start += pg_mblen(context_start);
1279 * We add "..." to indicate that the excerpt doesn't start at the
1280 * beginning of the line ... but if we're within 3 characters of the
1281 * beginning of the line, we might as well just show the whole line.
1283 if (context_start - line_start <= 3)
1284 context_start = line_start;
1286 /* Get a null-terminated copy of the data to present */
1287 ctxtlen = context_end - context_start;
1288 ctxt = palloc(ctxtlen + 1);
1289 memcpy(ctxt, context_start, ctxtlen);
1290 ctxt[ctxtlen] = '\0';
1293 * Show the context, prefixing "..." if not starting at start of line, and
1294 * suffixing "..." if not ending at end of line.
1296 prefix = (context_start > line_start) ? "..." : "";
1297 suffix = (lex->token_type != JSON_TOKEN_END && context_end - lex->input < lex->input_length && *context_end != '\n' && *context_end != '\r') ? "..." : "";
1299 return errcontext("JSON data, line %d: %s%s%s",
1300 line_number, prefix, ctxt, suffix);
1304 * Extract a single, possibly multi-byte char from the input string.
1307 extract_mb_char(char *s)
1313 res = palloc(len + 1);
1314 memcpy(res, s, len);
1321 * Determine how we want to print values of a given type in datum_to_json.
1323 * Given the datatype OID, return its JsonTypeCategory, as well as the type's
1324 * output function OID. If the returned category is JSONTYPE_CAST, we
1325 * return the OID of the type->JSON cast function instead.
1328 json_categorize_type(Oid typoid,
1329 JsonTypeCategory *tcategory,
1334 /* Look through any domain */
1335 typoid = getBaseType(typoid);
1337 *outfuncoid = InvalidOid;
1340 * We need to get the output function for everything except date and
1341 * timestamp types, array and composite types, booleans, and non-builtin
1342 * types where there's a cast to json.
1348 *tcategory = JSONTYPE_BOOL;
1357 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
1358 *tcategory = JSONTYPE_NUMERIC;
1362 *tcategory = JSONTYPE_DATE;
1366 *tcategory = JSONTYPE_TIMESTAMP;
1369 case TIMESTAMPTZOID:
1370 *tcategory = JSONTYPE_TIMESTAMPTZ;
1375 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
1376 *tcategory = JSONTYPE_JSON;
1380 /* Check for arrays and composites */
1381 if (OidIsValid(get_element_type(typoid)))
1382 *tcategory = JSONTYPE_ARRAY;
1383 else if (type_is_rowtype(typoid))
1384 *tcategory = JSONTYPE_COMPOSITE;
1387 /* It's probably the general case ... */
1388 *tcategory = JSONTYPE_OTHER;
1389 /* but let's look for a cast to json, if it's not built-in */
1390 if (typoid >= FirstNormalObjectId)
1393 CoercionPathType ctype;
1395 ctype = find_coercion_pathway(JSONOID, typoid,
1398 if (ctype == COERCION_PATH_FUNC && OidIsValid(castfunc))
1400 *tcategory = JSONTYPE_CAST;
1401 *outfuncoid = castfunc;
1405 /* non builtin type with no cast */
1406 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
1411 /* any other builtin type */
1412 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
1420 * Turn a Datum into JSON text, appending the string to "result".
1422 * tcategory and outfuncoid are from a previous call to json_categorize_type,
1423 * except that if is_null is true then they can be invalid.
1425 * If key_scalar is true, the value is being printed as a key, so insist
1426 * it's of an acceptable type, and force it to be quoted.
1429 datum_to_json(Datum val, bool is_null, StringInfo result,
1430 JsonTypeCategory tcategory, Oid outfuncoid,
1436 /* callers are expected to ensure that null keys are not passed in */
1437 Assert(!(key_scalar && is_null));
1441 appendStringInfoString(result, "null");
1446 (tcategory == JSONTYPE_ARRAY ||
1447 tcategory == JSONTYPE_COMPOSITE ||
1448 tcategory == JSONTYPE_JSON ||
1449 tcategory == JSONTYPE_CAST))
1451 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1452 errmsg("key value must be scalar, not array, composite, or json")));
1456 case JSONTYPE_ARRAY:
1457 array_to_json_internal(val, result, false);
1459 case JSONTYPE_COMPOSITE:
1460 composite_to_json(val, result, false);
1463 outputstr = DatumGetBool(val) ? "true" : "false";
1465 escape_json(result, outputstr);
1467 appendStringInfoString(result, outputstr);
1469 case JSONTYPE_NUMERIC:
1470 outputstr = OidOutputFunctionCall(outfuncoid, val);
1473 * Don't call escape_json for a non-key if it's a valid JSON
1476 if (!key_scalar && IsValidJsonNumber(outputstr, strlen(outputstr)))
1477 appendStringInfoString(result, outputstr);
1479 escape_json(result, outputstr);
1486 char buf[MAXDATELEN + 1];
1488 date = DatumGetDateADT(val);
1490 if (DATE_NOT_FINITE(date))
1492 /* we have to format infinity ourselves */
1493 appendStringInfoString(result, DT_INFINITY);
1497 j2date(date + POSTGRES_EPOCH_JDATE,
1498 &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
1499 EncodeDateOnly(&tm, USE_XSD_DATES, buf);
1500 appendStringInfo(result, "\"%s\"", buf);
1504 case JSONTYPE_TIMESTAMP:
1506 Timestamp timestamp;
1509 char buf[MAXDATELEN + 1];
1511 timestamp = DatumGetTimestamp(val);
1513 if (TIMESTAMP_NOT_FINITE(timestamp))
1515 /* we have to format infinity ourselves */
1516 appendStringInfoString(result, DT_INFINITY);
1518 else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
1520 EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
1521 appendStringInfo(result, "\"%s\"", buf);
1525 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1526 errmsg("timestamp out of range")));
1529 case JSONTYPE_TIMESTAMPTZ:
1531 TimestampTz timestamp;
1535 const char *tzn = NULL;
1536 char buf[MAXDATELEN + 1];
1538 timestamp = DatumGetTimestamp(val);
1540 if (TIMESTAMP_NOT_FINITE(timestamp))
1542 /* we have to format infinity ourselves */
1543 appendStringInfoString(result, DT_INFINITY);
1545 else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
1547 EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
1548 appendStringInfo(result, "\"%s\"", buf);
1552 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1553 errmsg("timestamp out of range")));
1557 /* JSON and JSONB output will already be escaped */
1558 outputstr = OidOutputFunctionCall(outfuncoid, val);
1559 appendStringInfoString(result, outputstr);
1563 /* outfuncoid refers to a cast function, not an output function */
1564 jsontext = DatumGetTextP(OidFunctionCall1(outfuncoid, val));
1565 outputstr = text_to_cstring(jsontext);
1566 appendStringInfoString(result, outputstr);
1571 outputstr = OidOutputFunctionCall(outfuncoid, val);
1572 escape_json(result, outputstr);
1579 * Process a single dimension of an array.
1580 * If it's the innermost dimension, output the values, otherwise call
1581 * ourselves recursively to process the next dimension.
1584 array_dim_to_json(StringInfo result, int dim, int ndims, int *dims, Datum *vals,
1585 bool *nulls, int *valcount, JsonTypeCategory tcategory,
1586 Oid outfuncoid, bool use_line_feeds)
1591 Assert(dim < ndims);
1593 sep = use_line_feeds ? ",\n " : ",";
1595 appendStringInfoChar(result, '[');
1597 for (i = 1; i <= dims[dim]; i++)
1600 appendStringInfoString(result, sep);
1602 if (dim + 1 == ndims)
1604 datum_to_json(vals[*valcount], nulls[*valcount], result, tcategory,
1611 * Do we want line feeds on inner dimensions of arrays? For now
1614 array_dim_to_json(result, dim + 1, ndims, dims, vals, nulls,
1615 valcount, tcategory, outfuncoid, false);
1619 appendStringInfoChar(result, ']');
1623 * Turn an array into JSON.
1626 array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
1628 ArrayType *v = DatumGetArrayTypeP(array);
1629 Oid element_type = ARR_ELEMTYPE(v);
1639 JsonTypeCategory tcategory;
1644 nitems = ArrayGetNItems(ndim, dim);
1648 appendStringInfoString(result, "[]");
1652 get_typlenbyvalalign(element_type,
1653 &typlen, &typbyval, &typalign);
1655 json_categorize_type(element_type,
1656 &tcategory, &outfuncoid);
1658 deconstruct_array(v, element_type, typlen, typbyval,
1659 typalign, &elements, &nulls,
1662 array_dim_to_json(result, 0, ndim, dim, elements, nulls, &count, tcategory,
1663 outfuncoid, use_line_feeds);
1670 * Turn a composite / record into JSON.
1673 composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
1679 HeapTupleData tmptup,
1682 bool needsep = false;
1685 sep = use_line_feeds ? ",\n " : ",";
1687 td = DatumGetHeapTupleHeader(composite);
1689 /* Extract rowtype info and find a tupdesc */
1690 tupType = HeapTupleHeaderGetTypeId(td);
1691 tupTypmod = HeapTupleHeaderGetTypMod(td);
1692 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
1694 /* Build a temporary HeapTuple control structure */
1695 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
1699 appendStringInfoChar(result, '{');
1701 for (i = 0; i < tupdesc->natts; i++)
1706 JsonTypeCategory tcategory;
1709 if (tupdesc->attrs[i]->attisdropped)
1713 appendStringInfoString(result, sep);
1716 attname = NameStr(tupdesc->attrs[i]->attname);
1717 escape_json(result, attname);
1718 appendStringInfoChar(result, ':');
1720 val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
1724 tcategory = JSONTYPE_NULL;
1725 outfuncoid = InvalidOid;
1728 json_categorize_type(tupdesc->attrs[i]->atttypid,
1729 &tcategory, &outfuncoid);
1731 datum_to_json(val, isnull, result, tcategory, outfuncoid, false);
1734 appendStringInfoChar(result, '}');
1735 ReleaseTupleDesc(tupdesc);
1739 * Append JSON text for "val" to "result".
1741 * This is just a thin wrapper around datum_to_json. If the same type will be
1742 * printed many times, avoid using this; better to do the json_categorize_type
1743 * lookups only once.
1746 add_json(Datum val, bool is_null, StringInfo result,
1747 Oid val_type, bool key_scalar)
1749 JsonTypeCategory tcategory;
1752 if (val_type == InvalidOid)
1754 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1755 errmsg("could not determine input data type")));
1759 tcategory = JSONTYPE_NULL;
1760 outfuncoid = InvalidOid;
1763 json_categorize_type(val_type,
1764 &tcategory, &outfuncoid);
1766 datum_to_json(val, is_null, result, tcategory, outfuncoid, key_scalar);
1770 * SQL function array_to_json(row)
1773 array_to_json(PG_FUNCTION_ARGS)
1775 Datum array = PG_GETARG_DATUM(0);
1778 result = makeStringInfo();
1780 array_to_json_internal(array, result, false);
1782 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1786 * SQL function array_to_json(row, prettybool)
1789 array_to_json_pretty(PG_FUNCTION_ARGS)
1791 Datum array = PG_GETARG_DATUM(0);
1792 bool use_line_feeds = PG_GETARG_BOOL(1);
1795 result = makeStringInfo();
1797 array_to_json_internal(array, result, use_line_feeds);
1799 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1803 * SQL function row_to_json(row)
1806 row_to_json(PG_FUNCTION_ARGS)
1808 Datum array = PG_GETARG_DATUM(0);
1811 result = makeStringInfo();
1813 composite_to_json(array, result, false);
1815 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1819 * SQL function row_to_json(row, prettybool)
1822 row_to_json_pretty(PG_FUNCTION_ARGS)
1824 Datum array = PG_GETARG_DATUM(0);
1825 bool use_line_feeds = PG_GETARG_BOOL(1);
1828 result = makeStringInfo();
1830 composite_to_json(array, result, use_line_feeds);
1832 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1836 * SQL function to_json(anyvalue)
1839 to_json(PG_FUNCTION_ARGS)
1841 Datum val = PG_GETARG_DATUM(0);
1842 Oid val_type = get_fn_expr_argtype(fcinfo->flinfo, 0);
1844 JsonTypeCategory tcategory;
1847 if (val_type == InvalidOid)
1849 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1850 errmsg("could not determine input data type")));
1852 json_categorize_type(val_type,
1853 &tcategory, &outfuncoid);
1855 result = makeStringInfo();
1857 datum_to_json(val, false, result, tcategory, outfuncoid, false);
1859 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1863 * json_agg transition function
1865 * aggregate input column as a json array value.
1868 json_agg_transfn(PG_FUNCTION_ARGS)
1870 MemoryContext aggcontext,
1872 JsonAggState *state;
1875 if (!AggCheckCallContext(fcinfo, &aggcontext))
1877 /* cannot be called directly because of internal-type argument */
1878 elog(ERROR, "json_agg_transfn called in non-aggregate context");
1881 if (PG_ARGISNULL(0))
1883 Oid arg_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
1885 if (arg_type == InvalidOid)
1887 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1888 errmsg("could not determine input data type")));
1891 * Make this state object in a context where it will persist for the
1892 * duration of the aggregate call. MemoryContextSwitchTo is only
1893 * needed the first time, as the StringInfo routines make sure they
1894 * use the right context to enlarge the object if necessary.
1896 oldcontext = MemoryContextSwitchTo(aggcontext);
1897 state = (JsonAggState *) palloc(sizeof(JsonAggState));
1898 state->str = makeStringInfo();
1899 MemoryContextSwitchTo(oldcontext);
1901 appendStringInfoChar(state->str, '[');
1902 json_categorize_type(arg_type,&state->val_category,
1903 &state->val_output_func);
1907 state = (JsonAggState *) PG_GETARG_POINTER(0);
1908 appendStringInfoString(state->str, ", ");
1911 /* fast path for NULLs */
1912 if (PG_ARGISNULL(1))
1914 datum_to_json((Datum) 0, true, state->str, JSONTYPE_NULL,
1916 PG_RETURN_POINTER(state);
1919 val = PG_GETARG_DATUM(1);
1921 /* add some whitespace if structured type and not first item */
1922 if (!PG_ARGISNULL(0) &&
1923 (state->val_category == JSONTYPE_ARRAY ||
1924 state->val_category == JSONTYPE_COMPOSITE))
1926 appendStringInfoString(state->str, "\n ");
1929 datum_to_json(val, false, state->str, state->val_category,
1930 state->val_output_func, false);
1933 * The transition type for array_agg() is declared to be "internal", which
1934 * is a pass-by-value type the same size as a pointer. So we can safely
1935 * pass the JsonAggState pointer through nodeAgg.c's machinations.
1937 PG_RETURN_POINTER(state);
1941 * json_agg final function
1944 json_agg_finalfn(PG_FUNCTION_ARGS)
1946 JsonAggState *state;
1948 /* cannot be called directly because of internal-type argument */
1949 Assert(AggCheckCallContext(fcinfo, NULL));
1951 state = PG_ARGISNULL(0) ?
1953 (JsonAggState *) PG_GETARG_POINTER(0);
1955 /* NULL result for no rows in, as is standard with aggregates */
1959 /* Else return state with appropriate array terminator added */
1960 PG_RETURN_TEXT_P(catenate_stringinfo_string(state->str, "]"));
1964 * json_object_agg transition function.
1966 * aggregate two input columns as a single json object value.
1969 json_object_agg_transfn(PG_FUNCTION_ARGS)
1971 MemoryContext aggcontext,
1973 JsonAggState *state;
1976 if (!AggCheckCallContext(fcinfo, &aggcontext))
1978 /* cannot be called directly because of internal-type argument */
1979 elog(ERROR, "json_object_agg_transfn called in non-aggregate context");
1982 if (PG_ARGISNULL(0))
1987 * Make the StringInfo in a context where it will persist for the
1988 * duration of the aggregate call. Switching context is only needed
1989 * for this initial step, as the StringInfo routines make sure they
1990 * use the right context to enlarge the object if necessary.
1992 oldcontext = MemoryContextSwitchTo(aggcontext);
1993 state = (JsonAggState *) palloc(sizeof(JsonAggState));
1994 state->str = makeStringInfo();
1995 MemoryContextSwitchTo(oldcontext);
1997 arg_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
1999 if (arg_type == InvalidOid)
2001 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2002 errmsg("could not determine data type for argument 1")));
2004 json_categorize_type(arg_type,&state->key_category,
2005 &state->key_output_func);
2007 arg_type = get_fn_expr_argtype(fcinfo->flinfo, 2);
2009 if (arg_type == InvalidOid)
2011 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2012 errmsg("could not determine data type for argument 2")));
2014 json_categorize_type(arg_type,&state->val_category,
2015 &state->val_output_func);
2017 appendStringInfoString(state->str, "{ ");
2021 state = (JsonAggState *) PG_GETARG_POINTER(0);
2022 appendStringInfoString(state->str, ", ");
2026 * Note: since json_object_agg() is declared as taking type "any", the
2027 * parser will not do any type conversion on unknown-type literals (that
2028 * is, undecorated strings or NULLs). Such values will arrive here as
2029 * type UNKNOWN, which fortunately does not matter to us, since
2030 * unknownout() works fine.
2033 if (PG_ARGISNULL(1))
2035 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2036 errmsg("field name must not be null")));
2038 arg = PG_GETARG_DATUM(1);
2040 datum_to_json(arg, false, state->str, state->key_category,
2041 state->key_output_func, true);
2043 appendStringInfoString(state->str, " : ");
2045 if (PG_ARGISNULL(2))
2048 arg = PG_GETARG_DATUM(2);
2050 datum_to_json(arg, PG_ARGISNULL(2), state->str, state->val_category,
2051 state->val_output_func, false);
2053 PG_RETURN_POINTER(state);
2057 * json_object_agg final function.
2060 json_object_agg_finalfn(PG_FUNCTION_ARGS)
2062 JsonAggState *state;
2064 /* cannot be called directly because of internal-type argument */
2065 Assert(AggCheckCallContext(fcinfo, NULL));
2067 state = PG_ARGISNULL(0) ? NULL : (JsonAggState *) PG_GETARG_POINTER(0);
2069 /* NULL result for no rows in, as is standard with aggregates */
2073 /* Else return state with appropriate object terminator added */
2074 PG_RETURN_TEXT_P(catenate_stringinfo_string(state->str, " }"));
2078 * Helper function for aggregates: return given StringInfo's contents plus
2079 * specified trailing string, as a text datum. We need this because aggregate
2080 * final functions are not allowed to modify the aggregate state.
2083 catenate_stringinfo_string(StringInfo buffer, const char *addon)
2085 /* custom version of cstring_to_text_with_len */
2086 int buflen = buffer->len;
2087 int addlen = strlen(addon);
2088 text *result = (text *) palloc(buflen + addlen + VARHDRSZ);
2090 SET_VARSIZE(result, buflen + addlen + VARHDRSZ);
2091 memcpy(VARDATA(result), buffer->data, buflen);
2092 memcpy(VARDATA(result) + buflen, addon, addlen);
2098 * SQL function json_build_object(variadic "any")
2101 json_build_object(PG_FUNCTION_ARGS)
2103 int nargs = PG_NARGS();
2106 const char *sep = "";
2112 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2113 errmsg("argument list must have even number of elements"),
2114 errhint("The arguments of json_build_object() must consist of alternating keys and values.")));
2116 result = makeStringInfo();
2118 appendStringInfoChar(result, '{');
2120 for (i = 0; i < nargs; i += 2)
2123 * Note: since json_build_object() is declared as taking type "any",
2124 * the parser will not do any type conversion on unknown-type literals
2125 * (that is, undecorated strings or NULLs). Such values will arrive
2126 * here as type UNKNOWN, which fortunately does not matter to us,
2127 * since unknownout() works fine.
2129 appendStringInfoString(result, sep);
2133 val_type = get_fn_expr_argtype(fcinfo->flinfo, i);
2135 if (val_type == InvalidOid)
2137 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2138 errmsg("could not determine data type for argument %d",
2141 if (PG_ARGISNULL(i))
2143 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2144 errmsg("argument %d cannot be null", i + 1),
2145 errhint("Object keys should be text.")));
2147 arg = PG_GETARG_DATUM(i);
2149 add_json(arg, false, result, val_type, true);
2151 appendStringInfoString(result, " : ");
2154 val_type = get_fn_expr_argtype(fcinfo->flinfo, i + 1);
2156 if (val_type == InvalidOid)
2158 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2159 errmsg("could not determine data type for argument %d",
2162 if (PG_ARGISNULL(i + 1))
2165 arg = PG_GETARG_DATUM(i + 1);
2167 add_json(arg, PG_ARGISNULL(i + 1), result, val_type, false);
2170 appendStringInfoChar(result, '}');
2172 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
2176 * degenerate case of json_build_object where it gets 0 arguments.
2179 json_build_object_noargs(PG_FUNCTION_ARGS)
2181 PG_RETURN_TEXT_P(cstring_to_text_with_len("{}", 2));
2185 * SQL function json_build_array(variadic "any")
2188 json_build_array(PG_FUNCTION_ARGS)
2190 int nargs = PG_NARGS();
2193 const char *sep = "";
2197 result = makeStringInfo();
2199 appendStringInfoChar(result, '[');
2201 for (i = 0; i < nargs; i++)
2204 * Note: since json_build_array() is declared as taking type "any",
2205 * the parser will not do any type conversion on unknown-type literals
2206 * (that is, undecorated strings or NULLs). Such values will arrive
2207 * here as type UNKNOWN, which fortunately does not matter to us,
2208 * since unknownout() works fine.
2210 appendStringInfoString(result, sep);
2213 val_type = get_fn_expr_argtype(fcinfo->flinfo, i);
2215 if (val_type == InvalidOid)
2217 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2218 errmsg("could not determine data type for argument %d",
2221 if (PG_ARGISNULL(i))
2224 arg = PG_GETARG_DATUM(i);
2226 add_json(arg, PG_ARGISNULL(i), result, val_type, false);
2229 appendStringInfoChar(result, ']');
2231 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
2235 * degenerate case of json_build_array where it gets 0 arguments.
2238 json_build_array_noargs(PG_FUNCTION_ARGS)
2240 PG_RETURN_TEXT_P(cstring_to_text_with_len("[]", 2));
2244 * SQL function json_object(text[])
2246 * take a one or two dimensional array of text as key/value pairs
2247 * for a json object.
2250 json_object(PG_FUNCTION_ARGS)
2252 ArrayType *in_array = PG_GETARG_ARRAYTYPE_P(0);
2253 int ndims = ARR_NDIM(in_array);
2254 StringInfoData result;
2266 PG_RETURN_DATUM(CStringGetTextDatum("{}"));
2270 if ((ARR_DIMS(in_array)[0]) % 2)
2272 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2273 errmsg("array must have even number of elements")));
2277 if ((ARR_DIMS(in_array)[1]) != 2)
2279 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2280 errmsg("array must have two columns")));
2285 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2286 errmsg("wrong number of array subscripts")));
2289 deconstruct_array(in_array,
2290 TEXTOID, -1, false, 'i',
2291 &in_datums, &in_nulls, &in_count);
2293 count = in_count / 2;
2295 initStringInfo(&result);
2297 appendStringInfoChar(&result, '{');
2299 for (i = 0; i < count; ++i)
2301 if (in_nulls[i * 2])
2303 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2304 errmsg("null value not allowed for object key")));
2306 v = TextDatumGetCString(in_datums[i * 2]);
2308 appendStringInfoString(&result, ", ");
2309 escape_json(&result, v);
2310 appendStringInfoString(&result, " : ");
2312 if (in_nulls[i * 2 + 1])
2313 appendStringInfoString(&result, "null");
2316 v = TextDatumGetCString(in_datums[i * 2 + 1]);
2317 escape_json(&result, v);
2322 appendStringInfoChar(&result, '}');
2327 rval = cstring_to_text_with_len(result.data, result.len);
2330 PG_RETURN_TEXT_P(rval);
2335 * SQL function json_object(text[], text[])
2337 * take separate key and value arrays of text to construct a json object
2341 json_object_two_arg(PG_FUNCTION_ARGS)
2343 ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(0);
2344 ArrayType *val_array = PG_GETARG_ARRAYTYPE_P(1);
2345 int nkdims = ARR_NDIM(key_array);
2346 int nvdims = ARR_NDIM(val_array);
2347 StringInfoData result;
2358 if (nkdims > 1 || nkdims != nvdims)
2360 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2361 errmsg("wrong number of array subscripts")));
2364 PG_RETURN_DATUM(CStringGetTextDatum("{}"));
2366 deconstruct_array(key_array,
2367 TEXTOID, -1, false, 'i',
2368 &key_datums, &key_nulls, &key_count);
2370 deconstruct_array(val_array,
2371 TEXTOID, -1, false, 'i',
2372 &val_datums, &val_nulls, &val_count);
2374 if (key_count != val_count)
2376 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2377 errmsg("mismatched array dimensions")));
2379 initStringInfo(&result);
2381 appendStringInfoChar(&result, '{');
2383 for (i = 0; i < key_count; ++i)
2387 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2388 errmsg("null value not allowed for object key")));
2390 v = TextDatumGetCString(key_datums[i]);
2392 appendStringInfoString(&result, ", ");
2393 escape_json(&result, v);
2394 appendStringInfoString(&result, " : ");
2397 appendStringInfoString(&result, "null");
2400 v = TextDatumGetCString(val_datums[i]);
2401 escape_json(&result, v);
2406 appendStringInfoChar(&result, '}');
2413 rval = cstring_to_text_with_len(result.data, result.len);
2416 PG_RETURN_TEXT_P(rval);
2421 * Produce a JSON string literal, properly escaping characters in the text.
2424 escape_json(StringInfo buf, const char *str)
2428 appendStringInfoCharMacro(buf, '\"');
2429 for (p = str; *p; p++)
2434 appendStringInfoString(buf, "\\b");
2437 appendStringInfoString(buf, "\\f");
2440 appendStringInfoString(buf, "\\n");
2443 appendStringInfoString(buf, "\\r");
2446 appendStringInfoString(buf, "\\t");
2449 appendStringInfoString(buf, "\\\"");
2452 appendStringInfoString(buf, "\\\\");
2455 if ((unsigned char) *p < ' ')
2456 appendStringInfo(buf, "\\u%04x", (int) *p);
2458 appendStringInfoCharMacro(buf, *p);
2462 appendStringInfoCharMacro(buf, '\"');
2466 * SQL function json_typeof(json) -> text
2468 * Returns the type of the outermost JSON value as TEXT. Possible types are
2469 * "object", "array", "string", "number", "boolean", and "null".
2471 * Performs a single call to json_lex() to get the first token of the supplied
2472 * value. This initial token uniquely determines the value's type. As our
2473 * input must already have been validated by json_in() or json_recv(), the
2474 * initial token should never be JSON_TOKEN_OBJECT_END, JSON_TOKEN_ARRAY_END,
2475 * JSON_TOKEN_COLON, JSON_TOKEN_COMMA, or JSON_TOKEN_END.
2478 json_typeof(PG_FUNCTION_ARGS)
2482 JsonLexContext *lex;
2486 json = PG_GETARG_TEXT_P(0);
2487 lex = makeJsonLexContext(json, false);
2489 /* Lex exactly one token from the input and check its type. */
2491 tok = lex_peek(lex);
2494 case JSON_TOKEN_OBJECT_START:
2497 case JSON_TOKEN_ARRAY_START:
2500 case JSON_TOKEN_STRING:
2503 case JSON_TOKEN_NUMBER:
2506 case JSON_TOKEN_TRUE:
2507 case JSON_TOKEN_FALSE:
2510 case JSON_TOKEN_NULL:
2514 elog(ERROR, "unexpected json token: %d", tok);
2517 PG_RETURN_TEXT_P(cstring_to_text(type));