BSON_JSON_READER_T(3)

πŸ’… BSON_JSON_READER_T πŸ’…

okay bestie, so this is a page from the libbson manual β€” section 3, and the whole vibe is Bulk JSON to BSON conversion. like, the entire point. slay.


✨ SYNOPSIS ✨

#include <bson/bson.h>

typedef struct _bson_json_reader_t bson_json_reader_t;

typedef enum {
   BSON_JSON_ERROR_READ_CORRUPT_JS = 1,
   BSON_JSON_ERROR_READ_INVALID_PARAM,
   BSON_JSON_ERROR_READ_CB_FAILURE,
} bson_json_error_code_t;

πŸŽ€ DESCRIPTION πŸŽ€

so the bson_json_reader_t structure is literally the girl who reads a whole sequence of JSON documents and transforms them into bson_t documents. one after another. no cap. πŸ’–

and the way this is useful? it's giving bulk operations β€” like when you've got all your ops defined in a file full of JSON documents and you just need to run through them all, that's her moment. main character energy fr fr.

🫢 TIP: bson_json_reader_t works upon JSON documents formatted in MongoDB extended JSON format. iykyk β€” don't come at her with plain vanilla JSON, she wants the extended stuff. ✨

πŸ¦‹ EXAMPLE πŸ¦‹

this is the whole thing, verbatim, bestie β€” every comment, every brace, don't touch a single character:

/*
 * Copyright 2009-present MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * This program will print each JSON document contained in the provided files
 * as a BSON string to STDOUT.
 */

#include <bson/bson.h>
#include <stdlib.h>
#include <stdio.h>

int
main (int argc, char *argv[])
{
   bson_json_reader_t *reader;
   bson_error_t error;
   const char *filename;
   bson_t doc = BSON_INITIALIZER;
   int i;
   int b;

   /*
    * Print program usage if no arguments are provided.
    */
   if (argc == 1) {
      fprintf (stderr, "usage: %s FILE...\n", argv[0]);
      return 1;
   }

   /*
    * Process command line arguments expecting each to be a filename.
    */
   for (i = 1; i < argc; i++) {
      filename = argv[i];

      /*
       * Open the filename provided in command line arguments.
       */
      if (0 == strcmp (filename, "-")) {
         reader = bson_json_reader_new_from_fd (STDIN_FILENO, false);
      } else {
         if (!(reader = bson_json_reader_new_from_file (filename, &error))) {
            fprintf (
               stderr, "Failed to open \"%s\": %s\n", filename, error.message);
            continue;
         }
      }

      /*
       * Convert each incoming document to BSON and print to stdout.
       */
      while ((b = bson_json_reader_read (reader, &doc, &error))) {
         if (b < 0) {
            fprintf (stderr, "Error in json parsing:\n%s\n", error.message);
            abort ();
         }

         if (fwrite (bson_get_data (&doc), 1, doc.len, stdout) != doc.len) {
            fprintf (stderr, "Failed to write to stdout, exiting.\n");
            exit (1);
         }
         bson_reinit (&doc);
      }

      bson_json_reader_destroy (reader);
      bson_destroy (&doc);
   }

   return 0;
}

okay real talk, quick vibe check on what this little program is doing ✨ β€” it prints each JSON document inside the files you hand it, straight to STDOUT, as a BSON string. the "-" filename means read from STDIN_FILENO (iconic), anything else gets opened with bson_json_reader_new_from_file() and if that fails it prints Failed to open "%s": %s and continues to the next file like a queen. then it loops on bson_json_reader_read(), and if that comes back negative it screams Error in json parsing:\n%s and calls abort () β€” dramatic but valid πŸ’€. a short write to stdout gets Failed to write to stdout, exiting. and exit (1). otherwise bson_reinit (&doc) and go again. 🧚


πŸ‘‘ AUTHOR πŸ‘‘

MongoDB, Inc β€” yeah, them. πŸ’œ


2009-present, MongoDB, Inc. β€” all rights reserved, bestie, don't be weird about it. πŸ“


and that's the page!! πŸŽ€βœ¨ this was libbson 2.5.6, dated Oct 09, 2026, page BSON_JSON_READER_T(3) β€” mwah, go read some extended JSON, ily πŸ’–πŸ«ΆπŸŒΈ

πŸ“¦ where this page comes from