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.

Enumerations

enum  JsonTypeCategory {
  JSONTYPE_NULL , JSONTYPE_BOOL , JSONTYPE_NUMERIC , JSONTYPE_DATE ,
  JSONTYPE_TIMESTAMP , JSONTYPE_TIMESTAMPTZ , JSONTYPE_JSON , JSONTYPE_ARRAY ,
  JSONTYPE_COMPOSITE , JSONTYPE_CAST , JSONTYPE_OTHER
}
 

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)
 
void json_categorize_type (Oid typoid, JsonTypeCategory *tcategory, Oid *outfuncoid)
 
Datum to_json_worker (Datum val, JsonTypeCategory tcategory, Oid outfuncoid)
 
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)
 
bool json_validate (text *json, bool check_unique_keys, bool throw_error)
 

Enumeration Type Documentation

◆ JsonTypeCategory

Enumerator
JSONTYPE_NULL 
JSONTYPE_BOOL 
JSONTYPE_NUMERIC 
JSONTYPE_DATE 
JSONTYPE_TIMESTAMP 
JSONTYPE_TIMESTAMPTZ 
JSONTYPE_JSON 
JSONTYPE_ARRAY 
JSONTYPE_COMPOSITE 
JSONTYPE_CAST 
JSONTYPE_OTHER 

Definition at line 19 of file json.h.

20 {
21  JSONTYPE_NULL, /* null, so we didn't bother to identify */
22  JSONTYPE_BOOL, /* boolean (built-in types only) */
23  JSONTYPE_NUMERIC, /* numeric (ditto) */
24  JSONTYPE_DATE, /* we use special formatting for datetimes */
27  JSONTYPE_JSON, /* JSON itself (and JSONB) */
28  JSONTYPE_ARRAY, /* array */
29  JSONTYPE_COMPOSITE, /* composite */
30  JSONTYPE_CAST, /* something with an explicit cast to JSON */
31  JSONTYPE_OTHER /* all else */
JsonTypeCategory
Definition: json.h:20
@ JSONTYPE_JSON
Definition: json.h:27
@ JSONTYPE_NULL
Definition: json.h:21
@ JSONTYPE_TIMESTAMP
Definition: json.h:25
@ JSONTYPE_NUMERIC
Definition: json.h:23
@ JSONTYPE_DATE
Definition: json.h:24
@ JSONTYPE_BOOL
Definition: json.h:22
@ JSONTYPE_OTHER
Definition: json.h:31
@ JSONTYPE_CAST
Definition: json.h:30
@ JSONTYPE_COMPOSITE
Definition: json.h:29
@ JSONTYPE_ARRAY
Definition: json.h:28
@ JSONTYPE_TIMESTAMPTZ
Definition: json.h:26

Function Documentation

◆ escape_json()

void escape_json ( StringInfo  buf,
const char *  str 
)

Definition at line 1587 of file json.c.

1588 {
1589  const char *p;
1590 
1592  for (p = str; *p; p++)
1593  {
1594  switch (*p)
1595  {
1596  case '\b':
1597  appendStringInfoString(buf, "\\b");
1598  break;
1599  case '\f':
1600  appendStringInfoString(buf, "\\f");
1601  break;
1602  case '\n':
1603  appendStringInfoString(buf, "\\n");
1604  break;
1605  case '\r':
1606  appendStringInfoString(buf, "\\r");
1607  break;
1608  case '\t':
1609  appendStringInfoString(buf, "\\t");
1610  break;
1611  case '"':
1612  appendStringInfoString(buf, "\\\"");
1613  break;
1614  case '\\':
1615  appendStringInfoString(buf, "\\\\");
1616  break;
1617  default:
1618  if ((unsigned char) *p < ' ')
1619  appendStringInfo(buf, "\\u%04x", (int) *p);
1620  else
1622  break;
1623  }
1624  }
1626 }
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(), transformJsonTableColumn(), 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 1352 of file json.c.

1354 {
1355  int i;
1356  const char *sep = "";
1357  StringInfo result;
1358 
1359  result = makeStringInfo();
1360 
1361  appendStringInfoChar(result, '[');
1362 
1363  for (i = 0; i < nargs; i++)
1364  {
1365  if (absent_on_null && nulls[i])
1366  continue;
1367 
1368  appendStringInfoString(result, sep);
1369  sep = ", ";
1370  add_json(args[i], nulls[i], result, types[i], false);
1371  }
1372 
1373  appendStringInfoChar(result, ']');
1374 
1375  return PointerGetDatum(cstring_to_text_with_len(result->data, result->len));
1376 }
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:658
#define PointerGetDatum(X)
Definition: postgres.h:600
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:200

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

1240 {
1241  int i;
1242  const char *sep = "";
1243  StringInfo result;
1244  JsonUniqueBuilderState unique_check;
1245 
1246  if (nargs % 2 != 0)
1247  ereport(ERROR,
1248  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1249  errmsg("argument list must have even number of elements"),
1250  /* translator: %s is a SQL function name */
1251  errhint("The arguments of %s must consist of alternating keys and values.",
1252  "json_build_object()")));
1253 
1254  result = makeStringInfo();
1255 
1256  appendStringInfoChar(result, '{');
1257 
1258  if (unique_keys)
1259  json_unique_builder_init(&unique_check);
1260 
1261  for (i = 0; i < nargs; i += 2)
1262  {
1263  StringInfo out;
1264  bool skip;
1265  int key_offset;
1266 
1267  /* Skip null values if absent_on_null */
1268  skip = absent_on_null && nulls[i + 1];
1269 
1270  if (skip)
1271  {
1272  /* If key uniqueness check is needed we must save skipped keys */
1273  if (!unique_keys)
1274  continue;
1275 
1276  out = json_unique_builder_get_skipped_keys(&unique_check);
1277  }
1278  else
1279  {
1280  appendStringInfoString(result, sep);
1281  sep = ", ";
1282  out = result;
1283  }
1284 
1285  /* process key */
1286  if (nulls[i])
1287  ereport(ERROR,
1288  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1289  errmsg("argument %d cannot be null", i + 1),
1290  errhint("Object keys should be text.")));
1291 
1292  /* save key offset before key appending */
1293  key_offset = out->len;
1294 
1295  add_json(args[i], false, out, types[i], true);
1296 
1297  if (unique_keys)
1298  {
1299  /* check key uniqueness after key appending */
1300  const char *key = &out->data[key_offset];
1301 
1302  if (!json_unique_check_key(&unique_check.check, key, 0))
1303  ereport(ERROR,
1304  (errcode(ERRCODE_DUPLICATE_JSON_OBJECT_KEY_VALUE),
1305  errmsg("duplicate JSON key %s", key)));
1306 
1307  if (skip)
1308  continue;
1309  }
1310 
1311  appendStringInfoString(result, " : ");
1312 
1313  /* process value */
1314  add_json(args[i + 1], nulls[i + 1], result, types[i + 1], false);
1315  }
1316 
1317  appendStringInfoChar(result, '}');
1318 
1319  return PointerGetDatum(cstring_to_text_with_len(result->data, result->len));
1320 }
int errhint(const char *fmt,...)
Definition: elog.c:1151
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
static bool json_unique_check_key(JsonUniqueCheckState *cxt, const char *key, int object_id)
Definition: json.c:983
static void json_unique_builder_init(JsonUniqueBuilderState *cxt)
Definition: json.c:998
static StringInfo json_unique_builder_get_skipped_keys(JsonUniqueBuilderState *cxt)
Definition: json.c:1007
static const struct exclude_list_item skip[]
Definition: pg_checksums.c:116
JsonUniqueCheckState check
Definition: json.c:47

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_skipped_keys(), 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().

◆ json_categorize_type()

void json_categorize_type ( Oid  typoid,
JsonTypeCategory tcategory,
Oid outfuncoid 
)

Definition at line 166 of file json.c.

169 {
170  bool typisvarlena;
171 
172  /* Look through any domain */
173  typoid = getBaseType(typoid);
174 
175  *outfuncoid = InvalidOid;
176 
177  /*
178  * We need to get the output function for everything except date and
179  * timestamp types, array and composite types, booleans, and non-builtin
180  * types where there's a cast to json.
181  */
182 
183  switch (typoid)
184  {
185  case BOOLOID:
186  *tcategory = JSONTYPE_BOOL;
187  break;
188 
189  case INT2OID:
190  case INT4OID:
191  case INT8OID:
192  case FLOAT4OID:
193  case FLOAT8OID:
194  case NUMERICOID:
195  getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
196  *tcategory = JSONTYPE_NUMERIC;
197  break;
198 
199  case DATEOID:
200  *tcategory = JSONTYPE_DATE;
201  break;
202 
203  case TIMESTAMPOID:
204  *tcategory = JSONTYPE_TIMESTAMP;
205  break;
206 
207  case TIMESTAMPTZOID:
208  *tcategory = JSONTYPE_TIMESTAMPTZ;
209  break;
210 
211  case JSONOID:
212  case JSONBOID:
213  getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
214  *tcategory = JSONTYPE_JSON;
215  break;
216 
217  default:
218  /* Check for arrays and composites */
219  if (OidIsValid(get_element_type(typoid)) || typoid == ANYARRAYOID
220  || typoid == ANYCOMPATIBLEARRAYOID || typoid == RECORDARRAYOID)
221  *tcategory = JSONTYPE_ARRAY;
222  else if (type_is_rowtype(typoid)) /* includes RECORDOID */
223  *tcategory = JSONTYPE_COMPOSITE;
224  else
225  {
226  /* It's probably the general case ... */
227  *tcategory = JSONTYPE_OTHER;
228  /* but let's look for a cast to json, if it's not built-in */
229  if (typoid >= FirstNormalObjectId)
230  {
231  Oid castfunc;
232  CoercionPathType ctype;
233 
234  ctype = find_coercion_pathway(JSONOID, typoid,
236  &castfunc);
237  if (ctype == COERCION_PATH_FUNC && OidIsValid(castfunc))
238  {
239  *tcategory = JSONTYPE_CAST;
240  *outfuncoid = castfunc;
241  }
242  else
243  {
244  /* non builtin type with no cast */
245  getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
246  }
247  }
248  else
249  {
250  /* any other builtin type */
251  getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
252  }
253  }
254  break;
255  }
256 }
#define OidIsValid(objectId)
Definition: c.h:710
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2716
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2612
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2864
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2478
CoercionPathType find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, CoercionContext ccontext, Oid *funcid)
CoercionPathType
Definition: parse_coerce.h:25
@ COERCION_PATH_FUNC
Definition: parse_coerce.h:27
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
@ COERCION_EXPLICIT
Definition: primnodes.h:475
#define FirstNormalObjectId
Definition: transam.h:197

References COERCION_EXPLICIT, COERCION_PATH_FUNC, find_coercion_pathway(), FirstNormalObjectId, get_element_type(), getBaseType(), getTypeOutputInfo(), InvalidOid, JSONTYPE_ARRAY, JSONTYPE_BOOL, JSONTYPE_CAST, JSONTYPE_COMPOSITE, JSONTYPE_DATE, JSONTYPE_JSON, JSONTYPE_NUMERIC, JSONTYPE_OTHER, JSONTYPE_TIMESTAMP, JSONTYPE_TIMESTAMPTZ, OidIsValid, and type_is_rowtype().

Referenced by add_json(), array_to_json_internal(), composite_to_json(), ExecInitExprRec(), json_agg_transfn_worker(), json_object_agg_transfn_worker(), to_json(), and to_json_is_immutable().

◆ json_validate()

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

Definition at line 1684 of file json.c.

1685 {
1686  JsonLexContext *lex = makeJsonLexContext(json, check_unique_keys);
1687  JsonSemAction uniqueSemAction = {0};
1689  JsonParseErrorType result;
1690 
1691  if (check_unique_keys)
1692  {
1693  state.lex = lex;
1694  state.stack = NULL;
1695  state.id_counter = 0;
1696  state.unique = true;
1697  json_unique_check_init(&state.check);
1698 
1699  uniqueSemAction.semstate = &state;
1700  uniqueSemAction.object_start = json_unique_object_start;
1702  uniqueSemAction.object_end = json_unique_object_end;
1703  }
1704 
1705  result = pg_parse_json(lex, check_unique_keys ? &uniqueSemAction : &nullSemAction);
1706 
1707  if (result != JSON_SUCCESS)
1708  {
1709  if (throw_error)
1710  json_ereport_error(result, lex);
1711 
1712  return false; /* invalid json */
1713  }
1714 
1715  if (check_unique_keys && !state.unique)
1716  {
1717  if (throw_error)
1718  ereport(ERROR,
1719  (errcode(ERRCODE_DUPLICATE_JSON_OBJECT_KEY_VALUE),
1720  errmsg("duplicate JSON object key value")));
1721 
1722  return false; /* not unique keys */
1723  }
1724 
1725  return true; /* ok */
1726 }
static void json_unique_object_start(void *_state)
Definition: json.c:1630
static void json_unique_check_init(JsonUniqueCheckState *cxt)
Definition: json.c:965
static void json_unique_object_field_start(void *_state, char *field, bool isnull)
Definition: json.c:1660
static void json_unique_object_end(void *_state)
Definition: json.c:1646
JsonSemAction nullSemAction
Definition: jsonapi.c:56
JsonParseErrorType pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
Definition: jsonapi.c:168
JsonParseErrorType
Definition: jsonapi.h:37
@ JSON_SUCCESS
Definition: jsonapi.h:38
JsonLexContext * makeJsonLexContext(text *json, bool need_escapes)
Definition: jsonfuncs.c:517
void json_ereport_error(JsonParseErrorType error, JsonLexContext *lex)
Definition: jsonfuncs.c:611
json_struct_action object_start
Definition: jsonapi.h:107
json_ofield_action object_field_start
Definition: jsonapi.h:111
void * semstate
Definition: jsonapi.h:106
json_struct_action object_end
Definition: jsonapi.h:108
Definition: regguts.h:318

References ereport, errcode(), errmsg(), ERROR, json_ereport_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 375 of file json.c.

376 {
377  if (!buf)
378  buf = palloc(MAXDATELEN + 1);
379 
380  switch (typid)
381  {
382  case DATEOID:
383  {
384  DateADT date;
385  struct pg_tm tm;
386 
388 
389  /* Same as date_out(), but forcing DateStyle */
390  if (DATE_NOT_FINITE(date))
392  else
393  {
395  &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
397  }
398  }
399  break;
400  case TIMEOID:
401  {
402  TimeADT time = DatumGetTimeADT(value);
403  struct pg_tm tt,
404  *tm = &tt;
405  fsec_t fsec;
406 
407  /* Same as time_out(), but forcing DateStyle */
408  time2tm(time, tm, &fsec);
409  EncodeTimeOnly(tm, fsec, false, 0, USE_XSD_DATES, buf);
410  }
411  break;
412  case TIMETZOID:
413  {
415  struct pg_tm tt,
416  *tm = &tt;
417  fsec_t fsec;
418  int tz;
419 
420  /* Same as timetz_out(), but forcing DateStyle */
421  timetz2tm(time, tm, &fsec, &tz);
422  EncodeTimeOnly(tm, fsec, true, tz, USE_XSD_DATES, buf);
423  }
424  break;
425  case TIMESTAMPOID:
426  {
428  struct pg_tm tm;
429  fsec_t fsec;
430 
432  /* Same as timestamp_out(), but forcing DateStyle */
435  else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
436  EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
437  else
438  ereport(ERROR,
439  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
440  errmsg("timestamp out of range")));
441  }
442  break;
443  case TIMESTAMPTZOID:
444  {
446  struct pg_tm tm;
447  int tz;
448  fsec_t fsec;
449  const char *tzn = NULL;
450 
452 
453  /*
454  * If a time zone is specified, we apply the time-zone shift,
455  * convert timestamptz to pg_tm as if it were without a time
456  * zone, and then use the specified time zone for converting
457  * the timestamp into a string.
458  */
459  if (tzp)
460  {
461  tz = *tzp;
463  }
464 
465  /* Same as timestamptz_out(), but forcing DateStyle */
468  else if (timestamp2tm(timestamp, tzp ? NULL : &tz, &tm, &fsec,
469  tzp ? NULL : &tzn, NULL) == 0)
470  {
471  if (tzp)
472  tm.tm_isdst = 1; /* set time-zone presence flag */
473 
474  EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
475  }
476  else
477  ereport(ERROR,
478  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
479  errmsg("timestamp out of range")));
480  }
481  break;
482  default:
483  elog(ERROR, "unknown jsonb value datetime type oid %u", typid);
484  return NULL;
485  }
486 
487  return buf;
488 }
void EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str)
Definition: datetime.c:4228
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:309
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:4258
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition: datetime.c:4143
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1527
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1816
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:2377
int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec)
Definition: date.c:1476
void EncodeSpecialDate(DateADT dt, char *str)
Definition: date.c:287
#define DATE_NOT_FINITE(j)
Definition: date.h:43
#define DatumGetTimeTzADTP(X)
Definition: date.h:55
int32 DateADT
Definition: date.h:23
#define DatumGetTimeADT(X)
Definition: date.h:54
int64 TimeADT
Definition: date.h:25
#define DatumGetDateADT(X)
Definition: date.h:53
#define elog(elevel,...)
Definition: elog.h:218
#define MAXDATELEN
Definition: datetime.h:201
static struct @151 value
static struct pg_tm tm
Definition: localtime.c:102
void * palloc(Size size)
Definition: mcxt.c:1068
#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
#define DatumGetTimestamp(X)
Definition: timestamp.h:27
#define DatumGetTimestampTz(X)
Definition: timestamp.h:28

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

759 {
760  JsonTypeCategory tcategory;
761  Oid outfuncoid;
762 
763  json_categorize_type(typoid, &tcategory, &outfuncoid);
764 
765  switch (tcategory)
766  {
767  case JSONTYPE_BOOL:
768  case JSONTYPE_JSON:
769  return true;
770 
771  case JSONTYPE_DATE:
772  case JSONTYPE_TIMESTAMP:
774  return false;
775 
776  case JSONTYPE_ARRAY:
777  return false; /* TODO recurse into elements */
778 
779  case JSONTYPE_COMPOSITE:
780  return false; /* TODO recurse into fields */
781 
782  case JSONTYPE_NUMERIC:
783  case JSONTYPE_CAST:
784  default:
785  return func_volatile(outfuncoid) == PROVOLATILE_IMMUTABLE;
786  }
787 }
void json_categorize_type(Oid typoid, JsonTypeCategory *tcategory, Oid *outfuncoid)
Definition: json.c:166
char func_volatile(Oid funcid)
Definition: lsyscache.c:1761

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

Referenced by contain_mutable_functions_walker().

◆ to_json_worker()

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

Definition at line 748 of file json.c.

749 {
750  StringInfo result = makeStringInfo();
751 
752  datum_to_json(val, false, result, tcategory, outfuncoid, false);
753 
754  return PointerGetDatum(cstring_to_text_with_len(result->data, result->len));
755 }
long val
Definition: informix.c:664
static void datum_to_json(Datum val, bool is_null, StringInfo result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
Definition: json.c:268

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

Referenced by ExecEvalJsonConstructor(), and to_json().