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;
1344 /* callers are expected to ensure that null keys are not passed in */
1345 Assert( ! (key_scalar && is_null));
1349 appendStringInfoString(result, "null");
1354 (tcategory == JSONTYPE_ARRAY ||
1355 tcategory == JSONTYPE_COMPOSITE ||
1356 tcategory == JSONTYPE_JSON ||
1357 tcategory == JSONTYPE_CAST))
1359 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1360 errmsg("key value must be scalar, not array, composite, or json")));
1364 case JSONTYPE_ARRAY:
1365 array_to_json_internal(val, result, false);
1367 case JSONTYPE_COMPOSITE:
1368 composite_to_json(val, result, false);
1371 outputstr = DatumGetBool(val) ? "true" : "false";
1373 escape_json(result, outputstr);
1375 appendStringInfoString(result, outputstr);
1377 case JSONTYPE_NUMERIC:
1378 outputstr = OidOutputFunctionCall(outfuncoid, val);
1381 /* always quote keys */
1382 escape_json(result, outputstr);
1387 * Don't call escape_json for a non-key if it's a valid JSON
1390 dummy_lex.input = *outputstr == '-' ? outputstr + 1 : outputstr;
1391 dummy_lex.input_length = strlen(dummy_lex.input);
1392 json_lex_number(&dummy_lex, dummy_lex.input, &numeric_error);
1394 appendStringInfoString(result, outputstr);
1396 escape_json(result, outputstr);
1404 char buf[MAXDATELEN + 1];
1406 date = DatumGetDateADT(val);
1408 /* XSD doesn't support infinite values */
1409 if (DATE_NOT_FINITE(date))
1411 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1412 errmsg("date out of range"),
1413 errdetail("JSON does not support infinite date values.")));
1416 j2date(date + POSTGRES_EPOCH_JDATE,
1417 &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
1418 EncodeDateOnly(&tm, USE_XSD_DATES, buf);
1421 appendStringInfo(result, "\"%s\"", buf);
1424 case JSONTYPE_TIMESTAMP:
1426 Timestamp timestamp;
1429 char buf[MAXDATELEN + 1];
1431 timestamp = DatumGetTimestamp(val);
1433 /* XSD doesn't support infinite values */
1434 if (TIMESTAMP_NOT_FINITE(timestamp))
1436 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1437 errmsg("timestamp out of range"),
1438 errdetail("JSON does not support infinite timestamp values.")));
1439 else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
1440 EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
1443 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1444 errmsg("timestamp out of range")));
1446 appendStringInfo(result, "\"%s\"", buf);
1449 case JSONTYPE_TIMESTAMPTZ:
1451 TimestampTz timestamp;
1455 const char *tzn = NULL;
1456 char buf[MAXDATELEN + 1];
1458 timestamp = DatumGetTimestamp(val);
1460 /* XSD doesn't support infinite values */
1461 if (TIMESTAMP_NOT_FINITE(timestamp))
1463 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1464 errmsg("timestamp out of range"),
1465 errdetail("JSON does not support infinite timestamp values.")));
1466 else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
1467 EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
1470 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1471 errmsg("timestamp out of range")));
1473 appendStringInfo(result, "\"%s\"", buf);
1477 /* JSON and JSONB output will already be escaped */
1478 outputstr = OidOutputFunctionCall(outfuncoid, val);
1479 appendStringInfoString(result, outputstr);
1483 /* outfuncoid refers to a cast function, not an output function */
1484 jsontext = DatumGetTextP(OidFunctionCall1(outfuncoid, val));
1485 outputstr = text_to_cstring(jsontext);
1486 appendStringInfoString(result, outputstr);
1491 outputstr = OidOutputFunctionCall(outfuncoid, val);
1492 escape_json(result, outputstr);
1499 * Process a single dimension of an array.
1500 * If it's the innermost dimension, output the values, otherwise call
1501 * ourselves recursively to process the next dimension.
1504 array_dim_to_json(StringInfo result, int dim, int ndims, int *dims, Datum *vals,
1505 bool *nulls, int *valcount, JsonTypeCategory tcategory,
1506 Oid outfuncoid, bool use_line_feeds)
1511 Assert(dim < ndims);
1513 sep = use_line_feeds ? ",\n " : ",";
1515 appendStringInfoChar(result, '[');
1517 for (i = 1; i <= dims[dim]; i++)
1520 appendStringInfoString(result, sep);
1522 if (dim + 1 == ndims)
1524 datum_to_json(vals[*valcount], nulls[*valcount], result, tcategory,
1531 * Do we want line feeds on inner dimensions of arrays? For now
1534 array_dim_to_json(result, dim + 1, ndims, dims, vals, nulls,
1535 valcount, tcategory, outfuncoid, false);
1539 appendStringInfoChar(result, ']');
1543 * Turn an array into JSON.
1546 array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
1548 ArrayType *v = DatumGetArrayTypeP(array);
1549 Oid element_type = ARR_ELEMTYPE(v);
1559 JsonTypeCategory tcategory;
1564 nitems = ArrayGetNItems(ndim, dim);
1568 appendStringInfoString(result, "[]");
1572 get_typlenbyvalalign(element_type,
1573 &typlen, &typbyval, &typalign);
1575 json_categorize_type(element_type,
1576 &tcategory, &outfuncoid);
1578 deconstruct_array(v, element_type, typlen, typbyval,
1579 typalign, &elements, &nulls,
1582 array_dim_to_json(result, 0, ndim, dim, elements, nulls, &count, tcategory,
1583 outfuncoid, use_line_feeds);
1590 * Turn a composite / record into JSON.
1593 composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
1599 HeapTupleData tmptup,
1602 bool needsep = false;
1605 sep = use_line_feeds ? ",\n " : ",";
1607 td = DatumGetHeapTupleHeader(composite);
1609 /* Extract rowtype info and find a tupdesc */
1610 tupType = HeapTupleHeaderGetTypeId(td);
1611 tupTypmod = HeapTupleHeaderGetTypMod(td);
1612 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
1614 /* Build a temporary HeapTuple control structure */
1615 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
1619 appendStringInfoChar(result, '{');
1621 for (i = 0; i < tupdesc->natts; i++)
1626 JsonTypeCategory tcategory;
1629 if (tupdesc->attrs[i]->attisdropped)
1633 appendStringInfoString(result, sep);
1636 attname = NameStr(tupdesc->attrs[i]->attname);
1637 escape_json(result, attname);
1638 appendStringInfoChar(result, ':');
1640 val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
1644 tcategory = JSONTYPE_NULL;
1645 outfuncoid = InvalidOid;
1648 json_categorize_type(tupdesc->attrs[i]->atttypid,
1649 &tcategory, &outfuncoid);
1651 datum_to_json(val, isnull, result, tcategory, outfuncoid, false);
1654 appendStringInfoChar(result, '}');
1655 ReleaseTupleDesc(tupdesc);
1659 * Append JSON text for "val" to "result".
1661 * This is just a thin wrapper around datum_to_json. If the same type will be
1662 * printed many times, avoid using this; better to do the json_categorize_type
1663 * lookups only once.
1666 add_json(Datum val, bool is_null, StringInfo result,
1667 Oid val_type, bool key_scalar)
1669 JsonTypeCategory tcategory;
1672 if (val_type == InvalidOid)
1674 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1675 errmsg("could not determine input data type")));
1679 tcategory = JSONTYPE_NULL;
1680 outfuncoid = InvalidOid;
1683 json_categorize_type(val_type,
1684 &tcategory, &outfuncoid);
1686 datum_to_json(val, is_null, result, tcategory, outfuncoid, key_scalar);
1690 * SQL function array_to_json(row)
1693 array_to_json(PG_FUNCTION_ARGS)
1695 Datum array = PG_GETARG_DATUM(0);
1698 result = makeStringInfo();
1700 array_to_json_internal(array, result, false);
1702 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1706 * SQL function array_to_json(row, prettybool)
1709 array_to_json_pretty(PG_FUNCTION_ARGS)
1711 Datum array = PG_GETARG_DATUM(0);
1712 bool use_line_feeds = PG_GETARG_BOOL(1);
1715 result = makeStringInfo();
1717 array_to_json_internal(array, result, use_line_feeds);
1719 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1723 * SQL function row_to_json(row)
1726 row_to_json(PG_FUNCTION_ARGS)
1728 Datum array = PG_GETARG_DATUM(0);
1731 result = makeStringInfo();
1733 composite_to_json(array, result, false);
1735 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1739 * SQL function row_to_json(row, prettybool)
1742 row_to_json_pretty(PG_FUNCTION_ARGS)
1744 Datum array = PG_GETARG_DATUM(0);
1745 bool use_line_feeds = PG_GETARG_BOOL(1);
1748 result = makeStringInfo();
1750 composite_to_json(array, result, use_line_feeds);
1752 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1756 * SQL function to_json(anyvalue)
1759 to_json(PG_FUNCTION_ARGS)
1761 Datum val = PG_GETARG_DATUM(0);
1762 Oid val_type = get_fn_expr_argtype(fcinfo->flinfo, 0);
1764 JsonTypeCategory tcategory;
1767 if (val_type == InvalidOid)
1769 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1770 errmsg("could not determine input data type")));
1772 json_categorize_type(val_type,
1773 &tcategory, &outfuncoid);
1775 result = makeStringInfo();
1777 datum_to_json(val, false, result, tcategory, outfuncoid, false);
1779 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1783 * json_agg transition function
1786 json_agg_transfn(PG_FUNCTION_ARGS)
1788 Oid val_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
1789 MemoryContext aggcontext,
1793 JsonTypeCategory tcategory;
1796 if (val_type == InvalidOid)
1798 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1799 errmsg("could not determine input data type")));
1801 if (!AggCheckCallContext(fcinfo, &aggcontext))
1803 /* cannot be called directly because of internal-type argument */
1804 elog(ERROR, "json_agg_transfn called in non-aggregate context");
1807 if (PG_ARGISNULL(0))
1810 * Make this StringInfo in a context where it will persist for the
1811 * duration of the aggregate call. MemoryContextSwitchTo is only
1812 * needed the first time, as the StringInfo routines make sure they
1813 * use the right context to enlarge the object if necessary.
1815 oldcontext = MemoryContextSwitchTo(aggcontext);
1816 state = makeStringInfo();
1817 MemoryContextSwitchTo(oldcontext);
1819 appendStringInfoChar(state, '[');
1823 state = (StringInfo) PG_GETARG_POINTER(0);
1824 appendStringInfoString(state, ", ");
1827 /* fast path for NULLs */
1828 if (PG_ARGISNULL(1))
1830 datum_to_json((Datum) 0, true, state, JSONTYPE_NULL, InvalidOid, false);
1831 PG_RETURN_POINTER(state);
1834 val = PG_GETARG_DATUM(1);
1836 /* XXX we do this every time?? */
1837 json_categorize_type(val_type,
1838 &tcategory, &outfuncoid);
1840 /* add some whitespace if structured type and not first item */
1841 if (!PG_ARGISNULL(0) &&
1842 (tcategory == JSONTYPE_ARRAY || tcategory == JSONTYPE_COMPOSITE))
1844 appendStringInfoString(state, "\n ");
1847 datum_to_json(val, false, state, tcategory, outfuncoid, false);
1850 * The transition type for array_agg() is declared to be "internal", which
1851 * is a pass-by-value type the same size as a pointer. So we can safely
1852 * pass the ArrayBuildState pointer through nodeAgg.c's machinations.
1854 PG_RETURN_POINTER(state);
1858 * json_agg final function
1861 json_agg_finalfn(PG_FUNCTION_ARGS)
1865 /* cannot be called directly because of internal-type argument */
1866 Assert(AggCheckCallContext(fcinfo, NULL));
1868 state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
1873 appendStringInfoChar(state, ']');
1875 PG_RETURN_TEXT_P(cstring_to_text_with_len(state->data, state->len));
1879 * json_object_agg transition function.
1881 * aggregate two input columns as a single json value.
1884 json_object_agg_transfn(PG_FUNCTION_ARGS)
1887 MemoryContext aggcontext,
1892 if (!AggCheckCallContext(fcinfo, &aggcontext))
1894 /* cannot be called directly because of internal-type argument */
1895 elog(ERROR, "json_agg_transfn called in non-aggregate context");
1898 if (PG_ARGISNULL(0))
1901 * Make the StringInfo in a context where it will persist for the
1902 * duration of the aggregate call. Switching context is only needed
1903 * for this initial step, as the StringInfo routines make sure they
1904 * use the right context to enlarge the object if necessary.
1906 oldcontext = MemoryContextSwitchTo(aggcontext);
1907 state = makeStringInfo();
1908 MemoryContextSwitchTo(oldcontext);
1910 appendStringInfoString(state, "{ ");
1914 state = (StringInfo) PG_GETARG_POINTER(0);
1915 appendStringInfoString(state, ", ");
1919 * Note: since json_object_agg() is declared as taking type "any", the
1920 * parser will not do any type conversion on unknown-type literals (that
1921 * is, undecorated strings or NULLs). Such values will arrive here as
1922 * type UNKNOWN, which fortunately does not matter to us, since
1923 * unknownout() works fine.
1925 val_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
1927 if (val_type == InvalidOid)
1929 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1930 errmsg("could not determine data type for argument %d", 1)));
1932 if (PG_ARGISNULL(1))
1934 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1935 errmsg("field name must not be null")));
1937 arg = PG_GETARG_DATUM(1);
1939 add_json(arg, false, state, val_type, true);
1941 appendStringInfoString(state, " : ");
1943 val_type = get_fn_expr_argtype(fcinfo->flinfo, 2);
1945 if (val_type == InvalidOid)
1947 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1948 errmsg("could not determine data type for argument %d", 2)));
1950 if (PG_ARGISNULL(2))
1953 arg = PG_GETARG_DATUM(2);
1955 add_json(arg, PG_ARGISNULL(2), state, val_type, false);
1957 PG_RETURN_POINTER(state);
1961 * json_object_agg final function.
1965 json_object_agg_finalfn(PG_FUNCTION_ARGS)
1969 /* cannot be called directly because of internal-type argument */
1970 Assert(AggCheckCallContext(fcinfo, NULL));
1972 state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
1977 appendStringInfoString(state, " }");
1979 PG_RETURN_TEXT_P(cstring_to_text_with_len(state->data, state->len));
1983 * SQL function json_build_object(variadic "any")
1986 json_build_object(PG_FUNCTION_ARGS)
1988 int nargs = PG_NARGS();
1991 const char *sep = "";
1997 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1998 errmsg("argument list must have even number of elements"),
1999 errhint("The arguments of json_build_object() must consist of alternating keys and values.")));
2001 result = makeStringInfo();
2003 appendStringInfoChar(result, '{');
2005 for (i = 0; i < nargs; i += 2)
2008 * Note: since json_build_object() is declared as taking type "any",
2009 * the parser will not do any type conversion on unknown-type literals
2010 * (that is, undecorated strings or NULLs). Such values will arrive
2011 * here as type UNKNOWN, which fortunately does not matter to us,
2012 * since unknownout() works fine.
2014 appendStringInfoString(result, sep);
2018 val_type = get_fn_expr_argtype(fcinfo->flinfo, i);
2020 if (val_type == InvalidOid)
2022 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2023 errmsg("could not determine data type for argument %d",
2026 if (PG_ARGISNULL(i))
2028 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2029 errmsg("argument %d cannot be null", i + 1),
2030 errhint("Object keys should be text.")));
2032 arg = PG_GETARG_DATUM(i);
2034 add_json(arg, false, result, val_type, true);
2036 appendStringInfoString(result, " : ");
2039 val_type = get_fn_expr_argtype(fcinfo->flinfo, i + 1);
2041 if (val_type == InvalidOid)
2043 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2044 errmsg("could not determine data type for argument %d",
2047 if (PG_ARGISNULL(i + 1))
2050 arg = PG_GETARG_DATUM(i + 1);
2052 add_json(arg, PG_ARGISNULL(i + 1), result, val_type, false);
2055 appendStringInfoChar(result, '}');
2057 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
2061 * degenerate case of json_build_object where it gets 0 arguments.
2064 json_build_object_noargs(PG_FUNCTION_ARGS)
2066 PG_RETURN_TEXT_P(cstring_to_text_with_len("{}", 2));
2070 * SQL function json_build_array(variadic "any")
2073 json_build_array(PG_FUNCTION_ARGS)
2075 int nargs = PG_NARGS();
2078 const char *sep = "";
2082 result = makeStringInfo();
2084 appendStringInfoChar(result, '[');
2086 for (i = 0; i < nargs; i++)
2089 * Note: since json_build_array() is declared as taking type "any",
2090 * the parser will not do any type conversion on unknown-type literals
2091 * (that is, undecorated strings or NULLs). Such values will arrive
2092 * here as type UNKNOWN, which fortunately does not matter to us,
2093 * since unknownout() works fine.
2095 appendStringInfoString(result, sep);
2098 val_type = get_fn_expr_argtype(fcinfo->flinfo, i);
2100 if (val_type == InvalidOid)
2102 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2103 errmsg("could not determine data type for argument %d",
2106 if (PG_ARGISNULL(i))
2109 arg = PG_GETARG_DATUM(i);
2111 add_json(arg, PG_ARGISNULL(i), result, val_type, false);
2114 appendStringInfoChar(result, ']');
2116 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
2120 * degenerate case of json_build_array where it gets 0 arguments.
2123 json_build_array_noargs(PG_FUNCTION_ARGS)
2125 PG_RETURN_TEXT_P(cstring_to_text_with_len("[]", 2));
2129 * SQL function json_object(text[])
2131 * take a one or two dimensional array of text as key/value pairs
2132 * for a json object.
2135 json_object(PG_FUNCTION_ARGS)
2137 ArrayType *in_array = PG_GETARG_ARRAYTYPE_P(0);
2138 int ndims = ARR_NDIM(in_array);
2139 StringInfoData result;
2151 PG_RETURN_DATUM(CStringGetTextDatum("{}"));
2155 if ((ARR_DIMS(in_array)[0]) % 2)
2157 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2158 errmsg("array must have even number of elements")));
2162 if ((ARR_DIMS(in_array)[1]) != 2)
2164 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2165 errmsg("array must have two columns")));
2170 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2171 errmsg("wrong number of array subscripts")));
2174 deconstruct_array(in_array,
2175 TEXTOID, -1, false, 'i',
2176 &in_datums, &in_nulls, &in_count);
2178 count = in_count / 2;
2180 initStringInfo(&result);
2182 appendStringInfoChar(&result, '{');
2184 for (i = 0; i < count; ++i)
2186 if (in_nulls[i * 2])
2188 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2189 errmsg("null value not allowed for object key")));
2191 v = TextDatumGetCString(in_datums[i * 2]);
2193 appendStringInfoString(&result, ", ");
2194 escape_json(&result, v);
2195 appendStringInfoString(&result, " : ");
2197 if (in_nulls[i * 2 + 1])
2198 appendStringInfoString(&result, "null");
2201 v = TextDatumGetCString(in_datums[i * 2 + 1]);
2202 escape_json(&result, v);
2207 appendStringInfoChar(&result, '}');
2212 rval = cstring_to_text_with_len(result.data, result.len);
2215 PG_RETURN_TEXT_P(rval);
2220 * SQL function json_object(text[], text[])
2222 * take separate key and value arrays of text to construct a json object
2226 json_object_two_arg(PG_FUNCTION_ARGS)
2228 ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(0);
2229 ArrayType *val_array = PG_GETARG_ARRAYTYPE_P(1);
2230 int nkdims = ARR_NDIM(key_array);
2231 int nvdims = ARR_NDIM(val_array);
2232 StringInfoData result;
2243 if (nkdims > 1 || nkdims != nvdims)
2245 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2246 errmsg("wrong number of array subscripts")));
2249 PG_RETURN_DATUM(CStringGetTextDatum("{}"));
2251 deconstruct_array(key_array,
2252 TEXTOID, -1, false, 'i',
2253 &key_datums, &key_nulls, &key_count);
2255 deconstruct_array(val_array,
2256 TEXTOID, -1, false, 'i',
2257 &val_datums, &val_nulls, &val_count);
2259 if (key_count != val_count)
2261 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2262 errmsg("mismatched array dimensions")));
2264 initStringInfo(&result);
2266 appendStringInfoChar(&result, '{');
2268 for (i = 0; i < key_count; ++i)
2272 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2273 errmsg("null value not allowed for object key")));
2275 v = TextDatumGetCString(key_datums[i]);
2277 appendStringInfoString(&result, ", ");
2278 escape_json(&result, v);
2279 appendStringInfoString(&result, " : ");
2282 appendStringInfoString(&result, "null");
2285 v = TextDatumGetCString(val_datums[i]);
2286 escape_json(&result, v);
2291 appendStringInfoChar(&result, '}');
2298 rval = cstring_to_text_with_len(result.data, result.len);
2301 PG_RETURN_TEXT_P(rval);
2306 * Produce a JSON string literal, properly escaping characters in the text.
2309 escape_json(StringInfo buf, const char *str)
2313 appendStringInfoCharMacro(buf, '\"');
2314 for (p = str; *p; p++)
2319 appendStringInfoString(buf, "\\b");
2322 appendStringInfoString(buf, "\\f");
2325 appendStringInfoString(buf, "\\n");
2328 appendStringInfoString(buf, "\\r");
2331 appendStringInfoString(buf, "\\t");
2334 appendStringInfoString(buf, "\\\"");
2339 * Unicode escapes are passed through as is. There is no
2340 * requirement that they denote a valid character in the
2341 * server encoding - indeed that is a big part of their
2344 * All we require is that they consist of \uXXXX where the Xs
2345 * are hexadecimal digits. It is the responsibility of the
2346 * caller of, say, to_json() to make sure that the unicode
2349 * In the case of a jsonb string value being escaped, the only
2350 * unicode escape that should be present is \u0000, all the
2351 * other unicode escapes will have been resolved.
2354 isxdigit((unsigned char) p[2]) &&
2355 isxdigit((unsigned char) p[3]) &&
2356 isxdigit((unsigned char) p[4]) &&
2357 isxdigit((unsigned char) p[5]))
2358 appendStringInfoCharMacro(buf, *p);
2360 appendStringInfoString(buf, "\\\\");
2363 if ((unsigned char) *p < ' ')
2364 appendStringInfo(buf, "\\u%04x", (int) *p);
2366 appendStringInfoCharMacro(buf, *p);
2370 appendStringInfoCharMacro(buf, '\"');
2374 * SQL function json_typeof(json) -> text
2376 * Returns the type of the outermost JSON value as TEXT. Possible types are
2377 * "object", "array", "string", "number", "boolean", and "null".
2379 * Performs a single call to json_lex() to get the first token of the supplied
2380 * value. This initial token uniquely determines the value's type. As our
2381 * input must already have been validated by json_in() or json_recv(), the
2382 * initial token should never be JSON_TOKEN_OBJECT_END, JSON_TOKEN_ARRAY_END,
2383 * JSON_TOKEN_COLON, JSON_TOKEN_COMMA, or JSON_TOKEN_END.
2386 json_typeof(PG_FUNCTION_ARGS)
2390 JsonLexContext *lex;
2394 json = PG_GETARG_TEXT_P(0);
2395 lex = makeJsonLexContext(json, false);
2397 /* Lex exactly one token from the input and check its type. */
2399 tok = lex_peek(lex);
2402 case JSON_TOKEN_OBJECT_START:
2405 case JSON_TOKEN_ARRAY_START:
2408 case JSON_TOKEN_STRING:
2411 case JSON_TOKEN_NUMBER:
2414 case JSON_TOKEN_TRUE:
2415 case JSON_TOKEN_FALSE:
2418 case JSON_TOKEN_NULL:
2422 elog(ERROR, "unexpected json token: %d", tok);
2425 PG_RETURN_TEXT_P(cstring_to_text(type));