mirror of
https://git.postgresql.org/git/postgresql.git
synced 2026-02-15 10:57:02 +08:00
There were quite a few places where we either had a non-NUL-terminated string or a text Datum which we needed to call escape_json() on. Many of these places required that a temporary string was created due to the fact that escape_json() needs a NUL-terminated cstring. For text types, those first had to be converted to cstring before calling escape_json() on them. Here we introduce two new functions to make escaping JSON more optimal: escape_json_text() can be given a text Datum to append onto the given buffer. This is more optimal as it foregoes the need to convert the text Datum into a cstring. A temporary allocation is only required if the text Datum needs to be detoasted. escape_json_with_len() can be used when the length of the cstring is already known or the given string isn't NUL-terminated. Having this allows various places which were creating a temporary NUL-terminated string to just call escape_json_with_len() without any temporary memory allocations. Discussion: https://postgr.es/m/CAApHDvpLXwMZvbCKcdGfU9XQjGCDm7tFpRdTXuB9PVgpNUYfEQ@mail.gmail.com Reviewed-by: Melih Mutlu, Heikki Linnakangas
34 lines
1.2 KiB
C
34 lines
1.2 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* json.h
|
|
* Declarations for JSON data type support.
|
|
*
|
|
* Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
*
|
|
* src/include/utils/json.h
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
|
|
#ifndef JSON_H
|
|
#define JSON_H
|
|
|
|
#include "lib/stringinfo.h"
|
|
|
|
/* functions in json.c */
|
|
extern void escape_json(StringInfo buf, const char *str);
|
|
extern void escape_json_with_len(StringInfo buf, const char *str, int len);
|
|
extern void escape_json_text(StringInfo buf, const text *txt);
|
|
extern char *JsonEncodeDateTime(char *buf, Datum value, Oid typid,
|
|
const int *tzp);
|
|
extern bool to_json_is_immutable(Oid typoid);
|
|
extern Datum json_build_object_worker(int nargs, const Datum *args, const bool *nulls,
|
|
const Oid *types, bool absent_on_null,
|
|
bool unique_keys);
|
|
extern Datum json_build_array_worker(int nargs, const Datum *args, const bool *nulls,
|
|
const Oid *types, bool absent_on_null);
|
|
extern bool json_validate(text *json, bool check_unique_keys, bool throw_error);
|
|
|
|
#endif /* JSON_H */
|