PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
json.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/transam.h"
#include "catalog/pg_type.h"
#include "executor/spi.h"
#include "lib/stringinfo.h"
#include "libpq/pqformat.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "parser/parse_coerce.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/datetime.h"
#include "utils/lsyscache.h"
#include "utils/json.h"
#include "utils/jsonapi.h"
#include "utils/typcache.h"
#include "utils/syscache.h"
Include dependency graph for json.c:

Go to the source code of this file.

Data Structures

struct  JsonAggState
 

Macros

#define JSON_ALPHANUMERIC_CHAR(c)
 

Typedefs

typedef struct JsonAggState JsonAggState
 

Enumerations

enum  JsonParseContext {
  JSON_PARSE_VALUE, JSON_PARSE_STRING, JSON_PARSE_ARRAY_START, JSON_PARSE_ARRAY_NEXT,
  JSON_PARSE_OBJECT_START, JSON_PARSE_OBJECT_LABEL, JSON_PARSE_OBJECT_NEXT, JSON_PARSE_OBJECT_COMMA,
  JSON_PARSE_END
}
 
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

static void json_lex (JsonLexContext *lex)
 
static void json_lex_string (JsonLexContext *lex)
 
static void json_lex_number (JsonLexContext *lex, char *s, bool *num_err, int *total_len)
 
static void parse_scalar (JsonLexContext *lex, JsonSemAction *sem)
 
static void parse_object_field (JsonLexContext *lex, JsonSemAction *sem)
 
static void parse_object (JsonLexContext *lex, JsonSemAction *sem)
 
static void parse_array_element (JsonLexContext *lex, JsonSemAction *sem)
 
static void parse_array (JsonLexContext *lex, JsonSemAction *sem)
 
static void report_parse_error (JsonParseContext ctx, JsonLexContext *lex)
 
static void report_invalid_token (JsonLexContext *lex)
 
static int report_json_context (JsonLexContext *lex)
 
static char * extract_mb_char (char *s)
 
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 json_categorize_type (Oid typoid, JsonTypeCategory *tcategory, Oid *outfuncoid)
 
static void datum_to_json (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)
 
static JsonTokenType lex_peek (JsonLexContext *lex)
 
static bool lex_accept (JsonLexContext *lex, JsonTokenType token, char **lexeme)
 
static void lex_expect (JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
 
bool IsValidJsonNumber (const char *str, int len)
 
Datum json_in (PG_FUNCTION_ARGS)
 
Datum json_out (PG_FUNCTION_ARGS)
 
Datum json_send (PG_FUNCTION_ARGS)
 
Datum json_recv (PG_FUNCTION_ARGS)
 
JsonLexContextmakeJsonLexContext (text *json, bool need_escapes)
 
JsonLexContextmakeJsonLexContextCstringLen (char *json, int len, bool need_escapes)
 
void pg_parse_json (JsonLexContext *lex, JsonSemAction *sem)
 
int json_count_array_elements (JsonLexContext *lex)
 
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)
 
Datum to_json (PG_FUNCTION_ARGS)
 
Datum json_agg_transfn (PG_FUNCTION_ARGS)
 
Datum json_agg_finalfn (PG_FUNCTION_ARGS)
 
Datum json_object_agg_transfn (PG_FUNCTION_ARGS)
 
Datum json_object_agg_finalfn (PG_FUNCTION_ARGS)
 
Datum json_build_object (PG_FUNCTION_ARGS)
 
Datum json_build_object_noargs (PG_FUNCTION_ARGS)
 
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)
 
void escape_json (StringInfo buf, const char *str)
 
Datum json_typeof (PG_FUNCTION_ARGS)
 

Variables

static JsonSemAction nullSemAction
 

Macro Definition Documentation

#define JSON_ALPHANUMERIC_CHAR (   c)
Value:
(((c) >= 'a' && (c) <= 'z') || \
((c) >= 'A' && (c) <= 'Z') || \
((c) >= '0' && (c) <= '9') || \
(c) == '_' || \
#define IS_HIGHBIT_SET(ch)
Definition: c.h:974
char * c

Definition at line 179 of file json.c.

Referenced by json_lex(), and json_lex_number().

Typedef Documentation

Enumeration Type Documentation

Enumerator
JSON_PARSE_VALUE 
JSON_PARSE_STRING 
JSON_PARSE_ARRAY_START 
JSON_PARSE_ARRAY_NEXT 
JSON_PARSE_OBJECT_START 
JSON_PARSE_OBJECT_LABEL 
JSON_PARSE_OBJECT_NEXT 
JSON_PARSE_OBJECT_COMMA 
JSON_PARSE_END 

Definition at line 40 of file json.c.

41 {
42  JSON_PARSE_VALUE, /* expecting a value */
43  JSON_PARSE_STRING, /* expecting a string (for a field name) */
44  JSON_PARSE_ARRAY_START, /* saw '[', expecting value or ']' */
45  JSON_PARSE_ARRAY_NEXT, /* saw array element, expecting ',' or ']' */
46  JSON_PARSE_OBJECT_START, /* saw '{', expecting label or '}' */
47  JSON_PARSE_OBJECT_LABEL, /* saw object label, expecting ':' */
48  JSON_PARSE_OBJECT_NEXT, /* saw object value, expecting ',' or '}' */
49  JSON_PARSE_OBJECT_COMMA, /* saw object ',', expecting next label */
50  JSON_PARSE_END /* saw the end of a document, expect nothing */
JsonParseContext
Definition: json.c:40
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 53 of file json.c.

54 {
55  JSONTYPE_NULL, /* null, so we didn't bother to identify */
56  JSONTYPE_BOOL, /* boolean (built-in types only) */
57  JSONTYPE_NUMERIC, /* numeric (ditto) */
58  JSONTYPE_DATE, /* we use special formatting for datetimes */
61  JSONTYPE_JSON, /* JSON itself (and JSONB) */
62  JSONTYPE_ARRAY, /* array */
63  JSONTYPE_COMPOSITE, /* composite */
64  JSONTYPE_CAST, /* something with an explicit cast to JSON */
65  JSONTYPE_OTHER /* all else */
JsonTypeCategory
Definition: json.c:53

Function Documentation

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

Definition at line 1755 of file json.c.

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

Referenced by json_build_array(), and json_build_object().

1757 {
1758  JsonTypeCategory tcategory;
1759  Oid outfuncoid;
1760 
1761  if (val_type == InvalidOid)
1762  ereport(ERROR,
1763  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1764  errmsg("could not determine input data type")));
1765 
1766  if (is_null)
1767  {
1768  tcategory = JSONTYPE_NULL;
1769  outfuncoid = InvalidOid;
1770  }
1771  else
1772  json_categorize_type(val_type,
1773  &tcategory, &outfuncoid);
1774 
1775  datum_to_json(val, is_null, result, tcategory, outfuncoid, key_scalar);
1776 }
static void datum_to_json(Datum val, bool is_null, StringInfo result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
Definition: json.c:1449
static void json_categorize_type(Oid typoid, JsonTypeCategory *tcategory, Oid *outfuncoid)
Definition: json.c:1347
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
JsonTypeCategory
Definition: json.c:53
#define ereport(elevel, rest)
Definition: elog.h:122
#define InvalidOid
Definition: postgres_ext.h:36
int errmsg(const char *fmt,...)
Definition: elog.c:797
long val
Definition: informix.c:689
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 1593 of file json.c.

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

Referenced by array_to_json_internal().

1596 {
1597  int i;
1598  const char *sep;
1599 
1600  Assert(dim < ndims);
1601 
1602  sep = use_line_feeds ? ",\n " : ",";
1603 
1604  appendStringInfoChar(result, '[');
1605 
1606  for (i = 1; i <= dims[dim]; i++)
1607  {
1608  if (i > 1)
1609  appendStringInfoString(result, sep);
1610 
1611  if (dim + 1 == ndims)
1612  {
1613  datum_to_json(vals[*valcount], nulls[*valcount], result, tcategory,
1614  outfuncoid, false);
1615  (*valcount)++;
1616  }
1617  else
1618  {
1619  /*
1620  * Do we want line feeds on inner dimensions of arrays? For now
1621  * we'll say no.
1622  */
1623  array_dim_to_json(result, dim + 1, ndims, dims, vals, nulls,
1624  valcount, tcategory, outfuncoid, false);
1625  }
1626  }
1627 
1628  appendStringInfoChar(result, ']');
1629 }
static void datum_to_json(Datum val, bool is_null, StringInfo result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
Definition: json.c:1449
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:1593
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
#define Assert(condition)
Definition: c.h:676
int i
Datum array_to_json ( PG_FUNCTION_ARGS  )

Definition at line 1782 of file json.c.

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

1783 {
1784  Datum array = PG_GETARG_DATUM(0);
1786 
1787  result = makeStringInfo();
1788 
1789  array_to_json_internal(array, result, false);
1790 
1792 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
return result
Definition: formatting.c:1633
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
static void array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
Definition: json.c:1635
static void array_to_json_internal ( Datum  array,
StringInfo  result,
bool  use_line_feeds 
)
static

Definition at line 1635 of file json.c.

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

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

1636 {
1637  ArrayType *v = DatumGetArrayTypeP(array);
1638  Oid element_type = ARR_ELEMTYPE(v);
1639  int *dim;
1640  int ndim;
1641  int nitems;
1642  int count = 0;
1643  Datum *elements;
1644  bool *nulls;
1645  int16 typlen;
1646  bool typbyval;
1647  char typalign;
1648  JsonTypeCategory tcategory;
1649  Oid outfuncoid;
1650 
1651  ndim = ARR_NDIM(v);
1652  dim = ARR_DIMS(v);
1653  nitems = ArrayGetNItems(ndim, dim);
1654 
1655  if (nitems <= 0)
1656  {
1657  appendStringInfoString(result, "[]");
1658  return;
1659  }
1660 
1661  get_typlenbyvalalign(element_type,
1662  &typlen, &typbyval, &typalign);
1663 
1664  json_categorize_type(element_type,
1665  &tcategory, &outfuncoid);
1666 
1667  deconstruct_array(v, element_type, typlen, typbyval,
1668  typalign, &elements, &nulls,
1669  &nitems);
1670 
1671  array_dim_to_json(result, 0, ndim, dim, elements, nulls, &count, tcategory,
1672  outfuncoid, use_line_feeds);
1673 
1674  pfree(elements);
1675  pfree(nulls);
1676 }
signed short int16
Definition: c.h:255
static void json_categorize_type(Oid typoid, JsonTypeCategory *tcategory, Oid *outfuncoid)
Definition: json.c:1347
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2021
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:1593
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:75
unsigned int Oid
Definition: postgres_ext.h:31
void pfree(void *pointer)
Definition: mcxt.c:950
#define ARR_DIMS(a)
Definition: array.h:275
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
JsonTypeCategory
Definition: json.c:53
uintptr_t Datum
Definition: postgres.h:372
#define ARR_NDIM(a)
Definition: array.h:271
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
#define ARR_ELEMTYPE(a)
Definition: array.h:273
#define DatumGetArrayTypeP(X)
Definition: array.h:242
Datum array_to_json_pretty ( PG_FUNCTION_ARGS  )

Definition at line 1798 of file json.c.

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

1799 {
1800  Datum array = PG_GETARG_DATUM(0);
1801  bool use_line_feeds = PG_GETARG_BOOL(1);
1803 
1804  result = makeStringInfo();
1805 
1806  array_to_json_internal(array, result, use_line_feeds);
1807 
1809 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
return result
Definition: formatting.c:1633
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
static void array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
Definition: json.c:1635
static text * catenate_stringinfo_string ( StringInfo  buffer,
const char *  addon 
)
static

Definition at line 2092 of file json.c.

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

Referenced by json_agg_finalfn(), and json_object_agg_finalfn().

2093 {
2094  /* custom version of cstring_to_text_with_len */
2095  int buflen = buffer->len;
2096  int addlen = strlen(addon);
2097  text *result = (text *) palloc(buflen + addlen + VARHDRSZ);
2098 
2099  SET_VARSIZE(result, buflen + addlen + VARHDRSZ);
2100  memcpy(VARDATA(result), buffer->data, buflen);
2101  memcpy(VARDATA(result) + buflen, addon, addlen);
2102 
2103  return result;
2104 }
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:445
return result
Definition: formatting.c:1633
void * palloc(Size size)
Definition: mcxt.c:849
Definition: c.h:439
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
static void composite_to_json ( Datum  composite,
StringInfo  result,
bool  use_line_feeds 
)
static

Definition at line 1682 of file json.c.

References appendStringInfoChar(), appendStringInfoString(), tupleDesc::attrs, datum_to_json(), DatumGetHeapTupleHeader, escape_json(), heap_getattr, HeapTupleHeaderGetDatumLength, HeapTupleHeaderGetTypeId, HeapTupleHeaderGetTypMod, i, InvalidOid, json_categorize_type(), JSONTYPE_NULL, lookup_rowtype_tupdesc(), NameStr, tupleDesc::natts, ReleaseTupleDesc, HeapTupleData::t_data, HeapTupleData::t_len, and val.

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

1683 {
1684  HeapTupleHeader td;
1685  Oid tupType;
1686  int32 tupTypmod;
1687  TupleDesc tupdesc;
1688  HeapTupleData tmptup,
1689  *tuple;
1690  int i;
1691  bool needsep = false;
1692  const char *sep;
1693 
1694  sep = use_line_feeds ? ",\n " : ",";
1695 
1696  td = DatumGetHeapTupleHeader(composite);
1697 
1698  /* Extract rowtype info and find a tupdesc */
1699  tupType = HeapTupleHeaderGetTypeId(td);
1700  tupTypmod = HeapTupleHeaderGetTypMod(td);
1701  tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
1702 
1703  /* Build a temporary HeapTuple control structure */
1704  tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
1705  tmptup.t_data = td;
1706  tuple = &tmptup;
1707 
1708  appendStringInfoChar(result, '{');
1709 
1710  for (i = 0; i < tupdesc->natts; i++)
1711  {
1712  Datum val;
1713  bool isnull;
1714  char *attname;
1715  JsonTypeCategory tcategory;
1716  Oid outfuncoid;
1717 
1718  if (tupdesc->attrs[i]->attisdropped)
1719  continue;
1720 
1721  if (needsep)
1722  appendStringInfoString(result, sep);
1723  needsep = true;
1724 
1725  attname = NameStr(tupdesc->attrs[i]->attname);
1726  escape_json(result, attname);
1727  appendStringInfoChar(result, ':');
1728 
1729  val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
1730 
1731  if (isnull)
1732  {
1733  tcategory = JSONTYPE_NULL;
1734  outfuncoid = InvalidOid;
1735  }
1736  else
1737  json_categorize_type(tupdesc->attrs[i]->atttypid,
1738  &tcategory, &outfuncoid);
1739 
1740  datum_to_json(val, isnull, result, tcategory, outfuncoid, false);
1741  }
1742 
1743  appendStringInfoChar(result, '}');
1744  ReleaseTupleDesc(tupdesc);
1745 }
static void datum_to_json(Datum val, bool is_null, StringInfo result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
Definition: json.c:1449
void escape_json(StringInfo buf, const char *str)
Definition: json.c:2433
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1257
static void json_categorize_type(Oid typoid, JsonTypeCategory *tcategory, Oid *outfuncoid)
Definition: json.c:1347
Form_pg_attribute * attrs
Definition: tupdesc.h:74
unsigned int Oid
Definition: postgres_ext.h:31
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:259
int natts
Definition: tupdesc.h:73
signed int int32
Definition: c.h:256
HeapTupleHeader t_data
Definition: htup.h:67
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:455
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
uint32 t_len
Definition: htup.h:64
JsonTypeCategory
Definition: json.c:53
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:769
uintptr_t Datum
Definition: postgres.h:372
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:445
#define InvalidOid
Definition: postgres_ext.h:36
int i
#define NameStr(name)
Definition: c.h:499
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:107
long val
Definition: informix.c:689
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:439
static void datum_to_json ( Datum  val,
bool  is_null,
StringInfo  result,
JsonTypeCategory  tcategory,
Oid  outfuncoid,
bool  key_scalar 
)
static

Definition at line 1449 of file json.c.

References appendStringInfo(), appendStringInfoString(), array_to_json_internal(), Assert, buf, check_stack_depth(), composite_to_json(), DATE_NOT_FINITE, DatumGetBool, DatumGetDateADT, DatumGetTextPP, DatumGetTimestamp, DatumGetTimestampTz, EncodeDateOnly(), EncodeDateTime(), EncodeSpecialDate(), EncodeSpecialTimestamp(), ereport, errcode(), errmsg(), ERROR, escape_json(), IsValidJsonNumber(), j2date(), JSONTYPE_ARRAY, JSONTYPE_BOOL, JSONTYPE_CAST, JSONTYPE_COMPOSITE, JSONTYPE_DATE, JSONTYPE_JSON, JSONTYPE_NUMERIC, JSONTYPE_TIMESTAMP, JSONTYPE_TIMESTAMPTZ, MAXDATELEN, NULL, OidFunctionCall1, OidOutputFunctionCall(), pfree(), POSTGRES_EPOCH_JDATE, text_to_cstring(), timestamp2tm(), TIMESTAMP_NOT_FINITE, pg_tm::tm_mday, pg_tm::tm_mon, pg_tm::tm_year, and USE_XSD_DATES.

Referenced by add_json(), array_dim_to_json(), composite_to_json(), json_agg_transfn(), json_object_agg_transfn(), and to_json().

1452 {
1453  char *outputstr;
1454  text *jsontext;
1455 
1457 
1458  /* callers are expected to ensure that null keys are not passed in */
1459  Assert(!(key_scalar && is_null));
1460 
1461  if (is_null)
1462  {
1463  appendStringInfoString(result, "null");
1464  return;
1465  }
1466 
1467  if (key_scalar &&
1468  (tcategory == JSONTYPE_ARRAY ||
1469  tcategory == JSONTYPE_COMPOSITE ||
1470  tcategory == JSONTYPE_JSON ||
1471  tcategory == JSONTYPE_CAST))
1472  ereport(ERROR,
1473  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1474  errmsg("key value must be scalar, not array, composite, or json")));
1475 
1476  switch (tcategory)
1477  {
1478  case JSONTYPE_ARRAY:
1479  array_to_json_internal(val, result, false);
1480  break;
1481  case JSONTYPE_COMPOSITE:
1482  composite_to_json(val, result, false);
1483  break;
1484  case JSONTYPE_BOOL:
1485  outputstr = DatumGetBool(val) ? "true" : "false";
1486  if (key_scalar)
1487  escape_json(result, outputstr);
1488  else
1489  appendStringInfoString(result, outputstr);
1490  break;
1491  case JSONTYPE_NUMERIC:
1492  outputstr = OidOutputFunctionCall(outfuncoid, val);
1493 
1494  /*
1495  * Don't call escape_json for a non-key if it's a valid JSON
1496  * number.
1497  */
1498  if (!key_scalar && IsValidJsonNumber(outputstr, strlen(outputstr)))
1499  appendStringInfoString(result, outputstr);
1500  else
1501  escape_json(result, outputstr);
1502  pfree(outputstr);
1503  break;
1504  case JSONTYPE_DATE:
1505  {
1506  DateADT date;
1507  struct pg_tm tm;
1508  char buf[MAXDATELEN + 1];
1509 
1510  date = DatumGetDateADT(val);
1511  /* Same as date_out(), but forcing DateStyle */
1512  if (DATE_NOT_FINITE(date))
1513  EncodeSpecialDate(date, buf);
1514  else
1515  {
1517  &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
1519  }
1520  appendStringInfo(result, "\"%s\"", buf);
1521  }
1522  break;
1523  case JSONTYPE_TIMESTAMP:
1524  {
1526  struct pg_tm tm;
1527  fsec_t fsec;
1528  char buf[MAXDATELEN + 1];
1529 
1530  timestamp = DatumGetTimestamp(val);
1531  /* Same as timestamp_out(), but forcing DateStyle */
1532  if (TIMESTAMP_NOT_FINITE(timestamp))
1533  EncodeSpecialTimestamp(timestamp, buf);
1534  else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
1535  EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
1536  else
1537  ereport(ERROR,
1538  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1539  errmsg("timestamp out of range")));
1540  appendStringInfo(result, "\"%s\"", buf);
1541  }
1542  break;
1543  case JSONTYPE_TIMESTAMPTZ:
1544  {
1546  struct pg_tm tm;
1547  int tz;
1548  fsec_t fsec;
1549  const char *tzn = NULL;
1550  char buf[MAXDATELEN + 1];
1551 
1552  timestamp = DatumGetTimestampTz(val);
1553  /* Same as timestamptz_out(), but forcing DateStyle */
1554  if (TIMESTAMP_NOT_FINITE(timestamp))
1555  EncodeSpecialTimestamp(timestamp, buf);
1556  else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
1557  EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
1558  else
1559  ereport(ERROR,
1560  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1561  errmsg("timestamp out of range")));
1562  appendStringInfo(result, "\"%s\"", buf);
1563  }
1564  break;
1565  case JSONTYPE_JSON:
1566  /* JSON and JSONB output will already be escaped */
1567  outputstr = OidOutputFunctionCall(outfuncoid, val);
1568  appendStringInfoString(result, outputstr);
1569  pfree(outputstr);
1570  break;
1571  case JSONTYPE_CAST:
1572  /* outfuncoid refers to a cast function, not an output function */
1573  jsontext = DatumGetTextPP(OidFunctionCall1(outfuncoid, val));
1574  outputstr = text_to_cstring(jsontext);
1575  appendStringInfoString(result, outputstr);
1576  pfree(outputstr);
1577  pfree(jsontext);
1578  break;
1579  default:
1580  outputstr = OidOutputFunctionCall(outfuncoid, val);
1581  escape_json(result, outputstr);
1582  pfree(outputstr);
1583  break;
1584  }
1585 }
#define MAXDATELEN
Definition: datetime.h:203
void EncodeDateOnly(struct pg_tm *tm, int style, char *str)
Definition: datetime.c:3884
#define DatumGetDateADT(X)
Definition: date.h:52
void escape_json(StringInfo buf, const char *str)
Definition: json.c:2433
int32 DateADT
Definition: date.h:22
int64 timestamp
int64 TimestampTz
Definition: timestamp.h:39
static void composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
Definition: json.c:1682
#define DatumGetTextPP(X)
Definition: fmgr.h:256
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1758
int errcode(int sqlerrcode)
Definition: elog.c:575
long date
Definition: pgtypes_date.h:8
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1523
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:111
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
void pfree(void *pointer)
Definition: mcxt.c:950
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define ERROR
Definition: elog.h:43
bool IsValidJsonNumber(const char *str, int len)
Definition: json.c:192
#define DATE_NOT_FINITE(j)
Definition: date.h:42
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:622
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
int tm_mday
Definition: pgtime.h:30
static char * buf
Definition: pg_test_fsync.c:66
int tm_mon
Definition: pgtime.h:31
void check_stack_depth(void)
Definition: postgres.c:3117
#define DatumGetTimestampTz(X)
Definition: timestamp.h:28
#define DatumGetBool(X)
Definition: postgres.h:399
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:3999
void EncodeSpecialDate(DateADT dt, char *str)
Definition: date.c:299
int32 fsec_t
Definition: timestamp.h:41
#define ereport(elevel, rest)
Definition: elog.h:122
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:317
int64 Timestamp
Definition: timestamp.h:38
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
static void array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
Definition: json.c:1635
char * text_to_cstring(const text *t)
Definition: varlena.c:182
#define USE_XSD_DATES
Definition: miscadmin.h:214
int tm_year
Definition: pgtime.h:32
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1747
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
long val
Definition: informix.c:689
#define DatumGetTimestamp(X)
Definition: timestamp.h:27
void escape_json ( StringInfo  buf,
const char *  str 
)

Definition at line 2433 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(), sn_object_field_start(), sn_scalar(), transform_string_values_object_field_start(), and transform_string_values_scalar().

2434 {
2435  const char *p;
2436 
2437  appendStringInfoCharMacro(buf, '"');
2438  for (p = str; *p; p++)
2439  {
2440  switch (*p)
2441  {
2442  case '\b':
2443  appendStringInfoString(buf, "\\b");
2444  break;
2445  case '\f':
2446  appendStringInfoString(buf, "\\f");
2447  break;
2448  case '\n':
2449  appendStringInfoString(buf, "\\n");
2450  break;
2451  case '\r':
2452  appendStringInfoString(buf, "\\r");
2453  break;
2454  case '\t':
2455  appendStringInfoString(buf, "\\t");
2456  break;
2457  case '"':
2458  appendStringInfoString(buf, "\\\"");
2459  break;
2460  case '\\':
2461  appendStringInfoString(buf, "\\\\");
2462  break;
2463  default:
2464  if ((unsigned char) *p < ' ')
2465  appendStringInfo(buf, "\\u%04x", (int) *p);
2466  else
2467  appendStringInfoCharMacro(buf, *p);
2468  break;
2469  }
2470  }
2471  appendStringInfoCharMacro(buf, '"');
2472 }
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:127
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
static char * extract_mb_char ( char *  s)
static

Definition at line 1326 of file json.c.

References palloc(), and pg_mblen().

Referenced by json_lex_string().

1327 {
1328  char *res;
1329  int len;
1330 
1331  len = pg_mblen(s);
1332  res = palloc(len + 1);
1333  memcpy(res, s, len);
1334  res[len] = '\0';
1335 
1336  return res;
1337 }
int pg_mblen(const char *mbstr)
Definition: mbutils.c:771
void * palloc(Size size)
Definition: mcxt.c:849
bool IsValidJsonNumber ( const char *  str,
int  len 
)

Definition at line 192 of file json.c.

References JsonLexContext::input, JsonLexContext::input_length, and json_lex_number().

Referenced by datum_to_json(), hstore_to_json_loose(), and hstore_to_jsonb_loose().

193 {
194  bool numeric_error;
195  int total_len;
196  JsonLexContext dummy_lex;
197 
198  if (len <= 0)
199  return false;
200 
201  /*
202  * json_lex_number expects a leading '-' to have been eaten already.
203  *
204  * having to cast away the constness of str is ugly, but there's not much
205  * easy alternative.
206  */
207  if (*str == '-')
208  {
209  dummy_lex.input = (char *) str + 1;
210  dummy_lex.input_length = len - 1;
211  }
212  else
213  {
214  dummy_lex.input = (char *) str;
215  dummy_lex.input_length = len;
216  }
217 
218  json_lex_number(&dummy_lex, dummy_lex.input, &numeric_error, &total_len);
219 
220  return (!numeric_error) && (total_len == dummy_lex.input_length);
221 }
static void json_lex_number(JsonLexContext *lex, char *s, bool *num_err, int *total_len)
Definition: json.c:1027
int input_length
Definition: jsonapi.h:54
char * input
Definition: jsonapi.h:53
Datum json_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 1953 of file json.c.

References AggCheckCallContext(), Assert, catenate_stringinfo_string(), NULL, PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_NULL, PG_RETURN_TEXT_P, and JsonAggState::str.

1954 {
1956 
1957  /* cannot be called directly because of internal-type argument */
1958  Assert(AggCheckCallContext(fcinfo, NULL));
1959 
1960  state = PG_ARGISNULL(0) ?
1961  NULL :
1963 
1964  /* NULL result for no rows in, as is standard with aggregates */
1965  if (state == NULL)
1966  PG_RETURN_NULL();
1967 
1968  /* Else return state with appropriate array terminator added */
1970 }
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
static text * catenate_stringinfo_string(StringInfo buffer, const char *addon)
Definition: json.c:2092
StringInfo str
Definition: json.c:70
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
Definition: regguts.h:298
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4027
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum json_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 1877 of file json.c.

References AggCheckCallContext(), appendStringInfoChar(), appendStringInfoString(), datum_to_json(), elog, ereport, errcode(), errmsg(), ERROR, 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, JsonAggState::str, val, JsonAggState::val_category, and JsonAggState::val_output_func.

1878 {
1879  MemoryContext aggcontext,
1880  oldcontext;
1882  Datum val;
1883 
1884  if (!AggCheckCallContext(fcinfo, &aggcontext))
1885  {
1886  /* cannot be called directly because of internal-type argument */
1887  elog(ERROR, "json_agg_transfn called in non-aggregate context");
1888  }
1889 
1890  if (PG_ARGISNULL(0))
1891  {
1892  Oid arg_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
1893 
1894  if (arg_type == InvalidOid)
1895  ereport(ERROR,
1896  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1897  errmsg("could not determine input data type")));
1898 
1899  /*
1900  * Make this state object in a context where it will persist for the
1901  * duration of the aggregate call. MemoryContextSwitchTo is only
1902  * needed the first time, as the StringInfo routines make sure they
1903  * use the right context to enlarge the object if necessary.
1904  */
1905  oldcontext = MemoryContextSwitchTo(aggcontext);
1906  state = (JsonAggState *) palloc(sizeof(JsonAggState));
1907  state->str = makeStringInfo();
1908  MemoryContextSwitchTo(oldcontext);
1909 
1910  appendStringInfoChar(state->str, '[');
1911  json_categorize_type(arg_type, &state->val_category,
1912  &state->val_output_func);
1913  }
1914  else
1915  {
1916  state = (JsonAggState *) PG_GETARG_POINTER(0);
1917  appendStringInfoString(state->str, ", ");
1918  }
1919 
1920  /* fast path for NULLs */
1921  if (PG_ARGISNULL(1))
1922  {
1923  datum_to_json((Datum) 0, true, state->str, JSONTYPE_NULL,
1924  InvalidOid, false);
1925  PG_RETURN_POINTER(state);
1926  }
1927 
1928  val = PG_GETARG_DATUM(1);
1929 
1930  /* add some whitespace if structured type and not first item */
1931  if (!PG_ARGISNULL(0) &&
1932  (state->val_category == JSONTYPE_ARRAY ||
1933  state->val_category == JSONTYPE_COMPOSITE))
1934  {
1935  appendStringInfoString(state->str, "\n ");
1936  }
1937 
1938  datum_to_json(val, false, state->str, state->val_category,
1939  state->val_output_func, false);
1940 
1941  /*
1942  * The transition type for array_agg() is declared to be "internal", which
1943  * is a pass-by-value type the same size as a pointer. So we can safely
1944  * pass the JsonAggState pointer through nodeAgg.c's machinations.
1945  */
1946  PG_RETURN_POINTER(state);
1947 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
static void datum_to_json(Datum val, bool is_null, StringInfo result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
Definition: json.c:1449
static void json_categorize_type(Oid typoid, JsonTypeCategory *tcategory, Oid *outfuncoid)
Definition: json.c:1347
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
Oid val_output_func
Definition: json.c:74
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1909
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
StringInfo str
Definition: json.c:70
#define ereport(elevel, rest)
Definition: elog.h:122
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
uintptr_t Datum
Definition: postgres.h:372
#define InvalidOid
Definition: postgres_ext.h:36
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
JsonTypeCategory val_category
Definition: json.c:73
Definition: regguts.h:298
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4027
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
long val
Definition: informix.c:689
Datum json_build_array ( PG_FUNCTION_ARGS  )

Definition at line 2197 of file json.c.

References add_json(), appendStringInfoChar(), appendStringInfoString(), arg, cstring_to_text_with_len(), StringInfoData::data, ereport, errcode(), errmsg(), ERROR, get_fn_expr_argtype(), i, InvalidOid, StringInfoData::len, makeStringInfo(), PG_ARGISNULL, PG_GETARG_DATUM, PG_NARGS, PG_RETURN_TEXT_P, and result.

2198 {
2199  int nargs = PG_NARGS();
2200  int i;
2201  Datum arg;
2202  const char *sep = "";
2204  Oid val_type;
2205 
2206  result = makeStringInfo();
2207 
2208  appendStringInfoChar(result, '[');
2209 
2210  for (i = 0; i < nargs; i++)
2211  {
2212  /*
2213  * Note: since json_build_array() is declared as taking type "any",
2214  * the parser will not do any type conversion on unknown-type literals
2215  * (that is, undecorated strings or NULLs). Such values will arrive
2216  * here as type UNKNOWN, which fortunately does not matter to us,
2217  * since unknownout() works fine.
2218  */
2219  appendStringInfoString(result, sep);
2220  sep = ", ";
2221 
2222  val_type = get_fn_expr_argtype(fcinfo->flinfo, i);
2223 
2224  if (val_type == InvalidOid)
2225  ereport(ERROR,
2226  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2227  errmsg("could not determine data type for argument %d",
2228  i + 1)));
2229 
2230  if (PG_ARGISNULL(i))
2231  arg = (Datum) 0;
2232  else
2233  arg = PG_GETARG_DATUM(i);
2234 
2235  add_json(arg, PG_ARGISNULL(i), result, val_type, false);
2236  }
2237 
2238  appendStringInfoChar(result, ']');
2239 
2241 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1633
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1909
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define ereport(elevel, rest)
Definition: elog.h:122
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
uintptr_t Datum
Definition: postgres.h:372
#define InvalidOid
Definition: postgres_ext.h:36
static void add_json(Datum val, bool is_null, StringInfo result, Oid val_type, bool key_scalar)
Definition: json.c:1755
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define PG_NARGS()
Definition: fmgr.h:168
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
void * arg
Datum json_build_array_noargs ( PG_FUNCTION_ARGS  )

Definition at line 2247 of file json.c.

References cstring_to_text_with_len(), and PG_RETURN_TEXT_P.

2248 {
2250 }
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
Datum json_build_object ( PG_FUNCTION_ARGS  )

Definition at line 2110 of file json.c.

References add_json(), appendStringInfoChar(), appendStringInfoString(), arg, cstring_to_text_with_len(), StringInfoData::data, ereport, errcode(), errhint(), errmsg(), ERROR, get_fn_expr_argtype(), i, InvalidOid, StringInfoData::len, makeStringInfo(), PG_ARGISNULL, PG_GETARG_DATUM, PG_NARGS, PG_RETURN_TEXT_P, and result.

2111 {
2112  int nargs = PG_NARGS();
2113  int i;
2114  Datum arg;
2115  const char *sep = "";
2117  Oid val_type;
2118 
2119  if (nargs % 2 != 0)
2120  ereport(ERROR,
2121  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2122  errmsg("argument list must have even number of elements"),
2123  errhint("The arguments of json_build_object() must consist of alternating keys and values.")));
2124 
2125  result = makeStringInfo();
2126 
2127  appendStringInfoChar(result, '{');
2128 
2129  for (i = 0; i < nargs; i += 2)
2130  {
2131  /*
2132  * Note: since json_build_object() is declared as taking type "any",
2133  * the parser will not do any type conversion on unknown-type literals
2134  * (that is, undecorated strings or NULLs). Such values will arrive
2135  * here as type UNKNOWN, which fortunately does not matter to us,
2136  * since unknownout() works fine.
2137  */
2138  appendStringInfoString(result, sep);
2139  sep = ", ";
2140 
2141  /* process key */
2142  val_type = get_fn_expr_argtype(fcinfo->flinfo, i);
2143 
2144  if (val_type == InvalidOid)
2145  ereport(ERROR,
2146  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2147  errmsg("could not determine data type for argument %d",
2148  i + 1)));
2149 
2150  if (PG_ARGISNULL(i))
2151  ereport(ERROR,
2152  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2153  errmsg("argument %d cannot be null", i + 1),
2154  errhint("Object keys should be text.")));
2155 
2156  arg = PG_GETARG_DATUM(i);
2157 
2158  add_json(arg, false, result, val_type, true);
2159 
2160  appendStringInfoString(result, " : ");
2161 
2162  /* process value */
2163  val_type = get_fn_expr_argtype(fcinfo->flinfo, i + 1);
2164 
2165  if (val_type == InvalidOid)
2166  ereport(ERROR,
2167  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2168  errmsg("could not determine data type for argument %d",
2169  i + 2)));
2170 
2171  if (PG_ARGISNULL(i + 1))
2172  arg = (Datum) 0;
2173  else
2174  arg = PG_GETARG_DATUM(i + 1);
2175 
2176  add_json(arg, PG_ARGISNULL(i + 1), result, val_type, false);
2177  }
2178 
2179  appendStringInfoChar(result, '}');
2180 
2182 }
int errhint(const char *fmt,...)
Definition: elog.c:987
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1633
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1909
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define ereport(elevel, rest)
Definition: elog.h:122
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
uintptr_t Datum
Definition: postgres.h:372
#define InvalidOid
Definition: postgres_ext.h:36
static void add_json(Datum val, bool is_null, StringInfo result, Oid val_type, bool key_scalar)
Definition: json.c:1755
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define PG_NARGS()
Definition: fmgr.h:168
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
void * arg
Datum json_build_object_noargs ( PG_FUNCTION_ARGS  )

Definition at line 2188 of file json.c.

References cstring_to_text_with_len(), and PG_RETURN_TEXT_P.

2189 {
2191 }
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
static void json_categorize_type ( Oid  typoid,
JsonTypeCategory tcategory,
Oid outfuncoid 
)
static

Definition at line 1347 of file json.c.

References ANYARRAYOID, BOOLOID, COERCION_EXPLICIT, COERCION_PATH_FUNC, DATEOID, find_coercion_pathway(), FirstNormalObjectId, FLOAT4OID, FLOAT8OID, get_element_type(), getBaseType(), getTypeOutputInfo(), INT2OID, INT4OID, INT8OID, InvalidOid, JSONBOID, JSONOID, JSONTYPE_ARRAY, JSONTYPE_BOOL, JSONTYPE_CAST, JSONTYPE_COMPOSITE, JSONTYPE_DATE, JSONTYPE_JSON, JSONTYPE_NUMERIC, JSONTYPE_OTHER, JSONTYPE_TIMESTAMP, JSONTYPE_TIMESTAMPTZ, NUMERICOID, OidIsValid, RECORDARRAYOID, TIMESTAMPOID, TIMESTAMPTZOID, and type_is_rowtype().

Referenced by add_json(), array_to_json_internal(), composite_to_json(), json_agg_transfn(), json_object_agg_transfn(), and to_json().

1350 {
1351  bool typisvarlena;
1352 
1353  /* Look through any domain */
1354  typoid = getBaseType(typoid);
1355 
1356  *outfuncoid = InvalidOid;
1357 
1358  /*
1359  * We need to get the output function for everything except date and
1360  * timestamp types, array and composite types, booleans, and non-builtin
1361  * types where there's a cast to json.
1362  */
1363 
1364  switch (typoid)
1365  {
1366  case BOOLOID:
1367  *tcategory = JSONTYPE_BOOL;
1368  break;
1369 
1370  case INT2OID:
1371  case INT4OID:
1372  case INT8OID:
1373  case FLOAT4OID:
1374  case FLOAT8OID:
1375  case NUMERICOID:
1376  getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
1377  *tcategory = JSONTYPE_NUMERIC;
1378  break;
1379 
1380  case DATEOID:
1381  *tcategory = JSONTYPE_DATE;
1382  break;
1383 
1384  case TIMESTAMPOID:
1385  *tcategory = JSONTYPE_TIMESTAMP;
1386  break;
1387 
1388  case TIMESTAMPTZOID:
1389  *tcategory = JSONTYPE_TIMESTAMPTZ;
1390  break;
1391 
1392  case JSONOID:
1393  case JSONBOID:
1394  getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
1395  *tcategory = JSONTYPE_JSON;
1396  break;
1397 
1398  default:
1399  /* Check for arrays and composites */
1400  if (OidIsValid(get_element_type(typoid)) || typoid == ANYARRAYOID
1401  || typoid == RECORDARRAYOID)
1402  *tcategory = JSONTYPE_ARRAY;
1403  else if (type_is_rowtype(typoid)) /* includes RECORDOID */
1404  *tcategory = JSONTYPE_COMPOSITE;
1405  else
1406  {
1407  /* It's probably the general case ... */
1408  *tcategory = JSONTYPE_OTHER;
1409  /* but let's look for a cast to json, if it's not built-in */
1410  if (typoid >= FirstNormalObjectId)
1411  {
1412  Oid castfunc;
1413  CoercionPathType ctype;
1414 
1415  ctype = find_coercion_pathway(JSONOID, typoid,
1417  &castfunc);
1418  if (ctype == COERCION_PATH_FUNC && OidIsValid(castfunc))
1419  {
1420  *tcategory = JSONTYPE_CAST;
1421  *outfuncoid = castfunc;
1422  }
1423  else
1424  {
1425  /* non builtin type with no cast */
1426  getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
1427  }
1428  }
1429  else
1430  {
1431  /* any other builtin type */
1432  getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
1433  }
1434  }
1435  break;
1436  }
1437 }
#define RECORDARRAYOID
Definition: pg_type.h:682
#define TIMESTAMPTZOID
Definition: pg_type.h:525
#define DATEOID
Definition: pg_type.h:511
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2632
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2484
#define NUMERICOID
Definition: pg_type.h:554
#define JSONOID
Definition: pg_type.h:356
#define INT4OID
Definition: pg_type.h:316
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
CoercionPathType
Definition: parse_coerce.h:24
#define FirstNormalObjectId
Definition: transam.h:94
#define TIMESTAMPOID
Definition: pg_type.h:519
#define INT2OID
Definition: pg_type.h:308
#define JSONBOID
Definition: pg_type.h:638
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2404
#define FLOAT4OID
Definition: pg_type.h:416
#define ANYARRAYOID
Definition: pg_type.h:688
#define InvalidOid
Definition: postgres_ext.h:36
#define INT8OID
Definition: pg_type.h:304
#define FLOAT8OID
Definition: pg_type.h:419
#define BOOLOID
Definition: pg_type.h:288
CoercionPathType find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, CoercionContext ccontext, Oid *funcid)
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2271
int json_count_array_elements ( JsonLexContext lex)

Definition at line 366 of file json.c.

References JSON_PARSE_ARRAY_NEXT, JSON_PARSE_ARRAY_START, JSON_TOKEN_ARRAY_END, JSON_TOKEN_ARRAY_START, JSON_TOKEN_COMMA, lex_accept(), lex_expect(), JsonLexContext::lex_level, lex_peek(), NULL, parse_array_element(), and JsonLexContext::strval.

Referenced by get_array_start().

367 {
368  JsonLexContext copylex;
369  int count;
370 
371  /*
372  * It's safe to do this with a shallow copy because the lexical routines
373  * don't scribble on the input. They do scribble on the other pointers
374  * etc, so doing this with a copy makes that safe.
375  */
376  memcpy(&copylex, lex, sizeof(JsonLexContext));
377  copylex.strval = NULL; /* not interested in values here */
378  copylex.lex_level++;
379 
380  count = 0;
382  if (lex_peek(&copylex) != JSON_TOKEN_ARRAY_END)
383  {
384  do
385  {
386  count++;
388  }
389  while (lex_accept(&copylex, JSON_TOKEN_COMMA, NULL));
390  }
392 
393  return count;
394 }
static void lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
Definition: json.c:172
static bool lex_accept(JsonLexContext *lex, JsonTokenType token, char **lexeme)
Definition: json.c:138
int lex_level
Definition: jsonapi.h:59
static void parse_array_element(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:538
StringInfo strval
Definition: jsonapi.h:62
static JsonTokenType lex_peek(JsonLexContext *lex)
Definition: json.c:123
#define NULL
Definition: c.h:229
static JsonSemAction nullSemAction
Definition: json.c:109
Datum json_in ( PG_FUNCTION_ARGS  )

Definition at line 227 of file json.c.

References cstring_to_text(), makeJsonLexContext(), PG_GETARG_CSTRING, pg_parse_json(), PG_RETURN_TEXT_P, and result.

228 {
229  char *json = PG_GETARG_CSTRING(0);
230  text *result = cstring_to_text(json);
231  JsonLexContext *lex;
232 
233  /* validate it */
234  lex = makeJsonLexContext(result, false);
236 
237  /* Internal representation is the same as text, for now */
238  PG_RETURN_TEXT_P(result);
239 }
return result
Definition: formatting.c:1633
JsonLexContext * makeJsonLexContext(text *json, bool need_escapes)
Definition: json.c:300
void pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:331
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
text * cstring_to_text(const char *s)
Definition: varlena.c:149
static JsonSemAction nullSemAction
Definition: json.c:109
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
Definition: c.h:439
static void json_lex ( JsonLexContext lex)
inlinestatic

Definition at line 613 of file json.c.

References JsonLexContext::input, JsonLexContext::input_length, JSON_ALPHANUMERIC_CHAR, json_lex_number(), json_lex_string(), JSON_TOKEN_ARRAY_END, JSON_TOKEN_ARRAY_START, JSON_TOKEN_COLON, JSON_TOKEN_COMMA, JSON_TOKEN_END, JSON_TOKEN_FALSE, JSON_TOKEN_NULL, JSON_TOKEN_NUMBER, JSON_TOKEN_OBJECT_END, JSON_TOKEN_OBJECT_START, JSON_TOKEN_STRING, JSON_TOKEN_TRUE, JsonLexContext::line_number, NULL, JsonLexContext::prev_token_terminator, report_invalid_token(), JsonLexContext::token_start, JsonLexContext::token_terminator, and JsonLexContext::token_type.

Referenced by json_typeof(), lex_accept(), and pg_parse_json().

614 {
615  char *s;
616  int len;
617 
618  /* Skip leading whitespace. */
619  s = lex->token_terminator;
620  len = s - lex->input;
621  while (len < lex->input_length &&
622  (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'))
623  {
624  if (*s == '\n')
625  ++lex->line_number;
626  ++s;
627  ++len;
628  }
629  lex->token_start = s;
630 
631  /* Determine token type. */
632  if (len >= lex->input_length)
633  {
634  lex->token_start = NULL;
636  lex->token_terminator = s;
637  lex->token_type = JSON_TOKEN_END;
638  }
639  else
640  switch (*s)
641  {
642  /* Single-character token, some kind of punctuation mark. */
643  case '{':
645  lex->token_terminator = s + 1;
647  break;
648  case '}':
650  lex->token_terminator = s + 1;
652  break;
653  case '[':
655  lex->token_terminator = s + 1;
657  break;
658  case ']':
660  lex->token_terminator = s + 1;
662  break;
663  case ',':
665  lex->token_terminator = s + 1;
667  break;
668  case ':':
670  lex->token_terminator = s + 1;
672  break;
673  case '"':
674  /* string */
675  json_lex_string(lex);
677  break;
678  case '-':
679  /* Negative number. */
680  json_lex_number(lex, s + 1, NULL, NULL);
682  break;
683  case '0':
684  case '1':
685  case '2':
686  case '3':
687  case '4':
688  case '5':
689  case '6':
690  case '7':
691  case '8':
692  case '9':
693  /* Positive number. */
694  json_lex_number(lex, s, NULL, NULL);
696  break;
697  default:
698  {
699  char *p;
700 
701  /*
702  * We're not dealing with a string, number, legal
703  * punctuation mark, or end of string. The only legal
704  * tokens we might find here are true, false, and null,
705  * but for error reporting purposes we scan until we see a
706  * non-alphanumeric character. That way, we can report
707  * the whole word as an unexpected token, rather than just
708  * some unintuitive prefix thereof.
709  */
710  for (p = s; p - s < lex->input_length - len && JSON_ALPHANUMERIC_CHAR(*p); p++)
711  /* skip */ ;
712 
713  /*
714  * We got some sort of unexpected punctuation or an
715  * otherwise unexpected character, so just complain about
716  * that one character.
717  */
718  if (p == s)
719  {
721  lex->token_terminator = s + 1;
723  }
724 
725  /*
726  * We've got a real alphanumeric token here. If it
727  * happens to be true, false, or null, all is well. If
728  * not, error out.
729  */
731  lex->token_terminator = p;
732  if (p - s == 4)
733  {
734  if (memcmp(s, "true", 4) == 0)
736  else if (memcmp(s, "null", 4) == 0)
738  else
740  }
741  else if (p - s == 5 && memcmp(s, "false", 5) == 0)
743  else
745 
746  }
747  } /* end of switch */
748 }
int line_number
Definition: jsonapi.h:60
JsonTokenType token_type
Definition: jsonapi.h:58
char * prev_token_terminator
Definition: jsonapi.h:57
#define JSON_ALPHANUMERIC_CHAR(c)
Definition: json.c:179
static void json_lex_number(JsonLexContext *lex, char *s, bool *num_err, int *total_len)
Definition: json.c:1027
int input_length
Definition: jsonapi.h:54
char * token_start
Definition: jsonapi.h:55
char * token_terminator
Definition: jsonapi.h:56
#define NULL
Definition: c.h:229
char * input
Definition: jsonapi.h:53
static void report_invalid_token(JsonLexContext *lex)
Definition: json.c:1232
static void json_lex_string(JsonLexContext *lex)
Definition: json.c:754
static void json_lex_number ( JsonLexContext lex,
char *  s,
bool num_err,
int *  total_len 
)
inlinestatic

Definition at line 1027 of file json.c.

References error(), JsonLexContext::input, JsonLexContext::input_length, JSON_ALPHANUMERIC_CHAR, NULL, JsonLexContext::prev_token_terminator, report_invalid_token(), and JsonLexContext::token_terminator.

Referenced by IsValidJsonNumber(), and json_lex().

1029 {
1030  bool error = false;
1031  int len = s - lex->input;
1032 
1033  /* Part (1): leading sign indicator. */
1034  /* Caller already did this for us; so do nothing. */
1035 
1036  /* Part (2): parse main digit string. */
1037  if (len < lex->input_length && *s == '0')
1038  {
1039  s++;
1040  len++;
1041  }
1042  else if (len < lex->input_length && *s >= '1' && *s <= '9')
1043  {
1044  do
1045  {
1046  s++;
1047  len++;
1048  } while (len < lex->input_length && *s >= '0' && *s <= '9');
1049  }
1050  else
1051  error = true;
1052 
1053  /* Part (3): parse optional decimal portion. */
1054  if (len < lex->input_length && *s == '.')
1055  {
1056  s++;
1057  len++;
1058  if (len == lex->input_length || *s < '0' || *s > '9')
1059  error = true;
1060  else
1061  {
1062  do
1063  {
1064  s++;
1065  len++;
1066  } while (len < lex->input_length && *s >= '0' && *s <= '9');
1067  }
1068  }
1069 
1070  /* Part (4): parse optional exponent. */
1071  if (len < lex->input_length && (*s == 'e' || *s == 'E'))
1072  {
1073  s++;
1074  len++;
1075  if (len < lex->input_length && (*s == '+' || *s == '-'))
1076  {
1077  s++;
1078  len++;
1079  }
1080  if (len == lex->input_length || *s < '0' || *s > '9')
1081  error = true;
1082  else
1083  {
1084  do
1085  {
1086  s++;
1087  len++;
1088  } while (len < lex->input_length && *s >= '0' && *s <= '9');
1089  }
1090  }
1091 
1092  /*
1093  * Check for trailing garbage. As in json_lex(), any alphanumeric stuff
1094  * here should be considered part of the token for error-reporting
1095  * purposes.
1096  */
1097  for (; len < lex->input_length && JSON_ALPHANUMERIC_CHAR(*s); s++, len++)
1098  error = true;
1099 
1100  if (total_len != NULL)
1101  *total_len = len;
1102 
1103  if (num_err != NULL)
1104  {
1105  /* let the caller handle any error */
1106  *num_err = error;
1107  }
1108  else
1109  {
1110  /* return token endpoint */
1112  lex->token_terminator = s;
1113  /* handle error if any */
1114  if (error)
1115  report_invalid_token(lex);
1116  }
1117 }
static void error(void)
Definition: sql-dyntest.c:147
char * prev_token_terminator
Definition: jsonapi.h:57
#define JSON_ALPHANUMERIC_CHAR(c)
Definition: json.c:179
int input_length
Definition: jsonapi.h:54
char * token_terminator
Definition: jsonapi.h:56
#define NULL
Definition: c.h:229
char * input
Definition: jsonapi.h:53
static void report_invalid_token(JsonLexContext *lex)
Definition: json.c:1232
static void json_lex_string ( JsonLexContext lex)
inlinestatic

Definition at line 754 of file json.c.

References appendBinaryStringInfo(), appendStringInfoChar(), Assert, ereport, errcode(), errdetail(), errmsg(), ERROR, extract_mb_char(), GetDatabaseEncoding(), i, JsonLexContext::input, JsonLexContext::input_length, NULL, pg_mblen(), PG_UTF8, pg_utf_mblen(), JsonLexContext::prev_token_terminator, report_invalid_token(), report_json_context(), resetStringInfo(), JsonLexContext::strval, JsonLexContext::token_start, JsonLexContext::token_terminator, and unicode_to_utf8().

Referenced by json_lex().

755 {
756  char *s;
757  int len;
758  int hi_surrogate = -1;
759 
760  if (lex->strval != NULL)
761  resetStringInfo(lex->strval);
762 
763  Assert(lex->input_length > 0);
764  s = lex->token_start;
765  len = lex->token_start - lex->input;
766  for (;;)
767  {
768  s++;
769  len++;
770  /* Premature end of the string. */
771  if (len >= lex->input_length)
772  {
773  lex->token_terminator = s;
775  }
776  else if (*s == '"')
777  break;
778  else if ((unsigned char) *s < 32)
779  {
780  /* Per RFC4627, these characters MUST be escaped. */
781  /* Since *s isn't printable, exclude it from the context string */
782  lex->token_terminator = s;
783  ereport(ERROR,
784  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
785  errmsg("invalid input syntax for type %s", "json"),
786  errdetail("Character with value 0x%02x must be escaped.",
787  (unsigned char) *s),
788  report_json_context(lex)));
789  }
790  else if (*s == '\\')
791  {
792  /* OK, we have an escape character. */
793  s++;
794  len++;
795  if (len >= lex->input_length)
796  {
797  lex->token_terminator = s;
799  }
800  else if (*s == 'u')
801  {
802  int i;
803  int ch = 0;
804 
805  for (i = 1; i <= 4; i++)
806  {
807  s++;
808  len++;
809  if (len >= lex->input_length)
810  {
811  lex->token_terminator = s;
813  }
814  else if (*s >= '0' && *s <= '9')
815  ch = (ch * 16) + (*s - '0');
816  else if (*s >= 'a' && *s <= 'f')
817  ch = (ch * 16) + (*s - 'a') + 10;
818  else if (*s >= 'A' && *s <= 'F')
819  ch = (ch * 16) + (*s - 'A') + 10;
820  else
821  {
822  lex->token_terminator = s + pg_mblen(s);
823  ereport(ERROR,
824  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
825  errmsg("invalid input syntax for type %s",
826  "json"),
827  errdetail("\"\\u\" must be followed by four hexadecimal digits."),
828  report_json_context(lex)));
829  }
830  }
831  if (lex->strval != NULL)
832  {
833  char utf8str[5];
834  int utf8len;
835 
836  if (ch >= 0xd800 && ch <= 0xdbff)
837  {
838  if (hi_surrogate != -1)
839  ereport(ERROR,
840  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
841  errmsg("invalid input syntax for type %s",
842  "json"),
843  errdetail("Unicode high surrogate must not follow a high surrogate."),
844  report_json_context(lex)));
845  hi_surrogate = (ch & 0x3ff) << 10;
846  continue;
847  }
848  else if (ch >= 0xdc00 && ch <= 0xdfff)
849  {
850  if (hi_surrogate == -1)
851  ereport(ERROR,
852  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
853  errmsg("invalid input syntax for type %s", "json"),
854  errdetail("Unicode low surrogate must follow a high surrogate."),
855  report_json_context(lex)));
856  ch = 0x10000 + hi_surrogate + (ch & 0x3ff);
857  hi_surrogate = -1;
858  }
859 
860  if (hi_surrogate != -1)
861  ereport(ERROR,
862  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
863  errmsg("invalid input syntax for type %s", "json"),
864  errdetail("Unicode low surrogate must follow a high surrogate."),
865  report_json_context(lex)));
866 
867  /*
868  * For UTF8, replace the escape sequence by the actual
869  * utf8 character in lex->strval. Do this also for other
870  * encodings if the escape designates an ASCII character,
871  * otherwise raise an error.
872  */
873 
874  if (ch == 0)
875  {
876  /* We can't allow this, since our TEXT type doesn't */
877  ereport(ERROR,
878  (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
879  errmsg("unsupported Unicode escape sequence"),
880  errdetail("\\u0000 cannot be converted to text."),
881  report_json_context(lex)));
882  }
883  else if (GetDatabaseEncoding() == PG_UTF8)
884  {
885  unicode_to_utf8(ch, (unsigned char *) utf8str);
886  utf8len = pg_utf_mblen((unsigned char *) utf8str);
887  appendBinaryStringInfo(lex->strval, utf8str, utf8len);
888  }
889  else if (ch <= 0x007f)
890  {
891  /*
892  * This is the only way to designate things like a
893  * form feed character in JSON, so it's useful in all
894  * encodings.
895  */
896  appendStringInfoChar(lex->strval, (char) ch);
897  }
898  else
899  {
900  ereport(ERROR,
901  (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
902  errmsg("unsupported Unicode escape sequence"),
903  errdetail("Unicode escape values cannot be used for code point values above 007F when the server encoding is not UTF8."),
904  report_json_context(lex)));
905  }
906 
907  }
908  }
909  else if (lex->strval != NULL)
910  {
911  if (hi_surrogate != -1)
912  ereport(ERROR,
913  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
914  errmsg("invalid input syntax for type %s",
915  "json"),
916  errdetail("Unicode low surrogate must follow a high surrogate."),
917  report_json_context(lex)));
918 
919  switch (*s)
920  {
921  case '"':
922  case '\\':
923  case '/':
924  appendStringInfoChar(lex->strval, *s);
925  break;
926  case 'b':
927  appendStringInfoChar(lex->strval, '\b');
928  break;
929  case 'f':
930  appendStringInfoChar(lex->strval, '\f');
931  break;
932  case 'n':
933  appendStringInfoChar(lex->strval, '\n');
934  break;
935  case 'r':
936  appendStringInfoChar(lex->strval, '\r');
937  break;
938  case 't':
939  appendStringInfoChar(lex->strval, '\t');
940  break;
941  default:
942  /* Not a valid string escape, so error out. */
943  lex->token_terminator = s + pg_mblen(s);
944  ereport(ERROR,
945  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
946  errmsg("invalid input syntax for type %s",
947  "json"),
948  errdetail("Escape sequence \"\\%s\" is invalid.",
949  extract_mb_char(s)),
950  report_json_context(lex)));
951  }
952  }
953  else if (strchr("\"\\/bfnrt", *s) == NULL)
954  {
955  /*
956  * Simpler processing if we're not bothered about de-escaping
957  *
958  * It's very tempting to remove the strchr() call here and
959  * replace it with a switch statement, but testing so far has
960  * shown it's not a performance win.
961  */
962  lex->token_terminator = s + pg_mblen(s);
963  ereport(ERROR,
964  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
965  errmsg("invalid input syntax for type %s", "json"),
966  errdetail("Escape sequence \"\\%s\" is invalid.",
967  extract_mb_char(s)),
968  report_json_context(lex)));
969  }
970 
971  }
972  else if (lex->strval != NULL)
973  {
974  if (hi_surrogate != -1)
975  ereport(ERROR,
976  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
977  errmsg("invalid input syntax for type %s", "json"),
978  errdetail("Unicode low surrogate must follow a high surrogate."),
979  report_json_context(lex)));
980 
981  appendStringInfoChar(lex->strval, *s);
982  }
983 
984  }
985 
986  if (hi_surrogate != -1)
987  ereport(ERROR,
988  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
989  errmsg("invalid input syntax for type %s", "json"),
990  errdetail("Unicode low surrogate must follow a high surrogate."),
991  report_json_context(lex)));
992 
993  /* Hooray, we found the end of the string! */
995  lex->token_terminator = s + 1;
996 }
unsigned char * unicode_to_utf8(pg_wchar c, unsigned char *utf8string)
Definition: wchar.c:475
char * prev_token_terminator
Definition: jsonapi.h:57
static char * extract_mb_char(char *s)
Definition: json.c:1326
int errcode(int sqlerrcode)
Definition: elog.c:575
int input_length
Definition: jsonapi.h:54
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:873
StringInfo strval
Definition: jsonapi.h:62
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:62
#define ereport(elevel, rest)
Definition: elog.h:122
char * token_start
Definition: jsonapi.h:55
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
char * token_terminator
Definition: jsonapi.h:56
int GetDatabaseEncoding(void)
Definition: mbutils.c:1015
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
char * input
Definition: jsonapi.h:53
static void report_invalid_token(JsonLexContext *lex)
Definition: json.c:1232
int pg_mblen(const char *mbstr)
Definition: mbutils.c:771
int pg_utf_mblen(const unsigned char *s)
Definition: wchar.c:541
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208
static int report_json_context(JsonLexContext *lex)
Definition: json.c:1261
Datum json_object ( PG_FUNCTION_ARGS  )

Definition at line 2259 of file json.c.

References appendStringInfoChar(), appendStringInfoString(), ARR_DIMS, ARR_NDIM, cstring_to_text_with_len(), CStringGetTextDatum, StringInfoData::data, deconstruct_array(), ereport, errcode(), errmsg(), ERROR, escape_json(), i, initStringInfo(), StringInfoData::len, pfree(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_DATUM, PG_RETURN_TEXT_P, result, TextDatumGetCString, and TEXTOID.

2260 {
2261  ArrayType *in_array = PG_GETARG_ARRAYTYPE_P(0);
2262  int ndims = ARR_NDIM(in_array);
2264  Datum *in_datums;
2265  bool *in_nulls;
2266  int in_count,
2267  count,
2268  i;
2269  text *rval;
2270  char *v;
2271 
2272  switch (ndims)
2273  {
2274  case 0:
2276  break;
2277 
2278  case 1:
2279  if ((ARR_DIMS(in_array)[0]) % 2)
2280  ereport(ERROR,
2281  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2282  errmsg("array must have even number of elements")));
2283  break;
2284 
2285  case 2:
2286  if ((ARR_DIMS(in_array)[1]) != 2)
2287  ereport(ERROR,
2288  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2289  errmsg("array must have two columns")));
2290  break;
2291 
2292  default:
2293  ereport(ERROR,
2294  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2295  errmsg("wrong number of array subscripts")));
2296  }
2297 
2298  deconstruct_array(in_array,
2299  TEXTOID, -1, false, 'i',
2300  &in_datums, &in_nulls, &in_count);
2301 
2302  count = in_count / 2;
2303 
2304  initStringInfo(&result);
2305 
2306  appendStringInfoChar(&result, '{');
2307 
2308  for (i = 0; i < count; ++i)
2309  {
2310  if (in_nulls[i * 2])
2311  ereport(ERROR,
2312  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2313  errmsg("null value not allowed for object key")));
2314 
2315  v = TextDatumGetCString(in_datums[i * 2]);
2316  if (i > 0)
2317  appendStringInfoString(&result, ", ");
2318  escape_json(&result, v);
2319  appendStringInfoString(&result, " : ");
2320  pfree(v);
2321  if (in_nulls[i * 2 + 1])
2322  appendStringInfoString(&result, "null");
2323  else
2324  {
2325  v = TextDatumGetCString(in_datums[i * 2 + 1]);
2326  escape_json(&result, v);
2327  pfree(v);
2328  }
2329  }
2330 
2331  appendStringInfoChar(&result, '}');
2332 
2333  pfree(in_datums);
2334  pfree(in_nulls);
2335 
2336  rval = cstring_to_text_with_len(result.data, result.len);
2337  pfree(result.data);
2338 
2339  PG_RETURN_TEXT_P(rval);
2340 
2341 }
void escape_json(StringInfo buf, const char *str)
Definition: json.c:2433
#define TEXTOID
Definition: pg_type.h:324
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1633
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:275
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define ereport(elevel, rest)
Definition: elog.h:122
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
#define ARR_NDIM(a)
Definition: array.h:271
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define CStringGetTextDatum(s)
Definition: builtins.h:91
Definition: c.h:439
Datum json_object_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 2069 of file json.c.

References AggCheckCallContext(), Assert, catenate_stringinfo_string(), NULL, PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_NULL, PG_RETURN_TEXT_P, and JsonAggState::str.

2070 {
2072 
2073  /* cannot be called directly because of internal-type argument */
2074  Assert(AggCheckCallContext(fcinfo, NULL));
2075 
2076  state = PG_ARGISNULL(0) ? NULL : (JsonAggState *) PG_GETARG_POINTER(0);
2077 
2078  /* NULL result for no rows in, as is standard with aggregates */
2079  if (state == NULL)
2080  PG_RETURN_NULL();
2081 
2082  /* Else return state with appropriate object terminator added */
2084 }
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
static text * catenate_stringinfo_string(StringInfo buffer, const char *addon)
Definition: json.c:2092
StringInfo str
Definition: json.c:70
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
Definition: regguts.h:298
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4027
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum json_object_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 1978 of file json.c.

References AggCheckCallContext(), appendStringInfoString(), arg, datum_to_json(), elog, ereport, errcode(), errmsg(), ERROR, get_fn_expr_argtype(), InvalidOid, json_categorize_type(), JsonAggState::key_category, JsonAggState::key_output_func, makeStringInfo(), MemoryContextSwitchTo(), palloc(), PG_ARGISNULL, PG_GETARG_DATUM, PG_GETARG_POINTER, PG_RETURN_POINTER, JsonAggState::str, JsonAggState::val_category, and JsonAggState::val_output_func.

1979 {
1980  MemoryContext aggcontext,
1981  oldcontext;
1983  Datum arg;
1984 
1985  if (!AggCheckCallContext(fcinfo, &aggcontext))
1986  {
1987  /* cannot be called directly because of internal-type argument */
1988  elog(ERROR, "json_object_agg_transfn called in non-aggregate context");
1989  }
1990 
1991  if (PG_ARGISNULL(0))
1992  {
1993  Oid arg_type;
1994 
1995  /*
1996  * Make the StringInfo in a context where it will persist for the
1997  * duration of the aggregate call. Switching context is only needed
1998  * for this initial step, as the StringInfo routines make sure they
1999  * use the right context to enlarge the object if necessary.
2000  */
2001  oldcontext = MemoryContextSwitchTo(aggcontext);
2002  state = (JsonAggState *) palloc(sizeof(JsonAggState));
2003  state->str = makeStringInfo();
2004  MemoryContextSwitchTo(oldcontext);
2005 
2006  arg_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
2007 
2008  if (arg_type == InvalidOid)
2009  ereport(ERROR,
2010  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2011  errmsg("could not determine data type for argument %d", 1)));
2012 
2013  json_categorize_type(arg_type, &state->key_category,
2014  &state->key_output_func);
2015 
2016  arg_type = get_fn_expr_argtype(fcinfo->flinfo, 2);
2017 
2018  if (arg_type == InvalidOid)
2019  ereport(ERROR,
2020  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2021  errmsg("could not determine data type for argument %d", 2)));
2022 
2023  json_categorize_type(arg_type, &state->val_category,
2024  &state->val_output_func);
2025 
2026  appendStringInfoString(state->str, "{ ");
2027  }
2028  else
2029  {
2030  state = (JsonAggState *) PG_GETARG_POINTER(0);
2031  appendStringInfoString(state->str, ", ");
2032  }
2033 
2034  /*
2035  * Note: since json_object_agg() is declared as taking type "any", the
2036  * parser will not do any type conversion on unknown-type literals (that
2037  * is, undecorated strings or NULLs). Such values will arrive here as
2038  * type UNKNOWN, which fortunately does not matter to us, since
2039  * unknownout() works fine.
2040  */
2041 
2042  if (PG_ARGISNULL(1))
2043  ereport(ERROR,
2044  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2045  errmsg("field name must not be null")));
2046 
2047  arg = PG_GETARG_DATUM(1);
2048 
2049  datum_to_json(arg, false, state->str, state->key_category,
2050  state->key_output_func, true);
2051 
2052  appendStringInfoString(state->str, " : ");
2053 
2054  if (PG_ARGISNULL(2))
2055  arg = (Datum) 0;
2056  else
2057  arg = PG_GETARG_DATUM(2);
2058 
2059  datum_to_json(arg, PG_ARGISNULL(2), state->str, state->val_category,
2060  state->val_output_func, false);
2061 
2062  PG_RETURN_POINTER(state);
2063 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
static void datum_to_json(Datum val, bool is_null, StringInfo result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
Definition: json.c:1449
static void json_categorize_type(Oid typoid, JsonTypeCategory *tcategory, Oid *outfuncoid)
Definition: json.c:1347
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
Oid val_output_func
Definition: json.c:74
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
Oid key_output_func
Definition: json.c:72
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1909
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
StringInfo str
Definition: json.c:70
#define ereport(elevel, rest)
Definition: elog.h:122
JsonTypeCategory key_category
Definition: json.c:71
uintptr_t Datum
Definition: postgres.h:372
#define InvalidOid
Definition: postgres_ext.h:36
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
JsonTypeCategory val_category
Definition: json.c:73
Definition: regguts.h:298
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4027
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * arg
#define elog
Definition: elog.h:219
Datum json_object_two_arg ( PG_FUNCTION_ARGS  )

Definition at line 2350 of file json.c.

References appendStringInfoChar(), appendStringInfoString(), ARR_NDIM, cstring_to_text_with_len(), CStringGetTextDatum, StringInfoData::data, deconstruct_array(), ereport, errcode(), errmsg(), ERROR, escape_json(), i, initStringInfo(), StringInfoData::len, pfree(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_DATUM, PG_RETURN_TEXT_P, result, TextDatumGetCString, and TEXTOID.

2351 {
2352  ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(0);
2353  ArrayType *val_array = PG_GETARG_ARRAYTYPE_P(1);
2354  int nkdims = ARR_NDIM(key_array);
2355  int nvdims = ARR_NDIM(val_array);
2357  Datum *key_datums,
2358  *val_datums;
2359  bool *key_nulls,
2360  *val_nulls;
2361  int key_count,
2362  val_count,
2363  i;
2364  text *rval;
2365  char *v;
2366 
2367  if (nkdims > 1 || nkdims != nvdims)
2368  ereport(ERROR,
2369  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2370  errmsg("wrong number of array subscripts")));
2371 
2372  if (nkdims == 0)
2374 
2375  deconstruct_array(key_array,
2376  TEXTOID, -1, false, 'i',
2377  &key_datums, &key_nulls, &key_count);
2378 
2379  deconstruct_array(val_array,
2380  TEXTOID, -1, false, 'i',
2381  &val_datums, &val_nulls, &val_count);
2382 
2383  if (key_count != val_count)
2384  ereport(ERROR,
2385  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2386  errmsg("mismatched array dimensions")));
2387 
2388  initStringInfo(&result);
2389 
2390  appendStringInfoChar(&result, '{');
2391 
2392  for (i = 0; i < key_count; ++i)
2393  {
2394  if (key_nulls[i])
2395  ereport(ERROR,
2396  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2397  errmsg("null value not allowed for object key")));
2398 
2399  v = TextDatumGetCString(key_datums[i]);
2400  if (i > 0)
2401  appendStringInfoString(&result, ", ");
2402  escape_json(&result, v);
2403  appendStringInfoString(&result, " : ");
2404  pfree(v);
2405  if (val_nulls[i])
2406  appendStringInfoString(&result, "null");
2407  else
2408  {
2409  v = TextDatumGetCString(val_datums[i]);
2410  escape_json(&result, v);
2411  pfree(v);
2412  }
2413  }
2414 
2415  appendStringInfoChar(&result, '}');
2416 
2417  pfree(key_datums);
2418  pfree(key_nulls);
2419  pfree(val_datums);
2420  pfree(val_nulls);
2421 
2422  rval = cstring_to_text_with_len(result.data, result.len);
2423  pfree(result.data);
2424 
2425  PG_RETURN_TEXT_P(rval);
2426 }
void escape_json(StringInfo buf, const char *str)
Definition: json.c:2433
#define TEXTOID
Definition: pg_type.h:324
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1633
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define ereport(elevel, rest)
Definition: elog.h:122
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
#define ARR_NDIM(a)
Definition: array.h:271
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define CStringGetTextDatum(s)
Definition: builtins.h:91
Definition: c.h:439
Datum json_out ( PG_FUNCTION_ARGS  )

Definition at line 245 of file json.c.

References PG_GETARG_DATUM, PG_RETURN_CSTRING, and TextDatumGetCString.

246 {
247  /* we needn't detoast because text_to_cstring will handle that */
248  Datum txt = PG_GETARG_DATUM(0);
249 
251 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
Datum json_recv ( PG_FUNCTION_ARGS  )

Definition at line 271 of file json.c.

References buf, cstring_to_text_with_len(), StringInfoData::cursor, StringInfoData::len, makeJsonLexContextCstringLen(), PG_GETARG_POINTER, pg_parse_json(), PG_RETURN_TEXT_P, and pq_getmsgtext().

272 {
274  char *str;
275  int nbytes;
276  JsonLexContext *lex;
277 
278  str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
279 
280  /* Validate it. */
281  lex = makeJsonLexContextCstringLen(str, nbytes, false);
283 
285 }
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
void pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:331
static char * buf
Definition: pg_test_fsync.c:66
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
char * pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes)
Definition: pqformat.c:588
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
JsonLexContext * makeJsonLexContextCstringLen(char *json, int len, bool need_escapes)
Definition: json.c:308
static JsonSemAction nullSemAction
Definition: json.c:109
Datum json_send ( PG_FUNCTION_ARGS  )

Definition at line 257 of file json.c.

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

258 {
259  text *t = PG_GETARG_TEXT_PP(0);
261 
262  pq_begintypsend(&buf);
265 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:359
void pq_sendtext(StringInfo buf, const char *str, int slen)
Definition: pqformat.c:163
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:329
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:379
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
static char * buf
Definition: pg_test_fsync.c:66
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:439
Datum json_typeof ( PG_FUNCTION_ARGS  )

Definition at line 2487 of file json.c.

References cstring_to_text(), elog, ERROR, json_lex(), JSON_TOKEN_ARRAY_START, JSON_TOKEN_FALSE, JSON_TOKEN_NULL, JSON_TOKEN_NUMBER, JSON_TOKEN_OBJECT_START, JSON_TOKEN_STRING, JSON_TOKEN_TRUE, lex_peek(), makeJsonLexContext(), PG_GETARG_TEXT_PP, and PG_RETURN_TEXT_P.

2488 {
2489  text *json;
2490 
2491  JsonLexContext *lex;
2492  JsonTokenType tok;
2493  char *type;
2494 
2495  json = PG_GETARG_TEXT_PP(0);
2496  lex = makeJsonLexContext(json, false);
2497 
2498  /* Lex exactly one token from the input and check its type. */
2499  json_lex(lex);
2500  tok = lex_peek(lex);
2501  switch (tok)
2502  {
2504  type = "object";
2505  break;
2507  type = "array";
2508  break;
2509  case JSON_TOKEN_STRING:
2510  type = "string";
2511  break;
2512  case JSON_TOKEN_NUMBER:
2513  type = "number";
2514  break;
2515  case JSON_TOKEN_TRUE:
2516  case JSON_TOKEN_FALSE:
2517  type = "boolean";
2518  break;
2519  case JSON_TOKEN_NULL:
2520  type = "null";
2521  break;
2522  default:
2523  elog(ERROR, "unexpected json token: %d", tok);
2524  }
2525 
2527 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
JsonLexContext * makeJsonLexContext(text *json, bool need_escapes)
Definition: json.c:300
#define ERROR
Definition: elog.h:43
static JsonTokenType lex_peek(JsonLexContext *lex)
Definition: json.c:123
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
text * cstring_to_text(const char *s)
Definition: varlena.c:149
static void json_lex(JsonLexContext *lex)
Definition: json.c:613
Definition: c.h:439
#define elog
Definition: elog.h:219
JsonTokenType
Definition: jsonapi.h:20
static bool lex_accept ( JsonLexContext lex,
JsonTokenType  token,
char **  lexeme 
)
inlinestatic

Definition at line 138 of file json.c.

References StringInfoData::data, json_lex(), JSON_TOKEN_STRING, NULL, palloc(), pstrdup(), JsonLexContext::strval, JsonLexContext::token_start, JsonLexContext::token_terminator, and JsonLexContext::token_type.

Referenced by json_count_array_elements(), lex_expect(), parse_array(), parse_object(), parse_object_field(), and parse_scalar().

139 {
140  if (lex->token_type == token)
141  {
142  if (lexeme != NULL)
143  {
144  if (lex->token_type == JSON_TOKEN_STRING)
145  {
146  if (lex->strval != NULL)
147  *lexeme = pstrdup(lex->strval->data);
148  }
149  else
150  {
151  int len = (lex->token_terminator - lex->token_start);
152  char *tokstr = palloc(len + 1);
153 
154  memcpy(tokstr, lex->token_start, len);
155  tokstr[len] = '\0';
156  *lexeme = tokstr;
157  }
158  }
159  json_lex(lex);
160  return true;
161  }
162  return false;
163 }
char * pstrdup(const char *in)
Definition: mcxt.c:1077
JsonTokenType token_type
Definition: jsonapi.h:58
StringInfo strval
Definition: jsonapi.h:62
char * token_start
Definition: jsonapi.h:55
char * token_terminator
Definition: jsonapi.h:56
#define NULL
Definition: c.h:229
static void json_lex(JsonLexContext *lex)
Definition: json.c:613
void * palloc(Size size)
Definition: mcxt.c:849
static void lex_expect ( JsonParseContext  ctx,
JsonLexContext lex,
JsonTokenType  token 
)
inlinestatic

Definition at line 172 of file json.c.

References lex_accept(), NULL, and report_parse_error().

Referenced by json_count_array_elements(), parse_array(), parse_object(), parse_object_field(), and pg_parse_json().

173 {
174  if (!lex_accept(lex, token, NULL))
175  report_parse_error(ctx, lex);
176 }
static bool lex_accept(JsonLexContext *lex, JsonTokenType token, char **lexeme)
Definition: json.c:138
static void report_parse_error(JsonParseContext ctx, JsonLexContext *lex)
Definition: json.c:1125
#define NULL
Definition: c.h:229
static JsonTokenType lex_peek ( JsonLexContext lex)
inlinestatic

Definition at line 123 of file json.c.

References JsonLexContext::token_type.

Referenced by json_count_array_elements(), json_typeof(), parse_array(), parse_array_element(), parse_object(), parse_object_field(), parse_scalar(), and pg_parse_json().

124 {
125  return lex->token_type;
126 }
JsonTokenType token_type
Definition: jsonapi.h:58
JsonLexContext* makeJsonLexContext ( text json,
bool  need_escapes 
)

Definition at line 300 of file json.c.

References makeJsonLexContextCstringLen(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by datum_to_jsonb(), each_worker(), elements_worker(), get_worker(), iterate_json_string_values(), json_array_length(), json_in(), json_object_keys(), json_strip_nulls(), json_typeof(), populate_recordset_worker(), and transform_json_string_values().

301 {
303  VARSIZE_ANY_EXHDR(json),
304  need_escapes);
305 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
JsonLexContext * makeJsonLexContextCstringLen(char *json, int len, bool need_escapes)
Definition: json.c:308
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
JsonLexContext* makeJsonLexContextCstringLen ( char *  json,
int  len,
bool  need_escapes 
)

Definition at line 308 of file json.c.

References JsonLexContext::input, JsonLexContext::input_length, JsonLexContext::line_number, JsonLexContext::line_start, makeStringInfo(), palloc0(), JsonLexContext::strval, and JsonLexContext::token_terminator.

Referenced by get_json_object_as_hash(), json_recv(), jsonb_from_cstring(), makeJsonLexContext(), and populate_array_json().

309 {
310  JsonLexContext *lex = palloc0(sizeof(JsonLexContext));
311 
312  lex->input = lex->token_terminator = lex->line_start = json;
313  lex->line_number = 1;
314  lex->input_length = len;
315  if (need_escapes)
316  lex->strval = makeStringInfo();
317  return lex;
318 }
int line_number
Definition: jsonapi.h:60
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
char * line_start
Definition: jsonapi.h:61
int input_length
Definition: jsonapi.h:54
StringInfo strval
Definition: jsonapi.h:62
void * palloc0(Size size)
Definition: mcxt.c:878
char * token_terminator
Definition: jsonapi.h:56
char * input
Definition: jsonapi.h:53
static void parse_array ( JsonLexContext lex,
JsonSemAction sem 
)
static

Definition at line 569 of file json.c.

References JsonSemAction::array_end, JsonSemAction::array_start, check_stack_depth(), JSON_PARSE_ARRAY_NEXT, JSON_PARSE_ARRAY_START, JSON_TOKEN_ARRAY_END, JSON_TOKEN_ARRAY_START, JSON_TOKEN_COMMA, lex_accept(), lex_expect(), JsonLexContext::lex_level, lex_peek(), NULL, parse_array_element(), and JsonSemAction::semstate.

Referenced by parse_array_element(), parse_object_field(), and pg_parse_json().

570 {
571  /*
572  * an array is a possibly empty sequence of array elements, separated by
573  * commas and surrounded by square brackets.
574  */
575  json_struct_action astart = sem->array_start;
576  json_struct_action aend = sem->array_end;
577 
579 
580  if (astart != NULL)
581  (*astart) (sem->semstate);
582 
583  /*
584  * Data inside an array is at a higher nesting level than the array
585  * itself. Note that we increment this after we call the semantic routine
586  * for the array start and restore it before we call the routine for the
587  * array end.
588  */
589  lex->lex_level++;
590 
592  if (lex_peek(lex) != JSON_TOKEN_ARRAY_END)
593  {
594 
595  parse_array_element(lex, sem);
596 
597  while (lex_accept(lex, JSON_TOKEN_COMMA, NULL))
598  parse_array_element(lex, sem);
599  }
600 
602 
603  lex->lex_level--;
604 
605  if (aend != NULL)
606  (*aend) (sem->semstate);
607 }
json_struct_action array_end
Definition: jsonapi.h:88
static void lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
Definition: json.c:172
static bool lex_accept(JsonLexContext *lex, JsonTokenType token, char **lexeme)
Definition: json.c:138
int lex_level
Definition: jsonapi.h:59
void(* json_struct_action)(void *state)
Definition: jsonapi.h:65
void check_stack_depth(void)
Definition: postgres.c:3117
static void parse_array_element(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:538
static JsonTokenType lex_peek(JsonLexContext *lex)
Definition: json.c:123
#define NULL
Definition: c.h:229
json_struct_action array_start
Definition: jsonapi.h:87
void * semstate
Definition: jsonapi.h:84
static void parse_array_element ( JsonLexContext lex,
JsonSemAction sem 
)
static

Definition at line 538 of file json.c.

References JsonSemAction::array_element_end, JsonSemAction::array_element_start, JSON_TOKEN_ARRAY_START, JSON_TOKEN_NULL, JSON_TOKEN_OBJECT_START, lex_peek(), NULL, parse_array(), parse_object(), parse_scalar(), and JsonSemAction::semstate.

Referenced by json_count_array_elements(), and parse_array().

539 {
542  JsonTokenType tok = lex_peek(lex);
543 
544  bool isnull;
545 
546  isnull = tok == JSON_TOKEN_NULL;
547 
548  if (astart != NULL)
549  (*astart) (sem->semstate, isnull);
550 
551  /* an array element is any object, array or scalar */
552  switch (tok)
553  {
555  parse_object(lex, sem);
556  break;
558  parse_array(lex, sem);
559  break;
560  default:
561  parse_scalar(lex, sem);
562  }
563 
564  if (aend != NULL)
565  (*aend) (sem->semstate, isnull);
566 }
static void parse_array(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:569
static JsonTokenType lex_peek(JsonLexContext *lex)
Definition: json.c:123
json_aelem_action array_element_start
Definition: jsonapi.h:91
#define NULL
Definition: c.h:229
static void parse_scalar(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:406
void(* json_aelem_action)(void *state, bool isnull)
Definition: jsonapi.h:67
void * semstate
Definition: jsonapi.h:84
static void parse_object(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:488
json_aelem_action array_element_end
Definition: jsonapi.h:92
JsonTokenType
Definition: jsonapi.h:20
static void parse_object ( JsonLexContext lex,
JsonSemAction sem 
)
static

Definition at line 488 of file json.c.

References check_stack_depth(), JSON_PARSE_OBJECT_NEXT, JSON_PARSE_OBJECT_START, JSON_TOKEN_COMMA, JSON_TOKEN_OBJECT_END, JSON_TOKEN_OBJECT_START, JSON_TOKEN_STRING, lex_accept(), lex_expect(), JsonLexContext::lex_level, lex_peek(), NULL, JsonSemAction::object_end, JsonSemAction::object_start, parse_object_field(), report_parse_error(), and JsonSemAction::semstate.

Referenced by parse_array_element(), parse_object_field(), and pg_parse_json().

489 {
490  /*
491  * an object is a possibly empty sequence of object fields, separated by
492  * commas and surrounded by curly braces.
493  */
494  json_struct_action ostart = sem->object_start;
495  json_struct_action oend = sem->object_end;
496  JsonTokenType tok;
497 
499 
500  if (ostart != NULL)
501  (*ostart) (sem->semstate);
502 
503  /*
504  * Data inside an object is at a higher nesting level than the object
505  * itself. Note that we increment this after we call the semantic routine
506  * for the object start and restore it before we call the routine for the
507  * object end.
508  */
509  lex->lex_level++;
510 
511  /* we know this will succeed, just clearing the token */
513 
514  tok = lex_peek(lex);
515  switch (tok)
516  {
517  case JSON_TOKEN_STRING:
518  parse_object_field(lex, sem);
519  while (lex_accept(lex, JSON_TOKEN_COMMA, NULL))
520  parse_object_field(lex, sem);
521  break;
523  break;
524  default:
525  /* case of an invalid initial token inside the object */
527  }
528 
530 
531  lex->lex_level--;
532 
533  if (oend != NULL)
534  (*oend) (sem->semstate);
535 }
json_struct_action object_end
Definition: jsonapi.h:86
json_struct_action object_start
Definition: jsonapi.h:85
static void lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
Definition: json.c:172
static bool lex_accept(JsonLexContext *lex, JsonTokenType token, char **lexeme)
Definition: json.c:138
int lex_level
Definition: jsonapi.h:59
void(* json_struct_action)(void *state)
Definition: jsonapi.h:65
void check_stack_depth(void)
Definition: postgres.c:3117
static void report_parse_error(JsonParseContext ctx, JsonLexContext *lex)
Definition: json.c:1125
static JsonTokenType lex_peek(JsonLexContext *lex)
Definition: json.c:123
#define NULL
Definition: c.h:229
static void parse_object_field(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:442
void * semstate
Definition: jsonapi.h:84
JsonTokenType
Definition: jsonapi.h:20
static void parse_object_field ( JsonLexContext lex,
JsonSemAction sem 
)
static

Definition at line 442 of file json.c.

References JSON_PARSE_OBJECT_LABEL, JSON_PARSE_STRING, JSON_TOKEN_ARRAY_START, JSON_TOKEN_COLON, JSON_TOKEN_NULL, JSON_TOKEN_OBJECT_START, JSON_TOKEN_STRING, lex_accept(), lex_expect(), lex_peek(), NULL, JsonSemAction::object_field_end, JsonSemAction::object_field_start, parse_array(), parse_object(), parse_scalar(), report_parse_error(), and JsonSemAction::semstate.

Referenced by parse_object().

443 {
444  /*
445  * An object field is "fieldname" : value where value can be a scalar,
446  * object or array. Note: in user-facing docs and error messages, we
447  * generally call a field name a "key".
448  */
449 
450  char *fname = NULL; /* keep compiler quiet */
453  bool isnull;
454  char **fnameaddr = NULL;
455  JsonTokenType tok;
456 
457  if (ostart != NULL || oend != NULL)
458  fnameaddr = &fname;
459 
460  if (!lex_accept(lex, JSON_TOKEN_STRING, fnameaddr))
462 
464 
465  tok = lex_peek(lex);
466  isnull = tok == JSON_TOKEN_NULL;
467 
468  if (ostart != NULL)
469  (*ostart) (sem->semstate, fname, isnull);
470 
471  switch (tok)
472  {
474  parse_object(lex, sem);
475  break;
477  parse_array(lex, sem);
478  break;
479  default:
480  parse_scalar(lex, sem);
481  }
482 
483  if (oend != NULL)
484  (*oend) (sem->semstate, fname, isnull);
485 }
static void lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
Definition: json.c:172
static bool lex_accept(JsonLexContext *lex, JsonTokenType token, char **lexeme)
Definition: json.c:138
static void parse_array(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:569
void(* json_ofield_action)(void *state, char *fname, bool isnull)
Definition: jsonapi.h:66
static void report_parse_error(JsonParseContext ctx, JsonLexContext *lex)
Definition: json.c:1125
json_ofield_action object_field_end
Definition: jsonapi.h:90
static JsonTokenType lex_peek(JsonLexContext *lex)
Definition: json.c:123
#define NULL
Definition: c.h:229
static void parse_scalar(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:406
json_ofield_action object_field_start
Definition: jsonapi.h:89
void * semstate
Definition: jsonapi.h:84
static void parse_object(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:488
JsonTokenType
Definition: jsonapi.h:20
static void parse_scalar ( JsonLexContext lex,
JsonSemAction sem 
)
inlinestatic

Definition at line 406 of file json.c.

References JSON_PARSE_VALUE, JSON_TOKEN_FALSE, JSON_TOKEN_NULL, JSON_TOKEN_NUMBER, JSON_TOKEN_STRING, JSON_TOKEN_TRUE, lex_accept(), lex_peek(), NULL, report_parse_error(), JsonSemAction::scalar, JsonSemAction::semstate, and val.

Referenced by parse_array_element(), parse_object_field(), and pg_parse_json().

407 {
408  char *val = NULL;
409  json_scalar_action sfunc = sem->scalar;
410  char **valaddr;
411  JsonTokenType tok = lex_peek(lex);
412 
413  valaddr = sfunc == NULL ? NULL : &val;
414 
415  /* a scalar must be a string, a number, true, false, or null */
416  switch (tok)
417  {
418  case JSON_TOKEN_TRUE:
419  lex_accept(lex, JSON_TOKEN_TRUE, valaddr);
420  break;
421  case JSON_TOKEN_FALSE:
422  lex_accept(lex, JSON_TOKEN_FALSE, valaddr);
423  break;
424  case JSON_TOKEN_NULL:
425  lex_accept(lex, JSON_TOKEN_NULL, valaddr);
426  break;
427  case JSON_TOKEN_NUMBER:
428  lex_accept(lex, JSON_TOKEN_NUMBER, valaddr);
429  break;
430  case JSON_TOKEN_STRING:
431  lex_accept(lex, JSON_TOKEN_STRING, valaddr);
432  break;
433  default:
435  }
436 
437  if (sfunc != NULL)
438  (*sfunc) (sem->semstate, val, tok);
439 }
void(* json_scalar_action)(void *state, char *token, JsonTokenType tokentype)
Definition: jsonapi.h:68
json_scalar_action scalar
Definition: jsonapi.h:93
static bool lex_accept(JsonLexContext *lex, JsonTokenType token, char **lexeme)
Definition: json.c:138
static void report_parse_error(JsonParseContext ctx, JsonLexContext *lex)
Definition: json.c:1125
static JsonTokenType lex_peek(JsonLexContext *lex)
Definition: json.c:123
#define NULL
Definition: c.h:229
void * semstate
Definition: jsonapi.h:84
long val
Definition: informix.c:689
JsonTokenType
Definition: jsonapi.h:20
void pg_parse_json ( JsonLexContext lex,
JsonSemAction sem 
)

Definition at line 331 of file json.c.

References json_lex(), JSON_PARSE_END, JSON_TOKEN_ARRAY_START, JSON_TOKEN_END, JSON_TOKEN_OBJECT_START, lex_expect(), lex_peek(), parse_array(), parse_object(), and parse_scalar().

Referenced by datum_to_jsonb(), each_worker(), elements_worker(), get_json_object_as_hash(), get_worker(), iterate_json_string_values(), json_array_length(), json_in(), json_object_keys(), json_recv(), json_strip_nulls(), jsonb_from_cstring(), populate_array_json(), populate_recordset_worker(), and transform_json_string_values().

332 {
333  JsonTokenType tok;
334 
335  /* get the initial token */
336  json_lex(lex);
337 
338  tok = lex_peek(lex);
339 
340  /* parse by recursive descent */
341  switch (tok)
342  {
344  parse_object(lex, sem);
345  break;
347  parse_array(lex, sem);
348  break;
349  default:
350  parse_scalar(lex, sem); /* json can be a bare scalar */
351  }
352 
354 
355 }
static void lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
Definition: json.c:172
static void parse_array(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:569
static JsonTokenType lex_peek(JsonLexContext *lex)
Definition: json.c:123
static void json_lex(JsonLexContext *lex)
Definition: json.c:613
static void parse_scalar(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:406
static void parse_object(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:488
JsonTokenType
Definition: jsonapi.h:20
static void report_invalid_token ( JsonLexContext lex)
static

Definition at line 1232 of file json.c.

References ereport, errcode(), errdetail(), errmsg(), ERROR, palloc(), report_json_context(), JsonLexContext::token_start, and JsonLexContext::token_terminator.

Referenced by json_lex(), json_lex_number(), and json_lex_string().

1233 {
1234  char *token;
1235  int toklen;
1236 
1237  /* Separate out the offending token. */
1238  toklen = lex->token_terminator - lex->token_start;
1239  token = palloc(toklen + 1);
1240  memcpy(token, lex->token_start, toklen);
1241  token[toklen] = '\0';
1242 
1243  ereport(ERROR,
1244  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1245  errmsg("invalid input syntax for type %s", "json"),
1246  errdetail("Token \"%s\" is invalid.", token),
1247  report_json_context(lex)));
1248 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
char * token_start
Definition: jsonapi.h:55
char * token_terminator
Definition: jsonapi.h:56
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
static int report_json_context(JsonLexContext *lex)
Definition: json.c:1261
static int report_json_context ( JsonLexContext lex)
static

Definition at line 1261 of file json.c.

References errcontext, JsonLexContext::input, JsonLexContext::input_length, IS_HIGHBIT_SET, JSON_TOKEN_END, palloc(), pg_mblen(), JsonLexContext::token_terminator, and JsonLexContext::token_type.

Referenced by json_lex_string(), report_invalid_token(), and report_parse_error().

1262 {
1263  const char *context_start;
1264  const char *context_end;
1265  const char *line_start;
1266  int line_number;
1267  char *ctxt;
1268  int ctxtlen;
1269  const char *prefix;
1270  const char *suffix;
1271 
1272  /* Choose boundaries for the part of the input we will display */
1273  context_start = lex->input;
1274  context_end = lex->token_terminator;
1275  line_start = context_start;
1276  line_number = 1;
1277  for (;;)
1278  {
1279  /* Always advance over newlines */
1280  if (context_start < context_end && *context_start == '\n')
1281  {
1282  context_start++;
1283  line_start = context_start;
1284  line_number++;
1285  continue;
1286  }
1287  /* Otherwise, done as soon as we are close enough to context_end */
1288  if (context_end - context_start < 50)
1289  break;
1290  /* Advance to next multibyte character */
1291  if (IS_HIGHBIT_SET(*context_start))
1292  context_start += pg_mblen(context_start);
1293  else
1294  context_start++;
1295  }
1296 
1297  /*
1298  * We add "..." to indicate that the excerpt doesn't start at the
1299  * beginning of the line ... but if we're within 3 characters of the
1300  * beginning of the line, we might as well just show the whole line.
1301  */
1302  if (context_start - line_start <= 3)
1303  context_start = line_start;
1304 
1305  /* Get a null-terminated copy of the data to present */
1306  ctxtlen = context_end - context_start;
1307  ctxt = palloc(ctxtlen + 1);
1308  memcpy(ctxt, context_start, ctxtlen);
1309  ctxt[ctxtlen] = '\0';
1310 
1311  /*
1312  * Show the context, prefixing "..." if not starting at start of line, and
1313  * suffixing "..." if not ending at end of line.
1314  */
1315  prefix = (context_start > line_start) ? "..." : "";
1316  suffix = (lex->token_type != JSON_TOKEN_END && context_end - lex->input < lex->input_length && *context_end != '\n' && *context_end != '\r') ? "..." : "";
1317 
1318  return errcontext("JSON data, line %d: %s%s%s",
1319  line_number, prefix, ctxt, suffix);
1320 }
JsonTokenType token_type
Definition: jsonapi.h:58
int input_length
Definition: jsonapi.h:54
#define IS_HIGHBIT_SET(ch)
Definition: c.h:974
char * token_terminator
Definition: jsonapi.h:56
char * input
Definition: jsonapi.h:53
int pg_mblen(const char *mbstr)
Definition: mbutils.c:771
void * palloc(Size size)
Definition: mcxt.c:849
#define errcontext
Definition: elog.h:164
static void report_parse_error ( JsonParseContext  ctx,
JsonLexContext lex 
)
static

Definition at line 1125 of file json.c.

References elog, ereport, errcode(), errdetail(), errmsg(), ERROR, JSON_PARSE_ARRAY_NEXT, JSON_PARSE_ARRAY_START, JSON_PARSE_END, JSON_PARSE_OBJECT_COMMA, JSON_PARSE_OBJECT_LABEL, JSON_PARSE_OBJECT_NEXT, JSON_PARSE_OBJECT_START, JSON_PARSE_STRING, JSON_PARSE_VALUE, JSON_TOKEN_END, NULL, palloc(), report_json_context(), JsonLexContext::token_start, JsonLexContext::token_terminator, and JsonLexContext::token_type.

Referenced by lex_expect(), parse_object(), parse_object_field(), and parse_scalar().

1126 {
1127  char *token;
1128  int toklen;
1129 
1130  /* Handle case where the input ended prematurely. */
1131  if (lex->token_start == NULL || lex->token_type == JSON_TOKEN_END)
1132  ereport(ERROR,
1133  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1134  errmsg("invalid input syntax for type %s", "json"),
1135  errdetail("The input string ended unexpectedly."),
1136  report_json_context(lex)));
1137 
1138  /* Separate out the current token. */
1139  toklen = lex->token_terminator - lex->token_start;
1140  token = palloc(toklen + 1);
1141  memcpy(token, lex->token_start, toklen);
1142  token[toklen] = '\0';
1143 
1144  /* Complain, with the appropriate detail message. */
1145  if (ctx == JSON_PARSE_END)
1146  ereport(ERROR,
1147  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1148  errmsg("invalid input syntax for type %s", "json"),
1149  errdetail("Expected end of input, but found \"%s\".",
1150  token),
1151  report_json_context(lex)));
1152  else
1153  {
1154  switch (ctx)
1155  {
1156  case JSON_PARSE_VALUE:
1157  ereport(ERROR,
1158  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1159  errmsg("invalid input syntax for type %s", "json"),
1160  errdetail("Expected JSON value, but found \"%s\".",
1161  token),
1162  report_json_context(lex)));
1163  break;
1164  case JSON_PARSE_STRING:
1165  ereport(ERROR,
1166  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1167  errmsg("invalid input syntax for type %s", "json"),
1168  errdetail("Expected string, but found \"%s\".",
1169  token),
1170  report_json_context(lex)));
1171  break;
1173  ereport(ERROR,
1174  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1175  errmsg("invalid input syntax for type %s", "json"),
1176  errdetail("Expected array element or \"]\", but found \"%s\".",
1177  token),
1178  report_json_context(lex)));
1179  break;
1180  case JSON_PARSE_ARRAY_NEXT:
1181  ereport(ERROR,
1182  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1183  errmsg("invalid input syntax for type %s", "json"),
1184  errdetail("Expected \",\" or \"]\", but found \"%s\".",
1185  token),
1186  report_json_context(lex)));
1187  break;
1189  ereport(ERROR,
1190  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1191  errmsg("invalid input syntax for type %s", "json"),
1192  errdetail("Expected string or \"}\", but found \"%s\".",
1193  token),
1194  report_json_context(lex)));
1195  break;
1197  ereport(ERROR,
1198  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1199  errmsg("invalid input syntax for type %s", "json"),
1200  errdetail("Expected \":\", but found \"%s\".",
1201  token),
1202  report_json_context(lex)));
1203  break;
1205  ereport(ERROR,
1206  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1207  errmsg("invalid input syntax for type %s", "json"),
1208  errdetail("Expected \",\" or \"}\", but found \"%s\".",
1209  token),
1210  report_json_context(lex)));
1211  break;
1213  ereport(ERROR,
1214  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1215  errmsg("invalid input syntax for type %s", "json"),
1216  errdetail("Expected string, but found \"%s\".",
1217  token),
1218  report_json_context(lex)));
1219  break;
1220  default:
1221  elog(ERROR, "unexpected json parse state: %d", ctx);
1222  }
1223  }
1224 }
JsonTokenType token_type
Definition: jsonapi.h:58
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
char * token_start
Definition: jsonapi.h:55
char * token_terminator
Definition: jsonapi.h:56
#define NULL
Definition: c.h:229
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
static int report_json_context(JsonLexContext *lex)
Definition: json.c:1261
Datum row_to_json ( PG_FUNCTION_ARGS  )

Definition at line 1815 of file json.c.

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

1816 {
1817  Datum array = PG_GETARG_DATUM(0);
1819 
1820  result = makeStringInfo();
1821 
1822  composite_to_json(array, result, false);
1823 
1825 }
static void composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
Definition: json.c:1682
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
return result
Definition: formatting.c:1633
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
Datum row_to_json_pretty ( PG_FUNCTION_ARGS  )

Definition at line 1831 of file json.c.

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

1832 {
1833  Datum array = PG_GETARG_DATUM(0);
1834  bool use_line_feeds = PG_GETARG_BOOL(1);
1836 
1837  result = makeStringInfo();
1838 
1839  composite_to_json(array, result, use_line_feeds);
1840 
1842 }
static void composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
Definition: json.c:1682
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
return result
Definition: formatting.c:1633
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
Datum to_json ( PG_FUNCTION_ARGS  )

Definition at line 1848 of file json.c.

References cstring_to_text_with_len(), StringInfoData::data, datum_to_json(), ereport, errcode(), errmsg(), ERROR, get_fn_expr_argtype(), InvalidOid, json_categorize_type(), StringInfoData::len, makeStringInfo(), PG_GETARG_DATUM, PG_RETURN_TEXT_P, result, and val.

1849 {
1850  Datum val = PG_GETARG_DATUM(0);
1851  Oid val_type = get_fn_expr_argtype(fcinfo->flinfo, 0);
1853  JsonTypeCategory tcategory;
1854  Oid outfuncoid;
1855 
1856  if (val_type == InvalidOid)
1857  ereport(ERROR,
1858  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1859  errmsg("could not determine input data type")));
1860 
1861  json_categorize_type(val_type,
1862  &tcategory, &outfuncoid);
1863 
1864  result = makeStringInfo();
1865 
1866  datum_to_json(val, false, result, tcategory, outfuncoid, false);
1867 
1869 }
static void datum_to_json(Datum val, bool is_null, StringInfo result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
Definition: json.c:1449
static void json_categorize_type(Oid typoid, JsonTypeCategory *tcategory, Oid *outfuncoid)
Definition: json.c:1347
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1633
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1909
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
JsonTypeCategory
Definition: json.c:53
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:372
#define InvalidOid
Definition: postgres_ext.h:36
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:330
int errmsg(const char *fmt,...)
Definition: elog.c:797
long val
Definition: informix.c:689

Variable Documentation

JsonSemAction nullSemAction
static
Initial value:
=
{
NULL, NULL, NULL, NULL, NULL
}
#define NULL
Definition: c.h:229

Definition at line 109 of file json.c.