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 1279 of file json.c.

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

Referenced by 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(), and transform_string_values_scalar().

1280 {
1281  const char *p;
1282 
1283  appendStringInfoCharMacro(buf, '"');
1284  for (p = str; *p; p++)
1285  {
1286  switch (*p)
1287  {
1288  case '\b':
1289  appendStringInfoString(buf, "\\b");
1290  break;
1291  case '\f':
1292  appendStringInfoString(buf, "\\f");
1293  break;
1294  case '\n':
1295  appendStringInfoString(buf, "\\n");
1296  break;
1297  case '\r':
1298  appendStringInfoString(buf, "\\r");
1299  break;
1300  case '\t':
1301  appendStringInfoString(buf, "\\t");
1302  break;
1303  case '"':
1304  appendStringInfoString(buf, "\\\"");
1305  break;
1306  case '\\':
1307  appendStringInfoString(buf, "\\\\");
1308  break;
1309  default:
1310  if ((unsigned char) *p < ' ')
1311  appendStringInfo(buf, "\\u%04x", (int) *p);
1312  else
1313  appendStringInfoCharMacro(buf, *p);
1314  break;
1315  }
1316  }
1317  appendStringInfoCharMacro(buf, '"');
1318 }
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:128
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176

◆ JsonEncodeDateTime()

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

Definition at line 353 of file json.c.

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, and USECS_PER_SEC.

Referenced by convertJsonbScalar(), datum_to_json(), and datum_to_jsonb().

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 
365  date = DatumGetDateADT(value);
366 
367  /* Same as date_out(), but forcing DateStyle */
368  if (DATE_NOT_FINITE(date))
369  EncodeSpecialDate(date, buf);
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 
409  timestamp = DatumGetTimestamp(value);
410  /* Same as timestamp_out(), but forcing DateStyle */
411  if (TIMESTAMP_NOT_FINITE(timestamp))
412  EncodeSpecialTimestamp(timestamp, buf);
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 
429  timestamp = DatumGetTimestampTz(value);
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;
440  timestamp -= (TimestampTz) tz * USECS_PER_SEC;
441  }
442 
443  /* Same as timestamptz_out(), but forcing DateStyle */
444  if (TIMESTAMP_NOT_FINITE(timestamp))
445  EncodeSpecialTimestamp(timestamp, buf);
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 %d", typid);
462  return NULL;
463  }
464 
465  return buf;
466 }
#define MAXDATELEN
Definition: datetime.h:201
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition: datetime.c:3855
#define DatumGetDateADT(X)
Definition: date.h:53
int tm_isdst
Definition: pgtime.h:35
#define USECS_PER_SEC
Definition: timestamp.h:94
int32 DateADT
Definition: date.h:23
int64 timestamp
int64 TimestampTz
Definition: timestamp.h:39
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1791
#define DatumGetTimeTzADTP(X)
Definition: date.h:55
static struct @145 value
void EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str)
Definition: datetime.c:3940
int errcode(int sqlerrcode)
Definition: elog.c:608
int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
Definition: date.c:1281
long date
Definition: pgtypes_date.h:9
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1536
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
#define ERROR
Definition: elog.h:43
#define DATE_NOT_FINITE(j)
Definition: date.h:43
int tm_mday
Definition: pgtime.h:30
static char * buf
Definition: pg_test_fsync.c:67
int tm_mon
Definition: pgtime.h:31
#define DatumGetTimestampTz(X)
Definition: timestamp.h:28
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:3970
void EncodeSpecialDate(DateADT dt, char *str)
Definition: date.c:284
int32 fsec_t
Definition: timestamp.h:41
int64 TimeADT
Definition: date.h:25
#define ereport(elevel, rest)
Definition: elog.h:141
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
int64 Timestamp
Definition: timestamp.h:38
#define DatumGetTimeADT(X)
Definition: date.h:54
int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: date.c:2154
#define USE_XSD_DATES
Definition: miscadmin.h:217
int tm_year
Definition: pgtime.h:32
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
Definition: date.h:27
#define DatumGetTimestamp(X)
Definition: timestamp.h:27