BSON_UTF8_ESCAPE_FOR_JSON(3)

πŸ’… BSON_UTF8_ESCAPE_FOR_JSON(3) ✨

okay so bestie, this is a page from libbson's manual β€” the bson_utf8_escape_for_json function, to be exact. buckle up, it's actually so good 🫢


🌸 SYNOPSIS 🌸

char *
bson_utf8_escape_for_json (const char *utf8, ssize_t utf8_len);

πŸŽ€ PARAMETERS πŸŽ€

  • utf8 β€” a UTF-8 encoded string, bestie. that's the vibe.
  • utf8_len β€” how long utf8 is, in bytes. or pass -1 if it's NUL terminated and you're feeling lazy πŸ’…

πŸ¦‹ DESCRIPTION πŸ¦‹

so this function allocates a brand-new string that matches utf8, except β€” plot twist β€” the special characters that JSON cares about get escaped. and yeah, the resulting string is also UTF-8 encoded. we stay consistent ✨

both " and \ characters get backslash-escaped. iconic behavior, honestly.

if a NUL byte shows up before utf8_len bytes, it gets converted to "\u0000". and the other non-ASCII characters in the input? preserved. untouched. main character energy πŸ‘‘

now this part's important so listen up: the two-byte sequence "C0 80" is also interpreted as an internal NUL, for historical reasons. and just so we're clear, that sequence is considered invalid according to RFC3629. so like... it's giving legacy baggage, but we deal πŸ’€


🌈 RETURNS 🌈

you get a newly allocated string back, and you should free it with bson_free() when utf8 is a valid UTF-8 string. or β€” if the (possibly invalid UTF-8) string could not be escaped β€” you get NULL. no notes 😭


πŸ’œ AUTHOR πŸ’œ

MongoDB, Inc


2009-present, MongoDB, Inc.


and that's the whole page, bestie β€” no cap, you're now officially fluent in bson_utf8_escape_for_json πŸ§šπŸ’–

signed off from version 2.5.6, dated Oct 09, 2026 β€” catch you in the next manpage, mwah πŸŽ€

πŸ“¦ where this page comes from