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/date.h"
29 #include "utils/datetime.h"
30 #include "utils/lsyscache.h"
31 #include "utils/json.h"
32 #include "utils/jsonapi.h"
33 #include "utils/typcache.h"
34 #include "utils/syscache.h"
37 * The context of the parser is maintained by the recursive descent
38 * mechanism, but is passed explicitly to the error reporting routine
39 * for better diagnostics.
41 typedef enum /* contexts of JSON parser */
43 JSON_PARSE_VALUE, /* expecting a value */
44 JSON_PARSE_STRING, /* expecting a string (for a field name) */
45 JSON_PARSE_ARRAY_START, /* saw '[', expecting value or ']' */
46 JSON_PARSE_ARRAY_NEXT, /* saw array element, expecting ',' or ']' */
47 JSON_PARSE_OBJECT_START, /* saw '{', expecting label or '}' */
48 JSON_PARSE_OBJECT_LABEL, /* saw object label, expecting ':' */
49 JSON_PARSE_OBJECT_NEXT, /* saw object value, expecting ',' or '}' */
50 JSON_PARSE_OBJECT_COMMA, /* saw object ',', expecting next label */
51 JSON_PARSE_END /* saw the end of a document, expect nothing */
54 typedef enum /* type categories for datum_to_json */
56 JSONTYPE_NULL, /* null, so we didn't bother to identify */
57 JSONTYPE_BOOL, /* boolean (built-in types only) */
58 JSONTYPE_NUMERIC, /* numeric (ditto) */
59 JSONTYPE_DATE, /* we use special formatting for datetimes */
62 JSONTYPE_JSON, /* JSON itself (and JSONB) */
63 JSONTYPE_ARRAY, /* array */
64 JSONTYPE_COMPOSITE, /* composite */
65 JSONTYPE_CAST, /* something with an explicit cast to JSON */
66 JSONTYPE_OTHER /* all else */
69 static inline void json_lex(JsonLexContext *lex);
70 static inline void json_lex_string(JsonLexContext *lex);
71 static inline void json_lex_number(JsonLexContext *lex, char *s, bool *num_err);
72 static inline void parse_scalar(JsonLexContext *lex, JsonSemAction *sem);
73 static void parse_object_field(JsonLexContext *lex, JsonSemAction *sem);
74 static void parse_object(JsonLexContext *lex, JsonSemAction *sem);
75 static void parse_array_element(JsonLexContext *lex, JsonSemAction *sem);
76 static void parse_array(JsonLexContext *lex, JsonSemAction *sem);
77 static void report_parse_error(JsonParseContext ctx, JsonLexContext *lex);
78 static void report_invalid_token(JsonLexContext *lex);
79 static int report_json_context(JsonLexContext *lex);
80 static char *extract_mb_char(char *s);
81 static void composite_to_json(Datum composite, StringInfo result,
83 static void array_dim_to_json(StringInfo result, int dim, int ndims, int *dims,
84 Datum *vals, bool *nulls, int *valcount,
85 JsonTypeCategory tcategory, Oid outfuncoid,
87 static void array_to_json_internal(Datum array, StringInfo result,
89 static void json_categorize_type(Oid typoid,
90 JsonTypeCategory *tcategory,
92 static void datum_to_json(Datum val, bool is_null, StringInfo result,
93 JsonTypeCategory tcategory, Oid outfuncoid,
95 static void add_json(Datum val, bool is_null, StringInfo result,
96 Oid val_type, bool key_scalar);
98 /* the null action object used for pure validation */
99 static JsonSemAction nullSemAction =
101 NULL, NULL, NULL, NULL, NULL,
102 NULL, NULL, NULL, NULL, NULL
105 /* Recursive Descent parser support routines */
110 * what is the current look_ahead token?
112 static inline JsonTokenType
113 lex_peek(JsonLexContext *lex)
115 return lex->token_type;
121 * accept the look_ahead token and move the lexer to the next token if the
122 * look_ahead token matches the token parameter. In that case, and if required,
123 * also hand back the de-escaped lexeme.
125 * returns true if the token matched, false otherwise.
128 lex_accept(JsonLexContext *lex, JsonTokenType token, char **lexeme)
130 if (lex->token_type == token)
134 if (lex->token_type == JSON_TOKEN_STRING)
136 if (lex->strval != NULL)
137 *lexeme = pstrdup(lex->strval->data);
141 int len = (lex->token_terminator - lex->token_start);
142 char *tokstr = palloc(len + 1);
144 memcpy(tokstr, lex->token_start, len);
158 * move the lexer to the next token if the current look_ahead token matches
159 * the parameter token. Otherwise, report an error.
162 lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
164 if (!lex_accept(lex, token, NULL))
165 report_parse_error(ctx, lex);;
168 /* chars to consider as part of an alphanumeric token */
169 #define JSON_ALPHANUMERIC_CHAR(c) \
170 (((c) >= 'a' && (c) <= 'z') || \
171 ((c) >= 'A' && (c) <= 'Z') || \
172 ((c) >= '0' && (c) <= '9') || \
180 json_in(PG_FUNCTION_ARGS)
182 char *json = PG_GETARG_CSTRING(0);
183 text *result = cstring_to_text(json);
187 lex = makeJsonLexContext(result, false);
188 pg_parse_json(lex, &nullSemAction);
190 /* Internal representation is the same as text, for now */
191 PG_RETURN_TEXT_P(result);
198 json_out(PG_FUNCTION_ARGS)
200 /* we needn't detoast because text_to_cstring will handle that */
201 Datum txt = PG_GETARG_DATUM(0);
203 PG_RETURN_CSTRING(TextDatumGetCString(txt));
210 json_send(PG_FUNCTION_ARGS)
212 text *t = PG_GETARG_TEXT_PP(0);
215 pq_begintypsend(&buf);
216 pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
217 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
224 json_recv(PG_FUNCTION_ARGS)
226 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
231 str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
234 lex = makeJsonLexContextCstringLen(str, nbytes, false);
235 pg_parse_json(lex, &nullSemAction);
237 PG_RETURN_TEXT_P(cstring_to_text_with_len(str, nbytes));
243 * lex constructor, with or without StringInfo object
244 * for de-escaped lexemes.
246 * Without is better as it makes the processing faster, so only make one
247 * if really required.
249 * If you already have the json as a text* value, use the first of these
250 * functions, otherwise use makeJsonLexContextCstringLen().
253 makeJsonLexContext(text *json, bool need_escapes)
255 return makeJsonLexContextCstringLen(VARDATA(json),
256 VARSIZE(json) - VARHDRSZ,
261 makeJsonLexContextCstringLen(char *json, int len, bool need_escapes)
263 JsonLexContext *lex = palloc0(sizeof(JsonLexContext));
265 lex->input = lex->token_terminator = lex->line_start = json;
266 lex->line_number = 1;
267 lex->input_length = len;
269 lex->strval = makeStringInfo();
276 * Publicly visible entry point for the JSON parser.
278 * lex is a lexing context, set up for the json to be processed by calling
279 * makeJsonLexContext(). sem is a strucure of function pointers to semantic
280 * action routines to be called at appropriate spots during parsing, and a
281 * pointer to a state object to be passed to those routines.
284 pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
288 /* get the initial token */
293 /* parse by recursive descent */
296 case JSON_TOKEN_OBJECT_START:
297 parse_object(lex, sem);
299 case JSON_TOKEN_ARRAY_START:
300 parse_array(lex, sem);
303 parse_scalar(lex, sem); /* json can be a bare scalar */
306 lex_expect(JSON_PARSE_END, lex, JSON_TOKEN_END);
311 * Recursive Descent parse routines. There is one for each structural
312 * element in a json document:
313 * - scalar (string, number, true, false, null)
320 parse_scalar(JsonLexContext *lex, JsonSemAction *sem)
323 json_scalar_action sfunc = sem->scalar;
325 JsonTokenType tok = lex_peek(lex);
327 valaddr = sfunc == NULL ? NULL : &val;
329 /* a scalar must be a string, a number, true, false, or null */
332 case JSON_TOKEN_TRUE:
333 lex_accept(lex, JSON_TOKEN_TRUE, valaddr);
335 case JSON_TOKEN_FALSE:
336 lex_accept(lex, JSON_TOKEN_FALSE, valaddr);
338 case JSON_TOKEN_NULL:
339 lex_accept(lex, JSON_TOKEN_NULL, valaddr);
341 case JSON_TOKEN_NUMBER:
342 lex_accept(lex, JSON_TOKEN_NUMBER, valaddr);
344 case JSON_TOKEN_STRING:
345 lex_accept(lex, JSON_TOKEN_STRING, valaddr);
348 report_parse_error(JSON_PARSE_VALUE, lex);
352 (*sfunc) (sem->semstate, val, tok);
356 parse_object_field(JsonLexContext *lex, JsonSemAction *sem)
359 * An object field is "fieldname" : value where value can be a scalar,
360 * object or array. Note: in user-facing docs and error messages, we
361 * generally call a field name a "key".
364 char *fname = NULL; /* keep compiler quiet */
365 json_ofield_action ostart = sem->object_field_start;
366 json_ofield_action oend = sem->object_field_end;
368 char **fnameaddr = NULL;
371 if (ostart != NULL || oend != NULL)
374 if (!lex_accept(lex, JSON_TOKEN_STRING, fnameaddr))
375 report_parse_error(JSON_PARSE_STRING, lex);
377 lex_expect(JSON_PARSE_OBJECT_LABEL, lex, JSON_TOKEN_COLON);
380 isnull = tok == JSON_TOKEN_NULL;
383 (*ostart) (sem->semstate, fname, isnull);
387 case JSON_TOKEN_OBJECT_START:
388 parse_object(lex, sem);
390 case JSON_TOKEN_ARRAY_START:
391 parse_array(lex, sem);
394 parse_scalar(lex, sem);
398 (*oend) (sem->semstate, fname, isnull);
402 parse_object(JsonLexContext *lex, JsonSemAction *sem)
405 * an object is a possibly empty sequence of object fields, separated by
406 * commas and surrounded by curly braces.
408 json_struct_action ostart = sem->object_start;
409 json_struct_action oend = sem->object_end;
413 (*ostart) (sem->semstate);
416 * Data inside an object is at a higher nesting level than the object
417 * itself. Note that we increment this after we call the semantic routine
418 * for the object start and restore it before we call the routine for the
423 /* we know this will succeeed, just clearing the token */
424 lex_expect(JSON_PARSE_OBJECT_START, lex, JSON_TOKEN_OBJECT_START);
429 case JSON_TOKEN_STRING:
430 parse_object_field(lex, sem);
431 while (lex_accept(lex, JSON_TOKEN_COMMA, NULL))
432 parse_object_field(lex, sem);
434 case JSON_TOKEN_OBJECT_END:
437 /* case of an invalid initial token inside the object */
438 report_parse_error(JSON_PARSE_OBJECT_START, lex);
441 lex_expect(JSON_PARSE_OBJECT_NEXT, lex, JSON_TOKEN_OBJECT_END);
446 (*oend) (sem->semstate);
450 parse_array_element(JsonLexContext *lex, JsonSemAction *sem)
452 json_aelem_action astart = sem->array_element_start;
453 json_aelem_action aend = sem->array_element_end;
454 JsonTokenType tok = lex_peek(lex);
458 isnull = tok == JSON_TOKEN_NULL;
461 (*astart) (sem->semstate, isnull);
463 /* an array element is any object, array or scalar */
466 case JSON_TOKEN_OBJECT_START:
467 parse_object(lex, sem);
469 case JSON_TOKEN_ARRAY_START:
470 parse_array(lex, sem);
473 parse_scalar(lex, sem);
477 (*aend) (sem->semstate, isnull);
481 parse_array(JsonLexContext *lex, JsonSemAction *sem)
484 * an array is a possibly empty sequence of array elements, separated by
485 * commas and surrounded by square brackets.
487 json_struct_action astart = sem->array_start;
488 json_struct_action aend = sem->array_end;
491 (*astart) (sem->semstate);
494 * Data inside an array is at a higher nesting level than the array
495 * itself. Note that we increment this after we call the semantic routine
496 * for the array start and restore it before we call the routine for the
501 lex_expect(JSON_PARSE_ARRAY_START, lex, JSON_TOKEN_ARRAY_START);
502 if (lex_peek(lex) != JSON_TOKEN_ARRAY_END)
505 parse_array_element(lex, sem);
507 while (lex_accept(lex, JSON_TOKEN_COMMA, NULL))
508 parse_array_element(lex, sem);
511 lex_expect(JSON_PARSE_ARRAY_NEXT, lex, JSON_TOKEN_ARRAY_END);
516 (*aend) (sem->semstate);
520 * Lex one token from the input stream.
523 json_lex(JsonLexContext *lex)
528 /* Skip leading whitespace. */
529 s = lex->token_terminator;
530 len = s - lex->input;
531 while (len < lex->input_length &&
532 (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'))
539 lex->token_start = s;
541 /* Determine token type. */
542 if (len >= lex->input_length)
544 lex->token_start = NULL;
545 lex->prev_token_terminator = lex->token_terminator;
546 lex->token_terminator = s;
547 lex->token_type = JSON_TOKEN_END;
552 /* Single-character token, some kind of punctuation mark. */
554 lex->prev_token_terminator = lex->token_terminator;
555 lex->token_terminator = s + 1;
556 lex->token_type = JSON_TOKEN_OBJECT_START;
559 lex->prev_token_terminator = lex->token_terminator;
560 lex->token_terminator = s + 1;
561 lex->token_type = JSON_TOKEN_OBJECT_END;
564 lex->prev_token_terminator = lex->token_terminator;
565 lex->token_terminator = s + 1;
566 lex->token_type = JSON_TOKEN_ARRAY_START;
569 lex->prev_token_terminator = lex->token_terminator;
570 lex->token_terminator = s + 1;
571 lex->token_type = JSON_TOKEN_ARRAY_END;
574 lex->prev_token_terminator = lex->token_terminator;
575 lex->token_terminator = s + 1;
576 lex->token_type = JSON_TOKEN_COMMA;
579 lex->prev_token_terminator = lex->token_terminator;
580 lex->token_terminator = s + 1;
581 lex->token_type = JSON_TOKEN_COLON;
585 json_lex_string(lex);
586 lex->token_type = JSON_TOKEN_STRING;
589 /* Negative number. */
590 json_lex_number(lex, s + 1, NULL);
591 lex->token_type = JSON_TOKEN_NUMBER;
603 /* Positive number. */
604 json_lex_number(lex, s, NULL);
605 lex->token_type = JSON_TOKEN_NUMBER;
612 * We're not dealing with a string, number, legal
613 * punctuation mark, or end of string. The only legal
614 * tokens we might find here are true, false, and null,
615 * but for error reporting purposes we scan until we see a
616 * non-alphanumeric character. That way, we can report
617 * the whole word as an unexpected token, rather than just
618 * some unintuitive prefix thereof.
620 for (p = s; p - s < lex->input_length - len && JSON_ALPHANUMERIC_CHAR(*p); p++)
624 * We got some sort of unexpected punctuation or an
625 * otherwise unexpected character, so just complain about
626 * that one character.
630 lex->prev_token_terminator = lex->token_terminator;
631 lex->token_terminator = s + 1;
632 report_invalid_token(lex);
636 * We've got a real alphanumeric token here. If it
637 * happens to be true, false, or null, all is well. If
640 lex->prev_token_terminator = lex->token_terminator;
641 lex->token_terminator = p;
644 if (memcmp(s, "true", 4) == 0)
645 lex->token_type = JSON_TOKEN_TRUE;
646 else if (memcmp(s, "null", 4) == 0)
647 lex->token_type = JSON_TOKEN_NULL;
649 report_invalid_token(lex);
651 else if (p - s == 5 && memcmp(s, "false", 5) == 0)
652 lex->token_type = JSON_TOKEN_FALSE;
654 report_invalid_token(lex);
657 } /* end of switch */
661 * The next token in the input stream is known to be a string; lex it.
664 json_lex_string(JsonLexContext *lex)
668 int hi_surrogate = -1;
670 if (lex->strval != NULL)
671 resetStringInfo(lex->strval);
673 Assert(lex->input_length > 0);
674 s = lex->token_start;
675 len = lex->token_start - lex->input;
680 /* Premature end of the string. */
681 if (len >= lex->input_length)
683 lex->token_terminator = s;
684 report_invalid_token(lex);
688 else if ((unsigned char) *s < 32)
690 /* Per RFC4627, these characters MUST be escaped. */
691 /* Since *s isn't printable, exclude it from the context string */
692 lex->token_terminator = s;
694 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
695 errmsg("invalid input syntax for type json"),
696 errdetail("Character with value 0x%02x must be escaped.",
698 report_json_context(lex)));
702 /* OK, we have an escape character. */
705 if (len >= lex->input_length)
707 lex->token_terminator = s;
708 report_invalid_token(lex);
715 for (i = 1; i <= 4; i++)
719 if (len >= lex->input_length)
721 lex->token_terminator = s;
722 report_invalid_token(lex);
724 else if (*s >= '0' && *s <= '9')
725 ch = (ch * 16) + (*s - '0');
726 else if (*s >= 'a' && *s <= 'f')
727 ch = (ch * 16) + (*s - 'a') + 10;
728 else if (*s >= 'A' && *s <= 'F')
729 ch = (ch * 16) + (*s - 'A') + 10;
732 lex->token_terminator = s + pg_mblen(s);
734 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
735 errmsg("invalid input syntax for type json"),
736 errdetail("\"\\u\" must be followed by four hexadecimal digits."),
737 report_json_context(lex)));
740 if (lex->strval != NULL)
745 if (ch >= 0xd800 && ch <= 0xdbff)
747 if (hi_surrogate != -1)
749 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
750 errmsg("invalid input syntax for type json"),
751 errdetail("Unicode high surrogate must not follow a high surrogate."),
752 report_json_context(lex)));
753 hi_surrogate = (ch & 0x3ff) << 10;
756 else if (ch >= 0xdc00 && ch <= 0xdfff)
758 if (hi_surrogate == -1)
760 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
761 errmsg("invalid input syntax for type json"),
762 errdetail("Unicode low surrogate must follow a high surrogate."),
763 report_json_context(lex)));
764 ch = 0x10000 + hi_surrogate + (ch & 0x3ff);
768 if (hi_surrogate != -1)
770 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
771 errmsg("invalid input syntax for type json"),
772 errdetail("Unicode low surrogate must follow a high surrogate."),
773 report_json_context(lex)));
776 * For UTF8, replace the escape sequence by the actual
777 * utf8 character in lex->strval. Do this also for other
778 * encodings if the escape designates an ASCII character,
779 * otherwise raise an error. We don't ever unescape a
780 * \u0000, since that would result in an impermissible nul
786 appendStringInfoString(lex->strval, "\\u0000");
788 else if (GetDatabaseEncoding() == PG_UTF8)
790 unicode_to_utf8(ch, (unsigned char *) utf8str);
791 utf8len = pg_utf_mblen((unsigned char *) utf8str);
792 appendBinaryStringInfo(lex->strval, utf8str, utf8len);
794 else if (ch <= 0x007f)
797 * This is the only way to designate things like a
798 * form feed character in JSON, so it's useful in all
801 appendStringInfoChar(lex->strval, (char) ch);
806 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
807 errmsg("invalid input syntax for type json"),
808 errdetail("Unicode escape values cannot be used for code point values above 007F when the server encoding is not UTF8."),
809 report_json_context(lex)));
814 else if (lex->strval != NULL)
816 if (hi_surrogate != -1)
818 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
819 errmsg("invalid input syntax for type json"),
820 errdetail("Unicode low surrogate must follow a high surrogate."),
821 report_json_context(lex)));
828 appendStringInfoChar(lex->strval, *s);
831 appendStringInfoChar(lex->strval, '\b');
834 appendStringInfoChar(lex->strval, '\f');
837 appendStringInfoChar(lex->strval, '\n');
840 appendStringInfoChar(lex->strval, '\r');
843 appendStringInfoChar(lex->strval, '\t');
846 /* Not a valid string escape, so error out. */
847 lex->token_terminator = s + pg_mblen(s);
849 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
850 errmsg("invalid input syntax for type json"),
851 errdetail("Escape sequence \"\\%s\" is invalid.",
853 report_json_context(lex)));
856 else if (strchr("\"\\/bfnrt", *s) == NULL)
859 * Simpler processing if we're not bothered about de-escaping
861 * It's very tempting to remove the strchr() call here and
862 * replace it with a switch statement, but testing so far has
863 * shown it's not a performance win.
865 lex->token_terminator = s + pg_mblen(s);
867 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
868 errmsg("invalid input syntax for type json"),
869 errdetail("Escape sequence \"\\%s\" is invalid.",
871 report_json_context(lex)));
875 else if (lex->strval != NULL)
877 if (hi_surrogate != -1)
879 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
880 errmsg("invalid input syntax for type json"),
881 errdetail("Unicode low surrogate must follow a high surrogate."),
882 report_json_context(lex)));
884 appendStringInfoChar(lex->strval, *s);
889 if (hi_surrogate != -1)
891 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
892 errmsg("invalid input syntax for type json"),
893 errdetail("Unicode low surrogate must follow a high surrogate."),
894 report_json_context(lex)));
896 /* Hooray, we found the end of the string! */
897 lex->prev_token_terminator = lex->token_terminator;
898 lex->token_terminator = s + 1;
901 /*-------------------------------------------------------------------------
902 * The next token in the input stream is known to be a number; lex it.
904 * In JSON, a number consists of four parts:
906 * (1) An optional minus sign ('-').
908 * (2) Either a single '0', or a string of one or more digits that does not
911 * (3) An optional decimal part, consisting of a period ('.') followed by
912 * one or more digits. (Note: While this part can be omitted
913 * completely, it's not OK to have only the decimal point without
914 * any digits afterwards.)
916 * (4) An optional exponent part, consisting of 'e' or 'E', optionally
917 * followed by '+' or '-', followed by one or more digits. (Note:
918 * As with the decimal part, if 'e' or 'E' is present, it must be
919 * followed by at least one digit.)
921 * The 's' argument to this function points to the ostensible beginning
922 * of part 2 - i.e. the character after any optional minus sign, and the
923 * first character of the string if there is none.
925 *-------------------------------------------------------------------------
928 json_lex_number(JsonLexContext *lex, char *s, bool *num_err)
934 len = s - lex->input;
935 /* Part (1): leading sign indicator. */
936 /* Caller already did this for us; so do nothing. */
938 /* Part (2): parse main digit string. */
944 else if (*s >= '1' && *s <= '9')
950 } while (len < lex->input_length && *s >= '0' && *s <= '9');
955 /* Part (3): parse optional decimal portion. */
956 if (len < lex->input_length && *s == '.')
960 if (len == lex->input_length || *s < '0' || *s > '9')
968 } while (len < lex->input_length && *s >= '0' && *s <= '9');
972 /* Part (4): parse optional exponent. */
973 if (len < lex->input_length && (*s == 'e' || *s == 'E'))
977 if (len < lex->input_length && (*s == '+' || *s == '-'))
982 if (len == lex->input_length || *s < '0' || *s > '9')
990 } while (len < lex->input_length && *s >= '0' && *s <= '9');
995 * Check for trailing garbage. As in json_lex(), any alphanumeric stuff
996 * here should be considered part of the token for error-reporting
999 for (p = s; len < lex->input_length && JSON_ALPHANUMERIC_CHAR(*p); p++, len++)
1002 if (num_err != NULL)
1004 /* let the caller handle the error */
1009 lex->prev_token_terminator = lex->token_terminator;
1010 lex->token_terminator = p;
1012 report_invalid_token(lex);
1017 * Report a parse error.
1019 * lex->token_start and lex->token_terminator must identify the current token.
1022 report_parse_error(JsonParseContext ctx, JsonLexContext *lex)
1027 /* Handle case where the input ended prematurely. */
1028 if (lex->token_start == NULL || lex->token_type == JSON_TOKEN_END)
1030 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1031 errmsg("invalid input syntax for type json"),
1032 errdetail("The input string ended unexpectedly."),
1033 report_json_context(lex)));
1035 /* Separate out the current token. */
1036 toklen = lex->token_terminator - lex->token_start;
1037 token = palloc(toklen + 1);
1038 memcpy(token, lex->token_start, toklen);
1039 token[toklen] = '\0';
1041 /* Complain, with the appropriate detail message. */
1042 if (ctx == JSON_PARSE_END)
1044 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1045 errmsg("invalid input syntax for type json"),
1046 errdetail("Expected end of input, but found \"%s\".",
1048 report_json_context(lex)));
1053 case JSON_PARSE_VALUE:
1055 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1056 errmsg("invalid input syntax for type json"),
1057 errdetail("Expected JSON value, but found \"%s\".",
1059 report_json_context(lex)));
1061 case JSON_PARSE_STRING:
1063 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1064 errmsg("invalid input syntax for type json"),
1065 errdetail("Expected string, but found \"%s\".",
1067 report_json_context(lex)));
1069 case JSON_PARSE_ARRAY_START:
1071 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1072 errmsg("invalid input syntax for type json"),
1073 errdetail("Expected array element or \"]\", but found \"%s\".",
1075 report_json_context(lex)));
1077 case JSON_PARSE_ARRAY_NEXT:
1079 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1080 errmsg("invalid input syntax for type json"),
1081 errdetail("Expected \",\" or \"]\", but found \"%s\".",
1083 report_json_context(lex)));
1085 case JSON_PARSE_OBJECT_START:
1087 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1088 errmsg("invalid input syntax for type json"),
1089 errdetail("Expected string or \"}\", but found \"%s\".",
1091 report_json_context(lex)));
1093 case JSON_PARSE_OBJECT_LABEL:
1095 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1096 errmsg("invalid input syntax for type json"),
1097 errdetail("Expected \":\", but found \"%s\".",
1099 report_json_context(lex)));
1101 case JSON_PARSE_OBJECT_NEXT:
1103 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1104 errmsg("invalid input syntax for type json"),
1105 errdetail("Expected \",\" or \"}\", but found \"%s\".",
1107 report_json_context(lex)));
1109 case JSON_PARSE_OBJECT_COMMA:
1111 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1112 errmsg("invalid input syntax for type json"),
1113 errdetail("Expected string, but found \"%s\".",
1115 report_json_context(lex)));
1118 elog(ERROR, "unexpected json parse state: %d", ctx);
1124 * Report an invalid input token.
1126 * lex->token_start and lex->token_terminator must identify the token.
1129 report_invalid_token(JsonLexContext *lex)
1134 /* Separate out the offending token. */
1135 toklen = lex->token_terminator - lex->token_start;
1136 token = palloc(toklen + 1);
1137 memcpy(token, lex->token_start, toklen);
1138 token[toklen] = '\0';
1141 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1142 errmsg("invalid input syntax for type json"),
1143 errdetail("Token \"%s\" is invalid.", token),
1144 report_json_context(lex)));
1148 * Report a CONTEXT line for bogus JSON input.
1150 * lex->token_terminator must be set to identify the spot where we detected
1151 * the error. Note that lex->token_start might be NULL, in case we recognized
1154 * The return value isn't meaningful, but we make it non-void so that this
1155 * can be invoked inside ereport().
1158 report_json_context(JsonLexContext *lex)
1160 const char *context_start;
1161 const char *context_end;
1162 const char *line_start;
1169 /* Choose boundaries for the part of the input we will display */
1170 context_start = lex->input;
1171 context_end = lex->token_terminator;
1172 line_start = context_start;
1176 /* Always advance over newlines */
1177 if (context_start < context_end && *context_start == '\n')
1180 line_start = context_start;
1184 /* Otherwise, done as soon as we are close enough to context_end */
1185 if (context_end - context_start < 50)
1187 /* Advance to next multibyte character */
1188 if (IS_HIGHBIT_SET(*context_start))
1189 context_start += pg_mblen(context_start);
1195 * We add "..." to indicate that the excerpt doesn't start at the
1196 * beginning of the line ... but if we're within 3 characters of the
1197 * beginning of the line, we might as well just show the whole line.
1199 if (context_start - line_start <= 3)
1200 context_start = line_start;
1202 /* Get a null-terminated copy of the data to present */
1203 ctxtlen = context_end - context_start;
1204 ctxt = palloc(ctxtlen + 1);
1205 memcpy(ctxt, context_start, ctxtlen);
1206 ctxt[ctxtlen] = '\0';
1209 * Show the context, prefixing "..." if not starting at start of line, and
1210 * suffixing "..." if not ending at end of line.
1212 prefix = (context_start > line_start) ? "..." : "";
1213 suffix = (lex->token_type != JSON_TOKEN_END && context_end - lex->input < lex->input_length && *context_end != '\n' && *context_end != '\r') ? "..." : "";
1215 return errcontext("JSON data, line %d: %s%s%s",
1216 line_number, prefix, ctxt, suffix);
1220 * Extract a single, possibly multi-byte char from the input string.
1223 extract_mb_char(char *s)
1229 res = palloc(len + 1);
1230 memcpy(res, s, len);
1237 * Determine how we want to print values of a given type in datum_to_json.
1239 * Given the datatype OID, return its JsonTypeCategory, as well as the type's
1240 * output function OID. If the returned category is JSONTYPE_CAST, we
1241 * return the OID of the type->JSON cast function instead.
1244 json_categorize_type(Oid typoid,
1245 JsonTypeCategory *tcategory,
1250 /* Look through any domain */
1251 typoid = getBaseType(typoid);
1253 /* We'll usually need to return the type output function */
1254 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
1256 /* Check for known types */
1260 *tcategory = JSONTYPE_BOOL;
1269 *tcategory = JSONTYPE_NUMERIC;
1273 *tcategory = JSONTYPE_DATE;
1277 *tcategory = JSONTYPE_TIMESTAMP;
1280 case TIMESTAMPTZOID:
1281 *tcategory = JSONTYPE_TIMESTAMPTZ;
1286 *tcategory = JSONTYPE_JSON;
1290 /* Check for arrays and composites */
1291 if (OidIsValid(get_element_type(typoid)))
1292 *tcategory = JSONTYPE_ARRAY;
1293 else if (type_is_rowtype(typoid))
1294 *tcategory = JSONTYPE_COMPOSITE;
1297 /* It's probably the general case ... */
1298 *tcategory = JSONTYPE_OTHER;
1299 /* but let's look for a cast to json, if it's not built-in */
1300 if (typoid >= FirstNormalObjectId)
1304 tuple = SearchSysCache2(CASTSOURCETARGET,
1305 ObjectIdGetDatum(typoid),
1306 ObjectIdGetDatum(JSONOID));
1307 if (HeapTupleIsValid(tuple))
1309 Form_pg_cast castForm = (Form_pg_cast) GETSTRUCT(tuple);
1311 if (castForm->castmethod == COERCION_METHOD_FUNCTION)
1313 *tcategory = JSONTYPE_CAST;
1314 *outfuncoid = castForm->castfunc;
1317 ReleaseSysCache(tuple);
1326 * Turn a Datum into JSON text, appending the string to "result".
1328 * tcategory and outfuncoid are from a previous call to json_categorize_type,
1329 * except that if is_null is true then they can be invalid.
1331 * If key_scalar is true, the value is being printed as a key, so insist
1332 * it's of an acceptable type, and force it to be quoted.
1335 datum_to_json(Datum val, bool is_null, StringInfo result,
1336 JsonTypeCategory tcategory, Oid outfuncoid,
1342 JsonLexContext dummy_lex;
1346 appendStringInfoString(result, "null");
1351 (tcategory == JSONTYPE_ARRAY ||
1352 tcategory == JSONTYPE_COMPOSITE ||
1353 tcategory == JSONTYPE_JSON ||
1354 tcategory == JSONTYPE_CAST))
1356 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1357 errmsg("key value must be scalar, not array, composite, or json")));
1361 case JSONTYPE_ARRAY:
1362 array_to_json_internal(val, result, false);
1364 case JSONTYPE_COMPOSITE:
1365 composite_to_json(val, result, false);
1368 outputstr = DatumGetBool(val) ? "true" : "false";
1370 escape_json(result, outputstr);
1372 appendStringInfoString(result, outputstr);
1374 case JSONTYPE_NUMERIC:
1375 outputstr = OidOutputFunctionCall(outfuncoid, val);
1378 /* always quote keys */
1379 escape_json(result, outputstr);
1384 * Don't call escape_json for a non-key if it's a valid JSON
1387 dummy_lex.input = *outputstr == '-' ? outputstr + 1 : outputstr;
1388 dummy_lex.input_length = strlen(dummy_lex.input);
1389 json_lex_number(&dummy_lex, dummy_lex.input, &numeric_error);
1391 appendStringInfoString(result, outputstr);
1393 escape_json(result, outputstr);
1401 char buf[MAXDATELEN + 1];
1403 date = DatumGetDateADT(val);
1405 /* XSD doesn't support infinite values */
1406 if (DATE_NOT_FINITE(date))
1408 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1409 errmsg("date out of range"),
1410 errdetail("JSON does not support infinite date values.")));
1413 j2date(date + POSTGRES_EPOCH_JDATE,
1414 &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
1415 EncodeDateOnly(&tm, USE_XSD_DATES, buf);
1418 appendStringInfo(result, "\"%s\"", buf);
1421 case JSONTYPE_TIMESTAMP:
1423 Timestamp timestamp;
1426 char buf[MAXDATELEN + 1];
1428 timestamp = DatumGetTimestamp(val);
1430 /* XSD doesn't support infinite values */
1431 if (TIMESTAMP_NOT_FINITE(timestamp))
1433 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1434 errmsg("timestamp out of range"),
1435 errdetail("JSON does not support infinite timestamp values.")));
1436 else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
1437 EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
1440 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1441 errmsg("timestamp out of range")));
1443 appendStringInfo(result, "\"%s\"", buf);
1446 case JSONTYPE_TIMESTAMPTZ:
1448 TimestampTz timestamp;
1452 const char *tzn = NULL;
1453 char buf[MAXDATELEN + 1];
1455 timestamp = DatumGetTimestamp(val);
1457 /* XSD doesn't support infinite values */
1458 if (TIMESTAMP_NOT_FINITE(timestamp))
1460 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1461 errmsg("timestamp out of range"),
1462 errdetail("JSON does not support infinite timestamp values.")));
1463 else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
1464 EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
1467 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1468 errmsg("timestamp out of range")));
1470 appendStringInfo(result, "\"%s\"", buf);
1474 /* JSON and JSONB output will already be escaped */
1475 outputstr = OidOutputFunctionCall(outfuncoid, val);
1476 appendStringInfoString(result, outputstr);
1480 /* outfuncoid refers to a cast function, not an output function */
1481 jsontext = DatumGetTextP(OidFunctionCall1(outfuncoid, val));
1482 outputstr = text_to_cstring(jsontext);
1483 appendStringInfoString(result, outputstr);
1488 outputstr = OidOutputFunctionCall(outfuncoid, val);
1489 if (key_scalar && *outputstr == '\0')
1491 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1492 errmsg("key value must not be empty")));
1493 escape_json(result, outputstr);
1500 * Process a single dimension of an array.
1501 * If it's the innermost dimension, output the values, otherwise call
1502 * ourselves recursively to process the next dimension.
1505 array_dim_to_json(StringInfo result, int dim, int ndims, int *dims, Datum *vals,
1506 bool *nulls, int *valcount, JsonTypeCategory tcategory,
1507 Oid outfuncoid, bool use_line_feeds)
1512 Assert(dim < ndims);
1514 sep = use_line_feeds ? ",\n " : ",";
1516 appendStringInfoChar(result, '[');
1518 for (i = 1; i <= dims[dim]; i++)
1521 appendStringInfoString(result, sep);
1523 if (dim + 1 == ndims)
1525 datum_to_json(vals[*valcount], nulls[*valcount], result, tcategory,
1532 * Do we want line feeds on inner dimensions of arrays? For now
1535 array_dim_to_json(result, dim + 1, ndims, dims, vals, nulls,
1536 valcount, tcategory, outfuncoid, false);
1540 appendStringInfoChar(result, ']');
1544 * Turn an array into JSON.
1547 array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
1549 ArrayType *v = DatumGetArrayTypeP(array);
1550 Oid element_type = ARR_ELEMTYPE(v);
1560 JsonTypeCategory tcategory;
1565 nitems = ArrayGetNItems(ndim, dim);
1569 appendStringInfoString(result, "[]");
1573 get_typlenbyvalalign(element_type,
1574 &typlen, &typbyval, &typalign);
1576 json_categorize_type(element_type,
1577 &tcategory, &outfuncoid);
1579 deconstruct_array(v, element_type, typlen, typbyval,
1580 typalign, &elements, &nulls,
1583 array_dim_to_json(result, 0, ndim, dim, elements, nulls, &count, tcategory,
1584 outfuncoid, use_line_feeds);
1591 * Turn a composite / record into JSON.
1594 composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
1600 HeapTupleData tmptup,
1603 bool needsep = false;
1606 sep = use_line_feeds ? ",\n " : ",";
1608 td = DatumGetHeapTupleHeader(composite);
1610 /* Extract rowtype info and find a tupdesc */
1611 tupType = HeapTupleHeaderGetTypeId(td);
1612 tupTypmod = HeapTupleHeaderGetTypMod(td);
1613 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
1615 /* Build a temporary HeapTuple control structure */
1616 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
1620 appendStringInfoChar(result, '{');
1622 for (i = 0; i < tupdesc->natts; i++)
1627 JsonTypeCategory tcategory;
1630 if (tupdesc->attrs[i]->attisdropped)
1634 appendStringInfoString(result, sep);
1637 attname = NameStr(tupdesc->attrs[i]->attname);
1638 escape_json(result, attname);
1639 appendStringInfoChar(result, ':');
1641 val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
1645 tcategory = JSONTYPE_NULL;
1646 outfuncoid = InvalidOid;
1649 json_categorize_type(tupdesc->attrs[i]->atttypid,
1650 &tcategory, &outfuncoid);
1652 datum_to_json(val, isnull, result, tcategory, outfuncoid, false);
1655 appendStringInfoChar(result, '}');
1656 ReleaseTupleDesc(tupdesc);
1660 * Append JSON text for "val" to "result".
1662 * This is just a thin wrapper around datum_to_json. If the same type will be
1663 * printed many times, avoid using this; better to do the json_categorize_type
1664 * lookups only once.
1667 add_json(Datum val, bool is_null, StringInfo result,
1668 Oid val_type, bool key_scalar)
1670 JsonTypeCategory tcategory;
1673 if (val_type == InvalidOid)
1675 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1676 errmsg("could not determine input data type")));
1680 tcategory = JSONTYPE_NULL;
1681 outfuncoid = InvalidOid;
1684 json_categorize_type(val_type,
1685 &tcategory, &outfuncoid);
1687 datum_to_json(val, is_null, result, tcategory, outfuncoid, key_scalar);
1691 * SQL function array_to_json(row)
1694 array_to_json(PG_FUNCTION_ARGS)
1696 Datum array = PG_GETARG_DATUM(0);
1699 result = makeStringInfo();
1701 array_to_json_internal(array, result, false);
1703 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1707 * SQL function array_to_json(row, prettybool)
1710 array_to_json_pretty(PG_FUNCTION_ARGS)
1712 Datum array = PG_GETARG_DATUM(0);
1713 bool use_line_feeds = PG_GETARG_BOOL(1);
1716 result = makeStringInfo();
1718 array_to_json_internal(array, result, use_line_feeds);
1720 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1724 * SQL function row_to_json(row)
1727 row_to_json(PG_FUNCTION_ARGS)
1729 Datum array = PG_GETARG_DATUM(0);
1732 result = makeStringInfo();
1734 composite_to_json(array, result, false);
1736 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1740 * SQL function row_to_json(row, prettybool)
1743 row_to_json_pretty(PG_FUNCTION_ARGS)
1745 Datum array = PG_GETARG_DATUM(0);
1746 bool use_line_feeds = PG_GETARG_BOOL(1);
1749 result = makeStringInfo();
1751 composite_to_json(array, result, use_line_feeds);
1753 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1757 * SQL function to_json(anyvalue)
1760 to_json(PG_FUNCTION_ARGS)
1762 Datum val = PG_GETARG_DATUM(0);
1763 Oid val_type = get_fn_expr_argtype(fcinfo->flinfo, 0);
1765 JsonTypeCategory tcategory;
1768 if (val_type == InvalidOid)
1770 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1771 errmsg("could not determine input data type")));
1773 json_categorize_type(val_type,
1774 &tcategory, &outfuncoid);
1776 result = makeStringInfo();
1778 datum_to_json(val, false, result, tcategory, outfuncoid, false);
1780 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1784 * json_agg transition function
1787 json_agg_transfn(PG_FUNCTION_ARGS)
1789 Oid val_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
1790 MemoryContext aggcontext,
1794 JsonTypeCategory tcategory;
1797 if (val_type == InvalidOid)
1799 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1800 errmsg("could not determine input data type")));
1802 if (!AggCheckCallContext(fcinfo, &aggcontext))
1804 /* cannot be called directly because of internal-type argument */
1805 elog(ERROR, "json_agg_transfn called in non-aggregate context");
1808 if (PG_ARGISNULL(0))
1811 * Make this StringInfo in a context where it will persist for the
1812 * duration of the aggregate call. MemoryContextSwitchTo is only
1813 * needed the first time, as the StringInfo routines make sure they
1814 * use the right context to enlarge the object if necessary.
1816 oldcontext = MemoryContextSwitchTo(aggcontext);
1817 state = makeStringInfo();
1818 MemoryContextSwitchTo(oldcontext);
1820 appendStringInfoChar(state, '[');
1824 state = (StringInfo) PG_GETARG_POINTER(0);
1825 appendStringInfoString(state, ", ");
1828 /* fast path for NULLs */
1829 if (PG_ARGISNULL(1))
1831 datum_to_json((Datum) 0, true, state, JSONTYPE_NULL, InvalidOid, false);
1832 PG_RETURN_POINTER(state);
1835 val = PG_GETARG_DATUM(1);
1837 /* XXX we do this every time?? */
1838 json_categorize_type(val_type,
1839 &tcategory, &outfuncoid);
1841 /* add some whitespace if structured type and not first item */
1842 if (!PG_ARGISNULL(0) &&
1843 (tcategory == JSONTYPE_ARRAY || tcategory == JSONTYPE_COMPOSITE))
1845 appendStringInfoString(state, "\n ");
1848 datum_to_json(val, false, state, tcategory, outfuncoid, false);
1851 * The transition type for array_agg() is declared to be "internal", which
1852 * is a pass-by-value type the same size as a pointer. So we can safely
1853 * pass the ArrayBuildState pointer through nodeAgg.c's machinations.
1855 PG_RETURN_POINTER(state);
1859 * json_agg final function
1862 json_agg_finalfn(PG_FUNCTION_ARGS)
1866 /* cannot be called directly because of internal-type argument */
1867 Assert(AggCheckCallContext(fcinfo, NULL));
1869 state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
1874 appendStringInfoChar(state, ']');
1876 PG_RETURN_TEXT_P(cstring_to_text_with_len(state->data, state->len));
1880 * json_object_agg transition function.
1882 * aggregate two input columns as a single json value.
1885 json_object_agg_transfn(PG_FUNCTION_ARGS)
1888 MemoryContext aggcontext,
1893 if (!AggCheckCallContext(fcinfo, &aggcontext))
1895 /* cannot be called directly because of internal-type argument */
1896 elog(ERROR, "json_agg_transfn called in non-aggregate context");
1899 if (PG_ARGISNULL(0))
1902 * Make the StringInfo in a context where it will persist for the
1903 * duration of the aggregate call. Switching context is only needed
1904 * for this initial step, as the StringInfo routines make sure they
1905 * use the right context to enlarge the object if necessary.
1907 oldcontext = MemoryContextSwitchTo(aggcontext);
1908 state = makeStringInfo();
1909 MemoryContextSwitchTo(oldcontext);
1911 appendStringInfoString(state, "{ ");
1915 state = (StringInfo) PG_GETARG_POINTER(0);
1916 appendStringInfoString(state, ", ");
1920 * Note: since json_object_agg() is declared as taking type "any", the
1921 * parser will not do any type conversion on unknown-type literals (that
1922 * is, undecorated strings or NULLs). Such values will arrive here as
1923 * type UNKNOWN, which fortunately does not matter to us, since
1924 * unknownout() works fine.
1926 val_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
1928 if (val_type == InvalidOid)
1930 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1931 errmsg("could not determine data type for argument %d", 1)));
1933 if (PG_ARGISNULL(1))
1935 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1936 errmsg("field name must not be null")));
1938 arg = PG_GETARG_DATUM(1);
1940 add_json(arg, false, state, val_type, true);
1942 appendStringInfoString(state, " : ");
1944 val_type = get_fn_expr_argtype(fcinfo->flinfo, 2);
1946 if (val_type == InvalidOid)
1948 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1949 errmsg("could not determine data type for argument %d", 2)));
1951 if (PG_ARGISNULL(2))
1954 arg = PG_GETARG_DATUM(2);
1956 add_json(arg, PG_ARGISNULL(2), state, val_type, false);
1958 PG_RETURN_POINTER(state);
1962 * json_object_agg final function.
1966 json_object_agg_finalfn(PG_FUNCTION_ARGS)
1970 /* cannot be called directly because of internal-type argument */
1971 Assert(AggCheckCallContext(fcinfo, NULL));
1973 state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
1976 PG_RETURN_TEXT_P(cstring_to_text("{}"));
1978 appendStringInfoString(state, " }");
1980 PG_RETURN_TEXT_P(cstring_to_text_with_len(state->data, state->len));
1984 * SQL function json_build_object(variadic "any")
1987 json_build_object(PG_FUNCTION_ARGS)
1989 int nargs = PG_NARGS();
1992 const char *sep = "";
1998 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1999 errmsg("argument list must have even number of elements"),
2000 errhint("The arguments of json_build_object() must consist of alternating keys and values.")));
2002 result = makeStringInfo();
2004 appendStringInfoChar(result, '{');
2006 for (i = 0; i < nargs; i += 2)
2009 * Note: since json_build_object() is declared as taking type "any",
2010 * the parser will not do any type conversion on unknown-type literals
2011 * (that is, undecorated strings or NULLs). Such values will arrive
2012 * here as type UNKNOWN, which fortunately does not matter to us,
2013 * since unknownout() works fine.
2015 appendStringInfoString(result, sep);
2019 val_type = get_fn_expr_argtype(fcinfo->flinfo, i);
2021 if (val_type == InvalidOid)
2023 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2024 errmsg("could not determine data type for argument %d",
2027 if (PG_ARGISNULL(i))
2029 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2030 errmsg("argument %d cannot be null", i + 1),
2031 errhint("Object keys should be text.")));
2033 arg = PG_GETARG_DATUM(i);
2035 add_json(arg, false, result, val_type, true);
2037 appendStringInfoString(result, " : ");
2040 val_type = get_fn_expr_argtype(fcinfo->flinfo, i + 1);
2042 if (val_type == InvalidOid)
2044 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2045 errmsg("could not determine data type for argument %d",
2048 if (PG_ARGISNULL(i + 1))
2051 arg = PG_GETARG_DATUM(i + 1);
2053 add_json(arg, PG_ARGISNULL(i + 1), result, val_type, false);
2056 appendStringInfoChar(result, '}');
2058 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
2062 * degenerate case of json_build_object where it gets 0 arguments.
2065 json_build_object_noargs(PG_FUNCTION_ARGS)
2067 PG_RETURN_TEXT_P(cstring_to_text_with_len("{}", 2));
2071 * SQL function json_build_array(variadic "any")
2074 json_build_array(PG_FUNCTION_ARGS)
2076 int nargs = PG_NARGS();
2079 const char *sep = "";
2083 result = makeStringInfo();
2085 appendStringInfoChar(result, '[');
2087 for (i = 0; i < nargs; i++)
2090 * Note: since json_build_array() is declared as taking type "any",
2091 * the parser will not do any type conversion on unknown-type literals
2092 * (that is, undecorated strings or NULLs). Such values will arrive
2093 * here as type UNKNOWN, which fortunately does not matter to us,
2094 * since unknownout() works fine.
2096 appendStringInfoString(result, sep);
2099 val_type = get_fn_expr_argtype(fcinfo->flinfo, i);
2101 if (val_type == InvalidOid)
2103 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2104 errmsg("could not determine data type for argument %d",
2107 if (PG_ARGISNULL(i))
2110 arg = PG_GETARG_DATUM(i);
2112 add_json(arg, PG_ARGISNULL(i), result, val_type, false);
2115 appendStringInfoChar(result, ']');
2117 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
2121 * degenerate case of json_build_array where it gets 0 arguments.
2124 json_build_array_noargs(PG_FUNCTION_ARGS)
2126 PG_RETURN_TEXT_P(cstring_to_text_with_len("[]", 2));
2130 * SQL function json_object(text[])
2132 * take a one or two dimensional array of text as key/value pairs
2133 * for a json object.
2136 json_object(PG_FUNCTION_ARGS)
2138 ArrayType *in_array = PG_GETARG_ARRAYTYPE_P(0);
2139 int ndims = ARR_NDIM(in_array);
2140 StringInfoData result;
2152 PG_RETURN_DATUM(CStringGetTextDatum("{}"));
2156 if ((ARR_DIMS(in_array)[0]) % 2)
2158 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2159 errmsg("array must have even number of elements")));
2163 if ((ARR_DIMS(in_array)[1]) != 2)
2165 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2166 errmsg("array must have two columns")));
2171 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2172 errmsg("wrong number of array subscripts")));
2175 deconstruct_array(in_array,
2176 TEXTOID, -1, false, 'i',
2177 &in_datums, &in_nulls, &in_count);
2179 count = in_count / 2;
2181 initStringInfo(&result);
2183 appendStringInfoChar(&result, '{');
2185 for (i = 0; i < count; ++i)
2187 if (in_nulls[i * 2])
2189 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2190 errmsg("null value not allowed for object key")));
2192 v = TextDatumGetCString(in_datums[i * 2]);
2194 appendStringInfoString(&result, ", ");
2195 escape_json(&result, v);
2196 appendStringInfoString(&result, " : ");
2198 if (in_nulls[i * 2 + 1])
2199 appendStringInfoString(&result, "null");
2202 v = TextDatumGetCString(in_datums[i * 2 + 1]);
2203 escape_json(&result, v);
2208 appendStringInfoChar(&result, '}');
2213 rval = cstring_to_text_with_len(result.data, result.len);
2216 PG_RETURN_TEXT_P(rval);
2221 * SQL function json_object(text[], text[])
2223 * take separate key and value arrays of text to construct a json object
2227 json_object_two_arg(PG_FUNCTION_ARGS)
2229 ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(0);
2230 ArrayType *val_array = PG_GETARG_ARRAYTYPE_P(1);
2231 int nkdims = ARR_NDIM(key_array);
2232 int nvdims = ARR_NDIM(val_array);
2233 StringInfoData result;
2244 if (nkdims > 1 || nkdims != nvdims)
2246 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2247 errmsg("wrong number of array subscripts")));
2250 PG_RETURN_DATUM(CStringGetTextDatum("{}"));
2252 deconstruct_array(key_array,
2253 TEXTOID, -1, false, 'i',
2254 &key_datums, &key_nulls, &key_count);
2256 deconstruct_array(val_array,
2257 TEXTOID, -1, false, 'i',
2258 &val_datums, &val_nulls, &val_count);
2260 if (key_count != val_count)
2262 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2263 errmsg("mismatched array dimensions")));
2265 initStringInfo(&result);
2267 appendStringInfoChar(&result, '{');
2269 for (i = 0; i < key_count; ++i)
2273 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2274 errmsg("null value not allowed for object key")));
2276 v = TextDatumGetCString(key_datums[i]);
2278 appendStringInfoString(&result, ", ");
2279 escape_json(&result, v);
2280 appendStringInfoString(&result, " : ");
2283 appendStringInfoString(&result, "null");
2286 v = TextDatumGetCString(val_datums[i]);
2287 escape_json(&result, v);
2292 appendStringInfoChar(&result, '}');
2299 rval = cstring_to_text_with_len(result.data, result.len);
2302 PG_RETURN_TEXT_P(rval);
2307 * Produce a JSON string literal, properly escaping characters in the text.
2310 escape_json(StringInfo buf, const char *str)
2314 appendStringInfoCharMacro(buf, '\"');
2315 for (p = str; *p; p++)
2320 appendStringInfoString(buf, "\\b");
2323 appendStringInfoString(buf, "\\f");
2326 appendStringInfoString(buf, "\\n");
2329 appendStringInfoString(buf, "\\r");
2332 appendStringInfoString(buf, "\\t");
2335 appendStringInfoString(buf, "\\\"");
2340 * Unicode escapes are passed through as is. There is no
2341 * requirement that they denote a valid character in the
2342 * server encoding - indeed that is a big part of their
2345 * All we require is that they consist of \uXXXX where the Xs
2346 * are hexadecimal digits. It is the responsibility of the
2347 * caller of, say, to_json() to make sure that the unicode
2350 * In the case of a jsonb string value being escaped, the only
2351 * unicode escape that should be present is \u0000, all the
2352 * other unicode escapes will have been resolved.
2355 isxdigit((unsigned char) p[2]) &&
2356 isxdigit((unsigned char) p[3]) &&
2357 isxdigit((unsigned char) p[4]) &&
2358 isxdigit((unsigned char) p[5]))
2359 appendStringInfoCharMacro(buf, *p);
2361 appendStringInfoString(buf, "\\\\");
2364 if ((unsigned char) *p < ' ')
2365 appendStringInfo(buf, "\\u%04x", (int) *p);
2367 appendStringInfoCharMacro(buf, *p);
2371 appendStringInfoCharMacro(buf, '\"');
2375 * SQL function json_typeof(json) -> text
2377 * Returns the type of the outermost JSON value as TEXT. Possible types are
2378 * "object", "array", "string", "number", "boolean", and "null".
2380 * Performs a single call to json_lex() to get the first token of the supplied
2381 * value. This initial token uniquely determines the value's type. As our
2382 * input must already have been validated by json_in() or json_recv(), the
2383 * initial token should never be JSON_TOKEN_OBJECT_END, JSON_TOKEN_ARRAY_END,
2384 * JSON_TOKEN_COLON, JSON_TOKEN_COMMA, or JSON_TOKEN_END.
2387 json_typeof(PG_FUNCTION_ARGS)
2391 JsonLexContext *lex;
2395 json = PG_GETARG_TEXT_P(0);
2396 lex = makeJsonLexContext(json, false);
2398 /* Lex exactly one token from the input and check its type. */
2400 tok = lex_peek(lex);
2403 case JSON_TOKEN_OBJECT_START:
2406 case JSON_TOKEN_ARRAY_START:
2409 case JSON_TOKEN_STRING:
2412 case JSON_TOKEN_NUMBER:
2415 case JSON_TOKEN_TRUE:
2416 case JSON_TOKEN_FALSE:
2419 case JSON_TOKEN_NULL:
2423 elog(ERROR, "unexpected json token: %d", tok);
2426 PG_RETURN_TEXT_P(cstring_to_text(type));