static inline void json_lex(JsonLexContext *lex);
static inline void json_lex_string(JsonLexContext *lex);
static inline void json_lex_number(JsonLexContext *lex, char *s);
-static inline 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 inline 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);
bool use_line_feeds);
/* the null action object used for pure validation */
-static jsonSemAction nullSemAction =
+static JsonSemAction nullSemAction =
{
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL
};
-static JsonSemAction NullSemAction = &nullSemAction;
/* Recursive Descent parser support routines */
/* validate it */
lex = makeJsonLexContext(result, false);
- pg_parse_json(lex, NullSemAction);
+ pg_parse_json(lex, &nullSemAction);
/* Internal representation is the same as text, for now */
PG_RETURN_TEXT_P(result);
/* Validate it. */
lex = makeJsonLexContext(result, false);
- pg_parse_json(lex, NullSemAction);
+ pg_parse_json(lex, &nullSemAction);
PG_RETURN_TEXT_P(result);
}
* pointer to a state object to be passed to those routines.
*/
void
-pg_parse_json(JsonLexContext *lex, JsonSemAction sem)
+pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
{
JsonTokenType tok;
* - object field
*/
static inline void
-parse_scalar(JsonLexContext *lex, JsonSemAction sem)
+parse_scalar(JsonLexContext *lex, JsonSemAction *sem)
{
char *val = NULL;
json_scalar_action sfunc = sem->scalar;
}
static void
-parse_object_field(JsonLexContext *lex, JsonSemAction sem)
+parse_object_field(JsonLexContext *lex, JsonSemAction *sem)
{
/*
* an object field is "fieldname" : value where value can be a scalar,
}
static void
-parse_object(JsonLexContext *lex, JsonSemAction sem)
+parse_object(JsonLexContext *lex, JsonSemAction *sem)
{
/*
* an object is a possibly empty sequence of object fields, separated by
}
static void
-parse_array_element(JsonLexContext *lex, JsonSemAction sem)
+parse_array_element(JsonLexContext *lex, JsonSemAction *sem)
{
json_aelem_action astart = sem->array_element_start;
json_aelem_action aend = sem->array_element_end;
}
static void
-parse_array(JsonLexContext *lex, JsonSemAction sem)
+parse_array(JsonLexContext *lex, JsonSemAction *sem)
{
/*
* an array is a possibly empty sequence of array elements, separated by
} JsonSearch;
/* state for json_object_keys */
-typedef struct okeysState
+typedef struct OkeysState
{
JsonLexContext *lex;
char **result;
int result_size;
int result_count;
int sent_count;
-} okeysState, *OkeysState;
+} OkeysState;
/* state for json_get* functions */
-typedef struct getState
+typedef struct GetState
{
JsonLexContext *lex;
JsonSearch search_type;
bool *pathok;
int *array_level_index;
int *path_level_index;
-} getState, *GetState;
+} GetState;
/* state for json_array_length */
-typedef struct alenState
+typedef struct AlenState
{
JsonLexContext *lex;
int count;
-} alenState, *AlenState;
+} AlenState;
/* state for json_each */
-typedef struct eachState
+typedef struct EachState
{
JsonLexContext *lex;
Tuplestorestate *tuple_store;
bool normalize_results;
bool next_scalar;
char *normalized_scalar;
-} eachState, *EachState;
+} EachState;
/* state for json_array_elements */
-typedef struct elementsState
+typedef struct ElementsState
{
JsonLexContext *lex;
Tuplestorestate *tuple_store;
TupleDesc ret_tdesc;
MemoryContext tmp_cxt;
char *result_start;
-} elementsState, *ElementsState;
+} ElementsState;
/* state for get_json_object_as_hash */
-typedef struct jhashState
+typedef struct JhashState
{
JsonLexContext *lex;
HTAB *hash;
char *save_json_start;
bool use_json_as_text;
char *function_name;
-} jhashState, *JHashState;
+} JHashState;
/* used to build the hashtable */
-typedef struct jsonHashEntry
+typedef struct JsonHashEntry
{
char fname[NAMEDATALEN];
char *val;
char *json;
bool isnull;
-} jsonHashEntry, *JsonHashEntry;
+} JsonHashEntry;
/* these two are stolen from hstore / record_out, used in populate_record* */
typedef struct ColumnIOData
} RecordIOData;
/* state for populate_recordset */
-typedef struct populateRecordsetState
+typedef struct PopulateRecordsetState
{
JsonLexContext *lex;
HTAB *json_hash;
HeapTupleHeader rec;
RecordIOData *my_extra;
MemoryContext fn_mcxt; /* used to stash IO funcs */
-} populateRecordsetState, *PopulateRecordsetState;
+} PopulateRecordsetState;
/*
* SQL function json_object-keys
json_object_keys(PG_FUNCTION_ARGS)
{
FuncCallContext *funcctx;
- OkeysState state;
+ OkeysState *state;
int i;
if (SRF_IS_FIRSTCALL())
{
text *json = PG_GETARG_TEXT_P(0);
JsonLexContext *lex = makeJsonLexContext(json, true);
- JsonSemAction sem;
+ JsonSemAction *sem;
MemoryContext oldcontext;
funcctx = SRF_FIRSTCALL_INIT();
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
- state = palloc(sizeof(okeysState));
- sem = palloc0(sizeof(jsonSemAction));
+ state = palloc(sizeof(OkeysState));
+ sem = palloc0(sizeof(JsonSemAction));
state->lex = lex;
state->result_size = 256;
}
funcctx = SRF_PERCALL_SETUP();
- state = (OkeysState) funcctx->user_fctx;
+ state = (OkeysState *) funcctx->user_fctx;
if (state->sent_count < state->result_count)
{
static void
okeys_object_field_start(void *state, char *fname, bool isnull)
{
- OkeysState _state = (OkeysState) state;
+ OkeysState *_state = (OkeysState *) state;
/* only collecting keys for the top level object */
if (_state->lex->lex_level != 1)
static void
okeys_array_start(void *state)
{
- OkeysState _state = (OkeysState) state;
+ OkeysState *_state = (OkeysState *) state;
/* top level must be a json object */
if (_state->lex->lex_level == 0)
static void
okeys_scalar(void *state, char *token, JsonTokenType tokentype)
{
- OkeysState _state = (OkeysState) state;
+ OkeysState *_state = (OkeysState *) state;
/* top level must be a json object */
if (_state->lex->lex_level == 0)
int npath,
bool normalize_results)
{
- GetState state;
+ GetState *state;
JsonLexContext *lex = makeJsonLexContext(json, true);
- JsonSemAction sem;
+ JsonSemAction *sem;
/* only allowed to use one of these */
Assert(elem_index < 0 || (tpath == NULL && ipath == NULL && field == NULL));
Assert(tpath == NULL || field == NULL);
- state = palloc0(sizeof(getState));
- sem = palloc0(sizeof(jsonSemAction));
+ state = palloc0(sizeof(GetState));
+ sem = palloc0(sizeof(JsonSemAction));
state->lex = lex;
/* is it "_as_text" variant? */
static void
get_object_start(void *state)
{
- GetState _state = (GetState) state;
+ GetState *_state = (GetState *) state;
/* json structure check */
if (_state->lex->lex_level == 0 && _state->search_type == JSON_SEARCH_ARRAY)
static void
get_object_field_start(void *state, char *fname, bool isnull)
{
- GetState _state = (GetState) state;
+ GetState *_state = (GetState *) state;
bool get_next = false;
int lex_level = _state->lex->lex_level;
static void
get_object_field_end(void *state, char *fname, bool isnull)
{
- GetState _state = (GetState) state;
+ GetState *_state = (GetState *) state;
bool get_last = false;
int lex_level = _state->lex->lex_level;
static void
get_array_start(void *state)
{
- GetState _state = (GetState) state;
+ GetState *_state = (GetState *) state;
int lex_level = _state->lex->lex_level;
/* json structure check */
static void
get_array_element_start(void *state, bool isnull)
{
- GetState _state = (GetState) state;
+ GetState *_state = (GetState *) state;
bool get_next = false;
int lex_level = _state->lex->lex_level;
static void
get_array_element_end(void *state, bool isnull)
{
- GetState _state = (GetState) state;
+ GetState *_state = (GetState *) state;
bool get_last = false;
int lex_level = _state->lex->lex_level;
static void
get_scalar(void *state, char *token, JsonTokenType tokentype)
{
- GetState _state = (GetState) state;
+ GetState *_state = (GetState *) state;
if (_state->lex->lex_level == 0 && _state->search_type != JSON_SEARCH_PATH)
ereport(ERROR,
{
text *json = PG_GETARG_TEXT_P(0);
- AlenState state;
+ AlenState *state;
JsonLexContext *lex = makeJsonLexContext(json, false);
- JsonSemAction sem;
+ JsonSemAction *sem;
- state = palloc0(sizeof(alenState));
- sem = palloc0(sizeof(jsonSemAction));
+ state = palloc0(sizeof(AlenState));
+ sem = palloc0(sizeof(JsonSemAction));
/* palloc0 does this for us */
#if 0
static void
alen_object_start(void *state)
{
- AlenState _state = (AlenState) state;
+ AlenState *_state = (AlenState *) state;
/* json structure check */
if (_state->lex->lex_level == 0)
static void
alen_scalar(void *state, char *token, JsonTokenType tokentype)
{
- AlenState _state = (AlenState) state;
+ AlenState *_state = (AlenState *) state;
/* json structure check */
if (_state->lex->lex_level == 0)
static void
alen_array_element_start(void *state, bool isnull)
{
- AlenState _state = (AlenState) state;
+ AlenState *_state = (AlenState *) state;
/* just count up all the level 1 elements */
if (_state->lex->lex_level == 1)
{
text *json = PG_GETARG_TEXT_P(0);
JsonLexContext *lex = makeJsonLexContext(json, true);
- JsonSemAction sem;
+ JsonSemAction *sem;
ReturnSetInfo *rsi;
MemoryContext old_cxt;
TupleDesc tupdesc;
- EachState state;
+ EachState *state;
- state = palloc0(sizeof(eachState));
- sem = palloc0(sizeof(jsonSemAction));
+ state = palloc0(sizeof(EachState));
+ sem = palloc0(sizeof(JsonSemAction));
rsi = (ReturnSetInfo *) fcinfo->resultinfo;
static void
each_object_field_start(void *state, char *fname, bool isnull)
{
- EachState _state = (EachState) state;
+ EachState *_state = (EachState *) state;
/* save a pointer to where the value starts */
if (_state->lex->lex_level == 1)
static void
each_object_field_end(void *state, char *fname, bool isnull)
{
- EachState _state = (EachState) state;
+ EachState *_state = (EachState *) state;
MemoryContext old_cxt;
int len;
text *val;
static void
each_array_start(void *state)
{
- EachState _state = (EachState) state;
+ EachState *_state = (EachState *) state;
/* json structure check */
if (_state->lex->lex_level == 0)
static void
each_scalar(void *state, char *token, JsonTokenType tokentype)
{
- EachState _state = (EachState) state;
+ EachState *_state = (EachState *) state;
/* json structure check */
if (_state->lex->lex_level == 0)
/* elements doesn't need any escaped strings, so use false here */
JsonLexContext *lex = makeJsonLexContext(json, false);
- JsonSemAction sem;
+ JsonSemAction *sem;
ReturnSetInfo *rsi;
MemoryContext old_cxt;
TupleDesc tupdesc;
- ElementsState state;
+ ElementsState *state;
- state = palloc0(sizeof(elementsState));
- sem = palloc0(sizeof(jsonSemAction));
+ state = palloc0(sizeof(ElementsState));
+ sem = palloc0(sizeof(JsonSemAction));
rsi = (ReturnSetInfo *) fcinfo->resultinfo;
static void
elements_array_element_start(void *state, bool isnull)
{
- ElementsState _state = (ElementsState) state;
+ ElementsState *_state = (ElementsState *) state;
/* save a pointer to where the value starts */
if (_state->lex->lex_level == 1)
static void
elements_array_element_end(void *state, bool isnull)
{
- ElementsState _state = (ElementsState) state;
+ ElementsState *_state = (ElementsState *) state;
MemoryContext old_cxt;
int len;
text *val;
static void
elements_object_start(void *state)
{
- ElementsState _state = (ElementsState) state;
+ ElementsState *_state = (ElementsState *) state;
/* json structure check */
if (_state->lex->lex_level == 0)
static void
elements_scalar(void *state, char *token, JsonTokenType tokentype)
{
- ElementsState _state = (ElementsState) state;
+ ElementsState *_state = (ElementsState *) state;
/* json structure check */
if (_state->lex->lex_level == 0)
Datum *values;
bool *nulls;
char fname[NAMEDATALEN];
- JsonHashEntry hashentry;
+ JsonHashEntry *hashentry;
use_json_as_text = PG_ARGISNULL(2) ? false : PG_GETARG_BOOL(2);
{
HASHCTL ctl;
HTAB *tab;
- JHashState state;
+ JHashState *state;
JsonLexContext *lex = makeJsonLexContext(json, true);
- JsonSemAction sem;
+ JsonSemAction *sem;
memset(&ctl, 0, sizeof(ctl));
ctl.keysize = NAMEDATALEN;
- ctl.entrysize = sizeof(jsonHashEntry);
+ ctl.entrysize = sizeof(JsonHashEntry);
ctl.hcxt = CurrentMemoryContext;
tab = hash_create("json object hashtable",
100,
&ctl,
HASH_ELEM | HASH_CONTEXT);
- state = palloc0(sizeof(jhashState));
- sem = palloc0(sizeof(jsonSemAction));
+ state = palloc0(sizeof(JHashState));
+ sem = palloc0(sizeof(JsonSemAction));
state->function_name = funcname;
state->hash = tab;
static void
hash_object_field_start(void *state, char *fname, bool isnull)
{
- JHashState _state = (JHashState) state;
+ JHashState *_state = (JHashState *) state;
if (_state->lex->lex_level > 1)
return;
static void
hash_object_field_end(void *state, char *fname, bool isnull)
{
- JHashState _state = (JHashState) state;
- JsonHashEntry hashentry;
+ JHashState *_state = (JHashState *) state;
+ JsonHashEntry *hashentry;
bool found;
char name[NAMEDATALEN];
static void
hash_array_start(void *state)
{
- JHashState _state = (JHashState) state;
+ JHashState *_state = (JHashState *) state;
if (_state->lex->lex_level == 0)
ereport(ERROR,
static void
hash_scalar(void *state, char *token, JsonTokenType tokentype)
{
- JHashState _state = (JHashState) state;
+ JHashState *_state = (JHashState *) state;
if (_state->lex->lex_level == 0)
ereport(ERROR,
RecordIOData *my_extra;
int ncolumns;
JsonLexContext *lex;
- JsonSemAction sem;
- PopulateRecordsetState state;
+ JsonSemAction *sem;
+ PopulateRecordsetState *state;
use_json_as_text = PG_ARGISNULL(2) ? false : PG_GETARG_BOOL(2);
*/
(void) get_call_result_type(fcinfo, NULL, &tupdesc);
- state = palloc0(sizeof(populateRecordsetState));
- sem = palloc0(sizeof(jsonSemAction));
+ state = palloc0(sizeof(PopulateRecordsetState));
+ sem = palloc0(sizeof(JsonSemAction));
/* make these in a sufficiently long-lived memory context */
static void
populate_recordset_object_start(void *state)
{
- PopulateRecordsetState _state = (PopulateRecordsetState) state;
+ PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
int lex_level = _state->lex->lex_level;
HASHCTL ctl;
/* set up a new hash for this entry */
memset(&ctl, 0, sizeof(ctl));
ctl.keysize = NAMEDATALEN;
- ctl.entrysize = sizeof(jsonHashEntry);
+ ctl.entrysize = sizeof(JsonHashEntry);
ctl.hcxt = CurrentMemoryContext;
_state->json_hash = hash_create("json object hashtable",
100,
static void
populate_recordset_object_end(void *state)
{
- PopulateRecordsetState _state = (PopulateRecordsetState) state;
+ PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
HTAB *json_hash = _state->json_hash;
Datum *values;
bool *nulls;
RecordIOData *my_extra = _state->my_extra;
int ncolumns = my_extra->ncolumns;
TupleDesc tupdesc = _state->ret_tdesc;
- JsonHashEntry hashentry;
+ JsonHashEntry *hashentry;
HeapTupleHeader rec = _state->rec;
HeapTuple rettuple;
static void
populate_recordset_array_element_start(void *state, bool isnull)
{
- PopulateRecordsetState _state = (PopulateRecordsetState) state;
+ PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
if (_state->lex->lex_level == 1 &&
_state->lex->token_type != JSON_TOKEN_OBJECT_START)
static void
populate_recordset_array_start(void *state)
{
- PopulateRecordsetState _state = (PopulateRecordsetState) state;
+ PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
if (_state->lex->lex_level != 0 && !_state->use_json_as_text)
ereport(ERROR,
static void
populate_recordset_scalar(void *state, char *token, JsonTokenType tokentype)
{
- PopulateRecordsetState _state = (PopulateRecordsetState) state;
+ PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
if (_state->lex->lex_level == 0)
ereport(ERROR,
static void
populate_recordset_object_field_start(void *state, char *fname, bool isnull)
{
- PopulateRecordsetState _state = (PopulateRecordsetState) state;
+ PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
if (_state->lex->lex_level > 2)
return;
static void
populate_recordset_object_field_end(void *state, char *fname, bool isnull)
{
- PopulateRecordsetState _state = (PopulateRecordsetState) state;
- JsonHashEntry hashentry;
+ PopulateRecordsetState *_state = (PopulateRecordsetState *) state;
+ JsonHashEntry *hashentry;
bool found;
char name[NAMEDATALEN];