BSON_ARRAY_AS_...D_EXTENDED_JSON(3)

πŸ’… BSON_ARRAY_AS_...D_EXTENDED_JSON(3) πŸ’…

okay bestie, this is the libbson manual page for turning a BSON array into Relaxed Extended JSON β€” and yes, the function name in the title is doing its own little abbreviation moment. iykyk. let’s get into it. πŸ¦‹


✨ SYNOPSIS ✨

char *
bson_array_as_relaxed_extended_json (const bson_t *bson, size_t *length);

πŸŽ€ PARAMETERS πŸŽ€

  • bson β€” a bson_t. that’s the BSON thing we’re about to encode, bestie.
  • length β€” an optional location for the length of the resulting string. optional, like your third coffee of the day. β˜•

🌸 DESCRIPTION 🌸

okay so the description says bson_as_relaxed_extended_json() encodes bson as a UTF-8 string in Relaxed Extended JSON. and the way it does it? the outermost element gets encoded as a JSON array ([ ... ]), not as a JSON document ({ ... }). it’s giving β€œarray main character energy,” fr fr. πŸ’…

for the whole lore on the Extended JSON formats, see MongoDB Extended JSON format.

you, the caller, are responsible for freeing the resulting UTF-8 encoded string by calling bson_free() with the result. no cap, don’t forget this part. 🫢

if length is non-NULL, length will be set to the length of the result in bytes.


⭐ RETURNS ⭐

if it slays and everything works: a newly allocated UTF-8 encoded string is returned, and length is set. periodt. ✨

upon failure: NULL is returned. 😭


πŸ¦‹ EXAMPLE πŸ¦‹

bson_t *b = BCON_NEW("0", BCON_INT32(1), "1", BCON_UTF8("bar"));
// The document for an array is a normal BSON document with integer values for the keys, starting with 0 and
// continuing sequentially.
char *str = bson_array_as_relaxed_extended_json(b, NULL);
printf("Relaxed Extended JSON array: %s\n", str);
// Prints:
// Relaxed Extended JSON array: [ 1, "bar" ]
bson_free(str);
bson_destroy(b);
bestie, seriously: bson_free(str) and bson_destroy(b) are right there in the example. don’t leave them on read. πŸ’€

πŸ‘‘ AUTHOR πŸ‘‘

MongoDB, Inc


2009-present, MongoDB, Inc.


kisses and clean builds πŸ’– β€” this came from the libbson manual, version 2.5.6, dated Oct 09, 20 (yes, the footer really ends like that, and we respect the mystery πŸŽ€).

πŸ“¦ where this page comes from