PostgreSQL Source Code  git master
json.h File Reference
#include "lib/stringinfo.h"
Include dependency graph for json.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void escape_json (StringInfo buf, const char *str)
 
char * JsonEncodeDateTime (char *buf, Datum value, Oid typid, const int *tzp)
 
bool to_json_is_immutable (Oid typoid)
 
Datum json_build_object_worker (int nargs, Datum *args, bool *nulls, Oid *types, bool absent_on_null, bool unique_keys)
 
Datum json_build_array_worker (int nargs, Datum *args, bool *nulls, Oid *types, bool absent_on_null)
 

Function Documentation

◆ escape_json()

void escape_json ( StringInfo  buf,
const char *  str 
)

Definition at line 1610 of file json.c.

1611 {
1612  const char *p;
1613 
1615  for (p = str; *p; p++)
1616  {
1617  switch (*p)
1618  {
1619  case '\b':
1620  appendStringInfoString(buf, "\\b");
1621  break;
1622  case '\f':
1623  appendStringInfoString(buf, "\\f");
1624  break;
1625  case '\n':
1626  appendStringInfoString(buf, "\\n");
1627  break;
1628  case '\r':
1629  appendStringInfoString(buf, "\\r");
1630  break;
1631  case '\t':
1632  appendStringInfoString(buf, "\\t");
1633  break;
1634  case '"':
1635  appendStringInfoString(buf, "\\\"");
1636  break;
1637  case '\\':
1638  appendStringInfoString(buf, "\\\\");
1639  break;
1640  default:
1641  if ((unsigned char) *p < ' ')
1642  appendStringInfo(buf, "\\u%04x", (int) *p);
1643  else
1645  break;
1646  }
1647  }
1649 }
static char * buf
Definition: pg_test_fsync.c:67
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:128

References appendStringInfo(), appendStringInfoCharMacro, appendStringInfoString(), buf, and generate_unaccent_rules::str.

Referenced by AddFileToBackupManifest(), appendJSONKeyValue(), composite_to_json(), datum_to_json(), escape_yaml(), ExplainDummyGroup(), ExplainOpenGroup(), ExplainProperty(), ExplainPropertyList(), ExplainPropertyListNested(), hstore_to_json(), hstore_to_json_loose(), json_object(), json_object_two_arg(), jsonb_put_escaped_value(), populate_scalar(), printJsonPathItem(), sn_object_field_start(), sn_scalar(), transform_string_values_object_field_start(), transform_string_values_scalar(), and write_jsonlog().

◆ json_build_array_worker()

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

Definition at line 1382 of file json.c.

1384 {
1385  int i;
1386  const char *sep = "";
1387  StringInfo result;
1388 
1389  result = makeStringInfo();
1390 
1391  appendStringInfoChar(result, '[');
1392 
1393  for (i = 0; i < nargs; i++)
1394  {
1395  if (absent_on_null && nulls[i])
1396  continue;
1397 
1398  appendStringInfoString(result, sep);
1399  sep = ", ";
1400  add_json(args[i], nulls[i], result, types[i], false);
1401  }
1402 
1403  appendStringInfoChar(result, ']');
1404 
1405  return PointerGetDatum(cstring_to_text_with_len(result->data, result->len));
1406 }
struct typedefs * types
Definition: ecpg.c:29
int i
Definition: isn.c:73
static void add_json(Datum val, bool is_null, StringInfo result, Oid val_type, bool key_scalar)
Definition: json.c:668
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:194

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_worker()

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

Definition at line 1269 of file json.c.

1271 {
1272  int i;
1273  const char *sep = "";
1274  StringInfo result;
1275  JsonUniqueBuilderState unique_check;
1276 
1277  if (nargs % 2 != 0)
1278  ereport(ERROR,
1279  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1280  errmsg("argument list must have even number of elements"),
1281  /* translator: %s is a SQL function name */
1282  errhint("The arguments of %s must consist of alternating keys and values.",
1283  "json_build_object()")));
1284 
1285  result = makeStringInfo();
1286 
1287  appendStringInfoChar(result, '{');
1288 
1289  if (unique_keys)
1290  json_unique_builder_init(&unique_check);
1291 
1292  for (i = 0; i < nargs; i += 2)
1293  {
1294  StringInfo out;
1295  bool skip;
1296  int key_offset;
1297 
1298  /* Skip null values if absent_on_null */
1299  skip = absent_on_null && nulls[i + 1];
1300 
1301  if (skip)
1302  {
1303  /* If key uniqueness check is needed we must save skipped keys */
1304  if (!unique_keys)
1305  continue;
1306 
1307  out = json_unique_builder_get_throwawaybuf(&unique_check);
1308  }
1309  else
1310  {
1311  appendStringInfoString(result, sep);
1312  sep = ", ";
1313  out = result;
1314  }
1315 
1316  /* process key */
1317  if (nulls[i])
1318  ereport(ERROR,
1319  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1320  errmsg("null value not allowed for object key")));
1321 
1322  /* save key offset before appending it */
1323  key_offset = out->len;
1324 
1325  add_json(args[i], false, out, types[i], true);
1326 
1327  if (unique_keys)
1328  {
1329  /* check key uniqueness after key appending */
1330  const char *key = &out->data[key_offset];
1331 
1332  if (!json_unique_check_key(&unique_check.check, key, 0))
1333  ereport(ERROR,
1334  errcode(ERRCODE_DUPLICATE_JSON_OBJECT_KEY_VALUE),
1335  errmsg("duplicate JSON key %s", key));
1336 
1337  if (skip)
1338  continue;
1339  }
1340 
1341  appendStringInfoString(result, " : ");
1342 
1343  /* process value */
1344  add_json(args[i + 1], nulls[i + 1], result, types[i + 1], false);
1345  }
1346 
1347  appendStringInfoChar(result, '}');
1348 
1349  return PointerGetDatum(cstring_to_text_with_len(result->data, result->len));
1350 }
int errhint(const char *fmt,...)
Definition: elog.c:1316
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
static StringInfo json_unique_builder_get_throwawaybuf(JsonUniqueBuilderState *cxt)
Definition: json.c:1031
static bool json_unique_check_key(JsonUniqueCheckState *cxt, const char *key, int object_id)
Definition: json.c:1011
static void json_unique_builder_init(JsonUniqueBuilderState *cxt)
Definition: json.c:1003
static const struct exclude_list_item skip[]
Definition: pg_checksums.c:116
JsonUniqueCheckState check
Definition: json.c:68

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(), skip, and types.

Referenced by ExecEvalJsonConstructor(), and json_build_object().

◆ JsonEncodeDateTime()

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

Definition at line 385 of file json.c.

386 {
387  if (!buf)
388  buf = palloc(MAXDATELEN + 1);
389 
390  switch (typid)
391  {
392  case DATEOID:
393  {
394  DateADT date;
395  struct pg_tm tm;
396 
398 
399  /* Same as date_out(), but forcing DateStyle */
400  if (DATE_NOT_FINITE(date))
402  else
403  {
405  &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
407  }
408  }
409  break;
410  case TIMEOID:
411  {
412  TimeADT time = DatumGetTimeADT(value);
413  struct pg_tm tt,
414  *tm = &tt;
415  fsec_t fsec;
416 
417  /* Same as time_out(), but forcing DateStyle */
418  time2tm(time, tm, &fsec);
419  EncodeTimeOnly(tm, fsec, false, 0, USE_XSD_DATES, buf);
420  }
421  break;
422  case TIMETZOID:
423  {
425  struct pg_tm tt,
426  *tm = &tt;
427  fsec_t fsec;
428  int tz;
429 
430  /* Same as timetz_out(), but forcing DateStyle */
431  timetz2tm(time, tm, &fsec, &tz);
432  EncodeTimeOnly(tm, fsec, true, tz, USE_XSD_DATES, buf);
433  }
434  break;
435  case TIMESTAMPOID:
436  {
438  struct pg_tm tm;
439  fsec_t fsec;
440 
442  /* Same as timestamp_out(), but forcing DateStyle */
445  else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
446  EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
447  else
448  ereport(ERROR,
449  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
450  errmsg("timestamp out of range")));
451  }
452  break;
453  case TIMESTAMPTZOID:
454  {
456  struct pg_tm tm;
457  int tz;
458  fsec_t fsec;
459  const char *tzn = NULL;
460 
462 
463  /*
464  * If a time zone is specified, we apply the time-zone shift,
465  * convert timestamptz to pg_tm as if it were without a time
466  * zone, and then use the specified time zone for converting
467  * the timestamp into a string.
468  */
469  if (tzp)
470  {
471  tz = *tzp;
473  }
474 
475  /* Same as timestamptz_out(), but forcing DateStyle */
478  else if (timestamp2tm(timestamp, tzp ? NULL : &tz, &tm, &fsec,
479  tzp ? NULL : &tzn, NULL) == 0)
480  {
481  if (tzp)
482  tm.tm_isdst = 1; /* set time-zone presence flag */
483 
484  EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
485  }
486  else
487  ereport(ERROR,
488  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
489  errmsg("timestamp out of range")));
490  }
491  break;
492  default:
493  elog(ERROR, "unknown jsonb value datetime type oid %u", typid);
494  return NULL;
495  }
496 
497  return buf;
498 }
void EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str)
Definition: datetime.c:4187
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:313
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:4217
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition: datetime.c:4102
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1535
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1838
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:133
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:168
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:209
int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: date.c:2403
int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
Definition: date.c:1496
void EncodeSpecialDate(DateADT dt, char *str)
Definition: date.c:292
#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
#define MAXDATELEN
Definition: datetime.h:200
static struct @145 value
static struct pg_tm tm
Definition: localtime.c:104
void * palloc(Size size)
Definition: mcxt.c:1210
#define USE_XSD_DATES
Definition: miscadmin.h:233
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(), and datum_to_jsonb().

◆ to_json_is_immutable()

bool to_json_is_immutable ( Oid  typoid)

Definition at line 765 of file json.c.

766 {
767  JsonTypeCategory tcategory;
768  Oid outfuncoid;
769 
770  json_categorize_type(typoid, &tcategory, &outfuncoid);
771 
772  switch (tcategory)
773  {
774  case JSONTYPE_BOOL:
775  case JSONTYPE_JSON:
776  case JSONTYPE_NULL:
777  return true;
778 
779  case JSONTYPE_DATE:
780  case JSONTYPE_TIMESTAMP:
782  return false;
783 
784  case JSONTYPE_ARRAY:
785  return false; /* TODO recurse into elements */
786 
787  case JSONTYPE_COMPOSITE:
788  return false; /* TODO recurse into fields */
789 
790  case JSONTYPE_NUMERIC:
791  case JSONTYPE_CAST:
792  case JSONTYPE_OTHER:
793  return func_volatile(outfuncoid) == PROVOLATILE_IMMUTABLE;
794  }
795 
796  return false; /* not reached */
797 }
static void json_categorize_type(Oid typoid, JsonTypeCategory *tcategory, Oid *outfuncoid)
Definition: json.c:176
JsonTypeCategory
Definition: json.c:33
@ JSONTYPE_JSON
Definition: json.c:40
@ JSONTYPE_NULL
Definition: json.c:34
@ JSONTYPE_TIMESTAMP
Definition: json.c:38
@ JSONTYPE_NUMERIC
Definition: json.c:36
@ JSONTYPE_DATE
Definition: json.c:37
@ JSONTYPE_BOOL
Definition: json.c:35
@ JSONTYPE_OTHER
Definition: json.c:44
@ JSONTYPE_CAST
Definition: json.c:43
@ JSONTYPE_COMPOSITE
Definition: json.c:42
@ JSONTYPE_ARRAY
Definition: json.c:41
@ JSONTYPE_TIMESTAMPTZ
Definition: json.c:39
char func_volatile(Oid funcid)
Definition: lsyscache.c:1762
unsigned int Oid
Definition: postgres_ext.h:31

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

Referenced by contain_mutable_functions_walker().