MCPcopy Create free account
hub / github.com/documentdb/documentdb / BsonValueToJsonForLoggingCore

Function BsonValueToJsonForLoggingCore

pg_documentdb_core/src/io/pgbson.c:229–334  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

227
228
229static const char *
230BsonValueToJsonForLoggingCore(const bson_value_t *value, bool quoteStrings)
231{
232 bson_t bson;
233 pgbson *bsonDocument;
234 const uint8_t *documentData;
235 uint32_t documentLength;
236 const char *returnValue;
237 char numBuffer[30];
238
239 /* For common types optimize the conversion to logging strings */
240 switch (value->value_type)
241 {
242 case BSON_TYPE_DOCUMENT:
243 {
244 documentData = value->value.v_doc.data;
245 documentLength = value->value.v_doc.data_len;
246
247 if (!bson_init_static(&bson, documentData, documentLength))
248 {
249 ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_BADVALUE),
250 errmsg("invalid input syntax for BSON")));
251 }
252
253 /* since bson strings are palloced - we can simply return the string created. */
254 returnValue = bson_as_relaxed_extended_json(&bson, NULL);
255 break;
256 }
257
258 case BSON_TYPE_UTF8:
259 {
260 /* create a string that has the original string, \0, and the quotes. */
261 if (quoteStrings)
262 {
263 StringInfoData strData;
264 initStringInfo(&strData);
265 escape_json(&strData, value->value.v_utf8.str);
266 returnValue = strData.data;
267 }
268 else
269 {
270 returnValue = pnstrdup(value->value.v_utf8.str, value->value.v_utf8.len);
271 }
272
273 break;
274 }
275
276 case BSON_TYPE_INT32:
277 {
278 int strLength = pg_ltoa(value->value.v_int32, numBuffer);
279 returnValue = pnstrdup(numBuffer, strLength);
280 break;
281 }
282
283 case BSON_TYPE_INT64:
284 {
285 int strLength = pg_lltoa(value->value.v_int64, numBuffer);
286 returnValue = pnstrdup(numBuffer, strLength);

Callers 2

Calls 1

Tested by

no test coverage detected