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)
 

Function Documentation

◆ escape_json()

void escape_json ( StringInfo  buf,
const char *  str 
)

Definition at line 1271 of file json.c.

1272 {
1273  const char *p;
1274 
1276  for (p = str; *p; p++)
1277  {
1278  switch (*p)
1279  {
1280  case '\b':
1281  appendStringInfoString(buf, "\\b");
1282  break;
1283  case '\f':
1284  appendStringInfoString(buf, "\\f");
1285  break;
1286  case '\n':
1287  appendStringInfoString(buf, "\\n");
1288  break;
1289  case '\r':
1290  appendStringInfoString(buf, "\\r");
1291  break;
1292  case '\t':
1293  appendStringInfoString(buf, "\\t");
1294  break;
1295  case '"':
1296  appendStringInfoString(buf, "\\\"");
1297  break;
1298  case '\\':
1299  appendStringInfoString(buf, "\\\\");
1300  break;
1301  default:
1302  if ((unsigned char) *p < ' ')
1303  appendStringInfo(buf, "\\u%04x", (int) *p);
1304  else
1306  break;
1307  }
1308  }
1310 }
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().

◆ JsonEncodeDateTime()

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

Definition at line 353 of file json.c.

354 {
355  if (!buf)
356  buf = palloc(MAXDATELEN + 1);
357 
358  switch (typid)
359  {
360  case DATEOID:
361  {
362  DateADT date;
363  struct pg_tm tm;
364 
366 
367  /* Same as date_out(), but forcing DateStyle */
368  if (DATE_NOT_FINITE(date))
370  else
371  {
373  &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
375  }
376  }
377  break;
378  case TIMEOID:
379  {
380  TimeADT time = DatumGetTimeADT(value);
381  struct pg_tm tt,
382  *tm = &tt;
383  fsec_t fsec;
384 
385  /* Same as time_out(), but forcing DateStyle */
386  time2tm(time, tm, &fsec);
387  EncodeTimeOnly(tm, fsec, false, 0, USE_XSD_DATES, buf);
388  }
389  break;
390  case TIMETZOID:
391  {
393  struct pg_tm tt,
394  *tm = &tt;
395  fsec_t fsec;
396  int tz;
397 
398  /* Same as timetz_out(), but forcing DateStyle */
399  timetz2tm(time, tm, &fsec, &tz);
400  EncodeTimeOnly(tm, fsec, true, tz, USE_XSD_DATES, buf);
401  }
402  break;
403  case TIMESTAMPOID:
404  {
406  struct pg_tm tm;
407  fsec_t fsec;
408 
410  /* Same as timestamp_out(), but forcing DateStyle */
413  else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
414  EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
415  else
416  ereport(ERROR,
417  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
418  errmsg("timestamp out of range")));
419  }
420  break;
421  case TIMESTAMPTZOID:
422  {
424  struct pg_tm tm;
425  int tz;
426  fsec_t fsec;
427  const char *tzn = NULL;
428 
430 
431  /*
432  * If a time zone is specified, we apply the time-zone shift,
433  * convert timestamptz to pg_tm as if it were without a time
434  * zone, and then use the specified time zone for converting
435  * the timestamp into a string.
436  */
437  if (tzp)
438  {
439  tz = *tzp;
441  }
442 
443  /* Same as timestamptz_out(), but forcing DateStyle */
446  else if (timestamp2tm(timestamp, tzp ? NULL : &tz, &tm, &fsec,
447  tzp ? NULL : &tzn, NULL) == 0)
448  {
449  if (tzp)
450  tm.tm_isdst = 1; /* set time-zone presence flag */
451 
452  EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
453  }
454  else
455  ereport(ERROR,
456  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
457  errmsg("timestamp out of range")));
458  }
459  break;
460  default:
461  elog(ERROR, "unknown jsonb value datetime type oid %u", typid);
462  return NULL;
463  }
464 
465  return buf;
466 }
void EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str)
Definition: datetime.c:4229
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:310
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:4259
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition: datetime.c:4144
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1526
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1830
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:161
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:202
int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: date.c:2392
int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
Definition: date.c:1491
void EncodeSpecialDate(DateADT dt, char *str)
Definition: date.c:286
#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
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
#define MAXDATELEN
Definition: datetime.h:201
static struct @143 value
static struct pg_tm tm
Definition: localtime.c:104
void * palloc(Size size)
Definition: mcxt.c:1199
#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().