57 Datum *vals,
bool *nulls,
int *valcount,
69 Oid val_type,
bool key_scalar);
198 || typoid == ANYCOMPATIBLEARRAYOID || typoid == RECORDARRAYOID)
218 *outfuncoid = castfunc;
256 Assert(!(key_scalar && is_null));
270 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
271 errmsg(
"key value must be scalar, not array, composite, or json")));
413 else if (
timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
417 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
418 errmsg(
"timestamp out of range")));
427 const char *tzn = NULL;
446 else if (
timestamp2tm(timestamp, tzp ? NULL : &tz, &tm, &fsec,
447 tzp ? NULL : &tzn, NULL) == 0)
456 (
errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
457 errmsg(
"timestamp out of range")));
461 elog(
ERROR,
"unknown jsonb value datetime type oid %u", typid);
476 Oid outfuncoid,
bool use_line_feeds)
483 sep = use_line_feeds ?
",\n " :
",";
487 for (i = 1; i <= dims[dim]; i++)
492 if (dim + 1 == ndims)
494 datum_to_json(vals[*valcount], nulls[*valcount], result, tcategory,
505 valcount, tcategory, outfuncoid,
false);
543 &typlen, &typbyval, &typalign);
546 &tcategory, &outfuncoid);
549 typalign, &elements, &nulls,
553 outfuncoid, use_line_feeds);
572 bool needsep =
false;
575 sep = use_line_feeds ?
",\n " :
",";
591 for (i = 0; i < tupdesc->
natts; i++)
600 if (att->attisdropped)
607 attname =
NameStr(att->attname);
621 datum_to_json(val, isnull, result, tcategory, outfuncoid,
false);
637 Oid val_type,
bool key_scalar)
644 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
645 errmsg(
"could not determine input data type")));
654 &tcategory, &outfuncoid);
656 datum_to_json(val, is_null, result, tcategory, outfuncoid, key_scalar);
739 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
740 errmsg(
"could not determine input data type")));
743 &tcategory, &outfuncoid);
747 datum_to_json(val,
false, result, tcategory, outfuncoid,
false);
768 elog(
ERROR,
"json_agg_transfn called in non-aggregate context");
777 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
778 errmsg(
"could not determine input data type")));
869 elog(
ERROR,
"json_object_agg_transfn called in non-aggregate context");
891 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
892 errmsg(
"could not determine data type for argument %d", 1)));
901 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
902 errmsg(
"could not determine data type for argument %d", 2)));
925 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
926 errmsg(
"field name must not be null")));
976 int buflen = buffer->
len;
977 int addlen = strlen(addon);
982 memcpy(
VARDATA(result) + buflen, addon, addlen);
995 const char *sep =
"";
1009 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1010 errmsg(
"argument list must have even number of elements"),
1012 errhint(
"The arguments of %s must consist of alternating keys and values.",
1013 "json_build_object()")));
1019 for (i = 0; i < nargs; i += 2)
1027 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1028 errmsg(
"argument %d cannot be null", i + 1),
1029 errhint(
"Object keys should be text.")));
1031 add_json(args[i],
false, result, types[i],
true);
1036 add_json(args[i + 1], nulls[i + 1], result, types[i + 1],
false);
1061 const char *sep =
"";
1077 for (i = 0; i < nargs; i++)
1081 add_json(args[i], nulls[i], result, types[i],
false);
1127 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1128 errmsg(
"array must have even number of elements")));
1134 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1135 errmsg(
"array must have two columns")));
1140 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1141 errmsg(
"wrong number of array subscripts")));
1145 TEXTOID, -1,
false, TYPALIGN_INT,
1146 &in_datums, &in_nulls, &in_count);
1148 count = in_count / 2;
1154 for (i = 0; i < count; ++
i)
1156 if (in_nulls[i * 2])
1158 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1159 errmsg(
"null value not allowed for object key")));
1167 if (in_nulls[i * 2 + 1])
1213 if (nkdims > 1 || nkdims != nvdims)
1215 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1216 errmsg(
"wrong number of array subscripts")));
1222 TEXTOID, -1,
false, TYPALIGN_INT,
1223 &key_datums, &key_nulls, &key_count);
1226 TEXTOID, -1,
false, TYPALIGN_INT,
1227 &val_datums, &val_nulls, &val_count);
1229 if (key_count != val_count)
1231 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1232 errmsg(
"mismatched array dimensions")));
1238 for (i = 0; i < key_count; ++
i)
1242 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1243 errmsg(
"null value not allowed for object key")));
1284 for (p = str; *p; p++)
1310 if ((
unsigned char) *p <
' ')
1372 elog(
ERROR,
"unexpected json token: %d", tok);
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
#define PG_RETURN_POINTER(x)
Datum to_json(PG_FUNCTION_ARGS)
static void datum_to_json(Datum val, bool is_null, StringInfo result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
#define DatumGetDateADT(X)
int errhint(const char *fmt,...)
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
int extract_variadic_args(FunctionCallInfo fcinfo, int variadic_start, bool convert_unknown, Datum **args, Oid **types, bool **nulls)
void escape_json(StringInfo buf, const char *str)
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Datum json_object_two_arg(PG_FUNCTION_ARGS)
static void json_categorize_type(Oid typoid, JsonTypeCategory *tcategory, Oid *outfuncoid)
Oid get_element_type(Oid typid)
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
static void composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
#define PG_GETARG_DATUM(n)
#define TupleDescAttr(tupdesc, i)
#define DatumGetTextPP(X)
static void array_dim_to_json(StringInfo result, int dim, int ndims, int *dims, Datum *vals, bool *nulls, int *valcount, JsonTypeCategory tcategory, Oid outfuncoid, bool use_line_feeds)
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
#define DatumGetTimeTzADTP(X)
Datum json_in(PG_FUNCTION_ARGS)
StringInfo makeStringInfo(void)
StringInfoData * StringInfo
int ArrayGetNItems(int ndim, const int *dims)
JsonLexContext * makeJsonLexContext(text *json, bool need_escapes)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Datum json_agg_finalfn(PG_FUNCTION_ARGS)
void EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str)
Datum array_to_json_pretty(PG_FUNCTION_ARGS)
int errcode(int sqlerrcode)
int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
JsonSemAction nullSemAction
#define PG_GETARG_POINTER(n)
void EncodeSpecialTimestamp(Timestamp dt, char *str)
#define PG_GETARG_BOOL(n)
#define PG_RETURN_BYTEA_P(x)
#define OidIsValid(objectId)
#define DatumGetHeapTupleHeader(X)
#define FirstNormalObjectId
struct JsonAggState JsonAggState
#define PG_GETARG_TEXT_PP(n)
#define HeapTupleHeaderGetTypMod(tup)
static text * catenate_stringinfo_string(StringInfo buffer, const char *addon)
#define PG_GETARG_ARRAYTYPE_P(n)
#define appendStringInfoCharMacro(str, ch)
#define TIMESTAMP_NOT_FINITE(j)
void pfree(void *pointer)
Datum json_build_object_noargs(PG_FUNCTION_ARGS)
void appendStringInfo(StringInfo str, const char *fmt,...)
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Datum array_to_json(PG_FUNCTION_ARGS)
Datum json_build_object(PG_FUNCTION_ARGS)
#define DATE_NOT_FINITE(j)
Datum row_to_json_pretty(PG_FUNCTION_ARGS)
Datum json_typeof(PG_FUNCTION_ARGS)
#define OidFunctionCall1(functionId, arg1)
char * JsonEncodeDateTime(char *buf, Datum value, Oid typid, const int *tzp)
void appendStringInfoString(StringInfo str, const char *s)
JsonLexContext * makeJsonLexContextCstringLen(char *json, int len, int encoding, bool need_escapes)
text * cstring_to_text_with_len(const char *s, int len)
void check_stack_depth(void)
#define DatumGetTimestampTz(X)
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
FormData_pg_attribute * Form_pg_attribute
void EncodeSpecialDate(DateADT dt, char *str)
bool type_is_rowtype(Oid typid)
bool IsValidJsonNumber(const char *str, int len)
void j2date(int jd, int *year, int *month, int *day)
JsonTypeCategory key_category
Datum json_object(PG_FUNCTION_ARGS)
void appendStringInfoChar(StringInfo str, char ch)
void initStringInfo(StringInfo str)
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Datum json_object_agg_finalfn(PG_FUNCTION_ARGS)
#define DatumGetTimeADT(X)
void pg_parse_json_or_ereport(JsonLexContext *lex, JsonSemAction *sem)
#define TextDatumGetCString(d)
#define PG_RETURN_DATUM(x)
int GetDatabaseEncoding(void)
int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
#define HeapTupleHeaderGetTypeId(tup)
Datum json_out(PG_FUNCTION_ARGS)
#define ereport(elevel,...)
Datum json_object_agg_transfn(PG_FUNCTION_ARGS)
static void add_json(Datum val, bool is_null, StringInfo result, Oid val_type, bool key_scalar)
#define PG_RETURN_TEXT_P(x)
text * cstring_to_text(const char *s)
JsonParseErrorType json_lex(JsonLexContext *lex)
JsonTypeCategory val_category
#define Assert(condition)
#define PG_RETURN_CSTRING(x)
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Datum json_recv(PG_FUNCTION_ARGS)
static void array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
Datum json_agg_transfn(PG_FUNCTION_ARGS)
void json_ereport_error(JsonParseErrorType error, JsonLexContext *lex)
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
char * text_to_cstring(const text *t)
Datum json_build_array_noargs(PG_FUNCTION_ARGS)
char * OidOutputFunctionCall(Oid functionId, Datum val)
#define VARSIZE_ANY_EXHDR(PTR)
int errmsg(const char *fmt,...)
Datum json_build_array(PG_FUNCTION_ARGS)
CoercionPathType find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, CoercionContext ccontext, Oid *funcid)
#define CStringGetTextDatum(s)
#define PG_GETARG_CSTRING(n)
#define POSTGRES_EPOCH_JDATE
#define SET_VARSIZE(PTR, len)
Datum row_to_json(PG_FUNCTION_ARGS)
#define ReleaseTupleDesc(tupdesc)
Oid getBaseType(Oid typid)
Datum json_send(PG_FUNCTION_ARGS)
#define DatumGetTimestamp(X)
#define HeapTupleHeaderGetDatumLength(tup)
#define DatumGetArrayTypeP(X)