diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c index 6a7aab2..bac08c0 100644 --- a/src/backend/utils/adt/jsonfuncs.c +++ b/src/backend/utils/adt/jsonfuncs.c @@ -42,6 +42,8 @@ #define JB_PATH_CREATE_OR_INSERT \ (JB_PATH_INSERT_BEFORE | JB_PATH_INSERT_AFTER | JB_PATH_CREATE) +#define is_jsonb_data(type) (type == WJB_KEY || type == WJB_VALUE || type == WJB_ELEM) + /* state for json_object_keys */ typedef struct OkeysState { @@ -52,6 +54,23 @@ typedef struct OkeysState int sent_count; } OkeysState; +/* state for iterate_json function */ +typedef struct IterateJsonState +{ + JsonLexContext *lex; + JsonIterateAction action; /* an action that will be applied to each json value */ + void *action_state; /* any necessary context for iteration */ +} IterateJsonState; + +/* state for transform_json function */ +typedef struct TransformJsonState +{ + JsonLexContext *lex; + StringInfo strval; /* resulting json */ + JsonTransformAction action; /* an action that will be applied to each json value */ + void *action_state; /* any necessary context for transformation */ +} TransformJsonState; + /* state for json_get* functions */ typedef struct GetState { @@ -271,6 +290,18 @@ static void setPathArray(JsonbIterator **it, Datum *path_elems, int level, Jsonb *newval, uint32 nelems, int op_type); static void addJsonbToParseState(JsonbParseState **jbps, Jsonb *jb); +/* function supporting iterate_json(b) */ +static void apply_action(void *state, char *token, JsonTokenType tokentype); + +/* function supporting transform_json(b) */ +static void transform_object_start(void *state); +static void transform_object_end(void *state); +static void transform_array_start(void *state); +static void transform_array_end(void *state); +static void transform_object_field_start(void *state, char *fname, bool isnull); +static void transform_array_element_start(void *state, bool isnull); +static void transform_scalar(void *state, char *token, JsonTokenType tokentype); + /* * SQL function json_object_keys @@ -4130,3 +4161,206 @@ setPathArray(JsonbIterator **it, Datum *path_elems, bool *path_nulls, } } } + +/* + * Iterate over jsonb string values or elements, and pass them together with + * an iteration state to a specified JsonIterateAction. + */ +void * +iterate_jsonb_values(Jsonb *jb, void *state, JsonIterateAction action) +{ + JsonbIterator *it; + JsonbValue v; + JsonbIteratorToken type; + + it = JsonbIteratorInit(&jb->root); + + while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE) + { + if ((type == WJB_VALUE || type == WJB_ELEM) && v.type == jbvString) + { + action(state, v.val.string.val, v.val.string.len); + } + } + + return state; +} + +/* + * Iterate over json string values or elements, and pass them together with an + * iteration state to a specified JsonIterateAction. + */ +void * +iterate_json_values(text *json, void *action_state, JsonIterateAction action) +{ + JsonLexContext *lex = makeJsonLexContext(json, true); + JsonSemAction *sem = palloc0(sizeof(JsonSemAction)); + IterateJsonState *state = palloc0(sizeof(IterateJsonState)); + + state->lex = lex; + state->action = action; + state->action_state = action_state; + + sem->semstate = (void *) state; + sem->scalar = apply_action; + + pg_parse_json(lex, sem); + + return state; +} + +/* + * An auxiliary function for iterate_json_values to invoke a specified + * JsonIterateAction. + */ +static void +apply_action(void *state, char *token, JsonTokenType tokentype) +{ + IterateJsonState *_state = (IterateJsonState *) state; + if (tokentype == JSON_TOKEN_STRING) + (*_state->action) (_state->action_state, token, strlen(token)); +} + +/* + * Iterate over a jsonb, and apply a specified JsonTransformAction to every + * string value or element. Any necessary context for a JsonTransformAction can + * be passed in the action_state variable. Function returns a copy of an original jsonb + * object with transformed values. + */ +Jsonb * +transform_jsonb(Jsonb *jsonb, void *action_state, JsonTransformAction transform_action) +{ + JsonbIterator *it; + JsonbValue v, *res = NULL; + JsonbIteratorToken type; + JsonbParseState *st = NULL; + text *out; + bool is_scalar = false; + + it = JsonbIteratorInit(&jsonb->root); + is_scalar = it->isScalar; + + while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE) + { + if ((type == WJB_VALUE || type == WJB_ELEM) && v.type == jbvString) + { + out = transform_action(action_state, v.val.string.val, v.val.string.len); + v.val.string.val = VARDATA_ANY(out); + v.val.string.len = VARSIZE_ANY_EXHDR(out); + res = pushJsonbValue(&st, type, type < WJB_BEGIN_ARRAY ? &v : NULL); + } + else + { + res = pushJsonbValue(&st, type, is_jsonb_data(type) ? &v : NULL); + } + } + + if (res->type == jbvArray) + res->val.array.rawScalar = is_scalar; + + return JsonbValueToJsonb(res); +} + +/* + * Iterate over a json, and apply a specified JsonTransformAction to every + * string value or element. Any necessary context for a JsonTransformAction can + * be passed in the action_state variable. Function returns a StringInfo, which + * is a copy of an original json with transformed values. + */ +StringInfo +transform_json(text *json, void *action_state, JsonTransformAction transform_action) +{ + JsonLexContext *lex = makeJsonLexContext(json, true); + JsonSemAction *sem = palloc0(sizeof(JsonSemAction)); + TransformJsonState *state = palloc0(sizeof(TransformJsonState)); + + state->lex = lex; + state->strval = makeStringInfo(); + state->action = transform_action; + state->action_state = action_state; + + sem->semstate = (void *) state; + sem->scalar = transform_scalar; + sem->object_start = transform_object_start; + sem->object_end = transform_object_end; + sem->array_start = transform_array_start; + sem->array_end = transform_array_end; + sem->scalar = transform_scalar; + sem->array_element_start = transform_array_element_start; + sem->object_field_start = transform_object_field_start; + + pg_parse_json(lex, sem); + + return state->strval; +} + +/* + * Set of auxiliary functions for transform_json to invoke a specified + * JsonTransformAction for all values and left everything else untouched. + */ +static void +transform_object_start(void *state) +{ + TransformJsonState *_state = (TransformJsonState *) state; + appendStringInfoCharMacro(_state->strval, '{'); +} + +static void +transform_object_end(void *state) +{ + TransformJsonState *_state = (TransformJsonState *) state; + appendStringInfoCharMacro(_state->strval, '}'); +} + +static void +transform_array_start(void *state) +{ + TransformJsonState *_state = (TransformJsonState *) state; + appendStringInfoCharMacro(_state->strval, '['); +} + +static void +transform_array_end(void *state) +{ + TransformJsonState *_state = (TransformJsonState *) state; + appendStringInfoCharMacro(_state->strval, ']'); +} + +static void +transform_object_field_start(void *state, char *fname, bool isnull) +{ + TransformJsonState *_state = (TransformJsonState *) state; + + if (_state->strval->data[_state->strval->len - 1] != '{') + appendStringInfoCharMacro(_state->strval, ','); + + /* + * Unfortunately we don't have the quoted and escaped string any more, so + * we have to re-escape it. + */ + escape_json(_state->strval, fname); + appendStringInfoCharMacro(_state->strval, ':'); +} + +static void +transform_array_element_start(void *state, bool isnull) +{ + TransformJsonState *_state = (TransformJsonState *) state; + + if (_state->strval->data[_state->strval->len - 1] != '[') + appendStringInfoCharMacro(_state->strval, ','); +} + +static void +transform_scalar(void *state, char *token, JsonTokenType tokentype) +{ + TransformJsonState *_state = (TransformJsonState *) state; + + if (tokentype == JSON_TOKEN_STRING) + { + text *out = (*_state->action) (_state->action_state, token, strlen(token)); + escape_json(_state->strval, text_to_cstring(out)); + } + else + appendStringInfoString(_state->strval, token); +} diff --git a/src/include/utils/jsonb.h b/src/include/utils/jsonb.h index 411e158..bf3de44 100644 --- a/src/include/utils/jsonb.h +++ b/src/include/utils/jsonb.h @@ -351,6 +351,12 @@ typedef struct JsonbIterator struct JsonbIterator *parent; } JsonbIterator; +/* an action that will be applied to each value in iterate_json(b) functions */ +typedef void (*JsonIterateAction) (void *state, char *elem_value, int elem_len); + +/* an action that will be applied to each value in transform_json(b) functions */ +typedef text * (*JsonTransformAction) (void *state, char *elem_value, int elem_len); + /* Support functions */ extern uint32 getJsonbOffset(const JsonbContainer *jc, int index); @@ -377,5 +383,12 @@ extern char *JsonbToCString(StringInfo out, JsonbContainer *in, extern char *JsonbToCStringIndent(StringInfo out, JsonbContainer *in, int estimated_len); +extern void *iterate_jsonb_values(Jsonb *jb, void *state, JsonIterateAction action); +extern void *iterate_json_values(text *json, void *action_state, JsonIterateAction action); +extern Jsonb *transform_jsonb(Jsonb *jsonb, void *action_state, + JsonTransformAction transform_action); +extern StringInfo transform_json(text *json, void *action_state, + JsonTransformAction transform_action); + #endif /* __JSONB_H__ */