PostgreSQL Source Code  git master
json.c File Reference
#include "postgres.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "common/hashfn.h"
#include "funcapi.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "port/simd.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/datetime.h"
#include "utils/fmgroids.h"
#include "utils/json.h"
#include "utils/jsonfuncs.h"
#include "utils/lsyscache.h"
#include "utils/typcache.h"
Include dependency graph for json.c:

Go to the source code of this file.

Data Structures

struct  JsonUniqueHashEntry
 
struct  JsonUniqueStackEntry
 
struct  JsonUniqueParsingState
 
struct  JsonUniqueBuilderState
 
struct  JsonAggState
 

Macros

#define ESCAPE_JSON_FLUSH_AFTER   512
 

Typedefs

typedef struct HTABJsonUniqueCheckState
 
typedef struct JsonUniqueHashEntry JsonUniqueHashEntry
 
typedef struct JsonUniqueStackEntry JsonUniqueStackEntry
 
typedef struct JsonUniqueParsingState JsonUniqueParsingState
 
typedef struct JsonUniqueBuilderState JsonUniqueBuilderState
 
typedef struct JsonAggState JsonAggState
 

Functions

static void composite_to_json (Datum composite, StringInfo result, bool use_line_feeds)
 
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)
 
static void array_to_json_internal (Datum array, StringInfo result, bool use_line_feeds)
 
static void datum_to_json_internal (Datum val, bool is_null, StringInfo result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
 
static void add_json (Datum val, bool is_null, StringInfo result, Oid val_type, bool key_scalar)
 
static textcatenate_stringinfo_string (StringInfo buffer, const char *addon)
 
Datum json_in (PG_FUNCTION_ARGS)
 
Datum json_out (PG_FUNCTION_ARGS)
 
Datum json_send (PG_FUNCTION_ARGS)
 
Datum json_recv (PG_FUNCTION_ARGS)
 
char * JsonEncodeDateTime (char *buf, Datum value, Oid typid, const int *tzp)
 
Datum array_to_json (PG_FUNCTION_ARGS)
 
Datum array_to_json_pretty (PG_FUNCTION_ARGS)
 
Datum row_to_json (PG_FUNCTION_ARGS)
 
Datum row_to_json_pretty (PG_FUNCTION_ARGS)
 
bool to_json_is_immutable (Oid typoid)
 
Datum to_json (PG_FUNCTION_ARGS)
 
Datum datum_to_json (Datum val, JsonTypeCategory tcategory, Oid outfuncoid)
 
static Datum json_agg_transfn_worker (FunctionCallInfo fcinfo, bool absent_on_null)
 
Datum json_agg_transfn (PG_FUNCTION_ARGS)
 
Datum json_agg_strict_transfn (PG_FUNCTION_ARGS)
 
Datum json_agg_finalfn (PG_FUNCTION_ARGS)
 
static uint32 json_unique_hash (const void *key, Size keysize)
 
static int json_unique_hash_match (const void *key1, const void *key2, Size keysize)
 
static void json_unique_check_init (JsonUniqueCheckState *cxt)
 
static void json_unique_builder_init (JsonUniqueBuilderState *cxt)
 
static bool json_unique_check_key (JsonUniqueCheckState *cxt, const char *key, int object_id)
 
static StringInfo json_unique_builder_get_throwawaybuf (JsonUniqueBuilderState *cxt)
 
static Datum json_object_agg_transfn_worker (FunctionCallInfo fcinfo, bool absent_on_null, bool unique_keys)
 
Datum json_object_agg_transfn (PG_FUNCTION_ARGS)
 
Datum json_object_agg_strict_transfn (PG_FUNCTION_ARGS)
 
Datum json_object_agg_unique_transfn (PG_FUNCTION_ARGS)
 
Datum json_object_agg_unique_strict_transfn (PG_FUNCTION_ARGS)
 
Datum json_object_agg_finalfn (PG_FUNCTION_ARGS)
 
Datum json_build_object_worker (int nargs, const Datum *args, const bool *nulls, const Oid *types, bool absent_on_null, bool unique_keys)
 
Datum json_build_object (PG_FUNCTION_ARGS)
 
Datum json_build_object_noargs (PG_FUNCTION_ARGS)
 
Datum json_build_array_worker (int nargs, const Datum *args, const bool *nulls, const Oid *types, bool absent_on_null)
 
Datum json_build_array (PG_FUNCTION_ARGS)
 
Datum json_build_array_noargs (PG_FUNCTION_ARGS)
 
Datum json_object (PG_FUNCTION_ARGS)
 
Datum json_object_two_arg (PG_FUNCTION_ARGS)
 
static pg_attribute_always_inline void escape_json_char (StringInfo buf, char c)
 
void escape_json (StringInfo buf, const char *str)
 
void escape_json_with_len (StringInfo buf, const char *str, int len)
 
void escape_json_text (StringInfo buf, const text *txt)
 
static JsonParseErrorType json_unique_object_start (void *_state)
 
static JsonParseErrorType json_unique_object_end (void *_state)
 
static JsonParseErrorType json_unique_object_field_start (void *_state, char *field, bool isnull)
 
bool json_validate (text *json, bool check_unique_keys, bool throw_error)
 
Datum json_typeof (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ ESCAPE_JSON_FLUSH_AFTER

#define ESCAPE_JSON_FLUSH_AFTER   512

Definition at line 1622 of file json.c.

Typedef Documentation

◆ JsonAggState

typedef struct JsonAggState JsonAggState

◆ JsonUniqueBuilderState

◆ JsonUniqueCheckState

typedef struct HTAB* JsonUniqueCheckState

Definition at line 41 of file json.c.

◆ JsonUniqueHashEntry

◆ JsonUniqueParsingState

◆ JsonUniqueStackEntry

Function Documentation

◆ add_json()

static void add_json ( Datum  val,
bool  is_null,
StringInfo  result,
Oid  val_type,
bool  key_scalar 
)
static

Definition at line 602 of file json.c.

604 {
605  JsonTypeCategory tcategory;
606  Oid outfuncoid;
607 
608  if (val_type == InvalidOid)
609  ereport(ERROR,
610  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
611  errmsg("could not determine input data type")));
612 
613  if (is_null)
614  {
615  tcategory = JSONTYPE_NULL;
616  outfuncoid = InvalidOid;
617  }
618  else
619  json_categorize_type(val_type, false,
620  &tcategory, &outfuncoid);
621 
622  datum_to_json_internal(val, is_null, result, tcategory, outfuncoid,
623  key_scalar);
624 }
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
long val
Definition: informix.c:689
static void datum_to_json_internal(Datum val, bool is_null, StringInfo result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
Definition: json.c:179
void json_categorize_type(Oid typoid, bool is_jsonb, JsonTypeCategory *tcategory, Oid *outfuncoid)
Definition: jsonfuncs.c:5976
JsonTypeCategory
Definition: jsonfuncs.h:69
@ JSONTYPE_NULL
Definition: jsonfuncs.h:70
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31

References datum_to_json_internal(), ereport, errcode(), errmsg(), ERROR, InvalidOid, json_categorize_type(), JSONTYPE_NULL, and val.

Referenced by json_build_array_worker(), and json_build_object_worker().

◆ array_dim_to_json()

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 
)
static

Definition at line 431 of file json.c.

434 {
435  int i;
436  const char *sep;
437 
438  Assert(dim < ndims);
439 
440  sep = use_line_feeds ? ",\n " : ",";
441 
442  appendStringInfoChar(result, '[');
443 
444  for (i = 1; i <= dims[dim]; i++)
445  {
446  if (i > 1)
447  appendStringInfoString(result, sep);
448 
449  if (dim + 1 == ndims)
450  {
451  datum_to_json_internal(vals[*valcount], nulls[*valcount],
452  result, tcategory,
453  outfuncoid, false);
454  (*valcount)++;
455  }
456  else
457  {
458  /*
459  * Do we want line feeds on inner dimensions of arrays? For now
460  * we'll say no.
461  */
462  array_dim_to_json(result, dim + 1, ndims, dims, vals, nulls,
463  valcount, tcategory, outfuncoid, false);
464  }
465  }
466 
467  appendStringInfoChar(result, ']');
468 }
#define Assert(condition)
Definition: c.h:861
int i
Definition: isn.c:73
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)
Definition: json.c:431
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:182
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:194

References appendStringInfoChar(), appendStringInfoString(), Assert, datum_to_json_internal(), and i.

Referenced by array_to_json_internal().

◆ array_to_json()

Datum array_to_json ( PG_FUNCTION_ARGS  )

Definition at line 630 of file json.c.

631 {
632  Datum array = PG_GETARG_DATUM(0);
633  StringInfo result;
634 
635  result = makeStringInfo();
636 
637  array_to_json_internal(array, result, false);
638 
640 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
static void array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
Definition: json.c:474
uintptr_t Datum
Definition: postgres.h:64
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:196

References array_to_json_internal(), cstring_to_text_with_len(), StringInfoData::data, StringInfoData::len, makeStringInfo(), PG_GETARG_DATUM, and PG_RETURN_TEXT_P.

◆ array_to_json_internal()

static void array_to_json_internal ( Datum  array,
StringInfo  result,
bool  use_line_feeds 
)
static

Definition at line 474 of file json.c.

475 {
476  ArrayType *v = DatumGetArrayTypeP(array);
477  Oid element_type = ARR_ELEMTYPE(v);
478  int *dim;
479  int ndim;
480  int nitems;
481  int count = 0;
482  Datum *elements;
483  bool *nulls;
484  int16 typlen;
485  bool typbyval;
486  char typalign;
487  JsonTypeCategory tcategory;
488  Oid outfuncoid;
489 
490  ndim = ARR_NDIM(v);
491  dim = ARR_DIMS(v);
492  nitems = ArrayGetNItems(ndim, dim);
493 
494  if (nitems <= 0)
495  {
496  appendStringInfoString(result, "[]");
497  return;
498  }
499 
500  get_typlenbyvalalign(element_type,
501  &typlen, &typbyval, &typalign);
502 
503  json_categorize_type(element_type, false,
504  &tcategory, &outfuncoid);
505 
506  deconstruct_array(v, element_type, typlen, typbyval,
507  typalign, &elements, &nulls,
508  &nitems);
509 
510  array_dim_to_json(result, 0, ndim, dim, elements, nulls, &count, tcategory,
511  outfuncoid, use_line_feeds);
512 
513  pfree(elements);
514  pfree(nulls);
515 }
#define ARR_NDIM(a)
Definition: array.h:290
#define DatumGetArrayTypeP(X)
Definition: array.h:261
#define ARR_ELEMTYPE(a)
Definition: array.h:292
#define ARR_DIMS(a)
Definition: array.h:294
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3619
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:57
signed short int16
Definition: c.h:496
#define nitems(x)
Definition: indent.h:31
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2271
void pfree(void *pointer)
Definition: mcxt.c:1521
char typalign
Definition: pg_type.h:176

References appendStringInfoString(), ARR_DIMS, ARR_ELEMTYPE, ARR_NDIM, array_dim_to_json(), ArrayGetNItems(), DatumGetArrayTypeP, deconstruct_array(), get_typlenbyvalalign(), json_categorize_type(), nitems, pfree(), and typalign.

Referenced by array_to_json(), array_to_json_pretty(), and datum_to_json_internal().

◆ array_to_json_pretty()

Datum array_to_json_pretty ( PG_FUNCTION_ARGS  )

Definition at line 646 of file json.c.

647 {
648  Datum array = PG_GETARG_DATUM(0);
649  bool use_line_feeds = PG_GETARG_BOOL(1);
650  StringInfo result;
651 
652  result = makeStringInfo();
653 
654  array_to_json_internal(array, result, use_line_feeds);
655 
657 }
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274

References array_to_json_internal(), cstring_to_text_with_len(), StringInfoData::data, StringInfoData::len, makeStringInfo(), PG_GETARG_BOOL, PG_GETARG_DATUM, and PG_RETURN_TEXT_P.

◆ catenate_stringinfo_string()

static text * catenate_stringinfo_string ( StringInfo  buffer,
const char *  addon 
)
static

Definition at line 1209 of file json.c.

1210 {
1211  /* custom version of cstring_to_text_with_len */
1212  int buflen = buffer->len;
1213  int addlen = strlen(addon);
1214  text *result = (text *) palloc(buflen + addlen + VARHDRSZ);
1215 
1216  SET_VARSIZE(result, buflen + addlen + VARHDRSZ);
1217  memcpy(VARDATA(result), buffer->data, buflen);
1218  memcpy(VARDATA(result) + buflen, addon, addlen);
1219 
1220  return result;
1221 }
#define VARHDRSZ
Definition: c.h:695
void * palloc(Size size)
Definition: mcxt.c:1317
Definition: c.h:690
#define VARDATA(PTR)
Definition: varatt.h:278
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305

References StringInfoData::data, StringInfoData::len, palloc(), SET_VARSIZE, VARDATA, and VARHDRSZ.

Referenced by json_agg_finalfn(), and json_object_agg_finalfn().

◆ composite_to_json()

static void composite_to_json ( Datum  composite,
StringInfo  result,
bool  use_line_feeds 
)
static

Definition at line 521 of file json.c.

522 {
523  HeapTupleHeader td;
524  Oid tupType;
525  int32 tupTypmod;
526  TupleDesc tupdesc;
527  HeapTupleData tmptup,
528  *tuple;
529  int i;
530  bool needsep = false;
531  const char *sep;
532  int seplen;
533 
534  /*
535  * We can avoid expensive strlen() calls by precalculating the separator
536  * length.
537  */
538  sep = use_line_feeds ? ",\n " : ",";
539  seplen = use_line_feeds ? strlen(",\n ") : strlen(",");
540 
541  td = DatumGetHeapTupleHeader(composite);
542 
543  /* Extract rowtype info and find a tupdesc */
544  tupType = HeapTupleHeaderGetTypeId(td);
545  tupTypmod = HeapTupleHeaderGetTypMod(td);
546  tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
547 
548  /* Build a temporary HeapTuple control structure */
550  tmptup.t_data = td;
551  tuple = &tmptup;
552 
553  appendStringInfoChar(result, '{');
554 
555  for (i = 0; i < tupdesc->natts; i++)
556  {
557  Datum val;
558  bool isnull;
559  char *attname;
560  JsonTypeCategory tcategory;
561  Oid outfuncoid;
562  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
563 
564  if (att->attisdropped)
565  continue;
566 
567  if (needsep)
568  appendBinaryStringInfo(result, sep, seplen);
569  needsep = true;
570 
571  attname = NameStr(att->attname);
572  escape_json(result, attname);
573  appendStringInfoChar(result, ':');
574 
575  val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
576 
577  if (isnull)
578  {
579  tcategory = JSONTYPE_NULL;
580  outfuncoid = InvalidOid;
581  }
582  else
583  json_categorize_type(att->atttypid, false, &tcategory,
584  &outfuncoid);
585 
586  datum_to_json_internal(val, isnull, result, tcategory, outfuncoid,
587  false);
588  }
589 
590  appendStringInfoChar(result, '}');
591  ReleaseTupleDesc(tupdesc);
592 }
#define NameStr(name)
Definition: c.h:749
signed int int32
Definition: c.h:497
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:295
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
Definition: htup_details.h:792
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:466
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:456
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:450
void escape_json(StringInfo buf, const char *str)
Definition: json.c:1602
NameData attname
Definition: pg_attribute.h:41
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
Definition: stringinfo.c:233
uint32 t_len
Definition: htup.h:64
HeapTupleHeader t_data
Definition: htup.h:68
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:122
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1850

References appendBinaryStringInfo(), appendStringInfoChar(), attname, datum_to_json_internal(), DatumGetHeapTupleHeader, escape_json(), heap_getattr(), HeapTupleHeaderGetDatumLength, HeapTupleHeaderGetTypeId, HeapTupleHeaderGetTypMod, i, InvalidOid, json_categorize_type(), JSONTYPE_NULL, lookup_rowtype_tupdesc(), NameStr, TupleDescData::natts, ReleaseTupleDesc, HeapTupleData::t_data, HeapTupleData::t_len, TupleDescAttr, and val.

Referenced by datum_to_json_internal(), row_to_json(), and row_to_json_pretty().

◆ datum_to_json()

Datum datum_to_json ( Datum  val,
JsonTypeCategory  tcategory,
Oid  outfuncoid 
)

Definition at line 763 of file json.c.

764 {
765  StringInfo result = makeStringInfo();
766 
767  datum_to_json_internal(val, false, result, tcategory, outfuncoid,
768  false);
769 
770  return PointerGetDatum(cstring_to_text_with_len(result->data, result->len));
771 }
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322

References cstring_to_text_with_len(), StringInfoData::data, datum_to_json_internal(), StringInfoData::len, makeStringInfo(), PointerGetDatum(), and val.

Referenced by ExecEvalJsonConstructor(), and to_json().

◆ datum_to_json_internal()

static void datum_to_json_internal ( Datum  val,
bool  is_null,
StringInfo  result,
JsonTypeCategory  tcategory,
Oid  outfuncoid,
bool  key_scalar 
)
static

Definition at line 179 of file json.c.

182 {
183  char *outputstr;
184  text *jsontext;
185 
187 
188  /* callers are expected to ensure that null keys are not passed in */
189  Assert(!(key_scalar && is_null));
190 
191  if (is_null)
192  {
193  appendBinaryStringInfo(result, "null", strlen("null"));
194  return;
195  }
196 
197  if (key_scalar &&
198  (tcategory == JSONTYPE_ARRAY ||
199  tcategory == JSONTYPE_COMPOSITE ||
200  tcategory == JSONTYPE_JSON ||
201  tcategory == JSONTYPE_CAST))
202  ereport(ERROR,
203  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
204  errmsg("key value must be scalar, not array, composite, or json")));
205 
206  switch (tcategory)
207  {
208  case JSONTYPE_ARRAY:
209  array_to_json_internal(val, result, false);
210  break;
211  case JSONTYPE_COMPOSITE:
212  composite_to_json(val, result, false);
213  break;
214  case JSONTYPE_BOOL:
215  if (key_scalar)
216  appendStringInfoChar(result, '"');
217  if (DatumGetBool(val))
218  appendBinaryStringInfo(result, "true", strlen("true"));
219  else
220  appendBinaryStringInfo(result, "false", strlen("false"));
221  if (key_scalar)
222  appendStringInfoChar(result, '"');
223  break;
224  case JSONTYPE_NUMERIC:
225  outputstr = OidOutputFunctionCall(outfuncoid, val);
226 
227  /*
228  * Don't quote a non-key if it's a valid JSON number (i.e., not
229  * "Infinity", "-Infinity", or "NaN"). Since we know this is a
230  * numeric data type's output, we simplify and open-code the
231  * validation for better performance.
232  */
233  if (!key_scalar &&
234  ((*outputstr >= '0' && *outputstr <= '9') ||
235  (*outputstr == '-' &&
236  (outputstr[1] >= '0' && outputstr[1] <= '9'))))
237  appendStringInfoString(result, outputstr);
238  else
239  {
240  appendStringInfoChar(result, '"');
241  appendStringInfoString(result, outputstr);
242  appendStringInfoChar(result, '"');
243  }
244  pfree(outputstr);
245  break;
246  case JSONTYPE_DATE:
247  {
248  char buf[MAXDATELEN + 1];
249 
250  JsonEncodeDateTime(buf, val, DATEOID, NULL);
251  appendStringInfoChar(result, '"');
252  appendStringInfoString(result, buf);
253  appendStringInfoChar(result, '"');
254  }
255  break;
256  case JSONTYPE_TIMESTAMP:
257  {
258  char buf[MAXDATELEN + 1];
259 
260  JsonEncodeDateTime(buf, val, TIMESTAMPOID, NULL);
261  appendStringInfoChar(result, '"');
262  appendStringInfoString(result, buf);
263  appendStringInfoChar(result, '"');
264  }
265  break;
267  {
268  char buf[MAXDATELEN + 1];
269 
270  JsonEncodeDateTime(buf, val, TIMESTAMPTZOID, NULL);
271  appendStringInfoChar(result, '"');
272  appendStringInfoString(result, buf);
273  appendStringInfoChar(result, '"');
274  }
275  break;
276  case JSONTYPE_JSON:
277  /* JSON and JSONB output will already be escaped */
278  outputstr = OidOutputFunctionCall(outfuncoid, val);
279  appendStringInfoString(result, outputstr);
280  pfree(outputstr);
281  break;
282  case JSONTYPE_CAST:
283  /* outfuncoid refers to a cast function, not an output function */
284  jsontext = DatumGetTextPP(OidFunctionCall1(outfuncoid, val));
285  appendBinaryStringInfo(result, VARDATA_ANY(jsontext),
286  VARSIZE_ANY_EXHDR(jsontext));
287  pfree(jsontext);
288  break;
289  default:
290  /* special-case text types to save useless palloc/memcpy cycles */
291  if (outfuncoid == F_TEXTOUT || outfuncoid == F_VARCHAROUT ||
292  outfuncoid == F_BPCHAROUT)
294  else
295  {
296  outputstr = OidOutputFunctionCall(outfuncoid, val);
297  escape_json(result, outputstr);
298  pfree(outputstr);
299  }
300  break;
301  }
302 }
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1763
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:679
#define DatumGetTextPP(X)
Definition: fmgr.h:292
#define MAXDATELEN
Definition: datetime.h:200
void escape_json_text(StringInfo buf, const text *txt)
Definition: json.c:1736
char * JsonEncodeDateTime(char *buf, Datum value, Oid typid, const int *tzp)
Definition: json.c:310
static void composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
Definition: json.c:521
@ JSONTYPE_JSON
Definition: jsonfuncs.h:76
@ JSONTYPE_TIMESTAMP
Definition: jsonfuncs.h:74
@ JSONTYPE_NUMERIC
Definition: jsonfuncs.h:72
@ JSONTYPE_DATE
Definition: jsonfuncs.h:73
@ JSONTYPE_BOOL
Definition: jsonfuncs.h:71
@ JSONTYPE_CAST
Definition: jsonfuncs.h:80
@ JSONTYPE_COMPOSITE
Definition: jsonfuncs.h:79
@ JSONTYPE_ARRAY
Definition: jsonfuncs.h:78
@ JSONTYPE_TIMESTAMPTZ
Definition: jsonfuncs.h:75
static char * buf
Definition: pg_test_fsync.c:73
void check_stack_depth(void)
Definition: postgres.c:3564
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317

References appendBinaryStringInfo(), appendStringInfoChar(), appendStringInfoString(), array_to_json_internal(), Assert, buf, check_stack_depth(), composite_to_json(), DatumGetBool(), DatumGetPointer(), DatumGetTextPP, ereport, errcode(), errmsg(), ERROR, escape_json(), escape_json_text(), JsonEncodeDateTime(), JSONTYPE_ARRAY, JSONTYPE_BOOL, JSONTYPE_CAST, JSONTYPE_COMPOSITE, JSONTYPE_DATE, JSONTYPE_JSON, JSONTYPE_NUMERIC, JSONTYPE_TIMESTAMP, JSONTYPE_TIMESTAMPTZ, MAXDATELEN, OidFunctionCall1, OidOutputFunctionCall(), pfree(), val, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by add_json(), array_dim_to_json(), composite_to_json(), datum_to_json(), json_agg_transfn_worker(), and json_object_agg_transfn_worker().

◆ escape_json()

static void escape_json ( StringInfo  buf,
const char *  str 
)

Definition at line 1602 of file json.c.

1603 {
1605 
1606  for (; *str != '\0'; str++)
1608 
1610 }
const char * str
static pg_attribute_always_inline void escape_json_char(StringInfo buf, char c)
Definition: json.c:1562
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:204

References appendStringInfoCharMacro, buf, escape_json_char(), and str.

Referenced by appendJSONKeyValue(), composite_to_json(), datum_to_json_internal(), escape_yaml(), ExplainDummyGroup(), ExplainOpenGroup(), ExplainProperty(), ExplainPropertyList(), ExplainPropertyListNested(), populate_scalar(), sn_object_field_start(), sn_scalar(), transform_string_values_object_field_start(), transformJsonTableColumn(), and write_jsonlog().

◆ escape_json_char()

static pg_attribute_always_inline void escape_json_char ( StringInfo  buf,
char  c 
)
static

Definition at line 1562 of file json.c.

1563 {
1564  switch (c)
1565  {
1566  case '\b':
1567  appendStringInfoString(buf, "\\b");
1568  break;
1569  case '\f':
1570  appendStringInfoString(buf, "\\f");
1571  break;
1572  case '\n':
1573  appendStringInfoString(buf, "\\n");
1574  break;
1575  case '\r':
1576  appendStringInfoString(buf, "\\r");
1577  break;
1578  case '\t':
1579  appendStringInfoString(buf, "\\t");
1580  break;
1581  case '"':
1582  appendStringInfoString(buf, "\\\"");
1583  break;
1584  case '\\':
1585  appendStringInfoString(buf, "\\\\");
1586  break;
1587  default:
1588  if ((unsigned char) c < ' ')
1589  appendStringInfo(buf, "\\u%04x", (int) c);
1590  else
1592  break;
1593  }
1594 }
char * c
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:97

References appendStringInfo(), appendStringInfoCharMacro, appendStringInfoString(), and buf.

Referenced by escape_json(), and escape_json_with_len().

◆ escape_json_text()

void escape_json_text ( StringInfo  buf,
const text txt 
)

Definition at line 1736 of file json.c.

1737 {
1738  /* must cast away the const, unfortunately */
1739  text *tunpacked = pg_detoast_datum_packed(unconstify(text *, txt));
1740  int len = VARSIZE_ANY_EXHDR(tunpacked);
1741  char *str;
1742 
1743  str = VARDATA_ANY(tunpacked);
1744 
1746 
1747  /* pfree any detoasted values */
1748  if (tunpacked != txt)
1749  pfree(tunpacked);
1750 }
#define unconstify(underlying_type, expr)
Definition: c.h:1248
struct varlena * pg_detoast_datum_packed(struct varlena *datum)
Definition: fmgr.c:1864
void escape_json_with_len(StringInfo buf, const char *str, int len)
Definition: json.c:1631
const void size_t len

References buf, escape_json_with_len(), len, pfree(), pg_detoast_datum_packed(), str, unconstify, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by datum_to_json_internal(), json_object(), json_object_two_arg(), and transform_string_values_scalar().

◆ escape_json_with_len()

void escape_json_with_len ( StringInfo  buf,
const char *  str,
int  len 
)

Definition at line 1631 of file json.c.

1632 {
1633  int vlen;
1634 
1635  Assert(len >= 0);
1636 
1637  /*
1638  * Since we know the minimum length we'll need to append, let's just
1639  * enlarge the buffer now rather than incrementally making more space when
1640  * we run out. Add two extra bytes for the enclosing quotes.
1641  */
1642  enlargeStringInfo(buf, len + 2);
1643 
1644  /*
1645  * Figure out how many bytes to process using SIMD. Round 'len' down to
1646  * the previous multiple of sizeof(Vector8), assuming that's a power-of-2.
1647  */
1648  vlen = len & (int) (~(sizeof(Vector8) - 1));
1649 
1651 
1652  for (int i = 0, copypos = 0;;)
1653  {
1654  /*
1655  * To speed this up, try searching sizeof(Vector8) bytes at once for
1656  * special characters that we need to escape. When we find one, we
1657  * fall out of the Vector8 loop and copy the portion we've vector
1658  * searched and then we process sizeof(Vector8) bytes one byte at a
1659  * time. Once done, come back and try doing vector searching again.
1660  * We'll also process any remaining bytes at the tail end of the
1661  * string byte-by-byte. This optimization assumes that most chunks of
1662  * sizeof(Vector8) bytes won't contain any special characters.
1663  */
1664  for (; i < vlen; i += sizeof(Vector8))
1665  {
1666  Vector8 chunk;
1667 
1668  vector8_load(&chunk, (const uint8 *) &str[i]);
1669 
1670  /*
1671  * Break on anything less than ' ' or if we find a '"' or '\\'.
1672  * Those need special handling. That's done in the per-byte loop.
1673  */
1674  if (vector8_has_le(chunk, (unsigned char) 0x1F) ||
1675  vector8_has(chunk, (unsigned char) '"') ||
1676  vector8_has(chunk, (unsigned char) '\\'))
1677  break;
1678 
1679 #ifdef ESCAPE_JSON_FLUSH_AFTER
1680 
1681  /*
1682  * Flush what's been checked so far out to the destination buffer
1683  * every so often to avoid having to re-read cachelines when
1684  * escaping large strings.
1685  */
1686  if (i - copypos >= ESCAPE_JSON_FLUSH_AFTER)
1687  {
1688  appendBinaryStringInfo(buf, &str[copypos], i - copypos);
1689  copypos = i;
1690  }
1691 #endif
1692  }
1693 
1694  /*
1695  * Write to the destination up to the point that we've vector searched
1696  * so far. Do this only when switching into per-byte mode rather than
1697  * once every sizeof(Vector8) bytes.
1698  */
1699  if (copypos < i)
1700  {
1701  appendBinaryStringInfo(buf, &str[copypos], i - copypos);
1702  copypos = i;
1703  }
1704 
1705  /*
1706  * Per-byte loop for Vector8s containing special chars and for
1707  * processing the tail of the string.
1708  */
1709  for (int b = 0; b < sizeof(Vector8); b++)
1710  {
1711  /* check if we've finished */
1712  if (i == len)
1713  goto done;
1714 
1715  Assert(i < len);
1716 
1717  escape_json_char(buf, str[i++]);
1718  }
1719 
1720  copypos = i;
1721  /* We're not done yet. Try the vector search again. */
1722  }
1723 
1724 done:
1726 }
unsigned char uint8
Definition: c.h:507
uint64 chunk
int b
Definition: isn.c:70
#define ESCAPE_JSON_FLUSH_AFTER
Definition: json.c:1622
static bool vector8_has_le(const Vector8 v, const uint8 c)
Definition: simd.h:213
static void vector8_load(Vector8 *v, const uint8 *s)
Definition: simd.h:108
uint64 Vector8
Definition: simd.h:60
static bool vector8_has(const Vector8 v, const uint8 c)
Definition: simd.h:162
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:289

References appendBinaryStringInfo(), appendStringInfoCharMacro, Assert, b, buf, chunk, enlargeStringInfo(), escape_json_char(), ESCAPE_JSON_FLUSH_AFTER, i, len, str, vector8_has(), vector8_has_le(), and vector8_load().

Referenced by AddFileToBackupManifest(), escape_json_text(), hstore_to_json(), hstore_to_json_loose(), jsonb_put_escaped_value(), populate_scalar(), and printJsonPathItem().

◆ json_agg_finalfn()

Datum json_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 879 of file json.c.

880 {
882 
883  /* cannot be called directly because of internal-type argument */
884  Assert(AggCheckCallContext(fcinfo, NULL));
885 
886  state = PG_ARGISNULL(0) ?
887  NULL :
889 
890  /* NULL result for no rows in, as is standard with aggregates */
891  if (state == NULL)
892  PG_RETURN_NULL();
893 
894  /* Else return state with appropriate array terminator added */
896 }
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_RETURN_NULL()
Definition: fmgr.h:345
static text * catenate_stringinfo_string(StringInfo buffer, const char *addon)
Definition: json.c:1209
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4511
Definition: regguts.h:323

References AggCheckCallContext(), Assert, catenate_stringinfo_string(), PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_NULL, and PG_RETURN_TEXT_P.

◆ json_agg_strict_transfn()

Datum json_agg_strict_transfn ( PG_FUNCTION_ARGS  )

Definition at line 870 of file json.c.

871 {
872  return json_agg_transfn_worker(fcinfo, true);
873 }
static Datum json_agg_transfn_worker(FunctionCallInfo fcinfo, bool absent_on_null)
Definition: json.c:779

References json_agg_transfn_worker().

◆ json_agg_transfn()

Datum json_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 861 of file json.c.

862 {
863  return json_agg_transfn_worker(fcinfo, false);
864 }

References json_agg_transfn_worker().

◆ json_agg_transfn_worker()

static Datum json_agg_transfn_worker ( FunctionCallInfo  fcinfo,
bool  absent_on_null 
)
static

Definition at line 779 of file json.c.

780 {
781  MemoryContext aggcontext,
782  oldcontext;
784  Datum val;
785 
786  if (!AggCheckCallContext(fcinfo, &aggcontext))
787  {
788  /* cannot be called directly because of internal-type argument */
789  elog(ERROR, "json_agg_transfn called in non-aggregate context");
790  }
791 
792  if (PG_ARGISNULL(0))
793  {
794  Oid arg_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
795 
796  if (arg_type == InvalidOid)
797  ereport(ERROR,
798  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
799  errmsg("could not determine input data type")));
800 
801  /*
802  * Make this state object in a context where it will persist for the
803  * duration of the aggregate call. MemoryContextSwitchTo is only
804  * needed the first time, as the StringInfo routines make sure they
805  * use the right context to enlarge the object if necessary.
806  */
807  oldcontext = MemoryContextSwitchTo(aggcontext);
808  state = (JsonAggState *) palloc(sizeof(JsonAggState));
809  state->str = makeStringInfo();
810  MemoryContextSwitchTo(oldcontext);
811 
812  appendStringInfoChar(state->str, '[');
813  json_categorize_type(arg_type, false, &state->val_category,
814  &state->val_output_func);
815  }
816  else
817  {
819  }
820 
821  if (absent_on_null && PG_ARGISNULL(1))
823 
824  if (state->str->len > 1)
825  appendStringInfoString(state->str, ", ");
826 
827  /* fast path for NULLs */
828  if (PG_ARGISNULL(1))
829  {
831  InvalidOid, false);
833  }
834 
835  val = PG_GETARG_DATUM(1);
836 
837  /* add some whitespace if structured type and not first item */
838  if (!PG_ARGISNULL(0) && state->str->len > 1 &&
839  (state->val_category == JSONTYPE_ARRAY ||
840  state->val_category == JSONTYPE_COMPOSITE))
841  {
842  appendStringInfoString(state->str, "\n ");
843  }
844 
845  datum_to_json_internal(val, false, state->str, state->val_category,
846  state->val_output_func, false);
847 
848  /*
849  * The transition type for json_agg() is declared to be "internal", which
850  * is a pass-by-value type the same size as a pointer. So we can safely
851  * pass the JsonAggState pointer through nodeAgg.c's machinations.
852  */
854 }
#define elog(elevel,...)
Definition: elog.h:225
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1910
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
MemoryContextSwitchTo(old_ctx)
FmgrInfo * flinfo
Definition: fmgr.h:87

References AggCheckCallContext(), appendStringInfoChar(), appendStringInfoString(), datum_to_json_internal(), elog, ereport, errcode(), errmsg(), ERROR, FunctionCallInfoBaseData::flinfo, get_fn_expr_argtype(), InvalidOid, json_categorize_type(), JSONTYPE_ARRAY, JSONTYPE_COMPOSITE, JSONTYPE_NULL, makeStringInfo(), MemoryContextSwitchTo(), palloc(), PG_ARGISNULL, PG_GETARG_DATUM, PG_GETARG_POINTER, PG_RETURN_POINTER, and val.

Referenced by json_agg_strict_transfn(), and json_agg_transfn().

◆ json_build_array()

Datum json_build_array ( PG_FUNCTION_ARGS  )

Definition at line 1374 of file json.c.

1375 {
1376  Datum *args;
1377  bool *nulls;
1378  Oid *types;
1379 
1380  /* build argument values to build the object */
1381  int nargs = extract_variadic_args(fcinfo, 0, true,
1382  &args, &types, &nulls);
1383 
1384  if (nargs < 0)
1385  PG_RETURN_NULL();
1386 
1387  PG_RETURN_DATUM(json_build_array_worker(nargs, args, nulls, types, false));
1388 }
struct typedefs * types
Definition: ecpg.c:29
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
int extract_variadic_args(FunctionCallInfo fcinfo, int variadic_start, bool convert_unknown, Datum **args, Oid **types, bool **nulls)
Definition: funcapi.c:2005
Datum json_build_array_worker(int nargs, const Datum *args, const bool *nulls, const Oid *types, bool absent_on_null)
Definition: json.c:1344

References generate_unaccent_rules::args, extract_variadic_args(), json_build_array_worker(), PG_RETURN_DATUM, PG_RETURN_NULL, and types.

◆ json_build_array_noargs()

Datum json_build_array_noargs ( PG_FUNCTION_ARGS  )

Definition at line 1394 of file json.c.

1395 {
1397 }

References cstring_to_text_with_len(), and PG_RETURN_TEXT_P.

◆ json_build_array_worker()

Datum json_build_array_worker ( int  nargs,
const Datum args,
const bool nulls,
const Oid types,
bool  absent_on_null 
)

Definition at line 1344 of file json.c.

1346 {
1347  int i;
1348  const char *sep = "";
1349  StringInfo result;
1350 
1351  result = makeStringInfo();
1352 
1353  appendStringInfoChar(result, '[');
1354 
1355  for (i = 0; i < nargs; i++)
1356  {
1357  if (absent_on_null && nulls[i])
1358  continue;
1359 
1360  appendStringInfoString(result, sep);
1361  sep = ", ";
1362  add_json(args[i], nulls[i], result, types[i], false);
1363  }
1364 
1365  appendStringInfoChar(result, ']');
1366 
1367  return PointerGetDatum(cstring_to_text_with_len(result->data, result->len));
1368 }
static void add_json(Datum val, bool is_null, StringInfo result, Oid val_type, bool key_scalar)
Definition: json.c:602

References add_json(), appendStringInfoChar(), appendStringInfoString(), generate_unaccent_rules::args, cstring_to_text_with_len(), StringInfoData::data, i, StringInfoData::len, makeStringInfo(), PointerGetDatum(), and types.

Referenced by ExecEvalJsonConstructor(), and json_build_array().

◆ json_build_object()

Datum json_build_object ( PG_FUNCTION_ARGS  )

Definition at line 1318 of file json.c.

1319 {
1320  Datum *args;
1321  bool *nulls;
1322  Oid *types;
1323 
1324  /* build argument values to build the object */
1325  int nargs = extract_variadic_args(fcinfo, 0, true,
1326  &args, &types, &nulls);
1327 
1328  if (nargs < 0)
1329  PG_RETURN_NULL();
1330 
1331  PG_RETURN_DATUM(json_build_object_worker(nargs, args, nulls, types, false, false));
1332 }
Datum json_build_object_worker(int nargs, const Datum *args, const bool *nulls, const Oid *types, bool absent_on_null, bool unique_keys)
Definition: json.c:1224

References generate_unaccent_rules::args, extract_variadic_args(), json_build_object_worker(), PG_RETURN_DATUM, PG_RETURN_NULL, and types.

◆ json_build_object_noargs()

Datum json_build_object_noargs ( PG_FUNCTION_ARGS  )

Definition at line 1338 of file json.c.

1339 {
1341 }

References cstring_to_text_with_len(), and PG_RETURN_TEXT_P.

◆ json_build_object_worker()

Datum json_build_object_worker ( int  nargs,
const Datum args,
const bool nulls,
const Oid types,
bool  absent_on_null,
bool  unique_keys 
)

Definition at line 1224 of file json.c.

1226 {
1227  int i;
1228  const char *sep = "";
1229  StringInfo result;
1230  JsonUniqueBuilderState unique_check;
1231 
1232  if (nargs % 2 != 0)
1233  ereport(ERROR,
1234  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1235  errmsg("argument list must have even number of elements"),
1236  /* translator: %s is a SQL function name */
1237  errhint("The arguments of %s must consist of alternating keys and values.",
1238  "json_build_object()")));
1239 
1240  result = makeStringInfo();
1241 
1242  appendStringInfoChar(result, '{');
1243 
1244  if (unique_keys)
1245  json_unique_builder_init(&unique_check);
1246 
1247  for (i = 0; i < nargs; i += 2)
1248  {
1249  StringInfo out;
1250  bool skip;
1251  int key_offset;
1252 
1253  /* Skip null values if absent_on_null */
1254  skip = absent_on_null && nulls[i + 1];
1255 
1256  if (skip)
1257  {
1258  /* If key uniqueness check is needed we must save skipped keys */
1259  if (!unique_keys)
1260  continue;
1261 
1262  out = json_unique_builder_get_throwawaybuf(&unique_check);
1263  }
1264  else
1265  {
1266  appendStringInfoString(result, sep);
1267  sep = ", ";
1268  out = result;
1269  }
1270 
1271  /* process key */
1272  if (nulls[i])
1273  ereport(ERROR,
1274  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1275  errmsg("null value not allowed for object key")));
1276 
1277  /* save key offset before appending it */
1278  key_offset = out->len;
1279 
1280  add_json(args[i], false, out, types[i], true);
1281 
1282  if (unique_keys)
1283  {
1284  /*
1285  * check key uniqueness after key appending
1286  *
1287  * Copy the key first, instead of pointing into the buffer. It
1288  * will be added to the hash table, but the buffer may get
1289  * reallocated as we're appending more data to it. That would
1290  * invalidate pointers to keys in the current buffer.
1291  */
1292  const char *key = pstrdup(&out->data[key_offset]);
1293 
1294  if (!json_unique_check_key(&unique_check.check, key, 0))
1295  ereport(ERROR,
1296  errcode(ERRCODE_DUPLICATE_JSON_OBJECT_KEY_VALUE),
1297  errmsg("duplicate JSON object key value: %s", key));
1298 
1299  if (skip)
1300  continue;
1301  }
1302 
1303  appendStringInfoString(result, " : ");
1304 
1305  /* process value */
1306  add_json(args[i + 1], nulls[i + 1], result, types[i + 1], false);
1307  }
1308 
1309  appendStringInfoChar(result, '}');
1310 
1311  return PointerGetDatum(cstring_to_text_with_len(result->data, result->len));
1312 }
int errhint(const char *fmt,...)
Definition: elog.c:1317
static StringInfo json_unique_builder_get_throwawaybuf(JsonUniqueBuilderState *cxt)
Definition: json.c:978
static bool json_unique_check_key(JsonUniqueCheckState *cxt, const char *key, int object_id)
Definition: json.c:958
static void json_unique_builder_init(JsonUniqueBuilderState *cxt)
Definition: json.c:950
char * pstrdup(const char *in)
Definition: mcxt.c:1696
static const struct exclude_list_item skip[]
Definition: pg_checksums.c:108
JsonUniqueCheckState check
Definition: json.c:71

References add_json(), appendStringInfoChar(), appendStringInfoString(), generate_unaccent_rules::args, JsonUniqueBuilderState::check, cstring_to_text_with_len(), StringInfoData::data, ereport, errcode(), errhint(), errmsg(), ERROR, i, json_unique_builder_get_throwawaybuf(), json_unique_builder_init(), json_unique_check_key(), sort-test::key, StringInfoData::len, makeStringInfo(), PointerGetDatum(), pstrdup(), skip, and types.

Referenced by ExecEvalJsonConstructor(), and json_build_object().

◆ json_in()

Datum json_in ( PG_FUNCTION_ARGS  )

Definition at line 107 of file json.c.

108 {
109  char *json = PG_GETARG_CSTRING(0);
110  text *result = cstring_to_text(json);
111  JsonLexContext lex;
112 
113  /* validate it */
114  makeJsonLexContext(&lex, result, false);
115  if (!pg_parse_json_or_errsave(&lex, &nullSemAction, fcinfo->context))
116  PG_RETURN_NULL();
117 
118  /* Internal representation is the same as text */
119  PG_RETURN_TEXT_P(result);
120 }
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
const JsonSemAction nullSemAction
Definition: jsonapi.c:285
JsonLexContext * makeJsonLexContext(JsonLexContext *lex, text *json, bool need_escapes)
Definition: jsonfuncs.c:539
bool pg_parse_json_or_errsave(JsonLexContext *lex, const JsonSemAction *sem, Node *escontext)
Definition: jsonfuncs.c:518
text * cstring_to_text(const char *s)
Definition: varlena.c:184

References cstring_to_text(), makeJsonLexContext(), nullSemAction, PG_GETARG_CSTRING, pg_parse_json_or_errsave(), PG_RETURN_NULL, and PG_RETURN_TEXT_P.

◆ json_object()

Datum json_object ( PG_FUNCTION_ARGS  )

Definition at line 1406 of file json.c.

1407 {
1408  ArrayType *in_array = PG_GETARG_ARRAYTYPE_P(0);
1409  int ndims = ARR_NDIM(in_array);
1410  StringInfoData result;
1411  Datum *in_datums;
1412  bool *in_nulls;
1413  int in_count,
1414  count,
1415  i;
1416  text *rval;
1417 
1418  switch (ndims)
1419  {
1420  case 0:
1422  break;
1423 
1424  case 1:
1425  if ((ARR_DIMS(in_array)[0]) % 2)
1426  ereport(ERROR,
1427  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1428  errmsg("array must have even number of elements")));
1429  break;
1430 
1431  case 2:
1432  if ((ARR_DIMS(in_array)[1]) != 2)
1433  ereport(ERROR,
1434  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1435  errmsg("array must have two columns")));
1436  break;
1437 
1438  default:
1439  ereport(ERROR,
1440  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1441  errmsg("wrong number of array subscripts")));
1442  }
1443 
1444  deconstruct_array_builtin(in_array, TEXTOID, &in_datums, &in_nulls, &in_count);
1445 
1446  count = in_count / 2;
1447 
1448  initStringInfo(&result);
1449 
1450  appendStringInfoChar(&result, '{');
1451 
1452  for (i = 0; i < count; ++i)
1453  {
1454  if (in_nulls[i * 2])
1455  ereport(ERROR,
1456  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1457  errmsg("null value not allowed for object key")));
1458 
1459  if (i > 0)
1460  appendStringInfoString(&result, ", ");
1461  escape_json_text(&result, (text *) DatumGetPointer(in_datums[i * 2]));
1462  appendStringInfoString(&result, " : ");
1463  if (in_nulls[i * 2 + 1])
1464  appendStringInfoString(&result, "null");
1465  else
1466  {
1467  escape_json_text(&result,
1468  (text *) DatumGetPointer(in_datums[i * 2 + 1]));
1469  }
1470  }
1471 
1472  appendStringInfoChar(&result, '}');
1473 
1474  pfree(in_datums);
1475  pfree(in_nulls);
1476 
1477  rval = cstring_to_text_with_len(result.data, result.len);
1478  pfree(result.data);
1479 
1480  PG_RETURN_TEXT_P(rval);
1481 }
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:263
void deconstruct_array_builtin(ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3685
#define CStringGetTextDatum(s)
Definition: builtins.h:97
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59

References appendStringInfoChar(), appendStringInfoString(), ARR_DIMS, ARR_NDIM, cstring_to_text_with_len(), CStringGetTextDatum, StringInfoData::data, DatumGetPointer(), deconstruct_array_builtin(), ereport, errcode(), errmsg(), ERROR, escape_json_text(), i, initStringInfo(), StringInfoData::len, pfree(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_DATUM, and PG_RETURN_TEXT_P.

◆ json_object_agg_finalfn()

Datum json_object_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 1186 of file json.c.

1187 {
1189 
1190  /* cannot be called directly because of internal-type argument */
1191  Assert(AggCheckCallContext(fcinfo, NULL));
1192 
1193  state = PG_ARGISNULL(0) ? NULL : (JsonAggState *) PG_GETARG_POINTER(0);
1194 
1195  /* NULL result for no rows in, as is standard with aggregates */
1196  if (state == NULL)
1197  PG_RETURN_NULL();
1198 
1199  /* Else return state with appropriate object terminator added */
1201 }

References AggCheckCallContext(), Assert, catenate_stringinfo_string(), PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_NULL, and PG_RETURN_TEXT_P.

◆ json_object_agg_strict_transfn()

Datum json_object_agg_strict_transfn ( PG_FUNCTION_ARGS  )

Definition at line 1159 of file json.c.

1160 {
1161  return json_object_agg_transfn_worker(fcinfo, true, false);
1162 }
static Datum json_object_agg_transfn_worker(FunctionCallInfo fcinfo, bool absent_on_null, bool unique_keys)
Definition: json.c:1002

References json_object_agg_transfn_worker().

◆ json_object_agg_transfn()

Datum json_object_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 1150 of file json.c.

1151 {
1152  return json_object_agg_transfn_worker(fcinfo, false, false);
1153 }

References json_object_agg_transfn_worker().

◆ json_object_agg_transfn_worker()

static Datum json_object_agg_transfn_worker ( FunctionCallInfo  fcinfo,
bool  absent_on_null,
bool  unique_keys 
)
static

Definition at line 1002 of file json.c.

1004 {
1005  MemoryContext aggcontext,
1006  oldcontext;
1008  StringInfo out;
1009  Datum arg;
1010  bool skip;
1011  int key_offset;
1012 
1013  if (!AggCheckCallContext(fcinfo, &aggcontext))
1014  {
1015  /* cannot be called directly because of internal-type argument */
1016  elog(ERROR, "json_object_agg_transfn called in non-aggregate context");
1017  }
1018 
1019  if (PG_ARGISNULL(0))
1020  {
1021  Oid arg_type;
1022 
1023  /*
1024  * Make the StringInfo in a context where it will persist for the
1025  * duration of the aggregate call. Switching context is only needed
1026  * for this initial step, as the StringInfo and dynahash routines make
1027  * sure they use the right context to enlarge the object if necessary.
1028  */
1029  oldcontext = MemoryContextSwitchTo(aggcontext);
1030  state = (JsonAggState *) palloc(sizeof(JsonAggState));
1031  state->str = makeStringInfo();
1032  if (unique_keys)
1033  json_unique_builder_init(&state->unique_check);
1034  else
1035  memset(&state->unique_check, 0, sizeof(state->unique_check));
1036  MemoryContextSwitchTo(oldcontext);
1037 
1038  arg_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
1039 
1040  if (arg_type == InvalidOid)
1041  ereport(ERROR,
1042  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1043  errmsg("could not determine data type for argument %d", 1)));
1044 
1045  json_categorize_type(arg_type, false, &state->key_category,
1046  &state->key_output_func);
1047 
1048  arg_type = get_fn_expr_argtype(fcinfo->flinfo, 2);
1049 
1050  if (arg_type == InvalidOid)
1051  ereport(ERROR,
1052  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1053  errmsg("could not determine data type for argument %d", 2)));
1054 
1055  json_categorize_type(arg_type, false, &state->val_category,
1056  &state->val_output_func);
1057 
1058  appendStringInfoString(state->str, "{ ");
1059  }
1060  else
1061  {
1063  }
1064 
1065  /*
1066  * Note: since json_object_agg() is declared as taking type "any", the
1067  * parser will not do any type conversion on unknown-type literals (that
1068  * is, undecorated strings or NULLs). Such values will arrive here as
1069  * type UNKNOWN, which fortunately does not matter to us, since
1070  * unknownout() works fine.
1071  */
1072 
1073  if (PG_ARGISNULL(1))
1074  ereport(ERROR,
1075  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1076  errmsg("null value not allowed for object key")));
1077 
1078  /* Skip null values if absent_on_null */
1079  skip = absent_on_null && PG_ARGISNULL(2);
1080 
1081  if (skip)
1082  {
1083  /*
1084  * We got a NULL value and we're not storing those; if we're not
1085  * testing key uniqueness, we're done. If we are, use the throwaway
1086  * buffer to store the key name so that we can check it.
1087  */
1088  if (!unique_keys)
1090 
1091  out = json_unique_builder_get_throwawaybuf(&state->unique_check);
1092  }
1093  else
1094  {
1095  out = state->str;
1096 
1097  /*
1098  * Append comma delimiter only if we have already output some fields
1099  * after the initial string "{ ".
1100  */
1101  if (out->len > 2)
1102  appendStringInfoString(out, ", ");
1103  }
1104 
1105  arg = PG_GETARG_DATUM(1);
1106 
1107  key_offset = out->len;
1108 
1109  datum_to_json_internal(arg, false, out, state->key_category,
1110  state->key_output_func, true);
1111 
1112  if (unique_keys)
1113  {
1114  /*
1115  * Copy the key first, instead of pointing into the buffer. It will be
1116  * added to the hash table, but the buffer may get reallocated as
1117  * we're appending more data to it. That would invalidate pointers to
1118  * keys in the current buffer.
1119  */
1120  const char *key = MemoryContextStrdup(aggcontext,
1121  &out->data[key_offset]);
1122 
1123  if (!json_unique_check_key(&state->unique_check.check, key, 0))
1124  ereport(ERROR,
1125  errcode(ERRCODE_DUPLICATE_JSON_OBJECT_KEY_VALUE),
1126  errmsg("duplicate JSON object key value: %s", key));
1127 
1128  if (skip)
1130  }
1131 
1132  appendStringInfoString(state->str, " : ");
1133 
1134  if (PG_ARGISNULL(2))
1135  arg = (Datum) 0;
1136  else
1137  arg = PG_GETARG_DATUM(2);
1138 
1140  state->val_category,
1141  state->val_output_func, false);
1142 
1144 }
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1683
void * arg

References AggCheckCallContext(), appendStringInfoString(), arg, StringInfoData::data, datum_to_json_internal(), elog, ereport, errcode(), errmsg(), ERROR, FunctionCallInfoBaseData::flinfo, get_fn_expr_argtype(), InvalidOid, json_categorize_type(), json_unique_builder_get_throwawaybuf(), json_unique_builder_init(), json_unique_check_key(), sort-test::key, StringInfoData::len, makeStringInfo(), MemoryContextStrdup(), MemoryContextSwitchTo(), palloc(), PG_ARGISNULL, PG_GETARG_DATUM, PG_GETARG_POINTER, PG_RETURN_POINTER, and skip.

Referenced by json_object_agg_strict_transfn(), json_object_agg_transfn(), json_object_agg_unique_strict_transfn(), and json_object_agg_unique_transfn().

◆ json_object_agg_unique_strict_transfn()

Datum json_object_agg_unique_strict_transfn ( PG_FUNCTION_ARGS  )

Definition at line 1177 of file json.c.

1178 {
1179  return json_object_agg_transfn_worker(fcinfo, true, true);
1180 }

References json_object_agg_transfn_worker().

◆ json_object_agg_unique_transfn()

Datum json_object_agg_unique_transfn ( PG_FUNCTION_ARGS  )

Definition at line 1168 of file json.c.

1169 {
1170  return json_object_agg_transfn_worker(fcinfo, false, true);
1171 }

References json_object_agg_transfn_worker().

◆ json_object_two_arg()

Datum json_object_two_arg ( PG_FUNCTION_ARGS  )

Definition at line 1490 of file json.c.

1491 {
1492  ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(0);
1493  ArrayType *val_array = PG_GETARG_ARRAYTYPE_P(1);
1494  int nkdims = ARR_NDIM(key_array);
1495  int nvdims = ARR_NDIM(val_array);
1496  StringInfoData result;
1497  Datum *key_datums,
1498  *val_datums;
1499  bool *key_nulls,
1500  *val_nulls;
1501  int key_count,
1502  val_count,
1503  i;
1504  text *rval;
1505 
1506  if (nkdims > 1 || nkdims != nvdims)
1507  ereport(ERROR,
1508  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1509  errmsg("wrong number of array subscripts")));
1510 
1511  if (nkdims == 0)
1513 
1514  deconstruct_array_builtin(key_array, TEXTOID, &key_datums, &key_nulls, &key_count);
1515  deconstruct_array_builtin(val_array, TEXTOID, &val_datums, &val_nulls, &val_count);
1516 
1517  if (key_count != val_count)
1518  ereport(ERROR,
1519  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1520  errmsg("mismatched array dimensions")));
1521 
1522  initStringInfo(&result);
1523 
1524  appendStringInfoChar(&result, '{');
1525 
1526  for (i = 0; i < key_count; ++i)
1527  {
1528  if (key_nulls[i])
1529  ereport(ERROR,
1530  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1531  errmsg("null value not allowed for object key")));
1532 
1533  if (i > 0)
1534  appendStringInfoString(&result, ", ");
1535  escape_json_text(&result, (text *) DatumGetPointer(key_datums[i]));
1536  appendStringInfoString(&result, " : ");
1537  if (val_nulls[i])
1538  appendStringInfoString(&result, "null");
1539  else
1540  escape_json_text(&result,
1541  (text *) DatumGetPointer(val_datums[i]));
1542  }
1543 
1544  appendStringInfoChar(&result, '}');
1545 
1546  pfree(key_datums);
1547  pfree(key_nulls);
1548  pfree(val_datums);
1549  pfree(val_nulls);
1550 
1551  rval = cstring_to_text_with_len(result.data, result.len);
1552  pfree(result.data);
1553 
1554  PG_RETURN_TEXT_P(rval);
1555 }

References appendStringInfoChar(), appendStringInfoString(), ARR_NDIM, cstring_to_text_with_len(), CStringGetTextDatum, StringInfoData::data, DatumGetPointer(), deconstruct_array_builtin(), ereport, errcode(), errmsg(), ERROR, escape_json_text(), i, initStringInfo(), StringInfoData::len, pfree(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_DATUM, and PG_RETURN_TEXT_P.

◆ json_out()

Datum json_out ( PG_FUNCTION_ARGS  )

Definition at line 126 of file json.c.

127 {
128  /* we needn't detoast because text_to_cstring will handle that */
129  Datum txt = PG_GETARG_DATUM(0);
130 
132 }
#define TextDatumGetCString(d)
Definition: builtins.h:98
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362

References PG_GETARG_DATUM, PG_RETURN_CSTRING, and TextDatumGetCString.

◆ json_recv()

Datum json_recv ( PG_FUNCTION_ARGS  )

Definition at line 152 of file json.c.

153 {
155  char *str;
156  int nbytes;
157  JsonLexContext lex;
158 
159  str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
160 
161  /* Validate it. */
163  false);
165 
167 }
JsonLexContext * makeJsonLexContextCstringLen(JsonLexContext *lex, const char *json, size_t len, int encoding, bool need_escapes)
Definition: jsonapi.c:390
#define pg_parse_json_or_ereport(lex, sem)
Definition: jsonfuncs.h:47
int GetDatabaseEncoding(void)
Definition: mbutils.c:1261
char * pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes)
Definition: pqformat.c:546
StringInfoData * StringInfo
Definition: stringinfo.h:54

References buf, cstring_to_text_with_len(), GetDatabaseEncoding(), makeJsonLexContextCstringLen(), nullSemAction, PG_GETARG_POINTER, pg_parse_json_or_ereport, PG_RETURN_TEXT_P, pq_getmsgtext(), and str.

◆ json_send()

Datum json_send ( PG_FUNCTION_ARGS  )

Definition at line 138 of file json.c.

139 {
140  text *t = PG_GETARG_TEXT_PP(0);
142 
146 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
void pq_sendtext(StringInfo buf, const char *str, int slen)
Definition: pqformat.c:172
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:326
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:346

References buf, PG_GETARG_TEXT_PP, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendtext(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ json_typeof()

Datum json_typeof ( PG_FUNCTION_ARGS  )

Definition at line 1874 of file json.c.

1875 {
1876  text *json = PG_GETARG_TEXT_PP(0);
1877  JsonLexContext lex;
1878  char *type;
1879  JsonParseErrorType result;
1880 
1881  /* Lex exactly one token from the input and check its type. */
1882  makeJsonLexContext(&lex, json, false);
1883  result = json_lex(&lex);
1884  if (result != JSON_SUCCESS)
1885  json_errsave_error(result, &lex, NULL);
1886 
1887  switch (lex.token_type)
1888  {
1890  type = "object";
1891  break;
1893  type = "array";
1894  break;
1895  case JSON_TOKEN_STRING:
1896  type = "string";
1897  break;
1898  case JSON_TOKEN_NUMBER:
1899  type = "number";
1900  break;
1901  case JSON_TOKEN_TRUE:
1902  case JSON_TOKEN_FALSE:
1903  type = "boolean";
1904  break;
1905  case JSON_TOKEN_NULL:
1906  type = "null";
1907  break;
1908  default:
1909  elog(ERROR, "unexpected json token: %d", lex.token_type);
1910  }
1911 
1913 }
JsonParseErrorType json_lex(JsonLexContext *lex)
Definition: jsonapi.c:1498
JsonParseErrorType
Definition: jsonapi.h:35
@ JSON_SUCCESS
Definition: jsonapi.h:36
@ JSON_TOKEN_FALSE
Definition: jsonapi.h:29
@ JSON_TOKEN_TRUE
Definition: jsonapi.h:28
@ JSON_TOKEN_NULL
Definition: jsonapi.h:30
@ JSON_TOKEN_OBJECT_START
Definition: jsonapi.h:22
@ JSON_TOKEN_NUMBER
Definition: jsonapi.h:21
@ JSON_TOKEN_STRING
Definition: jsonapi.h:20
@ JSON_TOKEN_ARRAY_START
Definition: jsonapi.h:24
void json_errsave_error(JsonParseErrorType error, JsonLexContext *lex, Node *escontext)
Definition: jsonfuncs.c:640
JsonTokenType token_type
Definition: jsonapi.h:107
const char * type

References cstring_to_text(), elog, ERROR, json_errsave_error(), json_lex(), JSON_SUCCESS, JSON_TOKEN_ARRAY_START, JSON_TOKEN_FALSE, JSON_TOKEN_NULL, JSON_TOKEN_NUMBER, JSON_TOKEN_OBJECT_START, JSON_TOKEN_STRING, JSON_TOKEN_TRUE, makeJsonLexContext(), PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, JsonLexContext::token_type, and type.

◆ json_unique_builder_get_throwawaybuf()

static StringInfo json_unique_builder_get_throwawaybuf ( JsonUniqueBuilderState cxt)
static

Definition at line 978 of file json.c.

979 {
980  StringInfo out = &cxt->skipped_keys;
981 
982  if (!out->data)
983  {
985 
986  initStringInfo(out);
987  MemoryContextSwitchTo(oldcxt);
988  }
989  else
990  /* Just reset the string to empty */
991  out->len = 0;
992 
993  return out;
994 }
StringInfoData skipped_keys
Definition: json.c:72
MemoryContext mcxt
Definition: json.c:73

References StringInfoData::data, initStringInfo(), StringInfoData::len, JsonUniqueBuilderState::mcxt, MemoryContextSwitchTo(), and JsonUniqueBuilderState::skipped_keys.

Referenced by json_build_object_worker(), and json_object_agg_transfn_worker().

◆ json_unique_builder_init()

static void json_unique_builder_init ( JsonUniqueBuilderState cxt)
static

Definition at line 950 of file json.c.

951 {
953  cxt->mcxt = CurrentMemoryContext;
954  cxt->skipped_keys.data = NULL;
955 }
static void json_unique_check_init(JsonUniqueCheckState *cxt)
Definition: json.c:932
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143

References JsonUniqueBuilderState::check, CurrentMemoryContext, StringInfoData::data, json_unique_check_init(), JsonUniqueBuilderState::mcxt, and JsonUniqueBuilderState::skipped_keys.

Referenced by json_build_object_worker(), and json_object_agg_transfn_worker().

◆ json_unique_check_init()

static void json_unique_check_init ( JsonUniqueCheckState cxt)
static

Definition at line 932 of file json.c.

933 {
934  HASHCTL ctl;
935 
936  memset(&ctl, 0, sizeof(ctl));
937  ctl.keysize = sizeof(JsonUniqueHashEntry);
938  ctl.entrysize = sizeof(JsonUniqueHashEntry);
939  ctl.hcxt = CurrentMemoryContext;
940  ctl.hash = json_unique_hash;
941  ctl.match = json_unique_hash_match;
942 
943  *cxt = hash_create("json object hashtable",
944  32,
945  &ctl,
947 }
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_COMPARE
Definition: hsearch.h:99
#define HASH_FUNCTION
Definition: hsearch.h:98
static uint32 json_unique_hash(const void *key, Size keysize)
Definition: json.c:900
static int json_unique_hash_match(const void *key1, const void *key2, Size keysize)
Definition: json.c:911
struct JsonUniqueHashEntry JsonUniqueHashEntry
tree ctl
Definition: radixtree.h:1853

References ctl, CurrentMemoryContext, HASH_COMPARE, HASH_CONTEXT, hash_create(), HASH_ELEM, HASH_FUNCTION, json_unique_hash(), and json_unique_hash_match().

Referenced by json_unique_builder_init(), and json_validate().

◆ json_unique_check_key()

static bool json_unique_check_key ( JsonUniqueCheckState cxt,
const char *  key,
int  object_id 
)
static

Definition at line 958 of file json.c.

959 {
960  JsonUniqueHashEntry entry;
961  bool found;
962 
963  entry.key = key;
964  entry.key_len = strlen(key);
965  entry.object_id = object_id;
966 
967  (void) hash_search(*cxt, &entry, HASH_ENTER, &found);
968 
969  return !found;
970 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
@ HASH_ENTER
Definition: hsearch.h:114
const char * key
Definition: json.c:46

References HASH_ENTER, hash_search(), JsonUniqueHashEntry::key, sort-test::key, JsonUniqueHashEntry::key_len, and JsonUniqueHashEntry::object_id.

Referenced by json_build_object_worker(), json_object_agg_transfn_worker(), and json_unique_object_field_start().

◆ json_unique_hash()

static uint32 json_unique_hash ( const void *  key,
Size  keysize 
)
static

Definition at line 900 of file json.c.

901 {
902  const JsonUniqueHashEntry *entry = (JsonUniqueHashEntry *) key;
904 
905  hash ^= hash_bytes((const unsigned char *) entry->key, entry->key_len);
906 
907  return DatumGetUInt32(hash);
908 }
unsigned int uint32
Definition: c.h:509
uint32 hash_bytes_uint32(uint32 k)
Definition: hashfn.c:610
uint32 hash_bytes(const unsigned char *k, int keylen)
Definition: hashfn.c:146
static uint32 DatumGetUInt32(Datum X)
Definition: postgres.h:222
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:715

References DatumGetUInt32(), hash(), hash_bytes(), hash_bytes_uint32(), JsonUniqueHashEntry::key, sort-test::key, JsonUniqueHashEntry::key_len, and JsonUniqueHashEntry::object_id.

Referenced by json_unique_check_init().

◆ json_unique_hash_match()

static int json_unique_hash_match ( const void *  key1,
const void *  key2,
Size  keysize 
)
static

Definition at line 911 of file json.c.

912 {
913  const JsonUniqueHashEntry *entry1 = (const JsonUniqueHashEntry *) key1;
914  const JsonUniqueHashEntry *entry2 = (const JsonUniqueHashEntry *) key2;
915 
916  if (entry1->object_id != entry2->object_id)
917  return entry1->object_id > entry2->object_id ? 1 : -1;
918 
919  if (entry1->key_len != entry2->key_len)
920  return entry1->key_len > entry2->key_len ? 1 : -1;
921 
922  return strncmp(entry1->key, entry2->key, entry1->key_len);
923 }

References JsonUniqueHashEntry::key, JsonUniqueHashEntry::key_len, and JsonUniqueHashEntry::object_id.

Referenced by json_unique_check_init().

◆ json_unique_object_end()

static JsonParseErrorType json_unique_object_end ( void *  _state)
static

Definition at line 1772 of file json.c.

1773 {
1774  JsonUniqueParsingState *state = _state;
1775  JsonUniqueStackEntry *entry;
1776 
1777  if (!state->unique)
1778  return JSON_SUCCESS;
1779 
1780  entry = state->stack;
1781  state->stack = entry->parent; /* pop object from stack */
1782  pfree(entry);
1783  return JSON_SUCCESS;
1784 }
struct JsonUniqueStackEntry * parent
Definition: json.c:54

References JSON_SUCCESS, JsonUniqueStackEntry::parent, and pfree().

Referenced by json_validate().

◆ json_unique_object_field_start()

static JsonParseErrorType json_unique_object_field_start ( void *  _state,
char *  field,
bool  isnull 
)
static

Definition at line 1787 of file json.c.

1788 {
1789  JsonUniqueParsingState *state = _state;
1790  JsonUniqueStackEntry *entry;
1791 
1792  if (!state->unique)
1793  return JSON_SUCCESS;
1794 
1795  /* find key collision in the current object */
1796  if (json_unique_check_key(&state->check, field, state->stack->object_id))
1797  return JSON_SUCCESS;
1798 
1799  state->unique = false;
1800 
1801  /* pop all objects entries */
1802  while ((entry = state->stack))
1803  {
1804  state->stack = entry->parent;
1805  pfree(entry);
1806  }
1807  return JSON_SUCCESS;
1808 }

References JSON_SUCCESS, json_unique_check_key(), JsonUniqueStackEntry::parent, and pfree().

Referenced by json_validate().

◆ json_unique_object_start()

static JsonParseErrorType json_unique_object_start ( void *  _state)
static

Definition at line 1754 of file json.c.

1755 {
1756  JsonUniqueParsingState *state = _state;
1757  JsonUniqueStackEntry *entry;
1758 
1759  if (!state->unique)
1760  return JSON_SUCCESS;
1761 
1762  /* push object entry to stack */
1763  entry = palloc(sizeof(*entry));
1764  entry->object_id = state->id_counter++;
1765  entry->parent = state->stack;
1766  state->stack = entry;
1767 
1768  return JSON_SUCCESS;
1769 }

References JSON_SUCCESS, JsonUniqueStackEntry::object_id, palloc(), and JsonUniqueStackEntry::parent.

Referenced by json_validate().

◆ json_validate()

bool json_validate ( text json,
bool  check_unique_keys,
bool  throw_error 
)

Definition at line 1812 of file json.c.

1813 {
1814  JsonLexContext lex;
1815  JsonSemAction uniqueSemAction = {0};
1817  JsonParseErrorType result;
1818 
1819  makeJsonLexContext(&lex, json, check_unique_keys);
1820 
1821  if (check_unique_keys)
1822  {
1823  state.lex = &lex;
1824  state.stack = NULL;
1825  state.id_counter = 0;
1826  state.unique = true;
1827  json_unique_check_init(&state.check);
1828 
1829  uniqueSemAction.semstate = &state;
1830  uniqueSemAction.object_start = json_unique_object_start;
1832  uniqueSemAction.object_end = json_unique_object_end;
1833  }
1834 
1835  result = pg_parse_json(&lex, check_unique_keys ? &uniqueSemAction : &nullSemAction);
1836 
1837  if (result != JSON_SUCCESS)
1838  {
1839  if (throw_error)
1840  json_errsave_error(result, &lex, NULL);
1841 
1842  return false; /* invalid json */
1843  }
1844 
1845  if (check_unique_keys && !state.unique)
1846  {
1847  if (throw_error)
1848  ereport(ERROR,
1849  (errcode(ERRCODE_DUPLICATE_JSON_OBJECT_KEY_VALUE),
1850  errmsg("duplicate JSON object key value")));
1851 
1852  return false; /* not unique keys */
1853  }
1854 
1855  if (check_unique_keys)
1856  freeJsonLexContext(&lex);
1857 
1858  return true; /* ok */
1859 }
static JsonParseErrorType json_unique_object_start(void *_state)
Definition: json.c:1754
static JsonParseErrorType json_unique_object_field_start(void *_state, char *field, bool isnull)
Definition: json.c:1787
static JsonParseErrorType json_unique_object_end(void *_state)
Definition: json.c:1772
JsonParseErrorType pg_parse_json(JsonLexContext *lex, const JsonSemAction *sem)
Definition: jsonapi.c:685
void freeJsonLexContext(JsonLexContext *lex)
Definition: jsonapi.c:639
json_struct_action object_start
Definition: jsonapi.h:145
json_ofield_action object_field_start
Definition: jsonapi.h:149
void * semstate
Definition: jsonapi.h:144
json_struct_action object_end
Definition: jsonapi.h:146

References ereport, errcode(), errmsg(), ERROR, freeJsonLexContext(), json_errsave_error(), JSON_SUCCESS, json_unique_check_init(), json_unique_object_end(), json_unique_object_field_start(), json_unique_object_start(), makeJsonLexContext(), nullSemAction, JsonSemAction::object_end, JsonSemAction::object_field_start, JsonSemAction::object_start, pg_parse_json(), and JsonSemAction::semstate.

Referenced by ExecEvalJsonConstructor(), and ExecEvalJsonIsPredicate().

◆ JsonEncodeDateTime()

char* JsonEncodeDateTime ( char *  buf,
Datum  value,
Oid  typid,
const int *  tzp 
)

Definition at line 310 of file json.c.

311 {
312  if (!buf)
313  buf = palloc(MAXDATELEN + 1);
314 
315  switch (typid)
316  {
317  case DATEOID:
318  {
319  DateADT date;
320  struct pg_tm tm;
321 
323 
324  /* Same as date_out(), but forcing DateStyle */
325  if (DATE_NOT_FINITE(date))
327  else
328  {
330  &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
332  }
333  }
334  break;
335  case TIMEOID:
336  {
337  TimeADT time = DatumGetTimeADT(value);
338  struct pg_tm tt,
339  *tm = &tt;
340  fsec_t fsec;
341 
342  /* Same as time_out(), but forcing DateStyle */
343  time2tm(time, tm, &fsec);
344  EncodeTimeOnly(tm, fsec, false, 0, USE_XSD_DATES, buf);
345  }
346  break;
347  case TIMETZOID:
348  {
350  struct pg_tm tt,
351  *tm = &tt;
352  fsec_t fsec;
353  int tz;
354 
355  /* Same as timetz_out(), but forcing DateStyle */
356  timetz2tm(time, tm, &fsec, &tz);
357  EncodeTimeOnly(tm, fsec, true, tz, USE_XSD_DATES, buf);
358  }
359  break;
360  case TIMESTAMPOID:
361  {
363  struct pg_tm tm;
364  fsec_t fsec;
365 
367  /* Same as timestamp_out(), but forcing DateStyle */
370  else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
371  EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
372  else
373  ereport(ERROR,
374  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
375  errmsg("timestamp out of range")));
376  }
377  break;
378  case TIMESTAMPTZOID:
379  {
381  struct pg_tm tm;
382  int tz;
383  fsec_t fsec;
384  const char *tzn = NULL;
385 
387 
388  /*
389  * If a time zone is specified, we apply the time-zone shift,
390  * convert timestamptz to pg_tm as if it were without a time
391  * zone, and then use the specified time zone for converting
392  * the timestamp into a string.
393  */
394  if (tzp)
395  {
396  tz = *tzp;
398  }
399 
400  /* Same as timestamptz_out(), but forcing DateStyle */
403  else if (timestamp2tm(timestamp, tzp ? NULL : &tz, &tm, &fsec,
404  tzp ? NULL : &tzn, NULL) == 0)
405  {
406  if (tzp)
407  tm.tm_isdst = 1; /* set time-zone presence flag */
408 
409  EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
410  }
411  else
412  ereport(ERROR,
413  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
414  errmsg("timestamp out of range")));
415  }
416  break;
417  default:
418  elog(ERROR, "unknown jsonb value datetime type oid %u", typid);
419  return NULL;
420  }
421 
422  return buf;
423 }
void EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str)
Definition: datetime.c:4312
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:311
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:4342
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition: datetime.c:4227
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1586
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1891
int64 Timestamp
Definition: timestamp.h:38
int64 TimestampTz
Definition: timestamp.h:39
int32 fsec_t
Definition: timestamp.h:41
#define USECS_PER_SEC
Definition: timestamp.h:134
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:169
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:235
int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: date.c:2415
int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
Definition: date.c:1500
void EncodeSpecialDate(DateADT dt, char *str)
Definition: date.c:294
#define DATE_NOT_FINITE(j)
Definition: date.h:43
int32 DateADT
Definition: date.h:23
static DateADT DatumGetDateADT(Datum X)
Definition: date.h:54
static TimeADT DatumGetTimeADT(Datum X)
Definition: date.h:60
static TimeTzADT * DatumGetTimeTzADTP(Datum X)
Definition: date.h:66
int64 TimeADT
Definition: date.h:25
static struct @157 value
static struct pg_tm tm
Definition: localtime.c:104
#define USE_XSD_DATES
Definition: miscadmin.h:239
long date
Definition: pgtypes_date.h:9
int64 timestamp
Definition: date.h:28
Definition: pgtime.h:35
int tm_mday
Definition: pgtime.h:39
int tm_mon
Definition: pgtime.h:40
int tm_isdst
Definition: pgtime.h:44
int tm_year
Definition: pgtime.h:41
static Timestamp DatumGetTimestamp(Datum X)
Definition: timestamp.h:28
static TimestampTz DatumGetTimestampTz(Datum X)
Definition: timestamp.h:34

References buf, DATE_NOT_FINITE, DatumGetDateADT(), DatumGetTimeADT(), DatumGetTimestamp(), DatumGetTimestampTz(), DatumGetTimeTzADTP(), elog, EncodeDateOnly(), EncodeDateTime(), EncodeSpecialDate(), EncodeSpecialTimestamp(), EncodeTimeOnly(), ereport, errcode(), errmsg(), ERROR, j2date(), MAXDATELEN, palloc(), POSTGRES_EPOCH_JDATE, time2tm(), timestamp2tm(), TIMESTAMP_NOT_FINITE, timetz2tm(), tm, pg_tm::tm_isdst, pg_tm::tm_mday, pg_tm::tm_mon, pg_tm::tm_year, USE_XSD_DATES, USECS_PER_SEC, and value.

Referenced by convertJsonbScalar(), datum_to_json_internal(), datum_to_jsonb_internal(), and executeItemOptUnwrapTarget().

◆ row_to_json()

Datum row_to_json ( PG_FUNCTION_ARGS  )

Definition at line 663 of file json.c.

664 {
665  Datum array = PG_GETARG_DATUM(0);
666  StringInfo result;
667 
668  result = makeStringInfo();
669 
670  composite_to_json(array, result, false);
671 
673 }

References composite_to_json(), cstring_to_text_with_len(), StringInfoData::data, StringInfoData::len, makeStringInfo(), PG_GETARG_DATUM, and PG_RETURN_TEXT_P.

◆ row_to_json_pretty()

Datum row_to_json_pretty ( PG_FUNCTION_ARGS  )

Definition at line 679 of file json.c.

680 {
681  Datum array = PG_GETARG_DATUM(0);
682  bool use_line_feeds = PG_GETARG_BOOL(1);
683  StringInfo result;
684 
685  result = makeStringInfo();
686 
687  composite_to_json(array, result, use_line_feeds);
688 
690 }

References composite_to_json(), cstring_to_text_with_len(), StringInfoData::data, StringInfoData::len, makeStringInfo(), PG_GETARG_BOOL, PG_GETARG_DATUM, and PG_RETURN_TEXT_P.

◆ to_json()

Datum to_json ( PG_FUNCTION_ARGS  )

Definition at line 739 of file json.c.

740 {
742  Oid val_type = get_fn_expr_argtype(fcinfo->flinfo, 0);
743  JsonTypeCategory tcategory;
744  Oid outfuncoid;
745 
746  if (val_type == InvalidOid)
747  ereport(ERROR,
748  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
749  errmsg("could not determine input data type")));
750 
751  json_categorize_type(val_type, false,
752  &tcategory, &outfuncoid);
753 
754  PG_RETURN_DATUM(datum_to_json(val, tcategory, outfuncoid));
755 }
Datum datum_to_json(Datum val, JsonTypeCategory tcategory, Oid outfuncoid)
Definition: json.c:763

References datum_to_json(), ereport, errcode(), errmsg(), ERROR, get_fn_expr_argtype(), InvalidOid, json_categorize_type(), PG_GETARG_DATUM, PG_RETURN_DATUM, and val.

◆ to_json_is_immutable()

bool to_json_is_immutable ( Oid  typoid)

Definition at line 700 of file json.c.

701 {
702  JsonTypeCategory tcategory;
703  Oid outfuncoid;
704 
705  json_categorize_type(typoid, false, &tcategory, &outfuncoid);
706 
707  switch (tcategory)
708  {
709  case JSONTYPE_BOOL:
710  case JSONTYPE_JSON:
711  case JSONTYPE_JSONB:
712  case JSONTYPE_NULL:
713  return true;
714 
715  case JSONTYPE_DATE:
716  case JSONTYPE_TIMESTAMP:
718  return false;
719 
720  case JSONTYPE_ARRAY:
721  return false; /* TODO recurse into elements */
722 
723  case JSONTYPE_COMPOSITE:
724  return false; /* TODO recurse into fields */
725 
726  case JSONTYPE_NUMERIC:
727  case JSONTYPE_CAST:
728  case JSONTYPE_OTHER:
729  return func_volatile(outfuncoid) == PROVOLATILE_IMMUTABLE;
730  }
731 
732  return false; /* not reached */
733 }
@ JSONTYPE_OTHER
Definition: jsonfuncs.h:81
@ JSONTYPE_JSONB
Definition: jsonfuncs.h:77
char func_volatile(Oid funcid)
Definition: lsyscache.c:1780

References func_volatile(), json_categorize_type(), JSONTYPE_ARRAY, JSONTYPE_BOOL, JSONTYPE_CAST, JSONTYPE_COMPOSITE, JSONTYPE_DATE, JSONTYPE_JSON, JSONTYPE_JSONB, JSONTYPE_NULL, JSONTYPE_NUMERIC, JSONTYPE_OTHER, JSONTYPE_TIMESTAMP, and JSONTYPE_TIMESTAMPTZ.

Referenced by contain_mutable_functions_walker().