Skip to content

Commit 8e22519

Browse files
author
Nikita Glukhov
committed
Add JsonValueInitXxx()
1 parent 048b449 commit 8e22519

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

src/include/utils/json_generic.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "postgres.h"
1919
#include "lib/stringinfo.h"
20+
#include "utils/builtins.h"
2021
#include "utils/expandeddatum.h"
2122
#include "utils/jsonb.h"
2223

@@ -379,6 +380,60 @@ JsonValueInitBinary(JsonValue *val, JsonContainer *cont)
379380
return val;
380381
}
381382

383+
static inline JsonbValue *
384+
JsonValueInitString(JsonbValue *jbv, const char *str)
385+
{
386+
jbv->type = jbvString;
387+
jbv->val.string.len = strlen(str);
388+
jbv->val.string.val = memcpy(palloc(jbv->val.string.len + 1), str,
389+
jbv->val.string.len + 1);
390+
return jbv;
391+
}
392+
393+
static inline JsonbValue *
394+
JsonValueInitStringWithLen(JsonbValue *jbv, const char *str, int len)
395+
{
396+
jbv->type = jbvString;
397+
jbv->val.string.val = str;
398+
jbv->val.string.len = len;
399+
return jbv;
400+
}
401+
402+
static inline JsonbValue *
403+
JsonValueInitText(JsonbValue *jbv, text *txt)
404+
{
405+
jbv->type = jbvString;
406+
jbv->val.string.val = VARDATA_ANY(txt);
407+
jbv->val.string.len = VARSIZE_ANY_EXHDR(txt);
408+
return jbv;
409+
}
410+
411+
static inline JsonbValue *
412+
JsonValueInitNumeric(JsonbValue *jbv, Numeric num)
413+
{
414+
jbv->type = jbvNumeric;
415+
jbv->val.numeric = num;
416+
return jbv;
417+
}
418+
419+
static inline JsonbValue *
420+
JsonValueInitInteger(JsonbValue *jbv, int64 i)
421+
{
422+
jbv->type = jbvNumeric;
423+
jbv->val.numeric = DatumGetNumeric(DirectFunctionCall1(
424+
int8_numeric, Int64GetDatum(i)));
425+
return jbv;
426+
}
427+
428+
static inline JsonbValue *
429+
JsonValueInitFloat(JsonbValue *jbv, float4 f)
430+
{
431+
jbv->type = jbvNumeric;
432+
jbv->val.numeric = DatumGetNumeric(DirectFunctionCall1(
433+
float4_numeric, Float4GetDatum(f)));
434+
return jbv;
435+
}
436+
382437
extern Json *JsonValueToJson(JsonValue *val);
383438
extern JsonValue *JsonToJsonValue(Json *json, JsonValue *jv);
384439
extern JsonValue *JsonValueUnpackBinary(const JsonValue *jbv);

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