BSON_ARRAY_AS_...L_EXTENDED_JSON(3)

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

okay so this is a libbson page, bestie β€” yes the title got a little cropped in the header (BSON_ARRAY_AS_...L_EXTENDED_JSON(3)), don't worry about it, that's just how it's filed πŸŽ€


✨ SYNOPSIS ✨

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

🫢 PARAMETERS 🫢

  • bson β€” a bson_t. that's the thing getting encoded, periodt.
  • length β€” an optional spot to stash the length of the resulting string. optional!! so you can pass NULL and nobody's mad πŸ’œ

🌸 DESCRIPTION 🌸

bson_array_as_canonical_extended_json() takes bson and encodes it as a UTF-8 string in Canonical Extended JSON. the way it does this: the outermost element gets encoded as a JSON array ([ ... ]), and not as a JSON document ({ ... }). highkey an important distinction, iykyk πŸ¦‹

for the whole rundown of Extended JSON formats, go see the MongoDB Extended JSON format docs. πŸ‘‘

⚠️ the caller is responsible for freeing the resulting UTF-8 encoded string β€” call bson_free() on the result. do not ghost it, do not leak it. free your girl 😭

if length is non-NULL, it gets set to the length of the result in bytes. πŸ“


πŸ”₯ RETURNS πŸ”₯

if it works out: a newly allocated UTF-8 encoded string, and length gets set. slay ✨

if it does not work out: 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_canonical_extended_json(b, NULL);
printf("Canonical Extended JSON array: %s\n", str);
// Prints:
// Canonical Extended JSON array: [ { "$numberInt" : "1" }, "bar" ]
bson_free(str);
bson_destroy(b);

not the [ { "$numberInt" : "1" }, "bar" ] ate and left literally no crumbs πŸ”₯ see how the keys are just "0" and "1"? an array's underlying document is a totally normal BSON doc with integer values for keys, starting at 0 and going sequentially. iconic behavior fr fr πŸŽ€


πŸ‘‘ AUTHOR πŸ‘‘

MongoDB, Inc 🫢


2009-present, MongoDB, Inc.


πŸ“ remember: bson_free(str) then bson_destroy(b). clean up ur room, bestie πŸ™ˆ

ok that's the whole page!! lots of love from libbson 2.5.6, dated Oct 09 (the year got ate by the header in the footer, ngl β€” 20... is all we got πŸ’€), and yes the footer font is huge, that's the vibe check, bye πŸ’…πŸŒˆ

πŸ“¦ where this page comes from