1 /*-------------------------------------------------------------------------
4 * JSON data type support.
6 * Portions Copyright (c) 1996-2014, 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_cast.h"
19 #include "catalog/pg_type.h"
20 #include "executor/spi.h"
21 #include "lib/stringinfo.h"
22 #include "libpq/pqformat.h"
23 #include "mb/pg_wchar.h"
24 #include "miscadmin.h"
25 #include "parser/parse_coerce.h"
26 #include "utils/array.h"
27 #include "utils/builtins.h"
28 #include "utils/datetime.h"
29 #include "utils/lsyscache.h"
30 #include "utils/json.h"
31 #include "utils/jsonapi.h"
32 #include "utils/typcache.h"
33 #include "utils/syscache.h"
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_TIMESTAMP, /* we use special formatting for timestamp */
59 JSONTYPE_TIMESTAMPTZ, /* ... and timestamptz */
60 JSONTYPE_JSON, /* JSON itself (and JSONB) */
61 JSONTYPE_ARRAY, /* array */
62 JSONTYPE_COMPOSITE, /* composite */
63 JSONTYPE_CAST, /* something with an explicit cast to JSON */
64 JSONTYPE_OTHER /* all else */
67 static inline void json_lex(JsonLexContext *lex);
68 static inline void json_lex_string(JsonLexContext *lex);
69 static inline void json_lex_number(JsonLexContext *lex, char *s, bool *num_err);
70 static inline void parse_scalar(JsonLexContext *lex, JsonSemAction *sem);
71 static void parse_object_field(JsonLexContext *lex, JsonSemAction *sem);
72 static void parse_object(JsonLexContext *lex, JsonSemAction *sem);
73 static void parse_array_element(JsonLexContext *lex, JsonSemAction *sem);
74 static void parse_array(JsonLexContext *lex, JsonSemAction *sem);
75 static void report_parse_error(JsonParseContext ctx, JsonLexContext *lex);
76 static void report_invalid_token(JsonLexContext *lex);
77 static int report_json_context(JsonLexContext *lex);
78 static char *extract_mb_char(char *s);
79 static void composite_to_json(Datum composite, StringInfo result,
81 static void array_dim_to_json(StringInfo result, int dim, int ndims, int *dims,
82 Datum *vals, bool *nulls, int *valcount,
83 JsonTypeCategory tcategory, Oid outfuncoid,
85 static void array_to_json_internal(Datum array, StringInfo result,
87 static void json_categorize_type(Oid typoid,
88 JsonTypeCategory *tcategory,
90 static void datum_to_json(Datum val, bool is_null, StringInfo result,
91 JsonTypeCategory tcategory, Oid outfuncoid,
93 static void add_json(Datum val, bool is_null, StringInfo result,
94 Oid val_type, bool key_scalar);
96 /* the null action object used for pure validation */
97 static JsonSemAction nullSemAction =
99 NULL, NULL, NULL, NULL, NULL,
100 NULL, NULL, NULL, NULL, NULL
103 /* Recursive Descent parser support routines */
108 * what is the current look_ahead token?
110 static inline JsonTokenType
111 lex_peek(JsonLexContext *lex)
113 return lex->token_type;
119 * accept the look_ahead token and move the lexer to the next token if the
120 * look_ahead token matches the token parameter. In that case, and if required,
121 * also hand back the de-escaped lexeme.
123 * returns true if the token matched, false otherwise.
126 lex_accept(JsonLexContext *lex, JsonTokenType token, char **lexeme)
128 if (lex->token_type == token)
132 if (lex->token_type == JSON_TOKEN_STRING)
134 if (lex->strval != NULL)
135 *lexeme = pstrdup(lex->strval->data);
139 int len = (lex->token_terminator - lex->token_start);
140 char *tokstr = palloc(len + 1);
142 memcpy(tokstr, lex->token_start, len);
156 * move the lexer to the next token if the current look_ahead token matches
157 * the parameter token. Otherwise, report an error.
160 lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
162 if (!lex_accept(lex, token, NULL))
163 report_parse_error(ctx, lex);;
166 /* chars to consider as part of an alphanumeric token */
167 #define JSON_ALPHANUMERIC_CHAR(c) \
168 (((c) >= 'a' && (c) <= 'z') || \
169 ((c) >= 'A' && (c) <= 'Z') || \
170 ((c) >= '0' && (c) <= '9') || \
178 json_in(PG_FUNCTION_ARGS)
180 char *json = PG_GETARG_CSTRING(0);
181 text *result = cstring_to_text(json);
185 lex = makeJsonLexContext(result, false);
186 pg_parse_json(lex, &nullSemAction);
188 /* Internal representation is the same as text, for now */
189 PG_RETURN_TEXT_P(result);
196 json_out(PG_FUNCTION_ARGS)
198 /* we needn't detoast because text_to_cstring will handle that */
199 Datum txt = PG_GETARG_DATUM(0);
201 PG_RETURN_CSTRING(TextDatumGetCString(txt));
208 json_send(PG_FUNCTION_ARGS)
210 text *t = PG_GETARG_TEXT_PP(0);
213 pq_begintypsend(&buf);
214 pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
215 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
222 json_recv(PG_FUNCTION_ARGS)
224 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
229 str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
232 lex = makeJsonLexContextCstringLen(str, nbytes, false);
233 pg_parse_json(lex, &nullSemAction);
235 PG_RETURN_TEXT_P(cstring_to_text_with_len(str, nbytes));
241 * lex constructor, with or without StringInfo object
242 * for de-escaped lexemes.
244 * Without is better as it makes the processing faster, so only make one
245 * if really required.
247 * If you already have the json as a text* value, use the first of these
248 * functions, otherwise use makeJsonLexContextCstringLen().
251 makeJsonLexContext(text *json, bool need_escapes)
253 return makeJsonLexContextCstringLen(VARDATA(json),
254 VARSIZE(json) - VARHDRSZ,
259 makeJsonLexContextCstringLen(char *json, int len, bool need_escapes)
261 JsonLexContext *lex = palloc0(sizeof(JsonLexContext));
263 lex->input = lex->token_terminator = lex->line_start = json;
264 lex->line_number = 1;
265 lex->input_length = len;
267 lex->strval = makeStringInfo();
274 * Publicly visible entry point for the JSON parser.
276 * lex is a lexing context, set up for the json to be processed by calling
277 * makeJsonLexContext(). sem is a strucure of function pointers to semantic
278 * action routines to be called at appropriate spots during parsing, and a
279 * pointer to a state object to be passed to those routines.
282 pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
286 /* get the initial token */
291 /* parse by recursive descent */
294 case JSON_TOKEN_OBJECT_START:
295 parse_object(lex, sem);
297 case JSON_TOKEN_ARRAY_START:
298 parse_array(lex, sem);
301 parse_scalar(lex, sem); /* json can be a bare scalar */
304 lex_expect(JSON_PARSE_END, lex, JSON_TOKEN_END);
309 * Recursive Descent parse routines. There is one for each structural
310 * element in a json document:
311 * - scalar (string, number, true, false, null)
318 parse_scalar(JsonLexContext *lex, JsonSemAction *sem)
321 json_scalar_action sfunc = sem->scalar;
323 JsonTokenType tok = lex_peek(lex);
325 valaddr = sfunc == NULL ? NULL : &val;
327 /* a scalar must be a string, a number, true, false, or null */
330 case JSON_TOKEN_TRUE:
331 lex_accept(lex, JSON_TOKEN_TRUE, valaddr);
333 case JSON_TOKEN_FALSE:
334 lex_accept(lex, JSON_TOKEN_FALSE, valaddr);
336 case JSON_TOKEN_NULL:
337 lex_accept(lex, JSON_TOKEN_NULL, valaddr);
339 case JSON_TOKEN_NUMBER:
340 lex_accept(lex, JSON_TOKEN_NUMBER, valaddr);
342 case JSON_TOKEN_STRING:
343 lex_accept(lex, JSON_TOKEN_STRING, valaddr);
346 report_parse_error(JSON_PARSE_VALUE, lex);
350 (*sfunc) (sem->semstate, val, tok);
354 parse_object_field(JsonLexContext *lex, JsonSemAction *sem)
357 * An object field is "fieldname" : value where value can be a scalar,
358 * object or array. Note: in user-facing docs and error messages, we
359 * generally call a field name a "key".
362 char *fname = NULL; /* keep compiler quiet */
363 json_ofield_action ostart = sem->object_field_start;
364 json_ofield_action oend = sem->object_field_end;
366 char **fnameaddr = NULL;
369 if (ostart != NULL || oend != NULL)
372 if (!lex_accept(lex, JSON_TOKEN_STRING, fnameaddr))
373 report_parse_error(JSON_PARSE_STRING, lex);
375 lex_expect(JSON_PARSE_OBJECT_LABEL, lex, JSON_TOKEN_COLON);
378 isnull = tok == JSON_TOKEN_NULL;
381 (*ostart) (sem->semstate, fname, isnull);
385 case JSON_TOKEN_OBJECT_START:
386 parse_object(lex, sem);
388 case JSON_TOKEN_ARRAY_START:
389 parse_array(lex, sem);
392 parse_scalar(lex, sem);
396 (*oend) (sem->semstate, fname, isnull);
400 parse_object(JsonLexContext *lex, JsonSemAction *sem)
403 * an object is a possibly empty sequence of object fields, separated by
404 * commas and surrounded by curly braces.
406 json_struct_action ostart = sem->object_start;
407 json_struct_action oend = sem->object_end;
411 (*ostart) (sem->semstate);
414 * Data inside an object is at a higher nesting level than the object
415 * itself. Note that we increment this after we call the semantic routine
416 * for the object start and restore it before we call the routine for the
421 /* we know this will succeeed, just clearing the token */
422 lex_expect(JSON_PARSE_OBJECT_START, lex, JSON_TOKEN_OBJECT_START);
427 case JSON_TOKEN_STRING:
428 parse_object_field(lex, sem);
429 while (lex_accept(lex, JSON_TOKEN_COMMA, NULL))
430 parse_object_field(lex, sem);
432 case JSON_TOKEN_OBJECT_END:
435 /* case of an invalid initial token inside the object */
436 report_parse_error(JSON_PARSE_OBJECT_START, lex);
439 lex_expect(JSON_PARSE_OBJECT_NEXT, lex, JSON_TOKEN_OBJECT_END);
444 (*oend) (sem->semstate);
448 parse_array_element(JsonLexContext *lex, JsonSemAction *sem)
450 json_aelem_action astart = sem->array_element_start;
451 json_aelem_action aend = sem->array_element_end;
452 JsonTokenType tok = lex_peek(lex);
456 isnull = tok == JSON_TOKEN_NULL;
459 (*astart) (sem->semstate, isnull);
461 /* an array element is any object, array or scalar */
464 case JSON_TOKEN_OBJECT_START:
465 parse_object(lex, sem);
467 case JSON_TOKEN_ARRAY_START:
468 parse_array(lex, sem);
471 parse_scalar(lex, sem);
475 (*aend) (sem->semstate, isnull);
479 parse_array(JsonLexContext *lex, JsonSemAction *sem)
482 * an array is a possibly empty sequence of array elements, separated by
483 * commas and surrounded by square brackets.
485 json_struct_action astart = sem->array_start;
486 json_struct_action aend = sem->array_end;
489 (*astart) (sem->semstate);
492 * Data inside an array is at a higher nesting level than the array
493 * itself. Note that we increment this after we call the semantic routine
494 * for the array start and restore it before we call the routine for the
499 lex_expect(JSON_PARSE_ARRAY_START, lex, JSON_TOKEN_ARRAY_START);
500 if (lex_peek(lex) != JSON_TOKEN_ARRAY_END)
503 parse_array_element(lex, sem);
505 while (lex_accept(lex, JSON_TOKEN_COMMA, NULL))
506 parse_array_element(lex, sem);
509 lex_expect(JSON_PARSE_ARRAY_NEXT, lex, JSON_TOKEN_ARRAY_END);
514 (*aend) (sem->semstate);
518 * Lex one token from the input stream.
521 json_lex(JsonLexContext *lex)
526 /* Skip leading whitespace. */
527 s = lex->token_terminator;
528 len = s - lex->input;
529 while (len < lex->input_length &&
530 (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'))
537 lex->token_start = s;
539 /* Determine token type. */
540 if (len >= lex->input_length)
542 lex->token_start = NULL;
543 lex->prev_token_terminator = lex->token_terminator;
544 lex->token_terminator = s;
545 lex->token_type = JSON_TOKEN_END;
550 /* Single-character token, some kind of punctuation mark. */
552 lex->prev_token_terminator = lex->token_terminator;
553 lex->token_terminator = s + 1;
554 lex->token_type = JSON_TOKEN_OBJECT_START;
557 lex->prev_token_terminator = lex->token_terminator;
558 lex->token_terminator = s + 1;
559 lex->token_type = JSON_TOKEN_OBJECT_END;
562 lex->prev_token_terminator = lex->token_terminator;
563 lex->token_terminator = s + 1;
564 lex->token_type = JSON_TOKEN_ARRAY_START;
567 lex->prev_token_terminator = lex->token_terminator;
568 lex->token_terminator = s + 1;
569 lex->token_type = JSON_TOKEN_ARRAY_END;
572 lex->prev_token_terminator = lex->token_terminator;
573 lex->token_terminator = s + 1;
574 lex->token_type = JSON_TOKEN_COMMA;
577 lex->prev_token_terminator = lex->token_terminator;
578 lex->token_terminator = s + 1;
579 lex->token_type = JSON_TOKEN_COLON;
583 json_lex_string(lex);
584 lex->token_type = JSON_TOKEN_STRING;
587 /* Negative number. */
588 json_lex_number(lex, s + 1, NULL);
589 lex->token_type = JSON_TOKEN_NUMBER;
601 /* Positive number. */
602 json_lex_number(lex, s, NULL);
603 lex->token_type = JSON_TOKEN_NUMBER;
610 * We're not dealing with a string, number, legal
611 * punctuation mark, or end of string. The only legal
612 * tokens we might find here are true, false, and null,
613 * but for error reporting purposes we scan until we see a
614 * non-alphanumeric character. That way, we can report
615 * the whole word as an unexpected token, rather than just
616 * some unintuitive prefix thereof.
618 for (p = s; p - s < lex->input_length - len && JSON_ALPHANUMERIC_CHAR(*p); p++)
622 * We got some sort of unexpected punctuation or an
623 * otherwise unexpected character, so just complain about
624 * that one character.
628 lex->prev_token_terminator = lex->token_terminator;
629 lex->token_terminator = s + 1;
630 report_invalid_token(lex);
634 * We've got a real alphanumeric token here. If it
635 * happens to be true, false, or null, all is well. If
638 lex->prev_token_terminator = lex->token_terminator;
639 lex->token_terminator = p;
642 if (memcmp(s, "true", 4) == 0)
643 lex->token_type = JSON_TOKEN_TRUE;
644 else if (memcmp(s, "null", 4) == 0)
645 lex->token_type = JSON_TOKEN_NULL;
647 report_invalid_token(lex);
649 else if (p - s == 5 && memcmp(s, "false", 5) == 0)
650 lex->token_type = JSON_TOKEN_FALSE;
652 report_invalid_token(lex);
655 } /* end of switch */
659 * The next token in the input stream is known to be a string; lex it.
662 json_lex_string(JsonLexContext *lex)
666 int hi_surrogate = -1;
668 if (lex->strval != NULL)
669 resetStringInfo(lex->strval);
671 Assert(lex->input_length > 0);
672 s = lex->token_start;
673 len = lex->token_start - lex->input;
678 /* Premature end of the string. */
679 if (len >= lex->input_length)
681 lex->token_terminator = s;
682 report_invalid_token(lex);
686 else if ((unsigned char) *s < 32)
688 /* Per RFC4627, these characters MUST be escaped. */
689 /* Since *s isn't printable, exclude it from the context string */
690 lex->token_terminator = s;
692 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
693 errmsg("invalid input syntax for type json"),
694 errdetail("Character with value 0x%02x must be escaped.",
696 report_json_context(lex)));
700 /* OK, we have an escape character. */
703 if (len >= lex->input_length)
705 lex->token_terminator = s;
706 report_invalid_token(lex);
713 for (i = 1; i <= 4; i++)
717 if (len >= lex->input_length)
719 lex->token_terminator = s;
720 report_invalid_token(lex);
722 else if (*s >= '0' && *s <= '9')
723 ch = (ch * 16) + (*s - '0');
724 else if (*s >= 'a' && *s <= 'f')
725 ch = (ch * 16) + (*s - 'a') + 10;
726 else if (*s >= 'A' && *s <= 'F')
727 ch = (ch * 16) + (*s - 'A') + 10;
730 lex->token_terminator = s + pg_mblen(s);
732 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
733 errmsg("invalid input syntax for type json"),
734 errdetail("\"\\u\" must be followed by four hexadecimal digits."),
735 report_json_context(lex)));
738 if (lex->strval != NULL)
743 if (ch >= 0xd800 && ch <= 0xdbff)
745 if (hi_surrogate != -1)
747 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
748 errmsg("invalid input syntax for type json"),
749 errdetail("Unicode high surrogate must not follow a high surrogate."),
750 report_json_context(lex)));
751 hi_surrogate = (ch & 0x3ff) << 10;
754 else if (ch >= 0xdc00 && ch <= 0xdfff)
756 if (hi_surrogate == -1)
758 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
759 errmsg("invalid input syntax for type json"),
760 errdetail("Unicode low surrogate must follow a high surrogate."),
761 report_json_context(lex)));
762 ch = 0x10000 + hi_surrogate + (ch & 0x3ff);
766 if (hi_surrogate != -1)
768 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
769 errmsg("invalid input syntax for type json"),
770 errdetail("Unicode low surrogate must follow a high surrogate."),
771 report_json_context(lex)));
774 * For UTF8, replace the escape sequence by the actual
775 * utf8 character in lex->strval. Do this also for other
776 * encodings if the escape designates an ASCII character,
777 * otherwise raise an error. We don't ever unescape a
778 * \u0000, since that would result in an impermissible nul
784 appendStringInfoString(lex->strval, "\\u0000");
786 else if (GetDatabaseEncoding() == PG_UTF8)
788 unicode_to_utf8(ch, (unsigned char *) utf8str);
789 utf8len = pg_utf_mblen((unsigned char *) utf8str);
790 appendBinaryStringInfo(lex->strval, utf8str, utf8len);
792 else if (ch <= 0x007f)
795 * This is the only way to designate things like a
796 * form feed character in JSON, so it's useful in all
799 appendStringInfoChar(lex->strval, (char) ch);
804 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
805 errmsg("invalid input syntax for type json"),
806 errdetail("Unicode escape values cannot be used for code point values above 007F when the server encoding is not UTF8."),
807 report_json_context(lex)));
812 else if (lex->strval != NULL)
814 if (hi_surrogate != -1)
816 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
817 errmsg("invalid input syntax for type json"),
818 errdetail("Unicode low surrogate must follow a high surrogate."),
819 report_json_context(lex)));
826 appendStringInfoChar(lex->strval, *s);
829 appendStringInfoChar(lex->strval, '\b');
832 appendStringInfoChar(lex->strval, '\f');
835 appendStringInfoChar(lex->strval, '\n');
838 appendStringInfoChar(lex->strval, '\r');
841 appendStringInfoChar(lex->strval, '\t');
844 /* Not a valid string escape, so error out. */
845 lex->token_terminator = s + pg_mblen(s);
847 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
848 errmsg("invalid input syntax for type json"),
849 errdetail("Escape sequence \"\\%s\" is invalid.",
851 report_json_context(lex)));
854 else if (strchr("\"\\/bfnrt", *s) == NULL)
857 * Simpler processing if we're not bothered about de-escaping
859 * It's very tempting to remove the strchr() call here and
860 * replace it with a switch statement, but testing so far has
861 * shown it's not a performance win.
863 lex->token_terminator = s + pg_mblen(s);
865 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
866 errmsg("invalid input syntax for type json"),
867 errdetail("Escape sequence \"\\%s\" is invalid.",
869 report_json_context(lex)));
873 else if (lex->strval != NULL)
875 if (hi_surrogate != -1)
877 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
878 errmsg("invalid input syntax for type json"),
879 errdetail("Unicode low surrogate must follow a high surrogate."),
880 report_json_context(lex)));
882 appendStringInfoChar(lex->strval, *s);
887 if (hi_surrogate != -1)
889 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
890 errmsg("invalid input syntax for type json"),
891 errdetail("Unicode low surrogate must follow a high surrogate."),
892 report_json_context(lex)));
894 /* Hooray, we found the end of the string! */
895 lex->prev_token_terminator = lex->token_terminator;
896 lex->token_terminator = s + 1;
899 /*-------------------------------------------------------------------------
900 * The next token in the input stream is known to be a number; lex it.
902 * In JSON, a number consists of four parts:
904 * (1) An optional minus sign ('-').
906 * (2) Either a single '0', or a string of one or more digits that does not
909 * (3) An optional decimal part, consisting of a period ('.') followed by
910 * one or more digits. (Note: While this part can be omitted
911 * completely, it's not OK to have only the decimal point without
912 * any digits afterwards.)
914 * (4) An optional exponent part, consisting of 'e' or 'E', optionally
915 * followed by '+' or '-', followed by one or more digits. (Note:
916 * As with the decimal part, if 'e' or 'E' is present, it must be
917 * followed by at least one digit.)
919 * The 's' argument to this function points to the ostensible beginning
920 * of part 2 - i.e. the character after any optional minus sign, and the
921 * first character of the string if there is none.
923 *-------------------------------------------------------------------------
926 json_lex_number(JsonLexContext *lex, char *s, bool *num_err)
932 len = s - lex->input;
933 /* Part (1): leading sign indicator. */
934 /* Caller already did this for us; so do nothing. */
936 /* Part (2): parse main digit string. */
942 else if (*s >= '1' && *s <= '9')
948 } while (len < lex->input_length && *s >= '0' && *s <= '9');
953 /* Part (3): parse optional decimal portion. */
954 if (len < lex->input_length && *s == '.')
958 if (len == lex->input_length || *s < '0' || *s > '9')
966 } while (len < lex->input_length && *s >= '0' && *s <= '9');
970 /* Part (4): parse optional exponent. */
971 if (len < lex->input_length && (*s == 'e' || *s == 'E'))
975 if (len < lex->input_length && (*s == '+' || *s == '-'))
980 if (len == lex->input_length || *s < '0' || *s > '9')
988 } while (len < lex->input_length && *s >= '0' && *s <= '9');
993 * Check for trailing garbage. As in json_lex(), any alphanumeric stuff
994 * here should be considered part of the token for error-reporting
997 for (p = s; len < lex->input_length && JSON_ALPHANUMERIC_CHAR(*p); p++, len++)
1000 if (num_err != NULL)
1002 /* let the caller handle the error */
1007 lex->prev_token_terminator = lex->token_terminator;
1008 lex->token_terminator = p;
1010 report_invalid_token(lex);
1015 * Report a parse error.
1017 * lex->token_start and lex->token_terminator must identify the current token.
1020 report_parse_error(JsonParseContext ctx, JsonLexContext *lex)
1025 /* Handle case where the input ended prematurely. */
1026 if (lex->token_start == NULL || lex->token_type == JSON_TOKEN_END)
1028 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1029 errmsg("invalid input syntax for type json"),
1030 errdetail("The input string ended unexpectedly."),
1031 report_json_context(lex)));
1033 /* Separate out the current token. */
1034 toklen = lex->token_terminator - lex->token_start;
1035 token = palloc(toklen + 1);
1036 memcpy(token, lex->token_start, toklen);
1037 token[toklen] = '\0';
1039 /* Complain, with the appropriate detail message. */
1040 if (ctx == JSON_PARSE_END)
1042 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1043 errmsg("invalid input syntax for type json"),
1044 errdetail("Expected end of input, but found \"%s\".",
1046 report_json_context(lex)));
1051 case JSON_PARSE_VALUE:
1053 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1054 errmsg("invalid input syntax for type json"),
1055 errdetail("Expected JSON value, but found \"%s\".",
1057 report_json_context(lex)));
1059 case JSON_PARSE_STRING:
1061 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1062 errmsg("invalid input syntax for type json"),
1063 errdetail("Expected string, but found \"%s\".",
1065 report_json_context(lex)));
1067 case JSON_PARSE_ARRAY_START:
1069 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1070 errmsg("invalid input syntax for type json"),
1071 errdetail("Expected array element or \"]\", but found \"%s\".",
1073 report_json_context(lex)));
1075 case JSON_PARSE_ARRAY_NEXT:
1077 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1078 errmsg("invalid input syntax for type json"),
1079 errdetail("Expected \",\" or \"]\", but found \"%s\".",
1081 report_json_context(lex)));
1083 case JSON_PARSE_OBJECT_START:
1085 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1086 errmsg("invalid input syntax for type json"),
1087 errdetail("Expected string or \"}\", but found \"%s\".",
1089 report_json_context(lex)));
1091 case JSON_PARSE_OBJECT_LABEL:
1093 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1094 errmsg("invalid input syntax for type json"),
1095 errdetail("Expected \":\", but found \"%s\".",
1097 report_json_context(lex)));
1099 case JSON_PARSE_OBJECT_NEXT:
1101 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1102 errmsg("invalid input syntax for type json"),
1103 errdetail("Expected \",\" or \"}\", but found \"%s\".",
1105 report_json_context(lex)));
1107 case JSON_PARSE_OBJECT_COMMA:
1109 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1110 errmsg("invalid input syntax for type json"),
1111 errdetail("Expected string, but found \"%s\".",
1113 report_json_context(lex)));
1116 elog(ERROR, "unexpected json parse state: %d", ctx);
1122 * Report an invalid input token.
1124 * lex->token_start and lex->token_terminator must identify the token.
1127 report_invalid_token(JsonLexContext *lex)
1132 /* Separate out the offending token. */
1133 toklen = lex->token_terminator - lex->token_start;
1134 token = palloc(toklen + 1);
1135 memcpy(token, lex->token_start, toklen);
1136 token[toklen] = '\0';
1139 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1140 errmsg("invalid input syntax for type json"),
1141 errdetail("Token \"%s\" is invalid.", token),
1142 report_json_context(lex)));
1146 * Report a CONTEXT line for bogus JSON input.
1148 * lex->token_terminator must be set to identify the spot where we detected
1149 * the error. Note that lex->token_start might be NULL, in case we recognized
1152 * The return value isn't meaningful, but we make it non-void so that this
1153 * can be invoked inside ereport().
1156 report_json_context(JsonLexContext *lex)
1158 const char *context_start;
1159 const char *context_end;
1160 const char *line_start;
1167 /* Choose boundaries for the part of the input we will display */
1168 context_start = lex->input;
1169 context_end = lex->token_terminator;
1170 line_start = context_start;
1174 /* Always advance over newlines */
1175 if (context_start < context_end && *context_start == '\n')
1178 line_start = context_start;
1182 /* Otherwise, done as soon as we are close enough to context_end */
1183 if (context_end - context_start < 50)
1185 /* Advance to next multibyte character */
1186 if (IS_HIGHBIT_SET(*context_start))
1187 context_start += pg_mblen(context_start);
1193 * We add "..." to indicate that the excerpt doesn't start at the
1194 * beginning of the line ... but if we're within 3 characters of the
1195 * beginning of the line, we might as well just show the whole line.
1197 if (context_start - line_start <= 3)
1198 context_start = line_start;
1200 /* Get a null-terminated copy of the data to present */
1201 ctxtlen = context_end - context_start;
1202 ctxt = palloc(ctxtlen + 1);
1203 memcpy(ctxt, context_start, ctxtlen);
1204 ctxt[ctxtlen] = '\0';
1207 * Show the context, prefixing "..." if not starting at start of line, and
1208 * suffixing "..." if not ending at end of line.
1210 prefix = (context_start > line_start) ? "..." : "";
1211 suffix = (lex->token_type != JSON_TOKEN_END && context_end - lex->input < lex->input_length && *context_end != '\n' && *context_end != '\r') ? "..." : "";
1213 return errcontext("JSON data, line %d: %s%s%s",
1214 line_number, prefix, ctxt, suffix);
1218 * Extract a single, possibly multi-byte char from the input string.
1221 extract_mb_char(char *s)
1227 res = palloc(len + 1);
1228 memcpy(res, s, len);
1235 * Determine how we want to print values of a given type in datum_to_json.
1237 * Given the datatype OID, return its JsonTypeCategory, as well as the type's
1238 * output function OID. If the returned category is JSONTYPE_CAST, we
1239 * return the OID of the type->JSON cast function instead.
1242 json_categorize_type(Oid typoid,
1243 JsonTypeCategory *tcategory,
1248 /* Look through any domain */
1249 typoid = getBaseType(typoid);
1251 /* We'll usually need to return the type output function */
1252 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
1254 /* Check for known types */
1258 *tcategory = JSONTYPE_BOOL;
1267 *tcategory = JSONTYPE_NUMERIC;
1271 *tcategory = JSONTYPE_TIMESTAMP;
1274 case TIMESTAMPTZOID:
1275 *tcategory = JSONTYPE_TIMESTAMPTZ;
1280 *tcategory = JSONTYPE_JSON;
1284 /* Check for arrays and composites */
1285 if (OidIsValid(get_element_type(typoid)))
1286 *tcategory = JSONTYPE_ARRAY;
1287 else if (type_is_rowtype(typoid))
1288 *tcategory = JSONTYPE_COMPOSITE;
1291 /* It's probably the general case ... */
1292 *tcategory = JSONTYPE_OTHER;
1293 /* but let's look for a cast to json, if it's not built-in */
1294 if (typoid >= FirstNormalObjectId)
1298 tuple = SearchSysCache2(CASTSOURCETARGET,
1299 ObjectIdGetDatum(typoid),
1300 ObjectIdGetDatum(JSONOID));
1301 if (HeapTupleIsValid(tuple))
1303 Form_pg_cast castForm = (Form_pg_cast) GETSTRUCT(tuple);
1305 if (castForm->castmethod == COERCION_METHOD_FUNCTION)
1307 *tcategory = JSONTYPE_CAST;
1308 *outfuncoid = castForm->castfunc;
1311 ReleaseSysCache(tuple);
1320 * Turn a Datum into JSON text, appending the string to "result".
1322 * tcategory and outfuncoid are from a previous call to json_categorize_type,
1323 * except that if is_null is true then they can be invalid.
1325 * If key_scalar is true, the value is being printed as a key, so insist
1326 * it's of an acceptable type, and force it to be quoted.
1329 datum_to_json(Datum val, bool is_null, StringInfo result,
1330 JsonTypeCategory tcategory, Oid outfuncoid,
1336 JsonLexContext dummy_lex;
1340 appendStringInfoString(result, "null");
1345 (tcategory == JSONTYPE_ARRAY ||
1346 tcategory == JSONTYPE_COMPOSITE ||
1347 tcategory == JSONTYPE_JSON ||
1348 tcategory == JSONTYPE_CAST))
1350 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1351 errmsg("key value must be scalar, not array, composite, or json")));
1355 case JSONTYPE_ARRAY:
1356 array_to_json_internal(val, result, false);
1358 case JSONTYPE_COMPOSITE:
1359 composite_to_json(val, result, false);
1362 outputstr = DatumGetBool(val) ? "true" : "false";
1364 escape_json(result, outputstr);
1366 appendStringInfoString(result, outputstr);
1368 case JSONTYPE_NUMERIC:
1369 outputstr = OidOutputFunctionCall(outfuncoid, val);
1372 /* always quote keys */
1373 escape_json(result, outputstr);
1378 * Don't call escape_json for a non-key if it's a valid JSON
1381 dummy_lex.input = *outputstr == '-' ? outputstr + 1 : outputstr;
1382 dummy_lex.input_length = strlen(dummy_lex.input);
1383 json_lex_number(&dummy_lex, dummy_lex.input, &numeric_error);
1385 appendStringInfoString(result, outputstr);
1387 escape_json(result, outputstr);
1391 case JSONTYPE_TIMESTAMP:
1393 Timestamp timestamp;
1396 char buf[MAXDATELEN + 1];
1398 timestamp = DatumGetTimestamp(val);
1400 /* XSD doesn't support infinite values */
1401 if (TIMESTAMP_NOT_FINITE(timestamp))
1403 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1404 errmsg("timestamp out of range"),
1405 errdetail("JSON does not support infinite timestamp values.")));
1406 else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
1407 EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
1410 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1411 errmsg("timestamp out of range")));
1413 appendStringInfo(result,"\"%s\"",buf);
1416 case JSONTYPE_TIMESTAMPTZ:
1418 TimestampTz timestamp;
1422 const char *tzn = NULL;
1423 char buf[MAXDATELEN + 1];
1425 timestamp = DatumGetTimestamp(val);
1427 /* XSD doesn't support infinite values */
1428 if (TIMESTAMP_NOT_FINITE(timestamp))
1430 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1431 errmsg("timestamp out of range"),
1432 errdetail("JSON does not support infinite timestamp values.")));
1433 else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
1434 EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
1437 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1438 errmsg("timestamp out of range")));
1440 appendStringInfo(result,"\"%s\"",buf);
1444 /* JSON and JSONB output will already be escaped */
1445 outputstr = OidOutputFunctionCall(outfuncoid, val);
1446 appendStringInfoString(result, outputstr);
1450 /* outfuncoid refers to a cast function, not an output function */
1451 jsontext = DatumGetTextP(OidFunctionCall1(outfuncoid, val));
1452 outputstr = text_to_cstring(jsontext);
1453 appendStringInfoString(result, outputstr);
1458 outputstr = OidOutputFunctionCall(outfuncoid, val);
1459 if (key_scalar && *outputstr == '\0')
1461 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1462 errmsg("key value must not be empty")));
1463 escape_json(result, outputstr);
1470 * Process a single dimension of an array.
1471 * If it's the innermost dimension, output the values, otherwise call
1472 * ourselves recursively to process the next dimension.
1475 array_dim_to_json(StringInfo result, int dim, int ndims, int *dims, Datum *vals,
1476 bool *nulls, int *valcount, JsonTypeCategory tcategory,
1477 Oid outfuncoid, bool use_line_feeds)
1482 Assert(dim < ndims);
1484 sep = use_line_feeds ? ",\n " : ",";
1486 appendStringInfoChar(result, '[');
1488 for (i = 1; i <= dims[dim]; i++)
1491 appendStringInfoString(result, sep);
1493 if (dim + 1 == ndims)
1495 datum_to_json(vals[*valcount], nulls[*valcount], result, tcategory,
1502 * Do we want line feeds on inner dimensions of arrays? For now
1505 array_dim_to_json(result, dim + 1, ndims, dims, vals, nulls,
1506 valcount, tcategory, outfuncoid, false);
1510 appendStringInfoChar(result, ']');
1514 * Turn an array into JSON.
1517 array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
1519 ArrayType *v = DatumGetArrayTypeP(array);
1520 Oid element_type = ARR_ELEMTYPE(v);
1530 JsonTypeCategory tcategory;
1535 nitems = ArrayGetNItems(ndim, dim);
1539 appendStringInfoString(result, "[]");
1543 get_typlenbyvalalign(element_type,
1544 &typlen, &typbyval, &typalign);
1546 json_categorize_type(element_type,
1547 &tcategory, &outfuncoid);
1549 deconstruct_array(v, element_type, typlen, typbyval,
1550 typalign, &elements, &nulls,
1553 array_dim_to_json(result, 0, ndim, dim, elements, nulls, &count, tcategory,
1554 outfuncoid, use_line_feeds);
1561 * Turn a composite / record into JSON.
1564 composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
1570 HeapTupleData tmptup,
1573 bool needsep = false;
1576 sep = use_line_feeds ? ",\n " : ",";
1578 td = DatumGetHeapTupleHeader(composite);
1580 /* Extract rowtype info and find a tupdesc */
1581 tupType = HeapTupleHeaderGetTypeId(td);
1582 tupTypmod = HeapTupleHeaderGetTypMod(td);
1583 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
1585 /* Build a temporary HeapTuple control structure */
1586 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
1590 appendStringInfoChar(result, '{');
1592 for (i = 0; i < tupdesc->natts; i++)
1597 JsonTypeCategory tcategory;
1600 if (tupdesc->attrs[i]->attisdropped)
1604 appendStringInfoString(result, sep);
1607 attname = NameStr(tupdesc->attrs[i]->attname);
1608 escape_json(result, attname);
1609 appendStringInfoChar(result, ':');
1611 val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
1615 tcategory = JSONTYPE_NULL;
1616 outfuncoid = InvalidOid;
1619 json_categorize_type(tupdesc->attrs[i]->atttypid,
1620 &tcategory, &outfuncoid);
1622 datum_to_json(val, isnull, result, tcategory, outfuncoid, false);
1625 appendStringInfoChar(result, '}');
1626 ReleaseTupleDesc(tupdesc);
1630 * Append JSON text for "val" to "result".
1632 * This is just a thin wrapper around datum_to_json. If the same type will be
1633 * printed many times, avoid using this; better to do the json_categorize_type
1634 * lookups only once.
1637 add_json(Datum val, bool is_null, StringInfo result,
1638 Oid val_type, bool key_scalar)
1640 JsonTypeCategory tcategory;
1643 if (val_type == InvalidOid)
1645 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1646 errmsg("could not determine input data type")));
1650 tcategory = JSONTYPE_NULL;
1651 outfuncoid = InvalidOid;
1654 json_categorize_type(val_type,
1655 &tcategory, &outfuncoid);
1657 datum_to_json(val, is_null, result, tcategory, outfuncoid, key_scalar);
1661 * SQL function array_to_json(row)
1664 array_to_json(PG_FUNCTION_ARGS)
1666 Datum array = PG_GETARG_DATUM(0);
1669 result = makeStringInfo();
1671 array_to_json_internal(array, result, false);
1673 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1677 * SQL function array_to_json(row, prettybool)
1680 array_to_json_pretty(PG_FUNCTION_ARGS)
1682 Datum array = PG_GETARG_DATUM(0);
1683 bool use_line_feeds = PG_GETARG_BOOL(1);
1686 result = makeStringInfo();
1688 array_to_json_internal(array, result, use_line_feeds);
1690 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1694 * SQL function row_to_json(row)
1697 row_to_json(PG_FUNCTION_ARGS)
1699 Datum array = PG_GETARG_DATUM(0);
1702 result = makeStringInfo();
1704 composite_to_json(array, result, false);
1706 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1710 * SQL function row_to_json(row, prettybool)
1713 row_to_json_pretty(PG_FUNCTION_ARGS)
1715 Datum array = PG_GETARG_DATUM(0);
1716 bool use_line_feeds = PG_GETARG_BOOL(1);
1719 result = makeStringInfo();
1721 composite_to_json(array, result, use_line_feeds);
1723 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1727 * SQL function to_json(anyvalue)
1730 to_json(PG_FUNCTION_ARGS)
1732 Datum val = PG_GETARG_DATUM(0);
1733 Oid val_type = get_fn_expr_argtype(fcinfo->flinfo, 0);
1735 JsonTypeCategory tcategory;
1738 if (val_type == InvalidOid)
1740 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1741 errmsg("could not determine input data type")));
1743 json_categorize_type(val_type,
1744 &tcategory, &outfuncoid);
1746 result = makeStringInfo();
1748 datum_to_json(val, false, result, tcategory, outfuncoid, false);
1750 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1754 * json_agg transition function
1757 json_agg_transfn(PG_FUNCTION_ARGS)
1759 Oid val_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
1760 MemoryContext aggcontext,
1764 JsonTypeCategory tcategory;
1767 if (val_type == InvalidOid)
1769 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1770 errmsg("could not determine input data type")));
1772 if (!AggCheckCallContext(fcinfo, &aggcontext))
1774 /* cannot be called directly because of internal-type argument */
1775 elog(ERROR, "json_agg_transfn called in non-aggregate context");
1778 if (PG_ARGISNULL(0))
1781 * Make this StringInfo in a context where it will persist for the
1782 * duration of the aggregate call. MemoryContextSwitchTo is only
1783 * needed the first time, as the StringInfo routines make sure they
1784 * use the right context to enlarge the object if necessary.
1786 oldcontext = MemoryContextSwitchTo(aggcontext);
1787 state = makeStringInfo();
1788 MemoryContextSwitchTo(oldcontext);
1790 appendStringInfoChar(state, '[');
1794 state = (StringInfo) PG_GETARG_POINTER(0);
1795 appendStringInfoString(state, ", ");
1798 /* fast path for NULLs */
1799 if (PG_ARGISNULL(1))
1801 datum_to_json((Datum) 0, true, state, JSONTYPE_NULL, InvalidOid, false);
1802 PG_RETURN_POINTER(state);
1805 val = PG_GETARG_DATUM(1);
1807 /* XXX we do this every time?? */
1808 json_categorize_type(val_type,
1809 &tcategory, &outfuncoid);
1811 /* add some whitespace if structured type and not first item */
1812 if (!PG_ARGISNULL(0) &&
1813 (tcategory == JSONTYPE_ARRAY || tcategory == JSONTYPE_COMPOSITE))
1815 appendStringInfoString(state, "\n ");
1818 datum_to_json(val, false, state, tcategory, outfuncoid, false);
1821 * The transition type for array_agg() is declared to be "internal", which
1822 * is a pass-by-value type the same size as a pointer. So we can safely
1823 * pass the ArrayBuildState pointer through nodeAgg.c's machinations.
1825 PG_RETURN_POINTER(state);
1829 * json_agg final function
1832 json_agg_finalfn(PG_FUNCTION_ARGS)
1836 /* cannot be called directly because of internal-type argument */
1837 Assert(AggCheckCallContext(fcinfo, NULL));
1839 state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
1844 appendStringInfoChar(state, ']');
1846 PG_RETURN_TEXT_P(cstring_to_text_with_len(state->data, state->len));
1850 * json_object_agg transition function.
1852 * aggregate two input columns as a single json value.
1855 json_object_agg_transfn(PG_FUNCTION_ARGS)
1858 MemoryContext aggcontext,
1863 if (!AggCheckCallContext(fcinfo, &aggcontext))
1865 /* cannot be called directly because of internal-type argument */
1866 elog(ERROR, "json_agg_transfn called in non-aggregate context");
1869 if (PG_ARGISNULL(0))
1872 * Make this StringInfo in a context where it will persist for the
1873 * duration off the aggregate call. It's only needed for this initial
1874 * piece, as the StringInfo routines make sure they use the right
1875 * context to enlarge the object if necessary.
1877 oldcontext = MemoryContextSwitchTo(aggcontext);
1878 state = makeStringInfo();
1879 MemoryContextSwitchTo(oldcontext);
1881 appendStringInfoString(state, "{ ");
1885 state = (StringInfo) PG_GETARG_POINTER(0);
1886 appendStringInfoString(state, ", ");
1889 if (PG_ARGISNULL(1))
1891 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1892 errmsg("field name must not be null")));
1894 val_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
1897 * turn a constant (more or less literal) value that's of unknown type
1898 * into text. Unknowns come in as a cstring pointer.
1900 if (val_type == UNKNOWNOID && get_fn_expr_arg_stable(fcinfo->flinfo, 1))
1903 arg = CStringGetTextDatum(PG_GETARG_POINTER(1));
1907 arg = PG_GETARG_DATUM(1);
1910 if (val_type == InvalidOid || val_type == UNKNOWNOID)
1912 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1913 errmsg("could not determine data type for argument %d", 1)));
1915 add_json(arg, false, state, val_type, true);
1917 appendStringInfoString(state, " : ");
1919 val_type = get_fn_expr_argtype(fcinfo->flinfo, 2);
1920 /* see comments above */
1921 if (val_type == UNKNOWNOID && get_fn_expr_arg_stable(fcinfo->flinfo, 2))
1924 if (PG_ARGISNULL(2))
1927 arg = CStringGetTextDatum(PG_GETARG_POINTER(2));
1931 arg = PG_GETARG_DATUM(2);
1934 if (val_type == InvalidOid || val_type == UNKNOWNOID)
1936 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1937 errmsg("could not determine data type for argument %d", 2)));
1939 add_json(arg, PG_ARGISNULL(2), state, val_type, false);
1941 PG_RETURN_POINTER(state);
1945 * json_object_agg final function.
1949 json_object_agg_finalfn(PG_FUNCTION_ARGS)
1953 /* cannot be called directly because of internal-type argument */
1954 Assert(AggCheckCallContext(fcinfo, NULL));
1956 state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
1959 PG_RETURN_TEXT_P(cstring_to_text("{}"));
1961 appendStringInfoString(state, " }");
1963 PG_RETURN_TEXT_P(cstring_to_text_with_len(state->data, state->len));
1967 * SQL function json_build_object(variadic "any")
1970 json_build_object(PG_FUNCTION_ARGS)
1972 int nargs = PG_NARGS();
1981 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1982 errmsg("argument list must have even number of elements"),
1983 errhint("The arguments of json_build_object() must consist of alternating keys and values.")));
1985 result = makeStringInfo();
1987 appendStringInfoChar(result, '{');
1989 for (i = 0; i < nargs; i += 2)
1993 if (PG_ARGISNULL(i))
1995 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1996 errmsg("argument %d cannot be null", i + 1),
1997 errhint("Object keys should be text.")));
1998 val_type = get_fn_expr_argtype(fcinfo->flinfo, i);
2001 * turn a constant (more or less literal) value that's of unknown type
2002 * into text. Unknowns come in as a cstring pointer.
2004 if (val_type == UNKNOWNOID && get_fn_expr_arg_stable(fcinfo->flinfo, i))
2007 arg = CStringGetTextDatum(PG_GETARG_POINTER(i));
2011 arg = PG_GETARG_DATUM(i);
2013 if (val_type == InvalidOid || val_type == UNKNOWNOID)
2015 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2016 errmsg("could not determine data type for argument %d",
2018 appendStringInfoString(result, sep);
2020 add_json(arg, false, result, val_type, true);
2022 appendStringInfoString(result, " : ");
2026 val_type = get_fn_expr_argtype(fcinfo->flinfo, i + 1);
2027 /* see comments above */
2028 if (val_type == UNKNOWNOID && get_fn_expr_arg_stable(fcinfo->flinfo, i + 1))
2031 if (PG_ARGISNULL(i + 1))
2034 arg = CStringGetTextDatum(PG_GETARG_POINTER(i + 1));
2038 arg = PG_GETARG_DATUM(i + 1);
2040 if (val_type == InvalidOid || val_type == UNKNOWNOID)
2042 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2043 errmsg("could not determine data type for argument %d",
2045 add_json(arg, PG_ARGISNULL(i + 1), result, val_type, false);
2048 appendStringInfoChar(result, '}');
2050 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
2054 * degenerate case of json_build_object where it gets 0 arguments.
2057 json_build_object_noargs(PG_FUNCTION_ARGS)
2059 PG_RETURN_TEXT_P(cstring_to_text_with_len("{}", 2));
2063 * SQL function json_build_array(variadic "any")
2066 json_build_array(PG_FUNCTION_ARGS)
2068 int nargs = PG_NARGS();
2076 result = makeStringInfo();
2078 appendStringInfoChar(result, '[');
2080 for (i = 0; i < nargs; i++)
2082 val_type = get_fn_expr_argtype(fcinfo->flinfo, i);
2083 arg = PG_GETARG_DATUM(i + 1);
2084 /* see comments in json_build_object above */
2085 if (val_type == UNKNOWNOID && get_fn_expr_arg_stable(fcinfo->flinfo, i))
2088 if (PG_ARGISNULL(i))
2091 arg = CStringGetTextDatum(PG_GETARG_POINTER(i));
2095 arg = PG_GETARG_DATUM(i);
2097 if (val_type == InvalidOid || val_type == UNKNOWNOID)
2099 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2100 errmsg("could not determine data type for argument %d",
2102 appendStringInfoString(result, sep);
2104 add_json(arg, PG_ARGISNULL(i), result, val_type, false);
2106 appendStringInfoChar(result, ']');
2108 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
2113 * degenerate case of json_build_array where it gets 0 arguments.
2116 json_build_array_noargs(PG_FUNCTION_ARGS)
2118 PG_RETURN_TEXT_P(cstring_to_text_with_len("[]", 2));
2122 * SQL function json_object(text[])
2124 * take a one or two dimensional array of text as key/value pairs
2125 * for a json object.
2128 json_object(PG_FUNCTION_ARGS)
2130 ArrayType *in_array = PG_GETARG_ARRAYTYPE_P(0);
2131 int ndims = ARR_NDIM(in_array);
2132 StringInfoData result;
2144 PG_RETURN_DATUM(CStringGetTextDatum("{}"));
2148 if ((ARR_DIMS(in_array)[0]) % 2)
2150 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2151 errmsg("array must have even number of elements")));
2155 if ((ARR_DIMS(in_array)[1]) != 2)
2157 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2158 errmsg("array must have two columns")));
2163 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2164 errmsg("wrong number of array subscripts")));
2167 deconstruct_array(in_array,
2168 TEXTOID, -1, false, 'i',
2169 &in_datums, &in_nulls, &in_count);
2171 count = in_count / 2;
2173 initStringInfo(&result);
2175 appendStringInfoChar(&result, '{');
2177 for (i = 0; i < count; ++i)
2179 if (in_nulls[i * 2])
2181 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2182 errmsg("null value not allowed for object key")));
2184 v = TextDatumGetCString(in_datums[i * 2]);
2186 appendStringInfoString(&result, ", ");
2187 escape_json(&result, v);
2188 appendStringInfoString(&result, " : ");
2190 if (in_nulls[i * 2 + 1])
2191 appendStringInfoString(&result, "null");
2194 v = TextDatumGetCString(in_datums[i * 2 + 1]);
2195 escape_json(&result, v);
2200 appendStringInfoChar(&result, '}');
2205 rval = cstring_to_text_with_len(result.data, result.len);
2208 PG_RETURN_TEXT_P(rval);
2213 * SQL function json_object(text[], text[])
2215 * take separate key and value arrays of text to construct a json object
2219 json_object_two_arg(PG_FUNCTION_ARGS)
2221 ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(0);
2222 ArrayType *val_array = PG_GETARG_ARRAYTYPE_P(1);
2223 int nkdims = ARR_NDIM(key_array);
2224 int nvdims = ARR_NDIM(val_array);
2225 StringInfoData result;
2236 if (nkdims > 1 || nkdims != nvdims)
2238 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2239 errmsg("wrong number of array subscripts")));
2242 PG_RETURN_DATUM(CStringGetTextDatum("{}"));
2244 deconstruct_array(key_array,
2245 TEXTOID, -1, false, 'i',
2246 &key_datums, &key_nulls, &key_count);
2248 deconstruct_array(val_array,
2249 TEXTOID, -1, false, 'i',
2250 &val_datums, &val_nulls, &val_count);
2252 if (key_count != val_count)
2254 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2255 errmsg("mismatched array dimensions")));
2257 initStringInfo(&result);
2259 appendStringInfoChar(&result, '{');
2261 for (i = 0; i < key_count; ++i)
2265 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2266 errmsg("null value not allowed for object key")));
2268 v = TextDatumGetCString(key_datums[i]);
2270 appendStringInfoString(&result, ", ");
2271 escape_json(&result, v);
2272 appendStringInfoString(&result, " : ");
2275 appendStringInfoString(&result, "null");
2278 v = TextDatumGetCString(val_datums[i]);
2279 escape_json(&result, v);
2284 appendStringInfoChar(&result, '}');
2291 rval = cstring_to_text_with_len(result.data, result.len);
2294 PG_RETURN_TEXT_P(rval);
2299 * Produce a JSON string literal, properly escaping characters in the text.
2302 escape_json(StringInfo buf, const char *str)
2306 appendStringInfoCharMacro(buf, '\"');
2307 for (p = str; *p; p++)
2312 appendStringInfoString(buf, "\\b");
2315 appendStringInfoString(buf, "\\f");
2318 appendStringInfoString(buf, "\\n");
2321 appendStringInfoString(buf, "\\r");
2324 appendStringInfoString(buf, "\\t");
2327 appendStringInfoString(buf, "\\\"");
2331 * Unicode escapes are passed through as is. There is no
2332 * requirement that they denote a valid character in the
2333 * server encoding - indeed that is a big part of their
2336 * All we require is that they consist of \uXXXX where
2337 * the Xs are hexadecimal digits. It is the responsibility
2338 * of the caller of, say, to_json() to make sure that the
2339 * unicode escape is valid.
2341 * In the case of a jsonb string value being escaped, the
2342 * only unicode escape that should be present is \u0000,
2343 * all the other unicode escapes will have been resolved.
2346 isxdigit((unsigned char) p[2]) &&
2347 isxdigit((unsigned char) p[3]) &&
2348 isxdigit((unsigned char) p[4]) &&
2349 isxdigit((unsigned char) p[5]))
2350 appendStringInfoCharMacro(buf, *p);
2352 appendStringInfoString(buf, "\\\\");
2355 if ((unsigned char) *p < ' ')
2356 appendStringInfo(buf, "\\u%04x", (int) *p);
2358 appendStringInfoCharMacro(buf, *p);
2362 appendStringInfoCharMacro(buf, '\"');
2366 * SQL function json_typeof(json) -> text
2368 * Returns the type of the outermost JSON value as TEXT. Possible types are
2369 * "object", "array", "string", "number", "boolean", and "null".
2371 * Performs a single call to json_lex() to get the first token of the supplied
2372 * value. This initial token uniquely determines the value's type. As our
2373 * input must already have been validated by json_in() or json_recv(), the
2374 * initial token should never be JSON_TOKEN_OBJECT_END, JSON_TOKEN_ARRAY_END,
2375 * JSON_TOKEN_COLON, JSON_TOKEN_COMMA, or JSON_TOKEN_END.
2378 json_typeof(PG_FUNCTION_ARGS)
2382 JsonLexContext *lex;
2386 json = PG_GETARG_TEXT_P(0);
2387 lex = makeJsonLexContext(json, false);
2389 /* Lex exactly one token from the input and check its type. */
2391 tok = lex_peek(lex);
2394 case JSON_TOKEN_OBJECT_START:
2397 case JSON_TOKEN_ARRAY_START:
2400 case JSON_TOKEN_STRING:
2403 case JSON_TOKEN_NUMBER:
2406 case JSON_TOKEN_TRUE:
2407 case JSON_TOKEN_FALSE:
2410 case JSON_TOKEN_NULL:
2414 elog(ERROR, "unexpected json token: %d", tok);
2417 PG_RETURN_TEXT_P(cstring_to_text(type));