BSON_APPEND_UTF8(3)
π BSON_APPEND_UTF8(3) β the libbson girlies' guide to yeeting strings into a bson π
Okay bestie, settle in, because this one is literally the moment string data enters the chat. This is the BSON_APPEND_UTF8 page from the libbson manual, and we're gonna make it make sense. β¨
β¨ SYNOPSIS β¨
So there's a macro (the convenient one) and a function (the full-control one). Both iconic, both doing the same job.
#define BSON_APPEND_UTF8(b, key, val) \
bson_append_utf8 (b, key, (int) strlen (key), val, (int) strlen (val))
bool
bson_append_utf8 (bson_t *bson,
const char *key,
int key_length,
const char *value,
int length);
Note how the macro just does the strlen() math for you β no cap, that's the whole flex. π«Ά
πΈ PARAMETERS πΈ
bsonβ abson_t. The doc you're adding to. Your main character.keyβ an ASCII C string holding the field name.key_lengthβ how many bytes longkeyis, or-1if you want it to figure that out withstrlen().valueβ a UTF-8 encoded string. The actual content, the moment.lengthβ how many bytes are invalue, not counting the trailing\0, or-1to letstrlen()do its thing.
π¦ DESCRIPTION π¦
bson_append_utf8() shall append a UTF-8 encoded string to bson. That's it, that's the plot. π
Now, important vibes-only warning: value MUST be valid UTF-8. Not "should", not "ideally" β must. Periodt.
Some UTF-8 implementations allow a \0 to live inside the string (that's separate from the terminating \0, bestie, don't get it twisted). Is that allowed? Yeah. But lowkey remember it could cause issues when you're talking to external systems that don't support it. π
The suggestion on the table: use modified UTF-8, which uses a 2 byte representation for embedded \0 within the string. Why? Because that lets these UTF-8 encoded strings be used with many libc functions. Practical. Smart. We love. β¨
And here's the legacy-behavior plot twist you need to know: due to legacy behavior, passing NULL for value appends a null value, not a UTF8 value. π If you actually want to append a null value directly, prefer using bson_append_null() instead. Like, that's the whole point of it existing.
β RETURNS β
Returns true if the operation was applied successfully. π
The function will fail if appending the value grows bson larger than INT32_MAX. That's the ceiling, and it's not a vibe check you can pass β it's a hard limit. π
π AUTHOR π
MongoDB, Inc
π COPYRIGHT π
2009-present, MongoDB, Inc.
fr fr bestie, bookmark this one βMUSTbe valid UTF-8, watch your embedded\0s, and don't sleep onbson_append_null()for actual nulls. π
That's the whole page, mwah π β this was libbson 2.5.6, dated Oct 09, 2026. Go append something iconic. ππ§π
π¦ where this page comes from
- epel 10: libbson-devel 2.5.6-1.el10_3 Β· Apache-2.0 AND ISC AND MIT AND Zlib
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms