Skip to content

Commit e080b22

Browse files
author
Nikita Glukhov
committed
Add GSON method setPath()
1 parent 78c9dbc commit e080b22

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

contrib/jsonb_toaster/jsonb_toaster.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,6 +2061,14 @@ jsonxzCopy(JsonContainer *jc)
20612061
return res;
20622062
}
20632063

2064+
static Datum
2065+
jsonxSetPath(JsonContainer *js, Datum *path_elems,
2066+
bool *path_nulls, int path_len,
2067+
JsonValue *newval, int flags)
2068+
{
2069+
return JsonSetPathGeneric(js, path_elems, path_nulls, path_len, newval, flags);
2070+
}
2071+
20642072
static JsonContainerOps
20652073
jsonxzContainerOps =
20662074
{
@@ -2074,7 +2082,8 @@ jsonxzContainerOps =
20742082
JsonbToCStringRaw,
20752083
jsonxzCopy,
20762084
jsonxzFree,
2077-
jsonxzEncode
2085+
jsonxzEncode,
2086+
jsonxSetPath
20782087
};
20792088

20802089
static JsonContainerOps
@@ -2090,7 +2099,8 @@ jsonxContainerOps =
20902099
JsonbToCStringRaw,
20912100
JsonCopyFlat,
20922101
NULL,
2093-
jsonxEncode
2102+
jsonxEncode,
2103+
jsonxSetPath
20942104
};
20952105

20962106
static JsonContainer *

src/backend/utils/adt/jsonb_util.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,7 +2578,8 @@ jsonbContainerOps =
25782578
JsonbToCStringRaw,
25792579
JsonCopyFlat,
25802580
NULL,
2581-
NULL
2581+
NULL,
2582+
JsonSetPathGeneric
25822583
};
25832584

25842585
#ifndef JSONB_DETOAST_ITERATOR
@@ -3169,7 +3170,8 @@ jsonbzContainerOps =
31693170
JsonbToCStringRaw,
31703171
JsonCopyFlat, // FIXME
31713172
jsonbzFree,
3172-
NULL
3173+
NULL,
3174+
JsonSetPathGeneric
31733175
};
31743176

31753177
Json *

src/backend/utils/adt/jsonfuncs.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4532,6 +4532,23 @@ jsonb_delete_idx(PG_FUNCTION_ARGS)
45324532
PG_RETURN_DATUM(JsonbValueToOrigJsonbDatum(res, in));
45334533
}
45344534

4535+
Datum
4536+
JsonSetPathGeneric(JsonContainer *js,
4537+
Datum *path_elems, bool *path_nulls, int path_len,
4538+
JsonValue *newval, int flags)
4539+
{
4540+
JsonbParseState *st = NULL;
4541+
JsonbIterator *it = JsonbIteratorInit(js);
4542+
JsonbValue *res;
4543+
4544+
res = setPath(&it, path_elems, path_nulls, path_len, &st, 0,
4545+
newval, flags);
4546+
4547+
Assert(res != NULL);
4548+
4549+
return JsonbValueToOrigJsonbDatum2(res, js);
4550+
}
4551+
45354552
/*
45364553
* SQL function jsonb_set(jsonb, text[], jsonb, boolean)
45374554
*/
@@ -4543,12 +4560,10 @@ jsonb_set(PG_FUNCTION_ARGS)
45434560
Jsonb *newjsonb = PG_GETARG_JSONB_P(2);
45444561
JsonbValue newval;
45454562
bool create = PG_GETARG_BOOL(3);
4546-
JsonbValue *res = NULL;
4563+
Datum res;
45474564
Datum *path_elems;
45484565
bool *path_nulls;
45494566
int path_len;
4550-
JsonbIterator *it;
4551-
JsonbParseState *st = NULL;
45524567

45534568
JsonToJsonValue(newjsonb, &newval);
45544569

@@ -4570,14 +4585,10 @@ jsonb_set(PG_FUNCTION_ARGS)
45704585
if (path_len == 0)
45714586
PG_RETURN_JSONB_P(in);
45724587

4573-
it = JsonbIteratorInit(JsonbRoot(in));
4574-
4575-
res = setPath(&it, path_elems, path_nulls, path_len, &st,
4576-
0, &newval, create ? JB_PATH_CREATE : JB_PATH_REPLACE);
4577-
4578-
Assert(res != NULL);
4588+
res = JsonSetPath(JsonbRoot(in), path_elems, path_nulls, path_len,
4589+
&newval, create ? JB_PATH_CREATE : JB_PATH_REPLACE);
45794590

4580-
PG_RETURN_DATUM(JsonbValueToOrigJsonbDatum(res, in));
4591+
PG_RETURN_DATUM(res);
45814592
}
45824593

45834594

src/include/utils/json_generic.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ struct JsonContainerOps
7474
JsonContainer *(*copy)(JsonContainer *jc);
7575
void (*free)(JsonContainer *jc);
7676
void *(*encode)(JsonValue *jc, JsonContainerOps *ops, Oid toasterid);
77+
Datum (*setPath)(JsonContainer *js, Datum *path_elems,
78+
bool *path_nulls, int path_len,
79+
JsonValue *newval, int flags);
7780
};
7881

7982
typedef struct CompressedObject
@@ -166,6 +169,9 @@ typedef JsonContainer JsonbContainer;
166169
#define JsonOp4(op, jscontainer, arg1, arg2, arg3, arg4) \
167170
JsonOp(op, jscontainer)(jscontainer, arg1, arg2, arg3, arg4)
168171

172+
#define JsonOp5(op, jscontainer, arg1, arg2, arg3, arg4, arg5) \
173+
JsonOp(op, jscontainer)(jscontainer, arg1, arg2, arg3, arg4, arg5)
174+
169175
#define JsonIteratorInit(jscontainer) \
170176
JsonOp0(iteratorInit, jscontainer)
171177

@@ -195,6 +201,8 @@ typedef JsonContainer JsonbContainer;
195201
(jc)->ops->free(jc); \
196202
} while (0)
197203

204+
#define JsonSetPath(jc, path_elems, path_nulls, path_len, newval, flags) \
205+
JsonOp5(setPath, jc, path_elems, path_nulls, path_len, newval, flags)
198206

199207
static inline JsonIteratorToken
200208
JsonIteratorNext(JsonIterator **it, JsonValue *val, bool skipNested)
@@ -303,6 +311,9 @@ extern JsonValue *JsonValueCopy(JsonValue *res, const JsonValue *val);
303311
extern const JsonValue *JsonValueUnwrap(const JsonValue *val, JsonValue *buf);
304312
extern JsonContainer *JsonCopyFlat(JsonContainer *flatContainer);
305313
extern JsonValue *JsonExtractScalar(JsonContainer *jc, JsonValue *scalar);
314+
extern Datum JsonSetPathGeneric(JsonContainer *js, Datum *path_elems,
315+
bool *path_nulls, int path_len,
316+
JsonValue *newval, int flags);
306317

307318
extern Jsonb *JsonbMakeEmptyArray(void);
308319
extern Jsonb *JsonbMakeEmptyObject(void);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy