1 /*-------------------------------------------------------------------------
4 * JSON data type support.
6 * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/backend/utils/adt/json.c
12 *-------------------------------------------------------------------------
16 #include "access/htup_details.h"
17 #include "access/transam.h"
18 #include "catalog/pg_type.h"
19 #include "executor/spi.h"
20 #include "lib/stringinfo.h"
21 #include "libpq/pqformat.h"
22 #include "mb/pg_wchar.h"
23 #include "miscadmin.h"
24 #include "parser/parse_coerce.h"
25 #include "utils/array.h"
26 #include "utils/builtins.h"
27 #include "utils/date.h"
28 #include "utils/datetime.h"
29 #include "utils/lsyscache.h"
30 #include "utils/json.h"
31 #include "utils/jsonapi.h"
32 #include "utils/typcache.h"
33 #include "utils/syscache.h"
36 * The context of the parser is maintained by the recursive descent
37 * mechanism, but is passed explicitly to the error reporting routine
38 * for better diagnostics.
40 typedef enum /* contexts of JSON parser */
42 JSON_PARSE_VALUE, /* expecting a value */
43 JSON_PARSE_STRING, /* expecting a string (for a field name) */
44 JSON_PARSE_ARRAY_START, /* saw '[', expecting value or ']' */
45 JSON_PARSE_ARRAY_NEXT, /* saw array element, expecting ',' or ']' */
46 JSON_PARSE_OBJECT_START, /* saw '{', expecting label or '}' */
47 JSON_PARSE_OBJECT_LABEL, /* saw object label, expecting ':' */
48 JSON_PARSE_OBJECT_NEXT, /* saw object value, expecting ',' or '}' */
49 JSON_PARSE_OBJECT_COMMA, /* saw object ',', expecting next label */
50 JSON_PARSE_END /* saw the end of a document, expect nothing */
53 typedef enum /* type categories for datum_to_json */
55 JSONTYPE_NULL, /* null, so we didn't bother to identify */
56 JSONTYPE_BOOL, /* boolean (built-in types only) */
57 JSONTYPE_NUMERIC, /* numeric (ditto) */
58 JSONTYPE_DATE, /* we use special formatting for datetimes */
61 JSONTYPE_JSON, /* JSON itself (and JSONB) */
62 JSONTYPE_ARRAY, /* array */
63 JSONTYPE_COMPOSITE, /* composite */
64 JSONTYPE_CAST, /* something with an explicit cast to JSON */
65 JSONTYPE_OTHER /* all else */
68 static inline void json_lex(JsonLexContext *lex);
69 static inline void json_lex_string(JsonLexContext *lex);
70 static inline void json_lex_number(JsonLexContext *lex, char *s, bool *num_err);
71 static inline void parse_scalar(JsonLexContext *lex, JsonSemAction *sem);
72 static void parse_object_field(JsonLexContext *lex, JsonSemAction *sem);
73 static void parse_object(JsonLexContext *lex, JsonSemAction *sem);
74 static void parse_array_element(JsonLexContext *lex, JsonSemAction *sem);
75 static void parse_array(JsonLexContext *lex, JsonSemAction *sem);
76 static void report_parse_error(JsonParseContext ctx, JsonLexContext *lex);
77 static void report_invalid_token(JsonLexContext *lex);
78 static int report_json_context(JsonLexContext *lex);
79 static char *extract_mb_char(char *s);
80 static void composite_to_json(Datum composite, StringInfo result,
82 static void array_dim_to_json(StringInfo result, int dim, int ndims, int *dims,
83 Datum *vals, bool *nulls, int *valcount,
84 JsonTypeCategory tcategory, Oid outfuncoid,
86 static void array_to_json_internal(Datum array, StringInfo result,
88 static void json_categorize_type(Oid typoid,
89 JsonTypeCategory *tcategory,
91 static void datum_to_json(Datum val, bool is_null, StringInfo result,
92 JsonTypeCategory tcategory, Oid outfuncoid,
94 static void add_json(Datum val, bool is_null, StringInfo result,
95 Oid val_type, bool key_scalar);
96 static text *catenate_stringinfo_string(StringInfo buffer, const char *addon);
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') || \
176 /* utility function to check if a string is a valid JSON number */
178 IsValidJsonNumber(const char *str, int len)
181 JsonLexContext dummy_lex;
185 * json_lex_number expects a leading '-' to have been eaten already.
187 * having to cast away the constness of str is ugly, but there's not much
192 dummy_lex.input = (char *) str + 1;
193 dummy_lex.input_length = len - 1;
197 dummy_lex.input = (char *) str;
198 dummy_lex.input_length = len;
201 json_lex_number(&dummy_lex, dummy_lex.input, &numeric_error);
203 return !numeric_error;
210 json_in(PG_FUNCTION_ARGS)
212 char *json = PG_GETARG_CSTRING(0);
213 text *result = cstring_to_text(json);
217 lex = makeJsonLexContext(result, false);
218 pg_parse_json(lex, &nullSemAction);
220 /* Internal representation is the same as text, for now */
221 PG_RETURN_TEXT_P(result);
228 json_out(PG_FUNCTION_ARGS)
230 /* we needn't detoast because text_to_cstring will handle that */
231 Datum txt = PG_GETARG_DATUM(0);
233 PG_RETURN_CSTRING(TextDatumGetCString(txt));
240 json_send(PG_FUNCTION_ARGS)
242 text *t = PG_GETARG_TEXT_PP(0);
245 pq_begintypsend(&buf);
246 pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
247 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
254 json_recv(PG_FUNCTION_ARGS)
256 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
261 str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
264 lex = makeJsonLexContextCstringLen(str, nbytes, false);
265 pg_parse_json(lex, &nullSemAction);
267 PG_RETURN_TEXT_P(cstring_to_text_with_len(str, nbytes));
273 * lex constructor, with or without StringInfo object
274 * for de-escaped lexemes.
276 * Without is better as it makes the processing faster, so only make one
277 * if really required.
279 * If you already have the json as a text* value, use the first of these
280 * functions, otherwise use makeJsonLexContextCstringLen().
283 makeJsonLexContext(text *json, bool need_escapes)
285 return makeJsonLexContextCstringLen(VARDATA(json),
286 VARSIZE(json) - VARHDRSZ,
291 makeJsonLexContextCstringLen(char *json, int len, bool need_escapes)
293 JsonLexContext *lex = palloc0(sizeof(JsonLexContext));
295 lex->input = lex->token_terminator = lex->line_start = json;
296 lex->line_number = 1;
297 lex->input_length = len;
299 lex->strval = makeStringInfo();
306 * Publicly visible entry point for the JSON parser.
308 * lex is a lexing context, set up for the json to be processed by calling
309 * makeJsonLexContext(). sem is a strucure of function pointers to semantic
310 * action routines to be called at appropriate spots during parsing, and a
311 * pointer to a state object to be passed to those routines.
314 pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
318 /* get the initial token */
323 /* parse by recursive descent */
326 case JSON_TOKEN_OBJECT_START:
327 parse_object(lex, sem);
329 case JSON_TOKEN_ARRAY_START:
330 parse_array(lex, sem);
333 parse_scalar(lex, sem); /* json can be a bare scalar */
336 lex_expect(JSON_PARSE_END, lex, JSON_TOKEN_END);
341 * Recursive Descent parse routines. There is one for each structural
342 * element in a json document:
343 * - scalar (string, number, true, false, null)
350 parse_scalar(JsonLexContext *lex, JsonSemAction *sem)
353 json_scalar_action sfunc = sem->scalar;
355 JsonTokenType tok = lex_peek(lex);
357 valaddr = sfunc == NULL ? NULL : &val;
359 /* a scalar must be a string, a number, true, false, or null */
362 case JSON_TOKEN_TRUE:
363 lex_accept(lex, JSON_TOKEN_TRUE, valaddr);
365 case JSON_TOKEN_FALSE:
366 lex_accept(lex, JSON_TOKEN_FALSE, valaddr);
368 case JSON_TOKEN_NULL:
369 lex_accept(lex, JSON_TOKEN_NULL, valaddr);
371 case JSON_TOKEN_NUMBER:
372 lex_accept(lex, JSON_TOKEN_NUMBER, valaddr);
374 case JSON_TOKEN_STRING:
375 lex_accept(lex, JSON_TOKEN_STRING, valaddr);
378 report_parse_error(JSON_PARSE_VALUE, lex);
382 (*sfunc) (sem->semstate, val, tok);
386 parse_object_field(JsonLexContext *lex, JsonSemAction *sem)
389 * An object field is "fieldname" : value where value can be a scalar,
390 * object or array. Note: in user-facing docs and error messages, we
391 * generally call a field name a "key".
394 char *fname = NULL; /* keep compiler quiet */
395 json_ofield_action ostart = sem->object_field_start;
396 json_ofield_action oend = sem->object_field_end;
398 char **fnameaddr = NULL;
401 if (ostart != NULL || oend != NULL)
404 if (!lex_accept(lex, JSON_TOKEN_STRING, fnameaddr))
405 report_parse_error(JSON_PARSE_STRING, lex);
407 lex_expect(JSON_PARSE_OBJECT_LABEL, lex, JSON_TOKEN_COLON);
410 isnull = tok == JSON_TOKEN_NULL;
413 (*ostart) (sem->semstate, fname, isnull);
417 case JSON_TOKEN_OBJECT_START:
418 parse_object(lex, sem);
420 case JSON_TOKEN_ARRAY_START:
421 parse_array(lex, sem);
424 parse_scalar(lex, sem);
428 (*oend) (sem->semstate, fname, isnull);
432 parse_object(JsonLexContext *lex, JsonSemAction *sem)
435 * an object is a possibly empty sequence of object fields, separated by
436 * commas and surrounded by curly braces.
438 json_struct_action ostart = sem->object_start;
439 json_struct_action oend = sem->object_end;
443 (*ostart) (sem->semstate);
446 * Data inside an object is at a higher nesting level than the object
447 * itself. Note that we increment this after we call the semantic routine
448 * for the object start and restore it before we call the routine for the
453 /* we know this will succeeed, just clearing the token */
454 lex_expect(JSON_PARSE_OBJECT_START, lex, JSON_TOKEN_OBJECT_START);
459 case JSON_TOKEN_STRING:
460 parse_object_field(lex, sem);
461 while (lex_accept(lex, JSON_TOKEN_COMMA, NULL))
462 parse_object_field(lex, sem);
464 case JSON_TOKEN_OBJECT_END:
467 /* case of an invalid initial token inside the object */
468 report_parse_error(JSON_PARSE_OBJECT_START, lex);
471 lex_expect(JSON_PARSE_OBJECT_NEXT, lex, JSON_TOKEN_OBJECT_END);
476 (*oend) (sem->semstate);
480 parse_array_element(JsonLexContext *lex, JsonSemAction *sem)
482 json_aelem_action astart = sem->array_element_start;
483 json_aelem_action aend = sem->array_element_end;
484 JsonTokenType tok = lex_peek(lex);
488 isnull = tok == JSON_TOKEN_NULL;
491 (*astart) (sem->semstate, isnull);
493 /* an array element is any object, array or scalar */
496 case JSON_TOKEN_OBJECT_START:
497 parse_object(lex, sem);
499 case JSON_TOKEN_ARRAY_START:
500 parse_array(lex, sem);
503 parse_scalar(lex, sem);
507 (*aend) (sem->semstate, isnull);
511 parse_array(JsonLexContext *lex, JsonSemAction *sem)
514 * an array is a possibly empty sequence of array elements, separated by
515 * commas and surrounded by square brackets.
517 json_struct_action astart = sem->array_start;
518 json_struct_action aend = sem->array_end;
521 (*astart) (sem->semstate);
524 * Data inside an array is at a higher nesting level than the array
525 * itself. Note that we increment this after we call the semantic routine
526 * for the array start and restore it before we call the routine for the
531 lex_expect(JSON_PARSE_ARRAY_START, lex, JSON_TOKEN_ARRAY_START);
532 if (lex_peek(lex) != JSON_TOKEN_ARRAY_END)
535 parse_array_element(lex, sem);
537 while (lex_accept(lex, JSON_TOKEN_COMMA, NULL))
538 parse_array_element(lex, sem);
541 lex_expect(JSON_PARSE_ARRAY_NEXT, lex, JSON_TOKEN_ARRAY_END);
546 (*aend) (sem->semstate);
550 * Lex one token from the input stream.
553 json_lex(JsonLexContext *lex)
558 /* Skip leading whitespace. */
559 s = lex->token_terminator;
560 len = s - lex->input;
561 while (len < lex->input_length &&
562 (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'))
569 lex->token_start = s;
571 /* Determine token type. */
572 if (len >= lex->input_length)
574 lex->token_start = NULL;
575 lex->prev_token_terminator = lex->token_terminator;
576 lex->token_terminator = s;
577 lex->token_type = JSON_TOKEN_END;
582 /* Single-character token, some kind of punctuation mark. */
584 lex->prev_token_terminator = lex->token_terminator;
585 lex->token_terminator = s + 1;
586 lex->token_type = JSON_TOKEN_OBJECT_START;
589 lex->prev_token_terminator = lex->token_terminator;
590 lex->token_terminator = s + 1;
591 lex->token_type = JSON_TOKEN_OBJECT_END;
594 lex->prev_token_terminator = lex->token_terminator;
595 lex->token_terminator = s + 1;
596 lex->token_type = JSON_TOKEN_ARRAY_START;
599 lex->prev_token_terminator = lex->token_terminator;
600 lex->token_terminator = s + 1;
601 lex->token_type = JSON_TOKEN_ARRAY_END;
604 lex->prev_token_terminator = lex->token_terminator;
605 lex->token_terminator = s + 1;
606 lex->token_type = JSON_TOKEN_COMMA;
609 lex->prev_token_terminator = lex->token_terminator;
610 lex->token_terminator = s + 1;
611 lex->token_type = JSON_TOKEN_COLON;
615 json_lex_string(lex);
616 lex->token_type = JSON_TOKEN_STRING;
619 /* Negative number. */
620 json_lex_number(lex, s + 1, NULL);
621 lex->token_type = JSON_TOKEN_NUMBER;
633 /* Positive number. */
634 json_lex_number(lex, s, NULL);
635 lex->token_type = JSON_TOKEN_NUMBER;
642 * We're not dealing with a string, number, legal
643 * punctuation mark, or end of string. The only legal
644 * tokens we might find here are true, false, and null,
645 * but for error reporting purposes we scan until we see a
646 * non-alphanumeric character. That way, we can report
647 * the whole word as an unexpected token, rather than just
648 * some unintuitive prefix thereof.
650 for (p = s; p - s < lex->input_length - len && JSON_ALPHANUMERIC_CHAR(*p); p++)
654 * We got some sort of unexpected punctuation or an
655 * otherwise unexpected character, so just complain about
656 * that one character.
660 lex->prev_token_terminator = lex->token_terminator;
661 lex->token_terminator = s + 1;
662 report_invalid_token(lex);
666 * We've got a real alphanumeric token here. If it
667 * happens to be true, false, or null, all is well. If
670 lex->prev_token_terminator = lex->token_terminator;
671 lex->token_terminator = p;
674 if (memcmp(s, "true", 4) == 0)
675 lex->token_type = JSON_TOKEN_TRUE;
676 else if (memcmp(s, "null", 4) == 0)
677 lex->token_type = JSON_TOKEN_NULL;
679 report_invalid_token(lex);
681 else if (p - s == 5 && memcmp(s, "false", 5) == 0)
682 lex->token_type = JSON_TOKEN_FALSE;
684 report_invalid_token(lex);
687 } /* end of switch */
691 * The next token in the input stream is known to be a string; lex it.
694 json_lex_string(JsonLexContext *lex)
698 int hi_surrogate = -1;
700 if (lex->strval != NULL)
701 resetStringInfo(lex->strval);
703 Assert(lex->input_length > 0);
704 s = lex->token_start;
705 len = lex->token_start - lex->input;
710 /* Premature end of the string. */
711 if (len >= lex->input_length)
713 lex->token_terminator = s;
714 report_invalid_token(lex);
718 else if ((unsigned char) *s < 32)
720 /* Per RFC4627, these characters MUST be escaped. */
721 /* Since *s isn't printable, exclude it from the context string */
722 lex->token_terminator = s;
724 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
725 errmsg("invalid input syntax for type json"),
726 errdetail("Character with value 0x%02x must be escaped.",
728 report_json_context(lex)));
732 /* OK, we have an escape character. */
735 if (len >= lex->input_length)
737 lex->token_terminator = s;
738 report_invalid_token(lex);
745 for (i = 1; i <= 4; i++)
749 if (len >= lex->input_length)
751 lex->token_terminator = s;
752 report_invalid_token(lex);
754 else if (*s >= '0' && *s <= '9')
755 ch = (ch * 16) + (*s - '0');
756 else if (*s >= 'a' && *s <= 'f')
757 ch = (ch * 16) + (*s - 'a') + 10;
758 else if (*s >= 'A' && *s <= 'F')
759 ch = (ch * 16) + (*s - 'A') + 10;
762 lex->token_terminator = s + pg_mblen(s);
764 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
765 errmsg("invalid input syntax for type json"),
766 errdetail("\"\\u\" must be followed by four hexadecimal digits."),
767 report_json_context(lex)));
770 if (lex->strval != NULL)
775 if (ch >= 0xd800 && ch <= 0xdbff)
777 if (hi_surrogate != -1)
779 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
780 errmsg("invalid input syntax for type json"),
781 errdetail("Unicode high surrogate must not follow a high surrogate."),
782 report_json_context(lex)));
783 hi_surrogate = (ch & 0x3ff) << 10;
786 else if (ch >= 0xdc00 && ch <= 0xdfff)
788 if (hi_surrogate == -1)
790 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
791 errmsg("invalid input syntax for type json"),
792 errdetail("Unicode low surrogate must follow a high surrogate."),
793 report_json_context(lex)));
794 ch = 0x10000 + hi_surrogate + (ch & 0x3ff);
798 if (hi_surrogate != -1)
800 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
801 errmsg("invalid input syntax for type json"),
802 errdetail("Unicode low surrogate must follow a high surrogate."),
803 report_json_context(lex)));
806 * For UTF8, replace the escape sequence by the actual
807 * utf8 character in lex->strval. Do this also for other
808 * encodings if the escape designates an ASCII character,
809 * otherwise raise an error.
814 /* We can't allow this, since our TEXT type doesn't */
816 (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
817 errmsg("unsupported Unicode escape sequence"),
818 errdetail("\\u0000 cannot be converted to text."),
819 report_json_context(lex)));
821 else if (GetDatabaseEncoding() == PG_UTF8)
823 unicode_to_utf8(ch, (unsigned char *) utf8str);
824 utf8len = pg_utf_mblen((unsigned char *) utf8str);
825 appendBinaryStringInfo(lex->strval, utf8str, utf8len);
827 else if (ch <= 0x007f)
830 * This is the only way to designate things like a
831 * form feed character in JSON, so it's useful in all
834 appendStringInfoChar(lex->strval, (char) ch);
839 (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
840 errmsg("unsupported Unicode escape sequence"),
841 errdetail("Unicode escape values cannot be used for code point values above 007F when the server encoding is not UTF8."),
842 report_json_context(lex)));
847 else if (lex->strval != NULL)
849 if (hi_surrogate != -1)
851 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
852 errmsg("invalid input syntax for type json"),
853 errdetail("Unicode low surrogate must follow a high surrogate."),
854 report_json_context(lex)));
861 appendStringInfoChar(lex->strval, *s);
864 appendStringInfoChar(lex->strval, '\b');
867 appendStringInfoChar(lex->strval, '\f');
870 appendStringInfoChar(lex->strval, '\n');
873 appendStringInfoChar(lex->strval, '\r');
876 appendStringInfoChar(lex->strval, '\t');
879 /* Not a valid string escape, so error out. */
880 lex->token_terminator = s + pg_mblen(s);
882 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
883 errmsg("invalid input syntax for type json"),
884 errdetail("Escape sequence \"\\%s\" is invalid.",
886 report_json_context(lex)));
889 else if (strchr("\"\\/bfnrt", *s) == NULL)
892 * Simpler processing if we're not bothered about de-escaping
894 * It's very tempting to remove the strchr() call here and
895 * replace it with a switch statement, but testing so far has
896 * shown it's not a performance win.
898 lex->token_terminator = s + pg_mblen(s);
900 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
901 errmsg("invalid input syntax for type json"),
902 errdetail("Escape sequence \"\\%s\" is invalid.",
904 report_json_context(lex)));
908 else if (lex->strval != NULL)
910 if (hi_surrogate != -1)
912 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
913 errmsg("invalid input syntax for type json"),
914 errdetail("Unicode low surrogate must follow a high surrogate."),
915 report_json_context(lex)));
917 appendStringInfoChar(lex->strval, *s);
922 if (hi_surrogate != -1)
924 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
925 errmsg("invalid input syntax for type json"),
926 errdetail("Unicode low surrogate must follow a high surrogate."),
927 report_json_context(lex)));
929 /* Hooray, we found the end of the string! */
930 lex->prev_token_terminator = lex->token_terminator;
931 lex->token_terminator = s + 1;
934 /*-------------------------------------------------------------------------
935 * The next token in the input stream is known to be a number; lex it.
937 * In JSON, a number consists of four parts:
939 * (1) An optional minus sign ('-').
941 * (2) Either a single '0', or a string of one or more digits that does not
944 * (3) An optional decimal part, consisting of a period ('.') followed by
945 * one or more digits. (Note: While this part can be omitted
946 * completely, it's not OK to have only the decimal point without
947 * any digits afterwards.)
949 * (4) An optional exponent part, consisting of 'e' or 'E', optionally
950 * followed by '+' or '-', followed by one or more digits. (Note:
951 * As with the decimal part, if 'e' or 'E' is present, it must be
952 * followed by at least one digit.)
954 * The 's' argument to this function points to the ostensible beginning
955 * of part 2 - i.e. the character after any optional minus sign, and the
956 * first character of the string if there is none.
958 *-------------------------------------------------------------------------
961 json_lex_number(JsonLexContext *lex, char *s, bool *num_err)
967 len = s - lex->input;
968 /* Part (1): leading sign indicator. */
969 /* Caller already did this for us; so do nothing. */
971 /* Part (2): parse main digit string. */
977 else if (*s >= '1' && *s <= '9')
983 } while (len < lex->input_length && *s >= '0' && *s <= '9');
988 /* Part (3): parse optional decimal portion. */
989 if (len < lex->input_length && *s == '.')
993 if (len == lex->input_length || *s < '0' || *s > '9')
1001 } while (len < lex->input_length && *s >= '0' && *s <= '9');
1005 /* Part (4): parse optional exponent. */
1006 if (len < lex->input_length && (*s == 'e' || *s == 'E'))
1010 if (len < lex->input_length && (*s == '+' || *s == '-'))
1015 if (len == lex->input_length || *s < '0' || *s > '9')
1023 } while (len < lex->input_length && *s >= '0' && *s <= '9');
1028 * Check for trailing garbage. As in json_lex(), any alphanumeric stuff
1029 * here should be considered part of the token for error-reporting
1032 for (p = s; len < lex->input_length && JSON_ALPHANUMERIC_CHAR(*p); p++, len++)
1035 if (num_err != NULL)
1037 /* let the caller handle the error */
1042 lex->prev_token_terminator = lex->token_terminator;
1043 lex->token_terminator = p;
1045 report_invalid_token(lex);
1050 * Report a parse error.
1052 * lex->token_start and lex->token_terminator must identify the current token.
1055 report_parse_error(JsonParseContext ctx, JsonLexContext *lex)
1060 /* Handle case where the input ended prematurely. */
1061 if (lex->token_start == NULL || lex->token_type == JSON_TOKEN_END)
1063 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1064 errmsg("invalid input syntax for type json"),
1065 errdetail("The input string ended unexpectedly."),
1066 report_json_context(lex)));
1068 /* Separate out the current token. */
1069 toklen = lex->token_terminator - lex->token_start;
1070 token = palloc(toklen + 1);
1071 memcpy(token, lex->token_start, toklen);
1072 token[toklen] = '\0';
1074 /* Complain, with the appropriate detail message. */
1075 if (ctx == JSON_PARSE_END)
1077 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1078 errmsg("invalid input syntax for type json"),
1079 errdetail("Expected end of input, but found \"%s\".",
1081 report_json_context(lex)));
1086 case JSON_PARSE_VALUE:
1088 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1089 errmsg("invalid input syntax for type json"),
1090 errdetail("Expected JSON value, but found \"%s\".",
1092 report_json_context(lex)));
1094 case JSON_PARSE_STRING:
1096 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1097 errmsg("invalid input syntax for type json"),
1098 errdetail("Expected string, but found \"%s\".",
1100 report_json_context(lex)));
1102 case JSON_PARSE_ARRAY_START:
1104 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1105 errmsg("invalid input syntax for type json"),
1106 errdetail("Expected array element or \"]\", but found \"%s\".",
1108 report_json_context(lex)));
1110 case JSON_PARSE_ARRAY_NEXT:
1112 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1113 errmsg("invalid input syntax for type json"),
1114 errdetail("Expected \",\" or \"]\", but found \"%s\".",
1116 report_json_context(lex)));
1118 case JSON_PARSE_OBJECT_START:
1120 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1121 errmsg("invalid input syntax for type json"),
1122 errdetail("Expected string or \"}\", but found \"%s\".",
1124 report_json_context(lex)));
1126 case JSON_PARSE_OBJECT_LABEL:
1128 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1129 errmsg("invalid input syntax for type json"),
1130 errdetail("Expected \":\", but found \"%s\".",
1132 report_json_context(lex)));
1134 case JSON_PARSE_OBJECT_NEXT:
1136 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1137 errmsg("invalid input syntax for type json"),
1138 errdetail("Expected \",\" or \"}\", but found \"%s\".",
1140 report_json_context(lex)));
1142 case JSON_PARSE_OBJECT_COMMA:
1144 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1145 errmsg("invalid input syntax for type json"),
1146 errdetail("Expected string, but found \"%s\".",
1148 report_json_context(lex)));
1151 elog(ERROR, "unexpected json parse state: %d", ctx);
1157 * Report an invalid input token.
1159 * lex->token_start and lex->token_terminator must identify the token.
1162 report_invalid_token(JsonLexContext *lex)
1167 /* Separate out the offending token. */
1168 toklen = lex->token_terminator - lex->token_start;
1169 token = palloc(toklen + 1);
1170 memcpy(token, lex->token_start, toklen);
1171 token[toklen] = '\0';
1174 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1175 errmsg("invalid input syntax for type json"),
1176 errdetail("Token \"%s\" is invalid.", token),
1177 report_json_context(lex)));
1181 * Report a CONTEXT line for bogus JSON input.
1183 * lex->token_terminator must be set to identify the spot where we detected
1184 * the error. Note that lex->token_start might be NULL, in case we recognized
1187 * The return value isn't meaningful, but we make it non-void so that this
1188 * can be invoked inside ereport().
1191 report_json_context(JsonLexContext *lex)
1193 const char *context_start;
1194 const char *context_end;
1195 const char *line_start;
1202 /* Choose boundaries for the part of the input we will display */
1203 context_start = lex->input;
1204 context_end = lex->token_terminator;
1205 line_start = context_start;
1209 /* Always advance over newlines */
1210 if (context_start < context_end && *context_start == '\n')
1213 line_start = context_start;
1217 /* Otherwise, done as soon as we are close enough to context_end */
1218 if (context_end - context_start < 50)
1220 /* Advance to next multibyte character */
1221 if (IS_HIGHBIT_SET(*context_start))
1222 context_start += pg_mblen(context_start);
1228 * We add "..." to indicate that the excerpt doesn't start at the
1229 * beginning of the line ... but if we're within 3 characters of the
1230 * beginning of the line, we might as well just show the whole line.
1232 if (context_start - line_start <= 3)
1233 context_start = line_start;
1235 /* Get a null-terminated copy of the data to present */
1236 ctxtlen = context_end - context_start;
1237 ctxt = palloc(ctxtlen + 1);
1238 memcpy(ctxt, context_start, ctxtlen);
1239 ctxt[ctxtlen] = '\0';
1242 * Show the context, prefixing "..." if not starting at start of line, and
1243 * suffixing "..." if not ending at end of line.
1245 prefix = (context_start > line_start) ? "..." : "";
1246 suffix = (lex->token_type != JSON_TOKEN_END && context_end - lex->input < lex->input_length && *context_end != '\n' && *context_end != '\r') ? "..." : "";
1248 return errcontext("JSON data, line %d: %s%s%s",
1249 line_number, prefix, ctxt, suffix);
1253 * Extract a single, possibly multi-byte char from the input string.
1256 extract_mb_char(char *s)
1262 res = palloc(len + 1);
1263 memcpy(res, s, len);
1270 * Determine how we want to print values of a given type in datum_to_json.
1272 * Given the datatype OID, return its JsonTypeCategory, as well as the type's
1273 * output function OID. If the returned category is JSONTYPE_CAST, we
1274 * return the OID of the type->JSON cast function instead.
1277 json_categorize_type(Oid typoid,
1278 JsonTypeCategory *tcategory,
1283 /* Look through any domain */
1284 typoid = getBaseType(typoid);
1286 *outfuncoid = InvalidOid;
1289 * We need to get the output function for everything except date and
1290 * timestamp types, array and composite types, booleans, and non-builtin
1291 * types where there's a cast to json.
1297 *tcategory = JSONTYPE_BOOL;
1306 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
1307 *tcategory = JSONTYPE_NUMERIC;
1311 *tcategory = JSONTYPE_DATE;
1315 *tcategory = JSONTYPE_TIMESTAMP;
1318 case TIMESTAMPTZOID:
1319 *tcategory = JSONTYPE_TIMESTAMPTZ;
1324 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
1325 *tcategory = JSONTYPE_JSON;
1329 /* Check for arrays and composites */
1330 if (OidIsValid(get_element_type(typoid)))
1331 *tcategory = JSONTYPE_ARRAY;
1332 else if (type_is_rowtype(typoid))
1333 *tcategory = JSONTYPE_COMPOSITE;
1336 /* It's probably the general case ... */
1337 *tcategory = JSONTYPE_OTHER;
1338 /* but let's look for a cast to json, if it's not built-in */
1339 if (typoid >= FirstNormalObjectId)
1342 CoercionPathType ctype;
1344 ctype = find_coercion_pathway(JSONOID, typoid,
1347 if (ctype == COERCION_PATH_FUNC && OidIsValid(castfunc))
1349 *tcategory = JSONTYPE_CAST;
1350 *outfuncoid = castfunc;
1354 /* non builtin type with no cast */
1355 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
1360 /* any other builtin type */
1361 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
1369 * Turn a Datum into JSON text, appending the string to "result".
1371 * tcategory and outfuncoid are from a previous call to json_categorize_type,
1372 * except that if is_null is true then they can be invalid.
1374 * If key_scalar is true, the value is being printed as a key, so insist
1375 * it's of an acceptable type, and force it to be quoted.
1378 datum_to_json(Datum val, bool is_null, StringInfo result,
1379 JsonTypeCategory tcategory, Oid outfuncoid,
1385 /* callers are expected to ensure that null keys are not passed in */
1386 Assert(!(key_scalar && is_null));
1390 appendStringInfoString(result, "null");
1395 (tcategory == JSONTYPE_ARRAY ||
1396 tcategory == JSONTYPE_COMPOSITE ||
1397 tcategory == JSONTYPE_JSON ||
1398 tcategory == JSONTYPE_CAST))
1400 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1401 errmsg("key value must be scalar, not array, composite, or json")));
1405 case JSONTYPE_ARRAY:
1406 array_to_json_internal(val, result, false);
1408 case JSONTYPE_COMPOSITE:
1409 composite_to_json(val, result, false);
1412 outputstr = DatumGetBool(val) ? "true" : "false";
1414 escape_json(result, outputstr);
1416 appendStringInfoString(result, outputstr);
1418 case JSONTYPE_NUMERIC:
1419 outputstr = OidOutputFunctionCall(outfuncoid, val);
1422 * Don't call escape_json for a non-key if it's a valid JSON
1425 if (!key_scalar && IsValidJsonNumber(outputstr, strlen(outputstr)))
1426 appendStringInfoString(result, outputstr);
1428 escape_json(result, outputstr);
1435 char buf[MAXDATELEN + 1];
1437 date = DatumGetDateADT(val);
1439 /* XSD doesn't support infinite values */
1440 if (DATE_NOT_FINITE(date))
1442 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1443 errmsg("date out of range"),
1444 errdetail("JSON does not support infinite date values.")));
1447 j2date(date + POSTGRES_EPOCH_JDATE,
1448 &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
1449 EncodeDateOnly(&tm, USE_XSD_DATES, buf);
1452 appendStringInfo(result, "\"%s\"", buf);
1455 case JSONTYPE_TIMESTAMP:
1457 Timestamp timestamp;
1460 char buf[MAXDATELEN + 1];
1462 timestamp = DatumGetTimestamp(val);
1464 /* XSD doesn't support infinite values */
1465 if (TIMESTAMP_NOT_FINITE(timestamp))
1467 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1468 errmsg("timestamp out of range"),
1469 errdetail("JSON does not support infinite timestamp values.")));
1470 else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
1471 EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
1474 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1475 errmsg("timestamp out of range")));
1477 appendStringInfo(result, "\"%s\"", buf);
1480 case JSONTYPE_TIMESTAMPTZ:
1482 TimestampTz timestamp;
1486 const char *tzn = NULL;
1487 char buf[MAXDATELEN + 1];
1489 timestamp = DatumGetTimestamp(val);
1491 /* XSD doesn't support infinite values */
1492 if (TIMESTAMP_NOT_FINITE(timestamp))
1494 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1495 errmsg("timestamp out of range"),
1496 errdetail("JSON does not support infinite timestamp values.")));
1497 else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
1498 EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
1501 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1502 errmsg("timestamp out of range")));
1504 appendStringInfo(result, "\"%s\"", buf);
1508 /* JSON and JSONB output will already be escaped */
1509 outputstr = OidOutputFunctionCall(outfuncoid, val);
1510 appendStringInfoString(result, outputstr);
1514 /* outfuncoid refers to a cast function, not an output function */
1515 jsontext = DatumGetTextP(OidFunctionCall1(outfuncoid, val));
1516 outputstr = text_to_cstring(jsontext);
1517 appendStringInfoString(result, outputstr);
1522 outputstr = OidOutputFunctionCall(outfuncoid, val);
1523 escape_json(result, outputstr);
1530 * Process a single dimension of an array.
1531 * If it's the innermost dimension, output the values, otherwise call
1532 * ourselves recursively to process the next dimension.
1535 array_dim_to_json(StringInfo result, int dim, int ndims, int *dims, Datum *vals,
1536 bool *nulls, int *valcount, JsonTypeCategory tcategory,
1537 Oid outfuncoid, bool use_line_feeds)
1542 Assert(dim < ndims);
1544 sep = use_line_feeds ? ",\n " : ",";
1546 appendStringInfoChar(result, '[');
1548 for (i = 1; i <= dims[dim]; i++)
1551 appendStringInfoString(result, sep);
1553 if (dim + 1 == ndims)
1555 datum_to_json(vals[*valcount], nulls[*valcount], result, tcategory,
1562 * Do we want line feeds on inner dimensions of arrays? For now
1565 array_dim_to_json(result, dim + 1, ndims, dims, vals, nulls,
1566 valcount, tcategory, outfuncoid, false);
1570 appendStringInfoChar(result, ']');
1574 * Turn an array into JSON.
1577 array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
1579 ArrayType *v = DatumGetArrayTypeP(array);
1580 Oid element_type = ARR_ELEMTYPE(v);
1590 JsonTypeCategory tcategory;
1595 nitems = ArrayGetNItems(ndim, dim);
1599 appendStringInfoString(result, "[]");
1603 get_typlenbyvalalign(element_type,
1604 &typlen, &typbyval, &typalign);
1606 json_categorize_type(element_type,
1607 &tcategory, &outfuncoid);
1609 deconstruct_array(v, element_type, typlen, typbyval,
1610 typalign, &elements, &nulls,
1613 array_dim_to_json(result, 0, ndim, dim, elements, nulls, &count, tcategory,
1614 outfuncoid, use_line_feeds);
1621 * Turn a composite / record into JSON.
1624 composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
1630 HeapTupleData tmptup,
1633 bool needsep = false;
1636 sep = use_line_feeds ? ",\n " : ",";
1638 td = DatumGetHeapTupleHeader(composite);
1640 /* Extract rowtype info and find a tupdesc */
1641 tupType = HeapTupleHeaderGetTypeId(td);
1642 tupTypmod = HeapTupleHeaderGetTypMod(td);
1643 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
1645 /* Build a temporary HeapTuple control structure */
1646 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
1650 appendStringInfoChar(result, '{');
1652 for (i = 0; i < tupdesc->natts; i++)
1657 JsonTypeCategory tcategory;
1660 if (tupdesc->attrs[i]->attisdropped)
1664 appendStringInfoString(result, sep);
1667 attname = NameStr(tupdesc->attrs[i]->attname);
1668 escape_json(result, attname);
1669 appendStringInfoChar(result, ':');
1671 val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
1675 tcategory = JSONTYPE_NULL;
1676 outfuncoid = InvalidOid;
1679 json_categorize_type(tupdesc->attrs[i]->atttypid,
1680 &tcategory, &outfuncoid);
1682 datum_to_json(val, isnull, result, tcategory, outfuncoid, false);
1685 appendStringInfoChar(result, '}');
1686 ReleaseTupleDesc(tupdesc);
1690 * Append JSON text for "val" to "result".
1692 * This is just a thin wrapper around datum_to_json. If the same type will be
1693 * printed many times, avoid using this; better to do the json_categorize_type
1694 * lookups only once.
1697 add_json(Datum val, bool is_null, StringInfo result,
1698 Oid val_type, bool key_scalar)
1700 JsonTypeCategory tcategory;
1703 if (val_type == InvalidOid)
1705 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1706 errmsg("could not determine input data type")));
1710 tcategory = JSONTYPE_NULL;
1711 outfuncoid = InvalidOid;
1714 json_categorize_type(val_type,
1715 &tcategory, &outfuncoid);
1717 datum_to_json(val, is_null, result, tcategory, outfuncoid, key_scalar);
1721 * SQL function array_to_json(row)
1724 array_to_json(PG_FUNCTION_ARGS)
1726 Datum array = PG_GETARG_DATUM(0);
1729 result = makeStringInfo();
1731 array_to_json_internal(array, result, false);
1733 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1737 * SQL function array_to_json(row, prettybool)
1740 array_to_json_pretty(PG_FUNCTION_ARGS)
1742 Datum array = PG_GETARG_DATUM(0);
1743 bool use_line_feeds = PG_GETARG_BOOL(1);
1746 result = makeStringInfo();
1748 array_to_json_internal(array, result, use_line_feeds);
1750 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1754 * SQL function row_to_json(row)
1757 row_to_json(PG_FUNCTION_ARGS)
1759 Datum array = PG_GETARG_DATUM(0);
1762 result = makeStringInfo();
1764 composite_to_json(array, result, false);
1766 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1770 * SQL function row_to_json(row, prettybool)
1773 row_to_json_pretty(PG_FUNCTION_ARGS)
1775 Datum array = PG_GETARG_DATUM(0);
1776 bool use_line_feeds = PG_GETARG_BOOL(1);
1779 result = makeStringInfo();
1781 composite_to_json(array, result, use_line_feeds);
1783 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1787 * SQL function to_json(anyvalue)
1790 to_json(PG_FUNCTION_ARGS)
1792 Datum val = PG_GETARG_DATUM(0);
1793 Oid val_type = get_fn_expr_argtype(fcinfo->flinfo, 0);
1795 JsonTypeCategory tcategory;
1798 if (val_type == InvalidOid)
1800 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1801 errmsg("could not determine input data type")));
1803 json_categorize_type(val_type,
1804 &tcategory, &outfuncoid);
1806 result = makeStringInfo();
1808 datum_to_json(val, false, result, tcategory, outfuncoid, false);
1810 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1814 * json_agg transition function
1816 * aggregate input column as a json array value.
1819 json_agg_transfn(PG_FUNCTION_ARGS)
1821 Oid val_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
1822 MemoryContext aggcontext,
1826 JsonTypeCategory tcategory;
1829 if (val_type == InvalidOid)
1831 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1832 errmsg("could not determine input data type")));
1834 if (!AggCheckCallContext(fcinfo, &aggcontext))
1836 /* cannot be called directly because of internal-type argument */
1837 elog(ERROR, "json_agg_transfn called in non-aggregate context");
1840 if (PG_ARGISNULL(0))
1843 * Make this StringInfo in a context where it will persist for the
1844 * duration of the aggregate call. MemoryContextSwitchTo is only
1845 * needed the first time, as the StringInfo routines make sure they
1846 * use the right context to enlarge the object if necessary.
1848 oldcontext = MemoryContextSwitchTo(aggcontext);
1849 state = makeStringInfo();
1850 MemoryContextSwitchTo(oldcontext);
1852 appendStringInfoChar(state, '[');
1856 state = (StringInfo) PG_GETARG_POINTER(0);
1857 appendStringInfoString(state, ", ");
1860 /* fast path for NULLs */
1861 if (PG_ARGISNULL(1))
1863 datum_to_json((Datum) 0, true, state, JSONTYPE_NULL, InvalidOid, false);
1864 PG_RETURN_POINTER(state);
1867 val = PG_GETARG_DATUM(1);
1869 /* XXX we do this every time?? */
1870 json_categorize_type(val_type,
1871 &tcategory, &outfuncoid);
1873 /* add some whitespace if structured type and not first item */
1874 if (!PG_ARGISNULL(0) &&
1875 (tcategory == JSONTYPE_ARRAY || tcategory == JSONTYPE_COMPOSITE))
1877 appendStringInfoString(state, "\n ");
1880 datum_to_json(val, false, state, tcategory, outfuncoid, false);
1883 * The transition type for array_agg() is declared to be "internal", which
1884 * is a pass-by-value type the same size as a pointer. So we can safely
1885 * pass the ArrayBuildState pointer through nodeAgg.c's machinations.
1887 PG_RETURN_POINTER(state);
1891 * json_agg final function
1894 json_agg_finalfn(PG_FUNCTION_ARGS)
1898 /* cannot be called directly because of internal-type argument */
1899 Assert(AggCheckCallContext(fcinfo, NULL));
1901 state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
1903 /* NULL result for no rows in, as is standard with aggregates */
1907 /* Else return state with appropriate array terminator added */
1908 PG_RETURN_TEXT_P(catenate_stringinfo_string(state, "]"));
1912 * json_object_agg transition function.
1914 * aggregate two input columns as a single json object value.
1917 json_object_agg_transfn(PG_FUNCTION_ARGS)
1920 MemoryContext aggcontext,
1925 if (!AggCheckCallContext(fcinfo, &aggcontext))
1927 /* cannot be called directly because of internal-type argument */
1928 elog(ERROR, "json_object_agg_transfn called in non-aggregate context");
1931 if (PG_ARGISNULL(0))
1934 * Make the StringInfo in a context where it will persist for the
1935 * duration of the aggregate call. Switching context is only needed
1936 * for this initial step, as the StringInfo routines make sure they
1937 * use the right context to enlarge the object if necessary.
1939 oldcontext = MemoryContextSwitchTo(aggcontext);
1940 state = makeStringInfo();
1941 MemoryContextSwitchTo(oldcontext);
1943 appendStringInfoString(state, "{ ");
1947 state = (StringInfo) PG_GETARG_POINTER(0);
1948 appendStringInfoString(state, ", ");
1952 * Note: since json_object_agg() is declared as taking type "any", the
1953 * parser will not do any type conversion on unknown-type literals (that
1954 * is, undecorated strings or NULLs). Such values will arrive here as
1955 * type UNKNOWN, which fortunately does not matter to us, since
1956 * unknownout() works fine.
1958 val_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
1960 if (val_type == InvalidOid)
1962 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1963 errmsg("could not determine data type for argument %d", 1)));
1965 if (PG_ARGISNULL(1))
1967 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1968 errmsg("field name must not be null")));
1970 arg = PG_GETARG_DATUM(1);
1972 add_json(arg, false, state, val_type, true);
1974 appendStringInfoString(state, " : ");
1976 val_type = get_fn_expr_argtype(fcinfo->flinfo, 2);
1978 if (val_type == InvalidOid)
1980 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1981 errmsg("could not determine data type for argument %d", 2)));
1983 if (PG_ARGISNULL(2))
1986 arg = PG_GETARG_DATUM(2);
1988 add_json(arg, PG_ARGISNULL(2), state, val_type, false);
1990 PG_RETURN_POINTER(state);
1994 * json_object_agg final function.
1997 json_object_agg_finalfn(PG_FUNCTION_ARGS)
2001 /* cannot be called directly because of internal-type argument */
2002 Assert(AggCheckCallContext(fcinfo, NULL));
2004 state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
2006 /* NULL result for no rows in, as is standard with aggregates */
2010 /* Else return state with appropriate object terminator added */
2011 PG_RETURN_TEXT_P(catenate_stringinfo_string(state, " }"));
2015 * Helper function for aggregates: return given StringInfo's contents plus
2016 * specified trailing string, as a text datum. We need this because aggregate
2017 * final functions are not allowed to modify the aggregate state.
2020 catenate_stringinfo_string(StringInfo buffer, const char *addon)
2022 /* custom version of cstring_to_text_with_len */
2023 int buflen = buffer->len;
2024 int addlen = strlen(addon);
2025 text *result = (text *) palloc(buflen + addlen + VARHDRSZ);
2027 SET_VARSIZE(result, buflen + addlen + VARHDRSZ);
2028 memcpy(VARDATA(result), buffer->data, buflen);
2029 memcpy(VARDATA(result) + buflen, addon, addlen);
2035 * SQL function json_build_object(variadic "any")
2038 json_build_object(PG_FUNCTION_ARGS)
2040 int nargs = PG_NARGS();
2043 const char *sep = "";
2049 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2050 errmsg("argument list must have even number of elements"),
2051 errhint("The arguments of json_build_object() must consist of alternating keys and values.")));
2053 result = makeStringInfo();
2055 appendStringInfoChar(result, '{');
2057 for (i = 0; i < nargs; i += 2)
2060 * Note: since json_build_object() is declared as taking type "any",
2061 * the parser will not do any type conversion on unknown-type literals
2062 * (that is, undecorated strings or NULLs). Such values will arrive
2063 * here as type UNKNOWN, which fortunately does not matter to us,
2064 * since unknownout() works fine.
2066 appendStringInfoString(result, sep);
2070 val_type = get_fn_expr_argtype(fcinfo->flinfo, i);
2072 if (val_type == InvalidOid)
2074 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2075 errmsg("could not determine data type for argument %d",
2078 if (PG_ARGISNULL(i))
2080 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2081 errmsg("argument %d cannot be null", i + 1),
2082 errhint("Object keys should be text.")));
2084 arg = PG_GETARG_DATUM(i);
2086 add_json(arg, false, result, val_type, true);
2088 appendStringInfoString(result, " : ");
2091 val_type = get_fn_expr_argtype(fcinfo->flinfo, i + 1);
2093 if (val_type == InvalidOid)
2095 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2096 errmsg("could not determine data type for argument %d",
2099 if (PG_ARGISNULL(i + 1))
2102 arg = PG_GETARG_DATUM(i + 1);
2104 add_json(arg, PG_ARGISNULL(i + 1), result, val_type, false);
2107 appendStringInfoChar(result, '}');
2109 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
2113 * degenerate case of json_build_object where it gets 0 arguments.
2116 json_build_object_noargs(PG_FUNCTION_ARGS)
2118 PG_RETURN_TEXT_P(cstring_to_text_with_len("{}", 2));
2122 * SQL function json_build_array(variadic "any")
2125 json_build_array(PG_FUNCTION_ARGS)
2127 int nargs = PG_NARGS();
2130 const char *sep = "";
2134 result = makeStringInfo();
2136 appendStringInfoChar(result, '[');
2138 for (i = 0; i < nargs; i++)
2141 * Note: since json_build_array() is declared as taking type "any",
2142 * the parser will not do any type conversion on unknown-type literals
2143 * (that is, undecorated strings or NULLs). Such values will arrive
2144 * here as type UNKNOWN, which fortunately does not matter to us,
2145 * since unknownout() works fine.
2147 appendStringInfoString(result, sep);
2150 val_type = get_fn_expr_argtype(fcinfo->flinfo, i);
2152 if (val_type == InvalidOid)
2154 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2155 errmsg("could not determine data type for argument %d",
2158 if (PG_ARGISNULL(i))
2161 arg = PG_GETARG_DATUM(i);
2163 add_json(arg, PG_ARGISNULL(i), result, val_type, false);
2166 appendStringInfoChar(result, ']');
2168 PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
2172 * degenerate case of json_build_array where it gets 0 arguments.
2175 json_build_array_noargs(PG_FUNCTION_ARGS)
2177 PG_RETURN_TEXT_P(cstring_to_text_with_len("[]", 2));
2181 * SQL function json_object(text[])
2183 * take a one or two dimensional array of text as key/value pairs
2184 * for a json object.
2187 json_object(PG_FUNCTION_ARGS)
2189 ArrayType *in_array = PG_GETARG_ARRAYTYPE_P(0);
2190 int ndims = ARR_NDIM(in_array);
2191 StringInfoData result;
2203 PG_RETURN_DATUM(CStringGetTextDatum("{}"));
2207 if ((ARR_DIMS(in_array)[0]) % 2)
2209 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2210 errmsg("array must have even number of elements")));
2214 if ((ARR_DIMS(in_array)[1]) != 2)
2216 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2217 errmsg("array must have two columns")));
2222 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2223 errmsg("wrong number of array subscripts")));
2226 deconstruct_array(in_array,
2227 TEXTOID, -1, false, 'i',
2228 &in_datums, &in_nulls, &in_count);
2230 count = in_count / 2;
2232 initStringInfo(&result);
2234 appendStringInfoChar(&result, '{');
2236 for (i = 0; i < count; ++i)
2238 if (in_nulls[i * 2])
2240 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2241 errmsg("null value not allowed for object key")));
2243 v = TextDatumGetCString(in_datums[i * 2]);
2245 appendStringInfoString(&result, ", ");
2246 escape_json(&result, v);
2247 appendStringInfoString(&result, " : ");
2249 if (in_nulls[i * 2 + 1])
2250 appendStringInfoString(&result, "null");
2253 v = TextDatumGetCString(in_datums[i * 2 + 1]);
2254 escape_json(&result, v);
2259 appendStringInfoChar(&result, '}');
2264 rval = cstring_to_text_with_len(result.data, result.len);
2267 PG_RETURN_TEXT_P(rval);
2272 * SQL function json_object(text[], text[])
2274 * take separate key and value arrays of text to construct a json object
2278 json_object_two_arg(PG_FUNCTION_ARGS)
2280 ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(0);
2281 ArrayType *val_array = PG_GETARG_ARRAYTYPE_P(1);
2282 int nkdims = ARR_NDIM(key_array);
2283 int nvdims = ARR_NDIM(val_array);
2284 StringInfoData result;
2295 if (nkdims > 1 || nkdims != nvdims)
2297 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2298 errmsg("wrong number of array subscripts")));
2301 PG_RETURN_DATUM(CStringGetTextDatum("{}"));
2303 deconstruct_array(key_array,
2304 TEXTOID, -1, false, 'i',
2305 &key_datums, &key_nulls, &key_count);
2307 deconstruct_array(val_array,
2308 TEXTOID, -1, false, 'i',
2309 &val_datums, &val_nulls, &val_count);
2311 if (key_count != val_count)
2313 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2314 errmsg("mismatched array dimensions")));
2316 initStringInfo(&result);
2318 appendStringInfoChar(&result, '{');
2320 for (i = 0; i < key_count; ++i)
2324 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2325 errmsg("null value not allowed for object key")));
2327 v = TextDatumGetCString(key_datums[i]);
2329 appendStringInfoString(&result, ", ");
2330 escape_json(&result, v);
2331 appendStringInfoString(&result, " : ");
2334 appendStringInfoString(&result, "null");
2337 v = TextDatumGetCString(val_datums[i]);
2338 escape_json(&result, v);
2343 appendStringInfoChar(&result, '}');
2350 rval = cstring_to_text_with_len(result.data, result.len);
2353 PG_RETURN_TEXT_P(rval);
2358 * Produce a JSON string literal, properly escaping characters in the text.
2361 escape_json(StringInfo buf, const char *str)
2365 appendStringInfoCharMacro(buf, '\"');
2366 for (p = str; *p; p++)
2371 appendStringInfoString(buf, "\\b");
2374 appendStringInfoString(buf, "\\f");
2377 appendStringInfoString(buf, "\\n");
2380 appendStringInfoString(buf, "\\r");
2383 appendStringInfoString(buf, "\\t");
2386 appendStringInfoString(buf, "\\\"");
2389 appendStringInfoString(buf, "\\\\");
2392 if ((unsigned char) *p < ' ')
2393 appendStringInfo(buf, "\\u%04x", (int) *p);
2395 appendStringInfoCharMacro(buf, *p);
2399 appendStringInfoCharMacro(buf, '\"');
2403 * SQL function json_typeof(json) -> text
2405 * Returns the type of the outermost JSON value as TEXT. Possible types are
2406 * "object", "array", "string", "number", "boolean", and "null".
2408 * Performs a single call to json_lex() to get the first token of the supplied
2409 * value. This initial token uniquely determines the value's type. As our
2410 * input must already have been validated by json_in() or json_recv(), the
2411 * initial token should never be JSON_TOKEN_OBJECT_END, JSON_TOKEN_ARRAY_END,
2412 * JSON_TOKEN_COLON, JSON_TOKEN_COMMA, or JSON_TOKEN_END.
2415 json_typeof(PG_FUNCTION_ARGS)
2419 JsonLexContext *lex;
2423 json = PG_GETARG_TEXT_P(0);
2424 lex = makeJsonLexContext(json, false);
2426 /* Lex exactly one token from the input and check its type. */
2428 tok = lex_peek(lex);
2431 case JSON_TOKEN_OBJECT_START:
2434 case JSON_TOKEN_ARRAY_START:
2437 case JSON_TOKEN_STRING:
2440 case JSON_TOKEN_NUMBER:
2443 case JSON_TOKEN_TRUE:
2444 case JSON_TOKEN_FALSE:
2447 case JSON_TOKEN_NULL:
2451 elog(ERROR, "unexpected json token: %d", tok);
2454 PG_RETURN_TEXT_P(cstring_to_text(type));